Nodes.ts 13 KB

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