ASTParserFacade.spec.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { assert } from 'chai';
  2. import { ecmaVersion } from '../../../src/constants/EcmaVersion';
  3. import { ASTParserFacade } from '../../../src/ASTParserFacade';
  4. describe('ASTParserFacade', () => {
  5. describe(`parse`, () => {
  6. describe(`\`Unexpected token\` error code preview`, () => {
  7. describe('Variant #1: 5 lines of code', () => {
  8. const sourceCode: string = `` +
  9. `var foo = 1;
  10. var bar = 2;
  11. var baz = 3;,
  12. var bark = 4;
  13. var hawk = 5;`;
  14. let testFunc: () => void;
  15. before(() => {
  16. testFunc = () => ASTParserFacade.parse(sourceCode, { ecmaVersion });
  17. });
  18. it('should output code preview when AST parser throws a parse error', () => {
  19. assert.throws(testFunc, /ERROR at line 3: Unexpected token \(3:28\)\n.*\.\.\.var baz = 3;,\.\.\./);
  20. });
  21. });
  22. describe('Variant #2: 15 lines of code', () => {
  23. const sourceCode: string = `` +
  24. `var var1 = 1;
  25. var var2 = 2;
  26. var var3 = 3;
  27. var var4 = 4;
  28. var var5 = 5;
  29. var var6 = 6;
  30. var var7 = 7;
  31. var var8 = 8;
  32. var var9 = 9;
  33. var var10 = 10;
  34. var foo = 1;
  35. var bar = 2;
  36. var baz = 3;,
  37. var bark = 4;
  38. var hawk = 5;`;
  39. let testFunc: () => void;
  40. before(() => {
  41. testFunc = () => ASTParserFacade.parse(sourceCode, { ecmaVersion });
  42. });
  43. it('should output code preview when AST parser throws a parse error', () => {
  44. assert.throws(testFunc, /ERROR at line 13: Unexpected token \(13:28\)\n.*\.\.\.var baz = 3;,\.\.\./);
  45. });
  46. });
  47. describe('Variant #3: code with functions', () => {
  48. const sourceCode: string = `` +
  49. `function bar () {
  50. var a = 1;
  51. }
  52. functin baz () {
  53. var a = 1;
  54. }
  55. function bark () {
  56. var a = 1;
  57. }`;
  58. let testFunc: () => void;
  59. before(() => {
  60. testFunc = () => ASTParserFacade.parse(sourceCode, { ecmaVersion });
  61. });
  62. it('should output code preview when AST parser throws a parse error', () => {
  63. assert.throws(testFunc, /ERROR at line 4: Unexpected token \(4:28\)\n.*\.\.\.functin baz \(\) {\.\.\./);
  64. });
  65. });
  66. });
  67. });
  68. });