如何创建;全称;使用esprima创建javascript代码段的AST
我遵循本教程:创建并遍历从任何javascript代码创建的AST。 我正在使用console.log(ast)打印终端中的树。虽然我使用了上面链接中提供的类似代码,但我的AST看起来信息量不大。 下面是6*7的AST输出如何创建;全称;使用esprima创建javascript代码段的AST,javascript,abstract-syntax-tree,Javascript,Abstract Syntax Tree,我遵循本教程:创建并遍历从任何javascript代码创建的AST。 我正在使用console.log(ast)打印终端中的树。虽然我使用了上面链接中提供的类似代码,但我的AST看起来信息量不大。 下面是6*7的AST输出 Program { type: 'Program', body: [ ExpressionStatement { type: 'ExpressionStatement', expression: [Object] } ], sourceType: 'script'
Program {
type: 'Program',
body: [ ExpressionStatement { type: 'ExpressionStatement', expression: [Object] } ],
sourceType: 'script' }
我希望它包含左右字段以及variabls(如教程中所示)。
以下是我使用的anasis.js代码:
var fs = require('fs'),
esprima = require('esprima');
function analyzeCode(code) {
var ast = esprima.parse(code);
console.log(ast);
}
if (process.argv.length < 3) {
console.log('Usage: analyze.js file.js');
process.exit(1);
}
var filename = process.argv[2];
console.log('Reading ' + filename);
var code = fs.readFileSync(filename, 'utf-8');
analyzeCode(code);
//console.log('Done');
var fs=require('fs'),
esprima=需要(“esprima”);
功能分析代码(代码){
var ast=esprima.parse(代码);
控制台日志(ast);
}
如果(进程argv.length<3){
log('用法:analyze.js file.js');
过程。退出(1);
}
var filename=process.argv[2];
log('Reading'+文件名);
var code=fs.readFileSync(文件名为“utf-8”);
分析代码;
//console.log('Done');
我应该做哪些更改才能获得js代码AST中包含变量的所有字段?您确实获得了所有字段,只是
console.log
没有打印所有字段,因为树很大。请尝试console.log(JSON.stringify(AST,null,4))
谢谢Bergi。这就是我要找的。