NodeFactory.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /* tslint:disable:max-file-line-count */
  2. import * as escodegen from 'escodegen-wallaby';
  3. import * as ESTree from 'estree';
  4. import { TStatement } from '../types/node/TStatement';
  5. import { NodeType } from '../enums/node/NodeType';
  6. export class NodeFactory {
  7. /**
  8. * @param {TStatement[]} body
  9. * @returns {Program}
  10. */
  11. public static programNode (body: TStatement[] = []): ESTree.Program {
  12. return {
  13. type: NodeType.Program,
  14. body,
  15. sourceType: 'script',
  16. metadata: { ignoredNode: false }
  17. };
  18. }
  19. /**
  20. * @param {(Expression | SpreadElement)[]} elements
  21. * @returns {ArrayExpression}
  22. */
  23. public static arrayExpressionNode (
  24. elements: (ESTree.Expression | ESTree.SpreadElement)[] = []
  25. ): ESTree.ArrayExpression {
  26. return {
  27. type: NodeType.ArrayExpression,
  28. elements,
  29. metadata: { ignoredNode: false }
  30. };
  31. }
  32. /**
  33. * @param {AssignmentOperator} operator
  34. * @param {Pattern | MemberExpression} left
  35. * @param {Expression} right
  36. * @returns {AssignmentExpression}
  37. */
  38. public static assignmentExpressionNode (
  39. operator: ESTree.AssignmentOperator,
  40. left: ESTree.Pattern | ESTree.MemberExpression,
  41. right: ESTree.Expression
  42. ): ESTree.AssignmentExpression {
  43. return {
  44. type: NodeType.AssignmentExpression,
  45. operator,
  46. left,
  47. right,
  48. metadata: { ignoredNode: false }
  49. };
  50. }
  51. /**
  52. * @param {BinaryOperator} operator
  53. * @param {Expression} left
  54. * @param {Expression} right
  55. * @returns {BinaryExpression}
  56. */
  57. public static binaryExpressionNode (
  58. operator: ESTree.BinaryOperator,
  59. left: ESTree.Expression,
  60. right: ESTree.Expression,
  61. ): ESTree.BinaryExpression {
  62. return {
  63. type: NodeType.BinaryExpression,
  64. operator,
  65. left,
  66. right,
  67. metadata: { ignoredNode: false }
  68. };
  69. }
  70. /**
  71. * @param {Statement[]} body
  72. * @returns {BlockStatement}
  73. */
  74. public static blockStatementNode (body: ESTree.Statement[] = []): ESTree.BlockStatement {
  75. return {
  76. type: NodeType.BlockStatement,
  77. body,
  78. metadata: { ignoredNode: false }
  79. };
  80. }
  81. /**
  82. * @param {Identifier} label
  83. * @returns {BreakStatement}
  84. */
  85. public static breakStatement (label?: ESTree.Identifier): ESTree.BreakStatement {
  86. return {
  87. type: NodeType.BreakStatement,
  88. label,
  89. metadata: { ignoredNode: false }
  90. };
  91. }
  92. /**
  93. * @param {Expression} callee
  94. * @param {(Expression | SpreadElement)[]} args
  95. * @returns {CallExpression}
  96. */
  97. public static callExpressionNode (
  98. callee: ESTree.Expression,
  99. args: (ESTree.Expression | ESTree.SpreadElement)[] = []
  100. ): ESTree.CallExpression {
  101. return {
  102. type: NodeType.CallExpression,
  103. callee,
  104. arguments: args,
  105. metadata: { ignoredNode: false }
  106. };
  107. }
  108. /**
  109. * @param {Identifier} label
  110. * @returns {ContinueStatement}
  111. */
  112. public static continueStatement (label?: ESTree.Identifier): ESTree.ContinueStatement {
  113. return {
  114. type: NodeType.ContinueStatement,
  115. label,
  116. metadata: { ignoredNode: false }
  117. };
  118. }
  119. /**
  120. * @param {Literal} expression
  121. * @param {string} directive
  122. * @returns {Directive}
  123. */
  124. public static directiveNode (
  125. expression: ESTree.Literal,
  126. directive: string
  127. ): ESTree.Directive {
  128. return {
  129. type: NodeType.ExpressionStatement,
  130. expression,
  131. directive,
  132. metadata: { ignoredNode: false }
  133. };
  134. }
  135. /**
  136. * @param {Expression} expression
  137. * @returns {ExpressionStatement}
  138. */
  139. public static expressionStatementNode (expression: ESTree.Expression): ESTree.ExpressionStatement {
  140. return {
  141. type: NodeType.ExpressionStatement,
  142. expression,
  143. metadata: { ignoredNode: false }
  144. };
  145. }
  146. /**
  147. * @param {string} functionName
  148. * @param {Identifier[]} params
  149. * @param {BlockStatement} body
  150. * @returns {FunctionDeclaration}
  151. */
  152. public static functionDeclarationNode (
  153. functionName: string,
  154. params: ESTree.Identifier[],
  155. body: ESTree.BlockStatement
  156. ): ESTree.FunctionDeclaration {
  157. return {
  158. type: NodeType.FunctionDeclaration,
  159. id: NodeFactory.identifierNode(functionName),
  160. params,
  161. body,
  162. generator: false,
  163. metadata: { ignoredNode: false }
  164. };
  165. }
  166. /**
  167. * @param {Identifier[]} params
  168. * @param {BlockStatement} body
  169. * @returns {FunctionExpression}
  170. */
  171. public static functionExpressionNode (
  172. params: ESTree.Identifier[],
  173. body: ESTree.BlockStatement
  174. ): ESTree.FunctionExpression {
  175. return {
  176. type: NodeType.FunctionExpression,
  177. params,
  178. body,
  179. generator: false,
  180. metadata: { ignoredNode: false }
  181. };
  182. }
  183. /**
  184. * @param {Expression} test
  185. * @param {BlockStatement} consequent
  186. * @param {BlockStatement} alternate
  187. * @returns {IfStatement}
  188. */
  189. public static ifStatementNode (
  190. test: ESTree.Expression,
  191. consequent: ESTree.BlockStatement,
  192. alternate?: ESTree.BlockStatement
  193. ): ESTree.IfStatement {
  194. return {
  195. type: NodeType.IfStatement,
  196. test,
  197. consequent,
  198. ...alternate && { alternate },
  199. metadata: { ignoredNode: false }
  200. };
  201. }
  202. /**
  203. * @param {string} name
  204. * @returns {Identifier}
  205. */
  206. public static identifierNode (name: string): ESTree.Identifier {
  207. return {
  208. type: NodeType.Identifier,
  209. name,
  210. metadata: { ignoredNode: false }
  211. };
  212. }
  213. /**
  214. * @param {boolean | number | string} value
  215. * @param {string} raw
  216. * @returns {Literal}
  217. */
  218. public static literalNode (value: boolean | number | string, raw?: string): ESTree.Literal {
  219. raw = raw !== undefined ? raw : `'${value}'`;
  220. return {
  221. type: NodeType.Literal,
  222. value,
  223. raw,
  224. 'x-verbatim-property': {
  225. content: raw,
  226. precedence: escodegen.Precedence.Primary
  227. },
  228. metadata: { ignoredNode: false }
  229. };
  230. }
  231. /**
  232. * @param {LogicalOperator} operator
  233. * @param {Expression} left
  234. * @param {Expression} right
  235. * @returns {LogicalExpression}
  236. */
  237. public static logicalExpressionNode (
  238. operator: ESTree.LogicalOperator,
  239. left: ESTree.Expression,
  240. right: ESTree.Expression,
  241. ): ESTree.LogicalExpression {
  242. return {
  243. type: NodeType.LogicalExpression,
  244. operator,
  245. left,
  246. right,
  247. metadata: { ignoredNode: false }
  248. };
  249. }
  250. /**
  251. * @param {Expression | Super} object
  252. * @param {Expression} property
  253. * @param {boolean} computed
  254. * @returns {MemberExpression}
  255. */
  256. public static memberExpressionNode (
  257. object: ESTree.Expression | ESTree.Super,
  258. property: ESTree.Expression,
  259. computed: boolean = false
  260. ): ESTree.MemberExpression {
  261. return {
  262. type: NodeType.MemberExpression,
  263. computed,
  264. object,
  265. property,
  266. metadata: { ignoredNode: false }
  267. };
  268. }
  269. /**
  270. * @param {Expression} key
  271. * @param {FunctionExpression} value
  272. * @param {"constructor" | "method" | "get" | "set"} kind
  273. * @param {boolean} computed
  274. * @returns {MethodDefinition}
  275. */
  276. public static methodDefinitionNode (
  277. key: ESTree.Expression,
  278. value: ESTree.FunctionExpression,
  279. kind: 'constructor' | 'method' | 'get' | 'set',
  280. computed: boolean,
  281. ): ESTree.MethodDefinition {
  282. return {
  283. type: NodeType.MethodDefinition,
  284. key,
  285. value,
  286. kind,
  287. computed,
  288. static: false,
  289. metadata: { ignoredNode: false }
  290. };
  291. }
  292. /**
  293. * @param {Property[]} properties
  294. * @returns {ObjectExpression}
  295. */
  296. public static objectExpressionNode (properties: ESTree.Property[]): ESTree.ObjectExpression {
  297. return {
  298. type: NodeType.ObjectExpression,
  299. properties,
  300. metadata: { ignoredNode: false }
  301. };
  302. }
  303. /**
  304. * @param {Expression} key
  305. * @param {Expression | Pattern} value
  306. * @param {boolean} computed
  307. * @returns {Property}
  308. */
  309. public static propertyNode (
  310. key: ESTree.Expression,
  311. value: ESTree.Expression | ESTree.Pattern,
  312. computed: boolean = false
  313. ): ESTree.Property {
  314. return {
  315. type: NodeType.Property,
  316. key,
  317. value,
  318. kind: 'init',
  319. method: false,
  320. shorthand: false,
  321. computed,
  322. metadata: { ignoredNode: false }
  323. };
  324. }
  325. /**
  326. * @param {Expression} argument
  327. * @returns {ReturnStatement}
  328. */
  329. public static returnStatementNode (argument: ESTree.Expression): ESTree.ReturnStatement {
  330. return {
  331. type: NodeType.ReturnStatement,
  332. argument,
  333. metadata: { ignoredNode: false }
  334. };
  335. }
  336. /**
  337. * @param {Expression} discriminant
  338. * @param {SwitchCase[]} cases
  339. * @returns {SwitchStatement}
  340. */
  341. public static switchStatementNode (
  342. discriminant: ESTree.Expression,
  343. cases: ESTree.SwitchCase[]
  344. ): ESTree.SwitchStatement {
  345. return {
  346. type: NodeType.SwitchStatement,
  347. discriminant,
  348. cases,
  349. metadata: { ignoredNode: false }
  350. };
  351. }
  352. /**
  353. * @param {Expression} test
  354. * @param {Statement[]} consequent
  355. * @returns {SwitchCase}
  356. */
  357. public static switchCaseNode (test: ESTree.Expression, consequent: ESTree.Statement[]): ESTree.SwitchCase {
  358. return {
  359. type: NodeType.SwitchCase,
  360. test,
  361. consequent,
  362. metadata: { ignoredNode: false }
  363. };
  364. }
  365. /**
  366. * @param {UnaryOperator} operator
  367. * @param {Expression} argument
  368. * @param {true} prefix
  369. * @returns {UnaryExpression}
  370. */
  371. public static unaryExpressionNode (
  372. operator: ESTree.UnaryOperator,
  373. argument: ESTree.Expression,
  374. prefix: true = true
  375. ): ESTree.UnaryExpression {
  376. return {
  377. type: NodeType.UnaryExpression,
  378. operator,
  379. argument,
  380. prefix,
  381. metadata: { ignoredNode: false }
  382. };
  383. }
  384. /**
  385. * @param {UpdateOperator} operator
  386. * @param {Expression} argumentExpr
  387. * @returns {UpdateExpression}
  388. */
  389. public static updateExpressionNode (operator: ESTree.UpdateOperator, argumentExpr: ESTree.Expression): ESTree.UpdateExpression {
  390. return {
  391. type: NodeType.UpdateExpression,
  392. operator,
  393. argument: argumentExpr,
  394. prefix: false,
  395. metadata: { ignoredNode: false }
  396. };
  397. }
  398. /**
  399. * @param {VariableDeclarator[]} declarations
  400. * @param {string} kind
  401. * @returns {VariableDeclaration}
  402. */
  403. public static variableDeclarationNode (
  404. declarations: ESTree.VariableDeclarator[] = [],
  405. kind: 'var' | 'let' | 'const' = 'var'
  406. ): ESTree.VariableDeclaration {
  407. return {
  408. type: NodeType.VariableDeclaration,
  409. declarations,
  410. kind,
  411. metadata: { ignoredNode: false }
  412. };
  413. }
  414. /**
  415. * @param {Identifier} id
  416. * @param {Expression | null} init
  417. * @returns {VariableDeclarator}
  418. */
  419. public static variableDeclaratorNode (id: ESTree.Identifier, init: ESTree.Expression | null): ESTree.VariableDeclarator {
  420. return {
  421. type: NodeType.VariableDeclarator,
  422. id,
  423. init,
  424. metadata: { ignoredNode: false }
  425. };
  426. }
  427. /**
  428. * @param {Expression} test
  429. * @param {Statement} body
  430. * @returns {WhileStatement}
  431. */
  432. public static whileStatementNode (test: ESTree.Expression, body: ESTree.Statement): ESTree.WhileStatement {
  433. return {
  434. type: NodeType.WhileStatement,
  435. test,
  436. body,
  437. metadata: { ignoredNode: false }
  438. };
  439. }
  440. }