浏览代码

Added tests for EsprimaFacade

sanex3339 7 年之前
父节点
当前提交
7753d53028
共有 5 个文件被更改,包括 84 次插入2 次删除
  1. 1 0
      CHANGELOG.md
  2. 0 0
      dist/index.js
  3. 2 2
      src/EsprimaFacade.ts
  4. 1 0
      test/index.spec.ts
  5. 80 0
      test/unit-tests/javascript-obfuscator/EsprimaFacade.spec.ts

+ 1 - 0
CHANGELOG.md

@@ -3,6 +3,7 @@ Change Log
 v0.14.3
 ---
 * Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/195
+* Added code preview to `esprima` error messages.
 
 v0.14.2
 ---

文件差异内容过多而无法显示
+ 0 - 0
dist/index.js


+ 2 - 2
src/EsprimaFacade.ts

@@ -28,7 +28,7 @@ export class EsprimaFacade {
         try {
             return esprima.parseScript(input, config, (node: ESTree.Node, meta: any) => lastMeta = meta);
         } catch (error) {
-            return this.processParsingError(input, error.message, lastMeta);
+            return EsprimaFacade.processParsingError(input, error.message, lastMeta);
         }
     }
 
@@ -43,7 +43,7 @@ export class EsprimaFacade {
             throw new Error(errorMessage);
         }
 
-        const lineNumberMatch: RegExpMatchArray | null = errorMessage.match(/Line *(\d)/);
+        const lineNumberMatch: RegExpMatchArray | null = errorMessage.match(/Line *(\d*)/);
 
         if (!lineNumberMatch) {
             throw new Error(errorMessage);

+ 1 - 0
test/index.spec.ts

@@ -17,6 +17,7 @@ import './unit-tests/cli/utils/SourceCodeReader.spec';
 import './unit-tests/decorators/initializable/Initializable.spec';
 import './unit-tests/generators/identifier-names-generators/HexadecimalIdentifierNamesGenerator.spec';
 import './unit-tests/generators/identifier-names-generators/MangledlIdentifierNamesGenerator.spec';
+import './unit-tests/javascript-obfuscator/EsprimaFacade.spec';
 import './unit-tests/javascript-obfuscator/JavaScriptObfuscator.spec';
 import './unit-tests/logger/Logger.spec';
 import './unit-tests/node/node-appender/NodeAppender.spec';

+ 80 - 0
test/unit-tests/javascript-obfuscator/EsprimaFacade.spec.ts

@@ -0,0 +1,80 @@
+import { assert } from 'chai';
+import { EsprimaFacade } from '../../../src/EsprimaFacade';
+
+
+describe('EsprimaFacade', () => {
+    describe(`parseScript (input: string, config: esprima.ParseOptions): ESTree.Program`, () => {
+        describe(`\`Unexpected token\` error code preview`, () => {
+            describe('variant #1: 5 lines of code', () => {
+                const sourceCode: string = `` +
+                `var foo = 1;
+                var bar = 2;
+                var baz = 3;,
+                var bark = 4;
+                var hawk = 5;`;
+
+                let testFunc: () => void;
+
+                before(() => {
+                    testFunc = () => EsprimaFacade.parseScript(sourceCode, {});
+                });
+
+                it('should output code preview when `esprima` throws a parse error', () => {
+                    assert.throws(testFunc, /Line 3: Unexpected token ,\n.*\.\.\.var baz = 3;,\.\.\./);
+                });
+            });
+
+            describe('variant #2: 15 lines of code', () => {
+                const sourceCode: string = `` +
+                `var var1 = 1;
+                var var2 = 2;
+                var var3 = 3;
+                var var4 = 4;
+                var var5 = 5;
+                var var6 = 6;
+                var var7 = 7;
+                var var8 = 8;
+                var var9 = 9;
+                var var10 = 10;
+                var foo = 1;
+                var bar = 2;
+                var baz = 3;,
+                var bark = 4;
+                var hawk = 5;`;
+
+                let testFunc: () => void;
+
+                before(() => {
+                    testFunc = () => EsprimaFacade.parseScript(sourceCode, {});
+                });
+
+                it('should output code preview when `esprima` throws a parse error', () => {
+                    assert.throws(testFunc, /Line 13: Unexpected token ,\n.*\.\.\.var baz = 3;,\.\.\./);
+                });
+            });
+        });
+
+        describe(`\`Unexpected identifier\` error code preview`, () => {
+            const sourceCode: string = `` +
+                `function bar () {
+                    var a = 1;
+                }
+                functin baz () {
+                    var a = 1;
+                }
+                function bark () {
+                    var a = 1;
+                }`;
+
+            let testFunc: () => void;
+
+            before(() => {
+                testFunc = () => EsprimaFacade.parseScript(sourceCode, {});
+            });
+
+            it('should output code preview when `esprima` throws a parse error', () => {
+                assert.throws(testFunc, /Line 4: Unexpected identifier\n.*\.\.\.functin baz \(\) {\.\.\./);
+            });
+        });
+    });
+});

部分文件因为文件数量过多而无法显示