Nodes.ts 11 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/NodeType';
  5. export class Nodes {
  6. /**
  7. * @param body
  8. * @returns {ESTree.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 elements
  20. * @return {ESTree.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 operator
  32. * @param left
  33. * @param right
  34. * @return {ESTree.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 operator
  51. * @param left
  52. * @param right
  53. * @returns {ESTree.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 body
  70. * @returns {ESTree.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 label
  81. * @return {ESTree.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 body
  95. * @returns {ESTree.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 callee
  107. * @param args
  108. * @returns {ESTree.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 label
  123. * @return {ESTree.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
  137. * @returns {ESTree.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 functionName
  148. * @param params
  149. * @param body
  150. * @returns {ESTree.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 params
  168. * @param body
  169. * @returns {ESTree.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 test
  185. * @param consequent
  186. * @param alternate
  187. * @returns {ESTree.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 name
  204. * @returns {ESTree.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 label
  215. * @param body
  216. * @returns {ESTree.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 value
  228. * @param raw
  229. * @returns {ESTree.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 operator
  246. * @param left
  247. * @param right
  248. * @returns {ESTree.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 object
  265. * @param property
  266. * @param computed
  267. * @return {ESTree.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 properties
  284. * @return {ESTree.ObjectExpression}
  285. */
  286. public static getObjectExpressionNode (properties: ESTree.Property[]): ESTree.ObjectExpression {
  287. return {
  288. type: NodeType.ObjectExpression,
  289. properties,
  290. obfuscatedNode: false
  291. };
  292. }
  293. /**
  294. * @return {ESTree.Property}
  295. */
  296. public static getPropertyNode (
  297. key: ESTree.Expression,
  298. value: ESTree.Expression | ESTree.Pattern,
  299. computed: boolean = false
  300. ): ESTree.Property {
  301. return {
  302. type: NodeType.Property,
  303. key,
  304. value,
  305. kind: 'init',
  306. method: false,
  307. shorthand: false,
  308. computed,
  309. obfuscatedNode: false
  310. };
  311. }
  312. /**
  313. * @param operator
  314. * @param argument
  315. * @param prefix
  316. * @returns {ESTree.Literal}
  317. */
  318. public static getUnaryExpressionNode (
  319. operator: ESTree.UnaryOperator,
  320. argument: ESTree.Expression,
  321. prefix: boolean = true
  322. ): ESTree.UnaryExpression {
  323. return {
  324. type: NodeType.UnaryExpression,
  325. operator,
  326. argument,
  327. prefix,
  328. obfuscatedNode: false
  329. };
  330. }
  331. /**
  332. * @param argument
  333. * @return {ReturnStatement}
  334. */
  335. public static getReturnStatementNode (argument: ESTree.Expression): ESTree.ReturnStatement {
  336. return {
  337. type: NodeType.ReturnStatement,
  338. argument,
  339. obfuscatedNode: false
  340. };
  341. }
  342. /**
  343. * @param discriminant
  344. * @param cases
  345. * @returns {ESTree.SwitchStatement}
  346. */
  347. public static getSwitchStatementNode (
  348. discriminant: ESTree.Expression,
  349. cases: ESTree.SwitchCase[]
  350. ): ESTree.SwitchStatement {
  351. return {
  352. type: NodeType.SwitchStatement,
  353. discriminant,
  354. cases,
  355. obfuscatedNode: false
  356. };
  357. }
  358. /**
  359. * @param test
  360. * @param consequent
  361. * @returns {ESTree.SwitchCase}
  362. */
  363. public static getSwitchCaseNode (test: ESTree.Expression, consequent: ESTree.Statement[]): ESTree.SwitchCase {
  364. return {
  365. type: NodeType.SwitchCase,
  366. test,
  367. consequent,
  368. obfuscatedNode: false
  369. };
  370. }
  371. /**
  372. * @param operator
  373. * @param argumentExpr
  374. * @returns {ESTree.UpdateExpression}
  375. */
  376. public static getUpdateExpressionNode (operator: ESTree.UpdateOperator, argumentExpr: ESTree.Expression): ESTree.UpdateExpression {
  377. return {
  378. type: NodeType.UpdateExpression,
  379. operator,
  380. argument: argumentExpr,
  381. prefix: false,
  382. obfuscatedNode: false
  383. };
  384. }
  385. /**
  386. * @param declarations
  387. * @param kind
  388. * @returns {ESTree.VariableDeclaration}
  389. */
  390. public static getVariableDeclarationNode (
  391. declarations: ESTree.VariableDeclarator[] = [],
  392. kind: 'var' | 'let' | 'const' = 'var'
  393. ): ESTree.VariableDeclaration {
  394. return {
  395. type: NodeType.VariableDeclaration,
  396. declarations,
  397. kind,
  398. obfuscatedNode: false
  399. };
  400. }
  401. /**
  402. * @param id
  403. * @param init
  404. * @returns {ESTree.VariableDeclarator}
  405. */
  406. public static getVariableDeclaratorNode (id: ESTree.Identifier, init: any): ESTree.VariableDeclarator {
  407. return {
  408. type: NodeType.VariableDeclarator,
  409. id,
  410. init,
  411. obfuscatedNode: false
  412. };
  413. }
  414. /**
  415. * @param test
  416. * @param body
  417. * @return {ESTree.WhileStatement}
  418. */
  419. public static getWhileStatementNode (test: ESTree.Expression, body: ESTree.Statement): ESTree.WhileStatement {
  420. return {
  421. type: NodeType.WhileStatement,
  422. test,
  423. body,
  424. obfuscatedNode: false
  425. };
  426. }
  427. }