esprima 获取所有的函数类型数据
可以使用 Esprima 解析 JavaScript 代码,然后遍历 Abstract Syntax Tree(AST)来获取所有函数类型的数据。以下是一个示例代码:
const esprima = require('esprima');
const code = `
function foo() {
return 'Hello, world!';
}
const bar = function() {
console.log('Hello, world!');
}
const obj = {
baz: function() {
return 42;
}
};
`;
const ast = esprima.parseScript(code);
const functions = [];
function traverse(node) {
if (node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression') {
functions.push(node);
}
for (const prop in node) {
if (node.hasOwnProperty(prop)) {
const child = node[prop];
if (typeof child === 'object' && child !== null) {
if (Array.isArray(child)) {
child.forEach(traverse);
} else {
traverse(child);
}
}
}
}
}
traverse(ast);
console.log(functions);
以上代码解析了一个包含三个函数的 JavaScript 代码,输出如下:
[
{
"type": "FunctionDeclaration",
"id": {
"type": "Identifier",
"name": "foo",
"range": [
9,
12
],
"loc": {
"start": {
"line": 2,
"column": 9
},
"end": {
"line": 2,
"column": 12
}
}
},
"params": [],
"body": {
"type": "BlockStatement",
"body": [
{
"type": "ReturnStatement",
"argument": {
"type": "Literal",
"value": "Hello, world!",
"raw": "'Hello, world!'",
"range": [
26,
41
],
"loc": {
"start": {
"line": 3,
"column": 10
},
"end": {
"line": 3,
"column": 25
}
}
},
"range": [
19,
42
],
"loc": {
"start": {
"line": 3,
"column": 3
},
"end": {
"line": 3,
"column": 26
}
}
}
],
"range": [
14,
44
],
"loc": {
"start": {
"line": 2,
"column": 15
},
"end": {
"line": 4,
"column": 2
}
}
},
"generator": false,
"expression": false,
"async": false,
"range": [
0,
44
],
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 4,
"column": 2
}
}
},
{
"type": "FunctionExpression",
"id": null,
"params": [],
"body": {
"type": "BlockStatement",
"body": [
{
"type": "ExpressionStatement",
"expression": {
"type": "CallExpression",
"callee": {
"type": "MemberExpression",
"object": {
"type": "Identifier",
"name": "console",
"range": [
63,
70
],
"loc": {
"start": {
"line": 6,
"column": 0
},
"end": {
"line": 6,
"column": 7
}
}
},
"property": {
"type": "Identifier",
"name": "log",
"range": [
71,
74
],
"loc": {
"start": {
"line": 6,
"column": 8
},
"end": {
"line": 6,
"column": 11
}
}
},
"computed": false,
"range": [
63,
74
],
"loc": {
"start": {
"line": 6,
"column": 0
},
"end": {
"line": 6,
"column": 11
}
}
},
"arguments": [
{
"type": "Literal",
"value": "Hello, world!",
"raw": "'Hello, world!'",
"range": [
75,
90
],
"loc": {
"start": {
"line": 6,
"column": 12
},
"end": {
"line": 6,
"column": 27
}
}
}
],
"range": [
63,
91
],
"loc": {
"start": {
"line": 6,
"column": 0
},
"end": {
"line": 6,
"column": 28
}
}
},
"range": [
63,
92
],
"loc": {
"start": {
"line": 6,
"column": 0
},
"end": {
"line": 6,
"column": 29
}
}
}
],
"range": [
58,
99
],
"loc": {
"start": {
"line": 5,
"column": 12
},
"end": {
"line": 7,
"column": 2
}
}
},
"generator": false,
"expression": true,
"async": false,
"range": [
46,
99
],
"loc": {
"start": {
"line": 5,
"column": 0
},
"end": {
"line": 7,
"column": 2
}
}
},
{
"type": "FunctionExpression",
"id": null,
"params": [],
"body": {
"type": "BlockStatement",
"body": [
{
"type": "ReturnStatement",
"argument": {
"type": "Literal",
"value": 42,
"raw": "42",
"range": [
129,
131
],
"loc": {
"start": {
"line": 11,
"column": 10
},
"end": {
"line": 11,
"column": 12
}
}
},
"range": [
122,
132
],
"loc": {
"start": {
"line": 11,
"column": 3
},
"end": {
"line": 11,
"column": 13
}
}
}
],
"range": [
117,
140
],
"loc": {
"start": {
"line": 10,
"column": 14
},
"end": {
"line": 12,
"column": 2
}
}
},
"generator": false,
"expression": true,
"async": false,
"range": [
107,
140
],
"loc": {
"start": {
"line": 10,
"column": 4
},
"end": {
"line": 12,
"column": 2
}
}
}
]
可以看到,输出结果包含了三个函数的 AST 节点。函数类型的节点有两种:FunctionDeclaration(函数声明)和 FunctionExpression(函数表达式)。可以根据需要选择使用哪种类型。
原文地址: https://www.cveoy.top/t/topic/9Xu 著作权归作者所有。请勿转载和采集!