ASTParserFacade.spec.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { assert } from 'chai';
  2. import { IASTParserFacadeInputData } from '../../../src/interfaces/IASTParserFacadeInputData';
  3. import { ecmaVersion } from '../../../src/constants/EcmaVersion';
  4. import { ASTParserFacade } from '../../../src/ASTParserFacade';
  5. describe('ASTParserFacade', () => {
  6. describe(`parse`, () => {
  7. describe(`\`Unexpected token\` error code preview`, () => {
  8. describe('Variant #1: 5 lines of code', () => {
  9. const sourceCode: string = `` +
  10. `var foo = 1;
  11. var bar = 2;
  12. var baz = 3;,
  13. var bark = 4;
  14. var hawk = 5;`;
  15. let testFunc: () => void;
  16. before(() => {
  17. const inputData: IASTParserFacadeInputData = {sourceCode};
  18. testFunc = () => ASTParserFacade.parse(inputData, { ecmaVersion });
  19. });
  20. it('should output code preview when AST parser throws a parse error', () => {
  21. assert.throws(testFunc, /ERROR in line 3: Unexpected token \(3:28\)\n.*\.\.\.var baz = 3;,\.\.\./);
  22. });
  23. });
  24. describe('Variant #2: 15 lines of code', () => {
  25. const sourceCode: string = `` +
  26. `var var1 = 1;
  27. var var2 = 2;
  28. var var3 = 3;
  29. var var4 = 4;
  30. var var5 = 5;
  31. var var6 = 6;
  32. var var7 = 7;
  33. var var8 = 8;
  34. var var9 = 9;
  35. var var10 = 10;
  36. var foo = 1;
  37. var bar = 2;
  38. var baz = 3;,
  39. var bark = 4;
  40. var hawk = 5;`;
  41. let testFunc: () => void;
  42. before(() => {
  43. const inputData: IASTParserFacadeInputData = {sourceCode};
  44. testFunc = () => ASTParserFacade.parse(inputData, { ecmaVersion });
  45. });
  46. it('should output code preview when AST parser throws a parse error', () => {
  47. assert.throws(testFunc, /ERROR in line 13: Unexpected token \(13:28\)\n.*\.\.\.var baz = 3;,\.\.\./);
  48. });
  49. });
  50. describe('Variant #3: code with functions', () => {
  51. const sourceCode: string = `` +
  52. `function bar () {
  53. var a = 1;
  54. }
  55. functin baz () {
  56. var a = 1;
  57. }
  58. function bark () {
  59. var a = 1;
  60. }`;
  61. let testFunc: () => void;
  62. before(() => {
  63. const inputData: IASTParserFacadeInputData = {sourceCode};
  64. testFunc = () => ASTParserFacade.parse(inputData, { ecmaVersion });
  65. });
  66. it('should output code preview when AST parser throws a parse error', () => {
  67. assert.throws(testFunc, /ERROR in line 4: Unexpected token \(4:28\)\n.*\.\.\.functin baz \(\) {\.\.\./);
  68. });
  69. });
  70. describe('Variant #4: input file path is set', () => {
  71. const sourceCode: string = `` +
  72. `var foo = 1;
  73. var bar = 2;
  74. var baz = 3;,
  75. var bark = 4;
  76. var hawk = 5;`;
  77. let testFunc: () => void;
  78. before(() => {
  79. const inputData: IASTParserFacadeInputData = {
  80. sourceCode,
  81. inputFilePath: '/src/foo.js'
  82. };
  83. testFunc = () => ASTParserFacade.parse(inputData, { ecmaVersion });
  84. });
  85. it('should output code preview when AST parser throws a parse error', () => {
  86. assert.throws(testFunc, /ERROR in \/src\/foo\.js, line 3: Unexpected token \(3:32\)\n.*\.\.\.var baz = 3;,\.\.\./);
  87. });
  88. });
  89. });
  90. });
  91. });