在 Esprima 4.0.1 中,可以使用以下代码获取函数类型的数据并获取函数体内容:

const esprima = require('esprima');

const code = 'function add(a, b) { return a + b; }';
const ast = esprima.parseScript(code);

const functionDeclaration = ast.body[0];
console.log(functionDeclaration.type); // Output: FunctionDeclaration
console.log(functionDeclaration.id.name); // Output: add

const functionBody = functionDeclaration.body;
console.log(functionBody.type); // Output: BlockStatement
console.log(functionBody.body[0].type); // Output: ReturnStatement
console.log(functionBody.body[0].argument.type); // Output: BinaryExpression

此代码将输出函数声明的类型为'FunctionDeclaration',函数的名称为'add'。还将输出函数体的类型为'BlockStatement',以及第一个语句的类型为'ReturnStatement',并且返回值的类型为'BinaryExpression'。

Esprima 4.0.1 获取函数类型数据和函数体内容

原文地址: https://www.cveoy.top/t/topic/mzUK 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录