Nodes.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 {Expression} callee
  95. * @param {(Expression | SpreadElement)[]} args
  96. * @returns {CallExpression}
  97. */
  98. public static getCallExpressionNode (
  99. callee: ESTree.Expression,
  100. args: (ESTree.Expression | ESTree.SpreadElement)[] = []
  101. ): ESTree.CallExpression {
  102. return {
  103. type: NodeType.CallExpression,
  104. callee,
  105. arguments: args,
  106. obfuscatedNode: false
  107. };
  108. }
  109. /**
  110. * @param {Identifier} label
  111. * @returns {ContinueStatement}
  112. */
  113. public static getContinueStatement (label?: ESTree.Identifier): ESTree.ContinueStatement {
  114. const continueStatementNode: ESTree.ContinueStatement = {
  115. type: NodeType.ContinueStatement,
  116. obfuscatedNode: false
  117. };
  118. if (label) {
  119. continueStatementNode.label = label;
  120. }
  121. return continueStatementNode;
  122. }
  123. /**
  124. * @param {Expression} expression
  125. * @returns {ExpressionStatement}
  126. */
  127. public static getExpressionStatementNode (expression: ESTree.Expression): ESTree.ExpressionStatement {
  128. return {
  129. type: NodeType.ExpressionStatement,
  130. expression,
  131. obfuscatedNode: false
  132. };
  133. }
  134. /**
  135. * @param {string} functionName
  136. * @param {Identifier[]} params
  137. * @param {BlockStatement} body
  138. * @returns {FunctionDeclaration}
  139. */
  140. public static getFunctionDeclarationNode (
  141. functionName: string,
  142. params: ESTree.Identifier[],
  143. body: ESTree.BlockStatement
  144. ): ESTree.FunctionDeclaration {
  145. return {
  146. type: NodeType.FunctionDeclaration,
  147. id: Nodes.getIdentifierNode(functionName),
  148. params,
  149. body,
  150. generator: false,
  151. obfuscatedNode: false
  152. };
  153. }
  154. /**
  155. * @param {Identifier[]} params
  156. * @param {BlockStatement} body
  157. * @returns {FunctionExpression}
  158. */
  159. public static getFunctionExpressionNode (
  160. params: ESTree.Identifier[],
  161. body: ESTree.BlockStatement
  162. ): ESTree.FunctionExpression {
  163. return {
  164. type: NodeType.FunctionExpression,
  165. params,
  166. body,
  167. generator: false,
  168. obfuscatedNode: false
  169. };
  170. }
  171. /**
  172. * @param {Expression} test
  173. * @param {BlockStatement} consequent
  174. * @param {BlockStatement} alternate
  175. * @returns {IfStatement}
  176. */
  177. public static getIfStatementNode (
  178. test: ESTree.Expression,
  179. consequent: ESTree.BlockStatement,
  180. alternate?: ESTree.BlockStatement
  181. ): ESTree.IfStatement {
  182. return {
  183. type: NodeType.IfStatement,
  184. test,
  185. consequent,
  186. ...alternate && { alternate },
  187. obfuscatedNode: false
  188. };
  189. }
  190. /**
  191. * @param {string} name
  192. * @returns {Identifier}
  193. */
  194. public static getIdentifierNode (name: string): ESTree.Identifier {
  195. return {
  196. type: NodeType.Identifier,
  197. name,
  198. obfuscatedNode: false
  199. };
  200. }
  201. /**
  202. * @param {boolean | number | string} value
  203. * @param {string} raw
  204. * @returns {Literal}
  205. */
  206. public static getLiteralNode (value: boolean | number | string, raw?: string): ESTree.Literal {
  207. raw = raw !== undefined ? raw : `'${value}'`;
  208. return {
  209. type: NodeType.Literal,
  210. value,
  211. raw,
  212. 'x-verbatim-property': {
  213. content: raw,
  214. precedence: escodegen.Precedence.Primary
  215. },
  216. obfuscatedNode: false
  217. };
  218. }
  219. /**
  220. * @param {LogicalOperator} operator
  221. * @param {Expression} left
  222. * @param {Expression} right
  223. * @returns {LogicalExpression}
  224. */
  225. public static getLogicalExpressionNode (
  226. operator: ESTree.LogicalOperator,
  227. left: ESTree.Expression,
  228. right: ESTree.Expression,
  229. ): ESTree.LogicalExpression {
  230. return {
  231. type: NodeType.LogicalExpression,
  232. operator,
  233. left,
  234. right,
  235. obfuscatedNode: false
  236. };
  237. }
  238. /**
  239. * @param {Expression | Super} object
  240. * @param {Expression} property
  241. * @param {boolean} computed
  242. * @returns {MemberExpression}
  243. */
  244. public static getMemberExpressionNode (
  245. object: ESTree.Expression | ESTree.Super,
  246. property: ESTree.Expression,
  247. computed: boolean = false
  248. ): ESTree.MemberExpression {
  249. return {
  250. type: NodeType.MemberExpression,
  251. computed,
  252. object,
  253. property,
  254. obfuscatedNode: false
  255. };
  256. }
  257. /**
  258. * @param {Expression} key
  259. * @param {FunctionExpression} value
  260. * @param {"constructor" | "method" | "get" | "set"} kind
  261. * @param {boolean} computed
  262. * @returns {MethodDefinition}
  263. */
  264. public static getMethodDefinitionNode (
  265. key: ESTree.Expression,
  266. value: ESTree.FunctionExpression,
  267. kind: 'constructor' | 'method' | 'get' | 'set',
  268. computed: boolean,
  269. ): ESTree.MethodDefinition {
  270. return {
  271. type: NodeType.MethodDefinition,
  272. key,
  273. value,
  274. kind,
  275. computed,
  276. static: false,
  277. obfuscatedNode: false
  278. };
  279. }
  280. /**
  281. * @param {Property[]} properties
  282. * @returns {ObjectExpression}
  283. */
  284. public static getObjectExpressionNode (properties: ESTree.Property[]): ESTree.ObjectExpression {
  285. return {
  286. type: NodeType.ObjectExpression,
  287. properties,
  288. obfuscatedNode: false
  289. };
  290. }
  291. /**
  292. * @param {Expression} key
  293. * @param {Expression | Pattern} value
  294. * @param {boolean} computed
  295. * @returns {Property}
  296. */
  297. public static getPropertyNode (
  298. key: ESTree.Expression,
  299. value: ESTree.Expression | ESTree.Pattern,
  300. computed: boolean = false
  301. ): ESTree.Property {
  302. return {
  303. type: NodeType.Property,
  304. key,
  305. value,
  306. kind: 'init',
  307. method: false,
  308. shorthand: false,
  309. computed,
  310. obfuscatedNode: false
  311. };
  312. }
  313. /**
  314. * @param {UnaryOperator} operator
  315. * @param {Expression} argument
  316. * @param {true} prefix
  317. * @returns {UnaryExpression}
  318. */
  319. public static getUnaryExpressionNode (
  320. operator: ESTree.UnaryOperator,
  321. argument: ESTree.Expression,
  322. prefix: true = true
  323. ): ESTree.UnaryExpression {
  324. return {
  325. type: NodeType.UnaryExpression,
  326. operator,
  327. argument,
  328. prefix,
  329. obfuscatedNode: false
  330. };
  331. }
  332. /**
  333. * @param {Expression} argument
  334. * @returns {ReturnStatement}
  335. */
  336. public static getReturnStatementNode (argument: ESTree.Expression): ESTree.ReturnStatement {
  337. return {
  338. type: NodeType.ReturnStatement,
  339. argument,
  340. obfuscatedNode: false
  341. };
  342. }
  343. /**
  344. * @param {Expression} discriminant
  345. * @param {SwitchCase[]} cases
  346. * @returns {SwitchStatement}
  347. */
  348. public static getSwitchStatementNode (
  349. discriminant: ESTree.Expression,
  350. cases: ESTree.SwitchCase[]
  351. ): ESTree.SwitchStatement {
  352. return {
  353. type: NodeType.SwitchStatement,
  354. discriminant,
  355. cases,
  356. obfuscatedNode: false
  357. };
  358. }
  359. /**
  360. * @param {Expression} test
  361. * @param {Statement[]} consequent
  362. * @returns {SwitchCase}
  363. */
  364. public static getSwitchCaseNode (test: ESTree.Expression, consequent: ESTree.Statement[]): ESTree.SwitchCase {
  365. return {
  366. type: NodeType.SwitchCase,
  367. test,
  368. consequent,
  369. obfuscatedNode: false
  370. };
  371. }
  372. /**
  373. * @param {UpdateOperator} operator
  374. * @param {Expression} argumentExpr
  375. * @returns {UpdateExpression}
  376. */
  377. public static getUpdateExpressionNode (operator: ESTree.UpdateOperator, argumentExpr: ESTree.Expression): ESTree.UpdateExpression {
  378. return {
  379. type: NodeType.UpdateExpression,
  380. operator,
  381. argument: argumentExpr,
  382. prefix: false,
  383. obfuscatedNode: false
  384. };
  385. }
  386. /**
  387. * @param {VariableDeclarator[]} declarations
  388. * @param {string} kind
  389. * @returns {VariableDeclaration}
  390. */
  391. public static getVariableDeclarationNode (
  392. declarations: ESTree.VariableDeclarator[] = [],
  393. kind: 'var' | 'let' | 'const' = 'var'
  394. ): ESTree.VariableDeclaration {
  395. return {
  396. type: NodeType.VariableDeclaration,
  397. declarations,
  398. kind,
  399. obfuscatedNode: false
  400. };
  401. }
  402. /**
  403. * @param {Identifier} id
  404. * @param {Expression | null} init
  405. * @returns {VariableDeclarator}
  406. */
  407. public static getVariableDeclaratorNode (id: ESTree.Identifier, init: ESTree.Expression | null): ESTree.VariableDeclarator {
  408. return {
  409. type: NodeType.VariableDeclarator,
  410. id,
  411. init,
  412. obfuscatedNode: false
  413. };
  414. }
  415. /**
  416. * @param {Expression} test
  417. * @param {Statement} body
  418. * @returns {WhileStatement}
  419. */
  420. public static getWhileStatementNode (test: ESTree.Expression, body: ESTree.Statement): ESTree.WhileStatement {
  421. return {
  422. type: NodeType.WhileStatement,
  423. test,
  424. body,
  425. obfuscatedNode: false
  426. };
  427. }
  428. }