mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
Added tests for EsprimaFacade
This commit is contained in:
@@ -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
|
||||
---
|
||||
|
||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
@@ -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);
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -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 \(\) {\.\.\./);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user