Code Monkey home page Code Monkey logo

estree-walker's Introduction

estree-walker

Simple utility for walking an ESTree-compliant AST, such as one generated by acorn.

Installation

npm i estree-walker

Usage

var walk = require('estree-walker').walk;
var acorn = require('acorn');

ast = acorn.parse(sourceCode, options); // https://github.com/acornjs/acorn

walk(ast, {
  enter(node, parent, prop, index) {
    // some code happens
  },
  leave(node, parent, prop, index) {
    // some code happens
  }
});

Inside the enter function, calling this.skip() will prevent the node's children being walked, or the leave function (which is optional) being called.

Call this.replace(new_node) in either enter or leave to replace the current node with a new one.

Call this.remove() in either enter or leave to remove the current node.

Why not use estraverse?

The ESTree spec is evolving to accommodate ES6/7. I've had a couple of experiences where estraverse was unable to handle an AST generated by recent versions of acorn, because it hard-codes visitor keys.

estree-walker, by contrast, simply enumerates a node's properties to find child nodes (and child lists of nodes), and is therefore resistant to spec changes. It's also much smaller. (The performance, if you're wondering, is basically identical.)

None of which should be taken as criticism of estraverse, which has more features and has been battle-tested in many more situations, and for which I'm very grateful.

License

MIT

estree-walker's People

Contributors

andrewleedham avatar benmccann avatar christianmurphy avatar conduitry avatar forivall avatar kristoferbaxter avatar larsrh avatar marvinhagemeister avatar mrkishi avatar rich-harris avatar tanhauhau avatar trysound avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

estree-walker's Issues

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in... error

Getting an error when I try to use estree-walker with esbuild:

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /Users/sam/Nine/components/node_modules/estree-walker/package.json
    at throwExportsNotFound (internal/modules/esm/resolve.js:290:9)
    at packageExportsResolve (internal/modules/esm/resolve.js:479:7)
    at resolveExports (internal/modules/cjs/loader.js:432:36)
    at Function.Module._findPath (internal/modules/cjs/loader.js:472:31)
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:867:27)
    at Function.Module._load (internal/modules/cjs/loader.js:725:27)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/Users/sam/Nine/components/dist/src/main.js:43:39)
    at Module._compile (internal/modules/cjs/loader.js:1063:30) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

Node Version: v14.15.4

tsconfig.json:

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "allowJs": true,
    "importHelpers": true,
    "alwaysStrict": true,
    "sourceMap": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitAny": false,
    "noImplicitThis": false,
    "strictNullChecks": false
  },
  "include": ["src/**/*", "__tests__/**/*"]
}

1.0.0 (and missing LICENSE file)

Any reason to hold off from a 1.0.0 release? Is this package considered stable?

Also, you unambiguously specify this is under the MIT LICENSE, but, you don't provide the LICENSE yourself in either the npm publication or this repository.

From the MIT LICENSE:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

Where (and what is the contents of) the copyright notice? etc

Move `@types/estree` to dependencies instead of devDeps

As estree is exposed in the types used by estree-walker, when using estree-walker with a strict package manager like yarn pnp, TypeScript warns us that it cannot find the package estree when using estree-walker:

image

Here is the error I have in TypeScript:

.yarn/cache/estree-walker-npm-3.0.1-f100136724-002ec6bba6.zip/node_modules/estree-walker/types/index.d.ts(18,56): error TS2307: Cannot find module 'estree' or its corresponding type declarations.

Moving @types/estree to the deps would allow other package to directly find estree (as devDeps are not installed by other packages, but deps are)

The usage instructions in the README.md are not working.

When I followed the usage instructions in the README, I received this error message.

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /Users/sam/Nine/components/node_modules/estree-walker/package.json
    at throwExportsNotFound (internal/modules/esm/resolve.js:290:9)
    at packageExportsResolve (internal/modules/esm/resolve.js:479:7)
    at resolveExports (internal/modules/cjs/loader.js:432:36)
    at Function.Module._findPath (internal/modules/cjs/loader.js:472:31)
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:867:27)
    at Function.Module._load (internal/modules/cjs/loader.js:725:27)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/Users/sam/Nine/components/dist/src/main.js:43:39)
    at Module._compile (internal/modules/cjs/loader.js:1063:30) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

Could i fix this usage instructions in the README?

Allow to `return` from `enter` and `leave`

Hey y'all

I think it would be great if there was an easy way to break the tree traversal and return something by calling return ....

Then, this code would be possible:

const result = walk(ast, {enter(node) {
    if(node === condition) return something
}})
// Logs the value of something
console.log(result)

What do you think?

Wrong TS types: BaseNode instead of Node

Hey, I'm using the svelte compiler to walk the AST. I'm using Typescript and it seems that the typings are wrong for the node that passed to the walk function - https://github.com/Rich-Harris/estree-walker/blob/master/src/async.js#L4

image

It's typed as BaseNode, but the possible results are some sort of of Node + INode mixed type (eg. ModuleSpecifier for import statements). The dependencies:

{
    "svelte": "^3.38.2",
    "@types/estree": "^0.0.48",
    "estree-walker": "^3.0.0"
}

P.S. Also Svelte doesn't automatically include those types, you need to install more of the packages. This isn't ideal as you could miss-match the dependency versions. Maybe there is a conventional way to bundle types, without the need for the user to install svelte's sub-dependencies? ๐Ÿค”

Caching child keys not always valid

I have some code including ES7 decorators parsed by acorn-es7.

ClassDeclaration.decorators is not always present, so caching the keys means that the decorator node is skipped unless the first class encountered happens to have a decorator.

The filter isn't really necessary given the check for value.type later, so removing the cache shouldn't have much of a performance impact.

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.