What is estree-util-attach-comments?
The estree-util-attach-comments package is a utility for attaching comments to nodes in an ESTree-compliant abstract syntax tree (AST). This is particularly useful for tools that need to preserve comments when transforming or analyzing JavaScript code.
What are estree-util-attach-comments's main functionalities?
Attach comments to AST nodes
This feature allows you to attach comments to the corresponding nodes in an AST. The code sample demonstrates parsing JavaScript code with Acorn, collecting comments, and then attaching those comments to the AST nodes using estree-util-attach-comments.
const { attachComments } = require('estree-util-attach-comments');
const acorn = require('acorn');
const comments = [];
const ast = acorn.parse('const x = 42; // a comment', {
onComment: comments
});
attachComments(ast, comments);
console.log(ast);
Other packages similar to estree-util-attach-comments
recast
Recast is a JavaScript syntax tree transformer, specifically designed to work with ESTree-compliant ASTs. It can parse, transform, and print JavaScript code while preserving comments. Compared to estree-util-attach-comments, Recast offers a more comprehensive set of features for code transformation and comment preservation.
babel
Babel is a widely-used JavaScript compiler that can transform ES6+ code into backwards-compatible JavaScript. It includes a powerful parser (Babylon) and a set of tools for manipulating ASTs. Babel also preserves comments during transformations, making it a more feature-rich alternative to estree-util-attach-comments for complex code transformations.
esprima
Esprima is a high-performance, standard-compliant ECMAScript parser that produces ESTree-compliant ASTs. It can also collect comments during parsing. While Esprima does not directly attach comments to AST nodes, it can be used in conjunction with other tools like estree-util-attach-comments to achieve similar functionality.
Attach semistandard estree comment nodes (such as from espree or
acorn with a couple lines of code) to the nodes in that tree.
This is useful because certain estree parsers give you an array (espree and
acorn) whereas other estree tools expect comments to be embedded on nodes in the
tree.
Install
This package is ESM only: Node 12+ is needed to use it and it must be import
ed
instead of require
d.
npm:
npm install estree-util-attach-comments
Use
Say we have this weird code
:
function a (b) { return b + 1 }
And our script, example.js
, looks as follows:
import * as acorn from 'acorn'
import recast from 'recast'
import {attachComments} from 'estree-util-attach-comments'
var comments = []
var tree = acorn.parse(code, {ecmaVersion: 2020, onComment: comments})
attachComments(tree, comments)
console.log(recast.print(tree).code)
Yields:
function
a(
b
)
{
return (
b +
1
);
}
Note that the lines are added by recast
in this case.
And, some of these weird comments are off, but they’re pretty close.
API
This package exports the following identifiers: attachComment
.
There is no default export.
Attach semistandard estree comment nodes to the tree.
This mutates the given tree
(Program
).
It takes comments
, walks the tree, and adds comments as close as possible
to where they originated.
Comment nodes are given two boolean fields: leading
(true
for /* a */ b
)
and trailing
(true
for a /* b */
).
Both fields are false
for dangling comments: [/* a */]
.
This is what recast
uses too, and is somewhat similar to Babel, which is not
estree but instead uses leadingComments
, trailingComments
, and
innerComments
arrays on nodes.
The algorithm checks any node: even recent (or future) proposals or nonstandard
syntax such as JSX, because it ducktypes to find nodes instead of having a list
of visitor keys.
The algorithm supports loc
fields (line/column), range
fields (offsets),
and direct start
/ end
fields.
Returns
Node
— The given tree
.
License
MIT © Titus Wormer