Skip to content

Instantly share code, notes, and snippets.

@hyiromori hyiromori/README.md
Last active Dec 3, 2019

Embed
What would you like to do?
AST Practice

AST Practice

Install

npm i

Test

npm test
const fs = require('fs');
const estraverse = require('estraverse');
const stdin = fs.readFileSync('/dev/stdin', 'utf8');
const ast = JSON.parse(stdin);
estraverse.traverse(ast, {
enter: (currentNode, parentNode) => {
// ここに変換処理を入れる
},
leave: (currentNode, parentNode) => {
// ここに変換処理を入れる
},
});
console.log(JSON.stringify(ast, null, 2));
{
"name": "ast",
"version": "1.0.0",
"description": "AST Example",
"private": true,
"scripts": {
"parse": "node parse.js",
"convert": "node convert.js",
"unparse": "node unparse.js",
"test": "echo 'const a = 1 + 2; console.log(a);' | npm -s run parse | npm -s run convert | npm -s run unparse"
},
"author": "hyiromori",
"license": "ISC",
"dependencies": {
"acorn": "^7.1.0",
"escodegen": "^1.12.0",
"espurify": "^2.0.1",
"estraverse": "^4.3.0"
}
}
const fs = require('fs');
const acorn = require('acorn');
// https://github.com/estree/estree
const espurify = require('espurify');
const stdin = fs.readFileSync('/dev/stdin', 'utf8');
const acornAst = acorn.parse(stdin);
const standardAst = espurify(acornAst);
console.log(JSON.stringify(standardAst, null, 2));
const fs = require('fs');
const escodegen = require('escodegen');
const stdin = fs.readFileSync('/dev/stdin', 'utf8');
const astObject = JSON.parse(stdin);
const jsSource = escodegen.generate(astObject);
console.log(jsSource);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.