Get started with acorn CDN
MIT licensed
Acorn parses ECMAScript code into abstract syntax trees .
Tags:- acorn
- javascript
- parser
- ECMAScript
- Escodegen
Stable version
Copied!
How to start using acorn CDN
const Acorn = require('acorn'); // Use require for CommonJS modules in Node.js
const acorn = require('acorn/dist/acorn.min.js'); // Use require for the minified version in the browser
const acornTraverse = require('acorn-traverse'); // For traversing the abstract syntax tree
// Assuming the following expression is in a string variable called `code`
const code = 'const result = 1 + 2 * 3';
// Parse the code using Acorn
const ast = acorn.parse(code, { ecmaVersion: 6 });
// Traverse the abstract syntax tree and transform the expression
acornTraverse(ast, {
ExpressionStatement(node) {
if (node.expression.type === 'BinaryExpression') {
node.expression.right.operand = { type: 'NumericLiteral', value: 4 };
}
},
});
// Print the transformed abstract syntax tree
console.log(JSON.stringify(ast, null, 2));
// Compile the transformed abstract syntax tree to JavaScript code
const { code: transformedCode } = Acorn.compile(ast);
// Evaluate the transformed code
const evalResult = new Function('result', transformedCode)('result');
console.log(evalResult); // Output: 1 + 2 * 4 = 7
Copied!
Copied!
All versions
0.0.1
0.1.0
0.10.0
0.11.0
0.12.0
0.2.0
0.3.0
0.3.1
0.4.0
0.4.2
0.5.0
0.6.0
0.7.0
0.8.0
0.9.0
1.0.0
1.0.1
1.0.3
1.1.0
1.2.0
1.2.1
1.2.2
2.0.0
2.0.1
2.0.4
2.1.0
2.2.0
2.3.0
2.4.0
2.5.0
2.5.2
2.6.0
2.6.2
2.6.4
2.7.0
3.0.0
3.0.2
3.0.4
3.1.0
3.2.0
3.3.0
4.0.0
4.0.1
4.0.10
4.0.11
4.0.12
4.0.13
4.0.2
4.0.3
4.0.4
4.0.5
4.0.6
4.0.7
4.0.8
4.0.9
5.0.0
5.0.1
5.0.2
5.0.3
5.1.0
5.1.1
5.1.2
5.2.0
5.2.1
5.3.0
5.4.0
5.4.1
5.5.0
5.5.1
5.5.2
5.5.3
5.6.0
5.6.1
5.6.2
5.7.0
5.7.1
5.7.2
5.7.3
5.7.4
6.0.0
6.0.1
6.0.2
6.0.3
6.0.4
6.0.5
6.0.6
6.0.7
6.1.0
6.1.1
6.2.0
6.2.1
6.3.0
6.4.0
6.4.1
6.4.2
7.0.0
7.1.0
7.1.1
7.2.0
7.3.0
7.3.1
7.4.0
7.4.1
8.0.0
8.0.1
8.0.2
8.0.3
8.0.4
8.0.5
8.1.0
8.1.1
8.10.0
8.11.0
8.11.1
8.11.2
8.11.3
8.12.0
*** 8.12.1
8.2.0
8.2.1
8.2.2
8.2.3
8.2.4
8.3.0
8.4.0
8.4.1
8.5.0
8.6.0
8.7.0
8.7.1
8.8.0
8.8.1
8.8.2
8.9.0