All Projects → davidbonnet → astravel

davidbonnet / astravel

Licence: MIT license
👟 Tiny and fast ESTree-compliant AST walker and modifier.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to astravel

eval-estree-expression
Safely evaluate JavaScript (estree) expressions, sync and async.
Stars: ✭ 22 (-42.11%)
Mutual labels:  ast, estree
kataw
An 100% spec compliant ES2022 JavaScript toolchain
Stars: ✭ 303 (+697.37%)
Mutual labels:  ast, estree
estree-to-babel
convert estree ast to babel
Stars: ✭ 23 (-39.47%)
Mutual labels:  ast, estree
esvalid
confirm that a SpiderMonkey format AST represents an ECMAScript program
Stars: ✭ 24 (-36.84%)
Mutual labels:  ast, estree
Estree
The ESTree Spec
Stars: ✭ 3,867 (+10076.32%)
Mutual labels:  ast, estree
Babylon
PSA: moved into babel/babel as @babel/parser -->
Stars: ✭ 1,692 (+4352.63%)
Mutual labels:  ast, estree
Astexplorer
A web tool to explore the ASTs generated by various parsers.
Stars: ✭ 4,330 (+11294.74%)
Mutual labels:  ast, ast-explorer
Escodegen
ECMAScript code generator
Stars: ✭ 2,328 (+6026.32%)
Mutual labels:  ast, estree
abstract-syntax-tree
A library for working with abstract syntax trees.
Stars: ✭ 77 (+102.63%)
Mutual labels:  ast
about-Vue
📔 Vue 源码的探讨和学习
Stars: ✭ 56 (+47.37%)
Mutual labels:  ast
babel-plugin-detective
Babel plugin that scans the AST for require calls and import statements
Stars: ✭ 26 (-31.58%)
Mutual labels:  ast
py2many
Transpiler of Python to many other languages
Stars: ✭ 420 (+1005.26%)
Mutual labels:  ast
ast-grep
🔍 Like grep, but more powerful than you can possibly imagine
Stars: ✭ 14 (-63.16%)
Mutual labels:  ast
clickhouse-ast-parser
AST parser and visitor for ClickHouse SQL
Stars: ✭ 60 (+57.89%)
Mutual labels:  ast
ts-transform-react-constant-elements
A TypeScript AST Transformer that can speed up reconciliation and reduce garbage collection pressure by hoisting React elements to the highest possible scope.
Stars: ✭ 44 (+15.79%)
Mutual labels:  ast
ts-transform-react-jsx-source
TypeScript AST Transformer that adds source file and line number to JSX elements
Stars: ✭ 12 (-68.42%)
Mutual labels:  ast
performance-decorator
🏇User behavior & Function execution tracking solution - 大型前端项目的用户行为跟踪,函数调用链分析,断点调试共享化和复用化实践
Stars: ✭ 39 (+2.63%)
Mutual labels:  ast
ninny-json
JSON typeclasses that know the difference between null and absent fields
Stars: ✭ 19 (-50%)
Mutual labels:  ast
CastXMLSuperbuild
Build CastXML and its dependencies (LLVM/Clang)
Stars: ✭ 32 (-15.79%)
Mutual labels:  ast
ctxexp-parser
In the dynamic execution of JS language environment (wechat applet) to execute JS class calling function.
Stars: ✭ 17 (-55.26%)
Mutual labels:  ast

Astravel

NPM Version Build Status Coverage devDependency Status Greenkeeper

👟 A tiny and fast ESTree-compliant AST walker and modifier.

Key features

  • Works on ESTree-compliant ASTs (JavaScript version 13 (2022)), such as the ones produced by Meriyah.
  • Out-of-the-box functions such as source code comments insertion for Astring.
  • Extensible with custom nodes.
  • No dependencies and small footprint.

Installation

Install with the Node Package Manager:

npm install astravel

Alternatively, checkout this repository and install the development dependencies to build the module file:

git clone https://github.com/davidbonnet/astravel.git
cd astravel
npm install

Usage

The astravel module exports the following items:

defaultTraveler

⬅️ traveler

⚠️ Deprecated in favor of ES6 class notation.

This object describes a basic AST traveler. It contains the following methods:

  • go(node, state): Travels through the provided AST node with a given state (an object that can be of any type) by recursively calling this method.
  • find(predicate, node, state) ➞ { node, state }?: Returns { node, state } for which predicate(node, state) returns truthy, starting at the specified AST node and with the provided state. Otherwise, returns undefined.
  • [NodeType](node, state): Method handler for a specific NodeType.
  • makeChild(properties) ➞ traveler: Returns a custom AST traveler that inherits from this traveler with its own provided properties and the property super that points to this traveler.

makeTraveler()

➡️ (properties) ⬅️ traveler

⚠️ Deprecated in favor of ES6 class notation.

This function is similar to astravel.defaultTraveler.makeChild: it returns a traveler that inherits from the defaultTraveler with its own provided properties and the property super that points to the defaultTraveler object. These properties should redefine the traveler's behavior by implementing the go(node, state) method and/or any node handler.

When redefining the go method, make sure its basic functionality is kept by calling the parent's go method to keep traveling through the AST:

const customTraveler = makeTraveler({
  go: function (node, state) {
    // Code before entering the node
    console.log('Entering ' + node.type)
    // Call the parent's `go` method
    this.super.go.call(this, node, state)
    // Code after leaving the node
    console.log('Leaving ' + node.type)
  },
})

To skip specific node types, the most effective way is to replace the corresponding node handlers with a function that does nothing:

import { makeTraveler } from 'astravel'

const ignore = Function.prototype
const customTraveler = makeTraveler({
  FunctionDeclaration: ignore,
  FunctionExpression: ignore,
  ArrowFunctionExpression: ignore,
})

attachComments()

➡️ (ast, comments) ⬅️ ast

This function attaches a list of comments to the corresponding nodes of a provided ast and returns that same ast. The ast is modified in-place and only the nodes getting comments are augmented with a comments and/or a trailingComments array property.

Each comment should be an object with the following properties:

  • type: "Line" or "Block"
  • value: Comment string value
  • start: Comment starting character offset number
  • end: Comment ending character offset number
  • loc: Location object with start and end properties containing one-based line number and zero-based column number properties.

The following examples show how to obtain a proper list of comments of a given source code and how to attach them on the generated ast:

Usage with Meriyah
import { parse } from 'meriyah'
import { attachComments } from 'astravel'

const comments = []
const ast = parse(code, {
  // Comments are stored in this array
  onComment: comments,
})
// Attach comments on the AST
attachComments(ast, comments)
Usage with Acorn
import { parse } from 'acorn'
import { attachComments } from 'astravel'

const comments = []
const ast = parse(code, {
  // This ensures that the `loc` property is present on comment objects
  locations: true,
  onComment: comments,
})
attachComments(ast, comments)

The algorithm assumes that comments are not put in exotic places, such as in-between function arguments, and proceeds as follows:

  • For a given statement, it stores all comments right above it and on the same line to it's right side in a comments property.
  • If a comment block is at the beginning of a code block, it is attached to that code block.
  • Comments not followed by any statement in a code block are attached as trailingComments to that code block.

In this example, the comments tell to which statement they are attached:

// Attached to the variable declaration just below
const point = {
  // Attached to the property definition just below
  x: 0,
  y: 0, // Attached to the property definition on its left
}
/*
Attached to the function declaration just below.
*/
function add(a, b) {
  /*
   Attached to the function body because it is the first comment block.
   */
  return a + b // Attached to the return statement on its left
  // Trailing comment attached as such to the function body
}
// Trailing comment attached as such to the program body
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].