NodeGuards.spec.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. import * as ESTree from 'estree';
  2. import { assert } from 'chai';
  3. import { NodeGuards } from '../../../../src/node/NodeGuards';
  4. import { NodeFactory } from '../../../../src/node/NodeFactory';
  5. import { NodeUtils } from '../../../../src/node/NodeUtils';
  6. describe('NodeGuards', () => {
  7. describe('isNodeHasBlockScope', () => {
  8. describe('truthful checks', () => {
  9. describe('Variant #1: block statement of function declaration', () => {
  10. const expectedResult: boolean = true;
  11. const node: ESTree.Node = NodeFactory.blockStatementNode();
  12. const parentNode: ESTree.FunctionDeclaration = NodeFactory.functionDeclarationNode(
  13. 'foo',
  14. [],
  15. node
  16. );
  17. let result: boolean;
  18. before(() => {
  19. NodeUtils.parentizeAst(parentNode);
  20. result = NodeGuards.isNodeHasBlockScope(node, parentNode);
  21. });
  22. it('should check if node has block scope', () => {
  23. assert.equal(result, expectedResult);
  24. });
  25. });
  26. describe('Variant #2: block statement of function expression', () => {
  27. const expectedResult: boolean = true;
  28. const node: ESTree.Node = NodeFactory.blockStatementNode();
  29. const parentNode: ESTree.FunctionExpression = NodeFactory.functionExpressionNode(
  30. [],
  31. node
  32. );
  33. let result: boolean;
  34. before(() => {
  35. NodeUtils.parentizeAst(parentNode);
  36. result = NodeGuards.isNodeHasBlockScope(node, parentNode);
  37. });
  38. it('should check if node has block scope', () => {
  39. assert.equal(result, expectedResult);
  40. });
  41. });
  42. });
  43. describe('false checks', () => {
  44. describe('Variant #1: switch-case node', () => {
  45. const expectedResult: boolean = false;
  46. const node: ESTree.Node = NodeFactory.switchCaseNode(
  47. NodeFactory.literalNode(1),
  48. []
  49. );
  50. const parentNode: ESTree.FunctionDeclaration = NodeFactory.functionDeclarationNode(
  51. 'foo',
  52. [],
  53. NodeFactory.blockStatementNode([
  54. NodeFactory.switchStatementNode(
  55. NodeFactory.memberExpressionNode(
  56. NodeFactory.identifierNode('bar'),
  57. NodeFactory.updateExpressionNode(
  58. '++',
  59. NodeFactory.identifierNode('baz')
  60. ),
  61. true
  62. ),
  63. [node]
  64. )
  65. ])
  66. );
  67. let result: boolean;
  68. before(() => {
  69. NodeUtils.parentizeAst(parentNode);
  70. result = NodeGuards.isNodeHasBlockScope(node, parentNode);
  71. });
  72. it('should check if node has block scope', () => {
  73. assert.equal(result, expectedResult);
  74. });
  75. });
  76. describe('Variant #2: literal node', () => {
  77. const expectedResult: boolean = false;
  78. const node: ESTree.Node = NodeFactory.literalNode(1);
  79. const parentNode: ESTree.FunctionDeclaration = NodeFactory.functionDeclarationNode(
  80. 'foo',
  81. [],
  82. NodeFactory.blockStatementNode([
  83. NodeFactory.expressionStatementNode(
  84. NodeFactory.callExpressionNode(
  85. NodeFactory.identifierNode('bar'),
  86. [node]
  87. )
  88. )
  89. ])
  90. );
  91. let result: boolean;
  92. before(() => {
  93. NodeUtils.parentizeAst(parentNode);
  94. result = NodeGuards.isNodeHasBlockScope(node, parentNode);
  95. });
  96. it('should check if node has block scope', () => {
  97. assert.equal(result, expectedResult);
  98. });
  99. });
  100. describe('Variant #3: block statement of if statement', () => {
  101. const expectedResult: boolean = false;
  102. const node: ESTree.Node = NodeFactory.blockStatementNode();
  103. const parentNode: ESTree.IfStatement = NodeFactory.ifStatementNode(
  104. NodeFactory.identifierNode('foo'),
  105. node
  106. );
  107. let result: boolean;
  108. before(() => {
  109. NodeUtils.parentizeAst(parentNode);
  110. result = NodeGuards.isNodeHasBlockScope(node, parentNode);
  111. });
  112. it('should check if node has block scope', () => {
  113. assert.equal(result, expectedResult);
  114. });
  115. });
  116. });
  117. });
  118. describe('isNodeHasScope', () => {
  119. describe('truthful checks', () => {
  120. describe('Variant #1: program node', () => {
  121. const expectedResult: boolean = true;
  122. const node: ESTree.Node = NodeFactory.programNode();
  123. let result: boolean;
  124. before(() => {
  125. result = NodeGuards.isNodeHasScope(node);
  126. });
  127. it('should check if node has scope', () => {
  128. assert.equal(result, expectedResult);
  129. });
  130. });
  131. describe('Variant #2: block statement node', () => {
  132. const expectedResult: boolean = true;
  133. const node: ESTree.Node = NodeFactory.blockStatementNode();
  134. let result: boolean;
  135. before(() => {
  136. result = NodeGuards.isNodeHasScope(node);
  137. });
  138. it('should check if node has scope', () => {
  139. assert.equal(result, expectedResult);
  140. });
  141. });
  142. describe('Variant #3: switch case node', () => {
  143. const expectedResult: boolean = true;
  144. const node: ESTree.Node = NodeFactory.switchCaseNode(
  145. NodeFactory.literalNode(1),
  146. []
  147. );
  148. let result: boolean;
  149. before(() => {
  150. result = NodeGuards.isNodeHasScope(node);
  151. });
  152. it('should check if node has scope', () => {
  153. assert.equal(result, expectedResult);
  154. });
  155. });
  156. });
  157. describe('false checks', () => {
  158. describe('Variant #1: literal node', () => {
  159. const expectedResult: boolean = false;
  160. const node: ESTree.Node = NodeFactory.literalNode(1);
  161. let result: boolean;
  162. before(() => {
  163. result = NodeGuards.isNodeHasScope(node);
  164. });
  165. it('should check if node has scope', () => {
  166. assert.equal(result, expectedResult);
  167. });
  168. });
  169. describe('Variant #2: identifier node', () => {
  170. const expectedResult: boolean = false;
  171. const node: ESTree.Node = NodeFactory.identifierNode('foo');
  172. let result: boolean;
  173. before(() => {
  174. result = NodeGuards.isNodeHasScope(node);
  175. });
  176. it('should check if node has scope', () => {
  177. assert.equal(result, expectedResult);
  178. });
  179. });
  180. describe('Variant #3: if-statement node', () => {
  181. const expectedResult: boolean = false;
  182. const node: ESTree.Node = NodeFactory.ifStatementNode(
  183. NodeFactory.identifierNode('foo'),
  184. NodeFactory.blockStatementNode()
  185. );
  186. let result: boolean;
  187. before(() => {
  188. result = NodeGuards.isNodeHasScope(node);
  189. });
  190. it('should check if node has scope', () => {
  191. assert.equal(result, expectedResult);
  192. });
  193. });
  194. describe('Variant #4: switch-statement node', () => {
  195. const expectedResult: boolean = false;
  196. const node: ESTree.Node = NodeFactory.switchStatementNode(
  197. NodeFactory.identifierNode('foo'),
  198. []
  199. );
  200. let result: boolean;
  201. before(() => {
  202. result = NodeGuards.isNodeHasScope(node);
  203. });
  204. it('should check if node has scope', () => {
  205. assert.equal(result, expectedResult);
  206. });
  207. });
  208. });
  209. });
  210. describe('isReplaceableIdentifierNode', () => {
  211. describe('truthful checks', () => {
  212. describe('Variant #1: parent node is function declaration node', () => {
  213. const expectedResult: boolean = true;
  214. const identifier: ESTree.Identifier = NodeFactory.identifierNode('foo');
  215. const parentNode: ESTree.Node = NodeFactory.functionDeclarationNode(
  216. 'bar',
  217. [identifier],
  218. NodeFactory.blockStatementNode()
  219. );
  220. let result: boolean;
  221. before(() => {
  222. NodeUtils.parentizeAst(parentNode);
  223. result = NodeGuards.isReplaceableIdentifierNode(identifier, parentNode);
  224. });
  225. it('should check if input identifier can be replaced by obfuscated one', () => {
  226. assert.equal(result, expectedResult);
  227. });
  228. });
  229. describe('Variant #2: parent node is computed property node', () => {
  230. const expectedResult: boolean = true;
  231. const identifier: ESTree.Identifier = NodeFactory.identifierNode('foo');
  232. const parentNode: ESTree.Node = NodeFactory.propertyNode(
  233. identifier,
  234. NodeFactory.literalNode('bar'),
  235. true
  236. );
  237. let result: boolean;
  238. before(() => {
  239. NodeUtils.parentizeAst(parentNode);
  240. result = NodeGuards.isReplaceableIdentifierNode(identifier, parentNode);
  241. });
  242. it('should check if input identifier can be replaced by obfuscated one', () => {
  243. assert.equal(result, expectedResult);
  244. });
  245. });
  246. describe('Variant #4: parent node is computed member expression node', () => {
  247. const expectedResult: boolean = true;
  248. const identifier: ESTree.Identifier = NodeFactory.identifierNode('foo');
  249. const parentNode: ESTree.Node = NodeFactory.memberExpressionNode(
  250. NodeFactory.identifierNode('bar'),
  251. identifier,
  252. true
  253. );
  254. let result: boolean;
  255. before(() => {
  256. NodeUtils.parentizeAst(parentNode);
  257. result = NodeGuards.isReplaceableIdentifierNode(identifier, parentNode);
  258. });
  259. it('should check if input identifier can be replaced by obfuscated one', () => {
  260. assert.equal(result, expectedResult);
  261. });
  262. });
  263. describe('Variant #4: parent node is computed method definition node', () => {
  264. const expectedResult: boolean = true;
  265. const identifier: ESTree.Identifier = NodeFactory.identifierNode('foo');
  266. const parentNode: ESTree.Node = NodeFactory.methodDefinitionNode(
  267. identifier,
  268. NodeFactory.functionExpressionNode([], NodeFactory.blockStatementNode()),
  269. 'method',
  270. true
  271. );
  272. let result: boolean;
  273. before(() => {
  274. NodeUtils.parentizeAst(parentNode);
  275. result = NodeGuards.isReplaceableIdentifierNode(identifier, parentNode);
  276. });
  277. it('should check if input identifier can be replaced by obfuscated one', () => {
  278. assert.equal(result, expectedResult);
  279. });
  280. });
  281. });
  282. describe('false checks', () => {
  283. describe('Variant #1: node isn\'t an identifier', () => {
  284. const expectedResult: boolean = false;
  285. const literal: ESTree.Literal = NodeFactory.literalNode(1);
  286. const parentNode: ESTree.Node = NodeFactory.expressionStatementNode(
  287. NodeFactory.callExpressionNode(
  288. NodeFactory.identifierNode('foo'),
  289. [literal]
  290. )
  291. );
  292. let result: boolean;
  293. before(() => {
  294. NodeUtils.parentizeAst(parentNode);
  295. result = NodeGuards.isReplaceableIdentifierNode(literal, parentNode);
  296. });
  297. it('should check if input identifier can be replaced by obfuscated one', () => {
  298. assert.equal(result, expectedResult);
  299. });
  300. });
  301. describe('Variant #2: parent node isn\'t computed property node', () => {
  302. const expectedResult: boolean = false;
  303. const identifier: ESTree.Identifier = NodeFactory.identifierNode('foo');
  304. const parentNode: ESTree.Node = NodeFactory.propertyNode(
  305. identifier,
  306. NodeFactory.literalNode('bar'),
  307. false
  308. );
  309. let result: boolean;
  310. before(() => {
  311. NodeUtils.parentizeAst(parentNode);
  312. result = NodeGuards.isReplaceableIdentifierNode(identifier, parentNode);
  313. });
  314. it('should check if input identifier can be replaced by obfuscated one', () => {
  315. assert.equal(result, expectedResult);
  316. });
  317. });
  318. describe('Variant #3: parent node isn\'t computed member expression node', () => {
  319. const expectedResult: boolean = false;
  320. const identifier: ESTree.Identifier = NodeFactory.identifierNode('foo');
  321. const parentNode: ESTree.Node = NodeFactory.memberExpressionNode(
  322. NodeFactory.identifierNode('bar'),
  323. identifier,
  324. false
  325. );
  326. let result: boolean;
  327. before(() => {
  328. NodeUtils.parentizeAst(parentNode);
  329. result = NodeGuards.isReplaceableIdentifierNode(identifier, parentNode);
  330. });
  331. it('should check if input identifier can be replaced by obfuscated one', () => {
  332. assert.equal(result, expectedResult);
  333. });
  334. });
  335. describe('Variant #4: parent node isn\'t computed method definition node', () => {
  336. const expectedResult: boolean = false;
  337. const identifier: ESTree.Identifier = NodeFactory.identifierNode('foo');
  338. const parentNode: ESTree.Node = NodeFactory.methodDefinitionNode(
  339. identifier,
  340. NodeFactory.functionExpressionNode([], NodeFactory.blockStatementNode()),
  341. 'method',
  342. false
  343. );
  344. let result: boolean;
  345. before(() => {
  346. NodeUtils.parentizeAst(parentNode);
  347. result = NodeGuards.isReplaceableIdentifierNode(identifier, parentNode);
  348. });
  349. it('should check if input identifier can be replaced by obfuscated one', () => {
  350. assert.equal(result, expectedResult);
  351. });
  352. });
  353. });
  354. });
  355. });