AST对象文档
AST基础认识
AST沙盒
AST入门基本知识
image.png

一段代码的souceCode

  1. class C { foo
  2. ;;bar }
  1. SourceCode {
  2. hasBOM: false,
  3. text: 'class C { foo\n;;bar }',
  4. ast: Node {
  5. type: 'Program',
  6. loc: SourceLocation { start: [Position], end: [Position] },
  7. range: [ 0, 21 ],
  8. body: [ [Node] ],
  9. sourceType: 'script',
  10. comments: [],
  11. tokens: [
  12. [Token], [Token],
  13. [Token], [Token],
  14. [Token], [Token],
  15. [Token], [Token]
  16. ],
  17. parent: null
  18. },
  19. parserServices: {},
  20. scopeManager: ScopeManager {
  21. scopes: [ [GlobalScope], [ClassScope] ],
  22. globalScope: <ref *1> GlobalScope {
  23. type: 'global',
  24. set: [Map],
  25. taints: Map(0) {},
  26. dynamic: true,
  27. block: [Node],
  28. through: [],
  29. variables: [Array],
  30. references: [],
  31. variableScope: [Circular *1],
  32. functionExpressionScope: false,
  33. directCallToEvalScope: false,
  34. thisFound: false,
  35. __left: null,
  36. upper: null,
  37. isStrict: false,
  38. childScopes: [Array],
  39. __declaredVariables: [WeakMap],
  40. implicit: [Object]
  41. },
  42. __nodeToScope: WeakMap { <items unknown> },
  43. __currentScope: null,
  44. __options: {
  45. optimistic: false,
  46. directive: false,
  47. nodejsScope: undefined,
  48. impliedStrict: undefined,
  49. sourceType: 'script',
  50. ecmaVersion: 2022,
  51. childVisitorKeys: [Object],
  52. fallback: [Function: getKeys],
  53. ignoreEval: true
  54. },
  55. __declaredVariables: WeakMap { <items unknown> }
  56. },
  57. visitorKeys: {
  58. AssignmentExpression: [ 'left', 'right' ],
  59. AssignmentPattern: [ 'left', 'right' ],
  60. ArrayExpression: [ 'elements' ],
  61. ArrayPattern: [ 'elements' ],
  62. ArrowFunctionExpression: [ 'params', 'body' ],
  63. AwaitExpression: [ 'argument' ],
  64. BlockStatement: [ 'body' ],
  65. BinaryExpression: [ 'left', 'right' ],
  66. BreakStatement: [ 'label' ],
  67. CallExpression: [ 'callee', 'arguments' ],
  68. CatchClause: [ 'param', 'body' ],
  69. ChainExpression: [ 'expression' ],
  70. ClassBody: [ 'body' ],
  71. ClassDeclaration: [ 'id', 'superClass', 'body' ],
  72. ClassExpression: [ 'id', 'superClass', 'body' ],
  73. ConditionalExpression: [ 'test', 'consequent', 'alternate' ],
  74. ContinueStatement: [ 'label' ],
  75. DebuggerStatement: [],
  76. DoWhileStatement: [ 'body', 'test' ],
  77. EmptyStatement: [],
  78. ExportAllDeclaration: [ 'exported', 'source' ],
  79. ExportDefaultDeclaration: [ 'declaration' ],
  80. ExportNamedDeclaration: [ 'declaration', 'specifiers', 'source' ],
  81. ExportSpecifier: [ 'exported', 'local' ],
  82. ExpressionStatement: [ 'expression' ],
  83. ExperimentalRestProperty: [ 'argument' ],
  84. ExperimentalSpreadProperty: [ 'argument' ],
  85. ForStatement: [ 'init', 'test', 'update', 'body' ],
  86. ForInStatement: [ 'left', 'right', 'body' ],
  87. ForOfStatement: [ 'left', 'right', 'body' ],
  88. FunctionDeclaration: [ 'id', 'params', 'body' ],
  89. FunctionExpression: [ 'id', 'params', 'body' ],
  90. Identifier: [],
  91. IfStatement: [ 'test', 'consequent', 'alternate' ],
  92. ImportDeclaration: [ 'specifiers', 'source' ],
  93. ImportDefaultSpecifier: [ 'local' ],
  94. ImportExpression: [ 'source' ],
  95. ImportNamespaceSpecifier: [ 'local' ],
  96. ImportSpecifier: [ 'imported', 'local' ],
  97. JSXAttribute: [ 'name', 'value' ],
  98. JSXClosingElement: [ 'name' ],
  99. JSXElement: [ 'openingElement', 'children', 'closingElement' ],
  100. JSXEmptyExpression: [],
  101. JSXExpressionContainer: [ 'expression' ],
  102. JSXIdentifier: [],
  103. JSXMemberExpression: [ 'object', 'property' ],
  104. JSXNamespacedName: [ 'namespace', 'name' ],
  105. JSXOpeningElement: [ 'name', 'attributes' ],
  106. JSXSpreadAttribute: [ 'argument' ],
  107. JSXText: [],
  108. JSXFragment: [ 'openingFragment', 'children', 'closingFragment' ],
  109. JSXClosingFragment: [],
  110. JSXOpeningFragment: [],
  111. Literal: [],
  112. LabeledStatement: [ 'label', 'body' ],
  113. LogicalExpression: [ 'left', 'right' ],
  114. MemberExpression: [ 'object', 'property' ],
  115. MetaProperty: [ 'meta', 'property' ],
  116. MethodDefinition: [ 'key', 'value' ],
  117. NewExpression: [ 'callee', 'arguments' ],
  118. ObjectExpression: [ 'properties' ],
  119. ObjectPattern: [ 'properties' ],
  120. PrivateIdentifier: [],
  121. Program: [ 'body' ],
  122. Property: [ 'key', 'value' ],
  123. PropertyDefinition: [ 'key', 'value' ],
  124. RestElement: [ 'argument' ],
  125. ReturnStatement: [ 'argument' ],
  126. SequenceExpression: [ 'expressions' ],
  127. SpreadElement: [ 'argument' ],
  128. StaticBlock: [ 'body' ],
  129. Super: [],
  130. SwitchStatement: [ 'discriminant', 'cases' ],
  131. SwitchCase: [ 'test', 'consequent' ],
  132. TaggedTemplateExpression: [ 'tag', 'quasi' ],
  133. TemplateElement: [],
  134. TemplateLiteral: [ 'quasis', 'expressions' ],
  135. ThisExpression: [],
  136. ThrowStatement: [ 'argument' ],
  137. TryStatement: [ 'block', 'handler', 'finalizer' ],
  138. UnaryExpression: [ 'argument' ],
  139. UpdateExpression: [ 'argument' ],
  140. VariableDeclaration: [ 'declarations' ],
  141. VariableDeclarator: [ 'id', 'init' ],
  142. WhileStatement: [ 'test', 'body' ],
  143. WithStatement: [ 'object', 'body' ],
  144. YieldExpression: [ 'argument' ]
  145. },
  146. tokensAndComments: [
  147. Token {
  148. type: 'Keyword',
  149. value: 'class',
  150. loc: [SourceLocation],
  151. range: [Array]
  152. },
  153. Token {
  154. type: 'Identifier',
  155. value: 'C',
  156. loc: [SourceLocation],
  157. range: [Array]
  158. },
  159. Token {
  160. type: 'Punctuator',
  161. value: '{',
  162. loc: [SourceLocation],
  163. range: [Array]
  164. },
  165. Token {
  166. type: 'Identifier',
  167. value: 'foo',
  168. loc: [SourceLocation],
  169. range: [Array]
  170. },
  171. Token {
  172. type: 'Punctuator',
  173. value: ';',
  174. loc: [SourceLocation],
  175. range: [Array]
  176. },
  177. Token {
  178. type: 'Punctuator',
  179. value: ';',
  180. loc: [SourceLocation],
  181. range: [Array]
  182. },
  183. Token {
  184. type: 'Identifier',
  185. value: 'bar',
  186. loc: [SourceLocation],
  187. range: [Array]
  188. },
  189. Token {
  190. type: 'Punctuator',
  191. value: '}',
  192. loc: [SourceLocation],
  193. range: [Array]
  194. }
  195. ],
  196. lines: [ 'class C { foo', ';;bar }' ],
  197. lineStartIndices: [ 0, 14 ],
  198. _commentCache: WeakMap { <items unknown> },
  199. [Symbol(tokens)]: [
  200. Token {
  201. type: 'Keyword',
  202. value: 'class',
  203. loc: [SourceLocation],
  204. range: [Array]
  205. },
  206. Token {
  207. type: 'Identifier',
  208. value: 'C',
  209. loc: [SourceLocation],
  210. range: [Array]
  211. },
  212. Token {
  213. type: 'Punctuator',
  214. value: '{',
  215. loc: [SourceLocation],
  216. range: [Array]
  217. },
  218. Token {
  219. type: 'Identifier',
  220. value: 'foo',
  221. loc: [SourceLocation],
  222. range: [Array]
  223. },
  224. Token {
  225. type: 'Punctuator',
  226. value: ';',
  227. loc: [SourceLocation],
  228. range: [Array]
  229. },
  230. Token {
  231. type: 'Punctuator',
  232. value: ';',
  233. loc: [SourceLocation],
  234. range: [Array]
  235. },
  236. Token {
  237. type: 'Identifier',
  238. value: 'bar',
  239. loc: [SourceLocation],
  240. range: [Array]
  241. },
  242. Token {
  243. type: 'Punctuator',
  244. value: '}',
  245. loc: [SourceLocation],
  246. range: [Array]
  247. }
  248. ],
  249. [Symbol(comments)]: [],
  250. [Symbol(indexMap)]: [Object: null prototype] {
  251. '0': 0,
  252. '4': 0,
  253. '6': 1,
  254. '8': 2,
  255. '10': 3,
  256. '12': 3,
  257. '14': 4,
  258. '15': 5,
  259. '16': 6,
  260. '18': 6,
  261. '20': 7
  262. }
  263. }

一段代码的弄的

  1. class C { foo
  2. ;;bar }
  1. {
  2. type: 'ClassBody',
  3. loc: SourceLocation {
  4. start: Position { line: 1, column: 8 },
  5. end: Position { line: 2, column: 7 }
  6. },
  7. range: [ 8, 21 ],
  8. body: [
  9. Node {
  10. type: 'PropertyDefinition',
  11. loc: [SourceLocation],
  12. range: [Array],
  13. static: false,
  14. computed: false,
  15. key: [Node],
  16. value: null,
  17. parent: [Circular *1]
  18. },
  19. Node {
  20. type: 'PropertyDefinition',
  21. loc: [SourceLocation],
  22. range: [Array],
  23. static: false,
  24. computed: false,
  25. key: [Node],
  26. value: null,
  27. parent: [Circular *1]
  28. }
  29. ],
  30. parent: <ref *2> Node {
  31. type: 'ClassDeclaration',
  32. loc: SourceLocation { start: [Position], end: [Position] },
  33. range: [ 0, 21 ],
  34. id: Node {
  35. type: 'Identifier',
  36. loc: [SourceLocation],
  37. range: [Array],
  38. name: 'C',
  39. parent: [Circular *2]
  40. },
  41. superClass: null,
  42. body: [Circular *1],
  43. parent: Node {
  44. type: 'Program',
  45. loc: [SourceLocation],
  46. range: [Array],
  47. body: [Array],
  48. sourceType: 'script',
  49. comments: [],
  50. tokens: [Array],
  51. parent: null
  52. }
  53. }
  54. }