Code Monkey home page Code Monkey logo

babel-plugin-debug-object-location's Introduction

Debug object location

Babel plugin to help you determine where an object or array was instantiated

Have you ever been debugging, come across an object, and wished you could easily find out which file it was instantiated in?

Well, now you can! This plugin keeps track of all object and array literals in your code by mapping them to their source paths in a global registry. So when you're debugging, you can easily locate the source file of any object or array, helping you to understand the code better and resolve issues faster.

Install

npm i -D babel-plugin-debug-object-location

Setup

Add the plugin to your Babel config:

{
    // Only add to the development environment
    "env": {
        "development": {
            "plugins": ["debug-object-location"]
        }
    },
    // ...
}

Warning: Since this is a debugging tool, you should only enable it during development or when you need it.

Use-case

Let's say you have an object literal in your code that gets passed around in your application. The contents of the object are generated at run-time:

// my-file.ts

const myObject = {
    valueA: someVariable,
    valueB: Math.random()
}

// `someFunction` passes `myObject` around and gets referenced from many places
someFunction(myObject)

// can even be mutated
myObject.valueB = 123

While debugging, you encouter it in the stack-trace:

const object = {
    valueA: 365,
    valueB: 123
}

You want to learn more about it by looking at the file it was initialized in, but since the contents are generated at run-time, you can't search for it in your codebase.

Using this plugin, you can now look up the object in the objectRegistry global to find out which file it was instantiated in:

objectRegistry.find(object)
// => '/my-file.ts'

API

objectRegistry.find(object)

Type:

type UnknownObject = Record<PropertyKey, unknown>
type find = (query: UnknownObject | unknown[]) => (
    string
    | [object: UnknownObject, filePath: string][]
)

If there's an identity match (===), it returns the source file path where the object was instantiated.

If there is no identity match, it falls back to querying the registry for a shallow match and return an array of tuples: [object, filePath].

For example, .find({ a: 1 }) will match { a: 1, b: 2, c: 3 }, so it's best to be as specific as possible.

objectRegistry.registry

Type:

type UnknownObject = Record<PropertyKey, unknown>
type registry = Map<UnknownObject, string>

The actual registry where all the registered objects/arrays are stored. You can iterate over this yourself if the find() API is not sufficient.

How does it work?

The plugin works by transforming the code when it encounters an object or array literal. It passes it into a function call that registers it.

For example, given the following code:

const myObject = {
    value: 123
}

The plugin transforms it to:

import { register } from 'babel-plugin-debug-object-location'

const myObject = register(
    {
        value: 123
    },
    '/my/file/path.ts'
)

Here, the register function is called with the object literal as its argument and the source path. The register function adds the object to the global registry and returns it without touching it.

Because all it does it reference the object, and doesn't try to access or mutate it's properties, it's completely safe to use.

babel-plugin-debug-object-location's People

Contributors

privatenumber avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

babel-plugin-debug-object-location's Issues

The automated release is failing 🚨

🚨 The automated release from the master branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you can benefit from your bug fixes and new features again.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can fix this πŸ’ͺ.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the master branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here are some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


Cannot push to the Git repository.

semantic-release cannot push the version tag to the branch master on the remote Git repository with URL https://[secure]@github.com/privatenumber/babel-plugin-debug-object-location.git.

This can be caused by:


Good luck with your project ✨

Your semantic-release bot πŸ“¦πŸš€

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.