ScopeAnalyzer.spec.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import 'reflect-metadata';
  2. import { assert } from 'chai';
  3. import * as eslintScope from 'eslint-scope';
  4. import * as ESTree from 'estree';
  5. import { ServiceIdentifiers } from '../../../../src/container/ServiceIdentifiers';
  6. import { IInversifyContainerFacade } from '../../../../src/interfaces/container/IInversifyContainerFacade';
  7. import { IScopeAnalyzer } from '../../../../src/interfaces/analyzers/scope-analyzer/IScopeAnalyzer';
  8. import { InversifyContainerFacade } from '../../../../src/container/InversifyContainerFacade';
  9. import { NodeFactory } from '../../../../src/node/NodeFactory';
  10. describe('ScopeAnalyzer', () => {
  11. let scopeAnalyzer: IScopeAnalyzer;
  12. beforeEach(() => {
  13. const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
  14. inversifyContainerFacade.load('', '', {});
  15. scopeAnalyzer = inversifyContainerFacade
  16. .get<IScopeAnalyzer>(ServiceIdentifiers.IScopeAnalyzer);
  17. });
  18. describe('analyze', () => {
  19. const expectedScopeVariablesLength: number = 1;
  20. const expectedScopeVariablesName: string = 'foo';
  21. let scopeVariablesLength: number;
  22. let scopeVariableName: string;
  23. describe('Variant #1: base analyze of the scope for ProgramNode', () => {
  24. beforeEach(() => {
  25. const programNode: ESTree.Program = NodeFactory.programNode([
  26. NodeFactory.variableDeclarationNode([
  27. NodeFactory.variableDeclaratorNode(
  28. NodeFactory.identifierNode('foo'),
  29. NodeFactory.literalNode(1)
  30. )
  31. ])
  32. ]);
  33. scopeAnalyzer.analyze(programNode);
  34. const scope: eslintScope.Scope = scopeAnalyzer.acquireScope(programNode);
  35. scopeVariablesLength = scope.variables.length;
  36. scopeVariableName = scope.variables[0].name;
  37. });
  38. it('should return scope data with variables', () => {
  39. assert.equal(scopeVariablesLength, expectedScopeVariablesLength);
  40. });
  41. it('should return scope data with correct variable name', () => {
  42. assert.equal(scopeVariableName, expectedScopeVariablesName);
  43. });
  44. });
  45. describe('Variant #2: Acquire of the scope for VariableDeclarationNode', () => {
  46. const expectedError: string = 'Cannot acquire scope for node';
  47. let testFunc: () => eslintScope.Scope;
  48. beforeEach(() => {
  49. const variableDeclarationNode: ESTree.VariableDeclaration = NodeFactory.variableDeclarationNode([
  50. NodeFactory.variableDeclaratorNode(
  51. NodeFactory.identifierNode('foo'),
  52. NodeFactory.literalNode(1)
  53. )
  54. ]);
  55. const programNode: ESTree.Program = NodeFactory.programNode([variableDeclarationNode]);
  56. scopeAnalyzer.analyze(programNode);
  57. testFunc = () => scopeAnalyzer.acquireScope(variableDeclarationNode);
  58. });
  59. it('should throw error', () => {
  60. assert.throws(testFunc, expectedError);
  61. });
  62. });
  63. describe('Variant #3: acquire scope without analyzing', () => {
  64. const expectedError: string = 'Scope manager is not defined';
  65. let testFunc: () => eslintScope.Scope;
  66. beforeEach(() => {
  67. const programNode: ESTree.Program = NodeFactory.programNode([
  68. NodeFactory.variableDeclarationNode([
  69. NodeFactory.variableDeclaratorNode(
  70. NodeFactory.identifierNode('foo'),
  71. NodeFactory.literalNode(1)
  72. )
  73. ])
  74. ]);
  75. testFunc = () => scopeAnalyzer.acquireScope(programNode);
  76. });
  77. it('should throw error', () => {
  78. assert.throws(testFunc, expectedError);
  79. });
  80. });
  81. describe('Variant #4: analyzing error', () => {
  82. const expectedError: ErrorConstructor = Error;
  83. let testFunc: () => void;
  84. beforeEach(() => {
  85. const variableDeclarationNode: ESTree.VariableDeclaration = NodeFactory.variableDeclarationNode([
  86. NodeFactory.variableDeclaratorNode(
  87. NodeFactory.identifierNode('foo'),
  88. NodeFactory.literalNode(1)
  89. )
  90. ]);
  91. const programNode: ESTree.Program = NodeFactory.programNode([variableDeclarationNode]);
  92. testFunc = () => {
  93. scopeAnalyzer.analyze(programNode);
  94. scopeAnalyzer.analyze(variableDeclarationNode);
  95. };
  96. });
  97. it('should throw error', () => {
  98. assert.throws(testFunc, expectedError);
  99. });
  100. });
  101. describe('Variant #5: cannot read property `0` of undefined error', () => {
  102. const expectedError: string = 'Cannot read property';
  103. let testFunc: () => void;
  104. beforeEach(() => {
  105. const variableDeclarationNode: ESTree.VariableDeclaration = NodeFactory.variableDeclarationNode([
  106. NodeFactory.variableDeclaratorNode(
  107. NodeFactory.identifierNode('foo'),
  108. NodeFactory.literalNode(1)
  109. )
  110. ]);
  111. const programNode: ESTree.Program = NodeFactory.programNode([
  112. NodeFactory.ifStatementNode(
  113. NodeFactory.binaryExpressionNode(
  114. '+',
  115. NodeFactory.literalNode(1),
  116. NodeFactory.literalNode(2)
  117. ),
  118. NodeFactory.blockStatementNode([
  119. variableDeclarationNode
  120. ]),
  121. NodeFactory.blockStatementNode([
  122. NodeFactory.functionDeclarationNode(
  123. 'bar',
  124. [],
  125. NodeFactory.blockStatementNode([
  126. variableDeclarationNode
  127. ])
  128. )
  129. ])
  130. )
  131. ]);
  132. testFunc = () => {
  133. scopeAnalyzer.analyze(programNode);
  134. };
  135. });
  136. it('should does not throw error', () => {
  137. assert.doesNotThrow(testFunc, expectedError);
  138. });
  139. });
  140. });
  141. });