AST对象文档
AST基础认识
AST沙盒
AST入门基本知识
一段代码的souceCode
class C { foo;;bar }
SourceCode { hasBOM: false, text: 'class C { foo\n;;bar }', ast: Node { type: 'Program', loc: SourceLocation { start: [Position], end: [Position] }, range: [ 0, 21 ], body: [ [Node] ], sourceType: 'script', comments: [], tokens: [ [Token], [Token], [Token], [Token], [Token], [Token], [Token], [Token] ], parent: null }, parserServices: {}, scopeManager: ScopeManager { scopes: [ [GlobalScope], [ClassScope] ], globalScope: <ref *1> GlobalScope { type: 'global', set: [Map], taints: Map(0) {}, dynamic: true, block: [Node], through: [], variables: [Array], references: [], variableScope: [Circular *1], functionExpressionScope: false, directCallToEvalScope: false, thisFound: false, __left: null, upper: null, isStrict: false, childScopes: [Array], __declaredVariables: [WeakMap], implicit: [Object] }, __nodeToScope: WeakMap { <items unknown> }, __currentScope: null, __options: { optimistic: false, directive: false, nodejsScope: undefined, impliedStrict: undefined, sourceType: 'script', ecmaVersion: 2022, childVisitorKeys: [Object], fallback: [Function: getKeys], ignoreEval: true }, __declaredVariables: WeakMap { <items unknown> } }, visitorKeys: { AssignmentExpression: [ 'left', 'right' ], AssignmentPattern: [ 'left', 'right' ], ArrayExpression: [ 'elements' ], ArrayPattern: [ 'elements' ], ArrowFunctionExpression: [ 'params', 'body' ], AwaitExpression: [ 'argument' ], BlockStatement: [ 'body' ], BinaryExpression: [ 'left', 'right' ], BreakStatement: [ 'label' ], CallExpression: [ 'callee', 'arguments' ], CatchClause: [ 'param', 'body' ], ChainExpression: [ 'expression' ], ClassBody: [ 'body' ], ClassDeclaration: [ 'id', 'superClass', 'body' ], ClassExpression: [ 'id', 'superClass', 'body' ], ConditionalExpression: [ 'test', 'consequent', 'alternate' ], ContinueStatement: [ 'label' ], DebuggerStatement: [], DoWhileStatement: [ 'body', 'test' ], EmptyStatement: [], ExportAllDeclaration: [ 'exported', 'source' ], ExportDefaultDeclaration: [ 'declaration' ], ExportNamedDeclaration: [ 'declaration', 'specifiers', 'source' ], ExportSpecifier: [ 'exported', 'local' ], ExpressionStatement: [ 'expression' ], ExperimentalRestProperty: [ 'argument' ], ExperimentalSpreadProperty: [ 'argument' ], ForStatement: [ 'init', 'test', 'update', 'body' ], ForInStatement: [ 'left', 'right', 'body' ], ForOfStatement: [ 'left', 'right', 'body' ], FunctionDeclaration: [ 'id', 'params', 'body' ], FunctionExpression: [ 'id', 'params', 'body' ], Identifier: [], IfStatement: [ 'test', 'consequent', 'alternate' ], ImportDeclaration: [ 'specifiers', 'source' ], ImportDefaultSpecifier: [ 'local' ], ImportExpression: [ 'source' ], ImportNamespaceSpecifier: [ 'local' ], ImportSpecifier: [ 'imported', 'local' ], JSXAttribute: [ 'name', 'value' ], JSXClosingElement: [ 'name' ], JSXElement: [ 'openingElement', 'children', 'closingElement' ], JSXEmptyExpression: [], JSXExpressionContainer: [ 'expression' ], JSXIdentifier: [], JSXMemberExpression: [ 'object', 'property' ], JSXNamespacedName: [ 'namespace', 'name' ], JSXOpeningElement: [ 'name', 'attributes' ], JSXSpreadAttribute: [ 'argument' ], JSXText: [], JSXFragment: [ 'openingFragment', 'children', 'closingFragment' ], JSXClosingFragment: [], JSXOpeningFragment: [], Literal: [], LabeledStatement: [ 'label', 'body' ], LogicalExpression: [ 'left', 'right' ], MemberExpression: [ 'object', 'property' ], MetaProperty: [ 'meta', 'property' ], MethodDefinition: [ 'key', 'value' ], NewExpression: [ 'callee', 'arguments' ], ObjectExpression: [ 'properties' ], ObjectPattern: [ 'properties' ], PrivateIdentifier: [], Program: [ 'body' ], Property: [ 'key', 'value' ], PropertyDefinition: [ 'key', 'value' ], RestElement: [ 'argument' ], ReturnStatement: [ 'argument' ], SequenceExpression: [ 'expressions' ], SpreadElement: [ 'argument' ], StaticBlock: [ 'body' ], Super: [], SwitchStatement: [ 'discriminant', 'cases' ], SwitchCase: [ 'test', 'consequent' ], TaggedTemplateExpression: [ 'tag', 'quasi' ], TemplateElement: [], TemplateLiteral: [ 'quasis', 'expressions' ], ThisExpression: [], ThrowStatement: [ 'argument' ], TryStatement: [ 'block', 'handler', 'finalizer' ], UnaryExpression: [ 'argument' ], UpdateExpression: [ 'argument' ], VariableDeclaration: [ 'declarations' ], VariableDeclarator: [ 'id', 'init' ], WhileStatement: [ 'test', 'body' ], WithStatement: [ 'object', 'body' ], YieldExpression: [ 'argument' ] }, tokensAndComments: [ Token { type: 'Keyword', value: 'class', loc: [SourceLocation], range: [Array] }, Token { type: 'Identifier', value: 'C', loc: [SourceLocation], range: [Array] }, Token { type: 'Punctuator', value: '{', loc: [SourceLocation], range: [Array] }, Token { type: 'Identifier', value: 'foo', loc: [SourceLocation], range: [Array] }, Token { type: 'Punctuator', value: ';', loc: [SourceLocation], range: [Array] }, Token { type: 'Punctuator', value: ';', loc: [SourceLocation], range: [Array] }, Token { type: 'Identifier', value: 'bar', loc: [SourceLocation], range: [Array] }, Token { type: 'Punctuator', value: '}', loc: [SourceLocation], range: [Array] } ], lines: [ 'class C { foo', ';;bar }' ], lineStartIndices: [ 0, 14 ], _commentCache: WeakMap { <items unknown> }, [Symbol(tokens)]: [ Token { type: 'Keyword', value: 'class', loc: [SourceLocation], range: [Array] }, Token { type: 'Identifier', value: 'C', loc: [SourceLocation], range: [Array] }, Token { type: 'Punctuator', value: '{', loc: [SourceLocation], range: [Array] }, Token { type: 'Identifier', value: 'foo', loc: [SourceLocation], range: [Array] }, Token { type: 'Punctuator', value: ';', loc: [SourceLocation], range: [Array] }, Token { type: 'Punctuator', value: ';', loc: [SourceLocation], range: [Array] }, Token { type: 'Identifier', value: 'bar', loc: [SourceLocation], range: [Array] }, Token { type: 'Punctuator', value: '}', loc: [SourceLocation], range: [Array] } ], [Symbol(comments)]: [], [Symbol(indexMap)]: [Object: null prototype] { '0': 0, '4': 0, '6': 1, '8': 2, '10': 3, '12': 3, '14': 4, '15': 5, '16': 6, '18': 6, '20': 7 }}
一段代码的弄的
class C { foo;;bar }
{ type: 'ClassBody', loc: SourceLocation { start: Position { line: 1, column: 8 }, end: Position { line: 2, column: 7 } }, range: [ 8, 21 ], body: [ Node { type: 'PropertyDefinition', loc: [SourceLocation], range: [Array], static: false, computed: false, key: [Node], value: null, parent: [Circular *1] }, Node { type: 'PropertyDefinition', loc: [SourceLocation], range: [Array], static: false, computed: false, key: [Node], value: null, parent: [Circular *1] } ], parent: <ref *2> Node { type: 'ClassDeclaration', loc: SourceLocation { start: [Position], end: [Position] }, range: [ 0, 21 ], id: Node { type: 'Identifier', loc: [SourceLocation], range: [Array], name: 'C', parent: [Circular *2] }, superClass: null, body: [Circular *1], parent: Node { type: 'Program', loc: [SourceLocation], range: [Array], body: [Array], sourceType: 'script', comments: [], tokens: [Array], parent: null } }}