Pārlūkot izejas kodu

Switched from `espree` to `acorn`

sanex3339 5 gadi atpakaļ
vecāks
revīzija
645e122640

+ 4 - 0
CHANGELOG.md

@@ -1,5 +1,9 @@
 Change Log
 
+v0.23.0
+---
+* **Internal change:** switched AST parser from `espree` on `acorn`
+
 v0.22.1
 ---
 * Fixed `TypeError: Assignment to constant variable` when auto-detection of kind of variables is inserted `const` variables for `controlFlowStorage` nodes

Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 0 - 0
dist/index.browser.js


Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 0 - 0
dist/index.cli.js


Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 0 - 0
dist/index.js


+ 2 - 2
package.json

@@ -1,6 +1,6 @@
 {
   "name": "javascript-obfuscator",
-  "version": "0.22.1",
+  "version": "0.23.0",
   "description": "JavaScript obfuscator",
   "keywords": [
     "obfuscator",
@@ -23,12 +23,12 @@
   "dependencies": {
     "@gradecam/tsenum": "1.2.0",
     "@nuxtjs/opencollective": "0.2.2",
+    "acorn": "^7.1.0",
     "chalk": "3.0.0",
     "chance": "1.1.4",
     "class-validator": "0.11.0",
     "commander": "4.1.0",
     "escodegen": "1.12.1",
-    "espree": "6.1.2",
     "estraverse": "4.3.0",
     "eventemitter3": "4.0.0",
     "inversify": "5.0.1",

+ 31 - 23
src/EspreeFacade.ts → src/ASTParserFacade.ts

@@ -1,12 +1,12 @@
-import * as espree from 'espree';
+import * as acorn from 'acorn';
 import * as ESTree from 'estree';
 
 import chalk, { Chalk } from 'chalk';
 
 /**
- * Facade over `espree`
+ * Facade over AST parser `acorn`
  */
-export class EspreeFacade {
+export class ASTParserFacade {
     /**
      * @type {Chalk}
      */
@@ -18,9 +18,9 @@ export class EspreeFacade {
     private static readonly nearestSymbolsCount: number = 15;
 
     /**
-     * @type {SourceType[]}
+     * @type {acorn.Options['sourceType'][]}
      */
-    private static readonly sourceTypes: espree.SourceType[] = [
+    private static readonly sourceTypes: acorn.Options['sourceType'][] = [
         'script',
         'module'
     ];
@@ -30,45 +30,53 @@ export class EspreeFacade {
      * @param {Options} config
      * @returns {Program}
      */
-    public static parse (input: string, config: espree.ParseOptions): ESTree.Program | never {
-        const sourceTypeLength: number = EspreeFacade.sourceTypes.length;
+    public static parse (input: string, config: acorn.Options): ESTree.Program | never {
+        const sourceTypeLength: number = ASTParserFacade.sourceTypes.length;
 
         for (let i: number = 0; i < sourceTypeLength; i++) {
             try {
-                return EspreeFacade.parseType(input, config, EspreeFacade.sourceTypes[i]);
+                return ASTParserFacade.parseType(input, config, ASTParserFacade.sourceTypes[i]);
             } catch (error) {
                 if (i < sourceTypeLength - 1) {
                     continue;
                 }
 
-                throw new Error(EspreeFacade.processParsingError(
+                throw new Error(ASTParserFacade.processParsingError(
                     input,
                     error.message,
-                    {
-                        line: error.lineNumber,
-                        column: error.column,
-                    }
+                    error.loc
                 ));
             }
         }
 
-        throw new Error(`Espree parsing error`);
+        throw new Error(`Acorn parsing error`);
     }
 
     /**
      * @param {string} input
-     * @param {ParseOptions} inputConfig
-     * @param {SourceType} sourceType
+     * @param {acorn.Options} inputConfig
+     * @param {acorn.Options["sourceType"]} sourceType
      * @returns {Program}
      */
     private static parseType (
         input: string,
-        inputConfig: espree.ParseOptions,
-        sourceType: espree.SourceType
+        inputConfig: acorn.Options,
+        sourceType: acorn.Options['sourceType']
     ): ESTree.Program {
-        const config: espree.ParseOptions = { ...inputConfig, sourceType };
+        const comments: ESTree.Comment[] = [];
+        const config: acorn.Options = {
+            ...inputConfig,
+            onComment: comments,
+            sourceType
+        };
 
-        return espree.parse(input, config);
+        const program: ESTree.Program = <any>acorn.parse(input, config);
+
+        if (comments.length) {
+            program.comments = comments;
+        }
+
+        return program;
     }
 
     /**
@@ -89,10 +97,10 @@ export class EspreeFacade {
             throw new Error(errorMessage);
         }
 
-        const startErrorIndex: number = Math.max(0, position.column - EspreeFacade.nearestSymbolsCount);
-        const endErrorIndex: number = Math.min(errorLine.length, position.column + EspreeFacade.nearestSymbolsCount);
+        const startErrorIndex: number = Math.max(0, position.column - ASTParserFacade.nearestSymbolsCount);
+        const endErrorIndex: number = Math.min(errorLine.length, position.column + ASTParserFacade.nearestSymbolsCount);
 
-        const formattedPointer: string = EspreeFacade.colorError('>');
+        const formattedPointer: string = ASTParserFacade.colorError('>');
         const formattedCodeSlice: string = `...${
             errorLine.substring(startErrorIndex, endErrorIndex).replace(/^\s+/, '')
         }...`;

+ 9 - 7
src/JavaScriptObfuscator.ts

@@ -1,8 +1,8 @@
 import { inject, injectable, } from 'inversify';
 import { ServiceIdentifiers } from './container/ServiceIdentifiers';
 
+import * as acorn from 'acorn';
 import * as escodegen from 'escodegen';
-import * as espree from 'espree';
 import * as ESTree from 'estree';
 
 import { TObfuscatedCodeFactory } from './types/container/source-code/TObfuscatedCodeFactory';
@@ -19,7 +19,7 @@ import { LoggingMessage } from './enums/logger/LoggingMessage';
 import { NodeTransformer } from './enums/node-transformers/NodeTransformer';
 import { TransformationStage } from './enums/node-transformers/TransformationStage';
 
-import { EspreeFacade } from './EspreeFacade';
+import { ASTParserFacade } from './ASTParserFacade';
 import { NodeGuards } from './node/NodeGuards';
 
 @injectable()
@@ -27,11 +27,13 @@ export class JavaScriptObfuscator implements IJavaScriptObfuscator {
     /**
      * @type {Options}
      */
-    private static readonly espreeParseOptions: espree.ParseOptions = {
-        comment: true,
+    private static readonly parseOptions: acorn.Options = {
+        allowHashBang: true,
+        allowImportExportEverywhere: true,
+        allowReturnOutsideFunction: true,
         ecmaVersion: 10,
-        loc: true,
-        range: true
+        locations: true,
+        ranges: true
     };
 
     /**
@@ -149,7 +151,7 @@ export class JavaScriptObfuscator implements IJavaScriptObfuscator {
      * @returns {Program}
      */
     private parseCode (sourceCode: string): ESTree.Program {
-        return EspreeFacade.parse(sourceCode, JavaScriptObfuscator.espreeParseOptions);
+        return ASTParserFacade.parse(sourceCode, JavaScriptObfuscator.parseOptions);
     }
 
     /**

+ 7 - 0
src/declarations/ESTree.d.ts

@@ -1,5 +1,6 @@
 /* tslint:disable:interface-name */
 
+import * as acorn from 'acorn';
 import * as escodegen from 'escodegen';
 
 declare module 'estree' {
@@ -7,6 +8,12 @@ declare module 'estree' {
         ignoredNode?: boolean;
     }
 
+    export interface Comment {
+        start: number;
+        end: number;
+        loc?: acorn.SourceLocation;
+    }
+
     export interface IdentifierNodeMetadata extends BaseNodeMetadata {
         renamedIdentifier?: boolean;
     }

+ 0 - 26
src/declarations/espree.d.ts

@@ -1,26 +0,0 @@
-/* tslint:disable:interface-name */
-
-declare module 'espree' {
-    import * as ESTree from 'estree';
-
-    export interface Comment {
-        value: string;
-    }
-
-    export type SourceType = 'script' | 'module';
-
-    export interface ParseOptions {
-        comment?: boolean;
-        ecmaFeatures?: {
-            globalReturn?: boolean;
-            impliedStrict?: boolean;
-            jsx?: boolean;
-        };
-        ecmaVersion?: 3 | 5 | 6 | 7 | 8 | 9 | 10 | 2015 | 2016 | 2017 | 2018 | 2019;
-        loc?: boolean;
-        range?: boolean;
-        sourceType?: SourceType;
-    }
-
-    export function parse (code: string | Buffer, options: ParseOptions): ESTree.Program;
-}

+ 2 - 2
src/node/NodeUtils.ts

@@ -1,8 +1,8 @@
 import * as escodegen from 'escodegen';
-import * as espree from 'espree';
 import * as estraverse from 'estraverse';
 import * as ESTree from 'estree';
 
+import { ASTParserFacade } from '../ASTParserFacade';
 import { NodeGuards } from './NodeGuards';
 import { NodeMetadata } from './NodeMetadata';
 
@@ -33,7 +33,7 @@ export class NodeUtils {
      * @returns {Statement[]}
      */
     public static convertCodeToStructure (code: string): ESTree.Statement[] {
-        const structure: ESTree.Program = espree.parse(code, {
+        const structure: ESTree.Program = ASTParserFacade.parse(code, {
             ecmaVersion: 10,
             sourceType: 'script'
         });

+ 0 - 1
test/declarations/index.d.ts

@@ -1,4 +1,3 @@
 /// <reference path="../../src/declarations/escodegen.d.ts" />
-/// <reference path="../../src/declarations/espree.d.ts" />
 /// <reference path="../../src/declarations/ESTree.d.ts" />
 /// <reference path="../../src/declarations/js-string-escape.d.ts" />

+ 3 - 0
test/functional-tests/javascript-obfuscator/fixtures/hashbang.js

@@ -0,0 +1,3 @@
+#!/usr/bin/env node
+
+var test = 1;

+ 1 - 1
test/index.spec.ts

@@ -19,7 +19,7 @@ import './unit-tests/decorators/initializable/Initializable.spec';
 import './unit-tests/generators/identifier-names-generators/DictionarylIdentifierNamesGenerator.spec';
 import './unit-tests/generators/identifier-names-generators/HexadecimalIdentifierNamesGenerator.spec';
 import './unit-tests/generators/identifier-names-generators/MangledlIdentifierNamesGenerator.spec';
-import './unit-tests/javascript-obfuscator/EspreeFacade.spec';
+import './unit-tests/javascript-obfuscator/ASTParserFacade.spec';
 import './unit-tests/javascript-obfuscator/JavaScriptObfuscator.spec';
 import './unit-tests/logger/Logger.spec';
 import './unit-tests/node/node-appender/NodeAppender.spec';

+ 11 - 11
test/unit-tests/javascript-obfuscator/EspreeFacade.spec.ts → test/unit-tests/javascript-obfuscator/ASTParserFacade.spec.ts

@@ -1,7 +1,7 @@
 import { assert } from 'chai';
-import { EspreeFacade } from '../../../src/EspreeFacade';
+import { ASTParserFacade } from '../../../src/ASTParserFacade';
 
-describe('EspreeFacade', () => {
+describe('ASTParserFacade', () => {
     describe(`parse`, () => {
         describe(`\`Unexpected token\` error code preview`, () => {
             describe('Variant #1: 5 lines of code', () => {
@@ -15,11 +15,11 @@ describe('EspreeFacade', () => {
                 let testFunc: () => void;
 
                 before(() => {
-                    testFunc = () => EspreeFacade.parse(sourceCode, { ecmaVersion: 9 });
+                    testFunc = () => ASTParserFacade.parse(sourceCode, { ecmaVersion: 9 });
                 });
 
-                it('should output code preview when `espree` throws a parse error', () => {
-                    assert.throws(testFunc, /Line 3: Unexpected token ,\n.*\.\.\.var baz = 3;,\.\.\./);
+                it('should output code preview when AST parser throws a parse error', () => {
+                    assert.throws(testFunc, /Line 3: Unexpected token \(3:28\)\n.*\.\.\.var baz = 3;,\.\.\./);
                 });
             });
 
@@ -44,11 +44,11 @@ describe('EspreeFacade', () => {
                 let testFunc: () => void;
 
                 before(() => {
-                    testFunc = () => EspreeFacade.parse(sourceCode, { ecmaVersion: 9 });
+                    testFunc = () => ASTParserFacade.parse(sourceCode, { ecmaVersion: 9 });
                 });
 
-                it('should output code preview when `espree` throws a parse error', () => {
-                    assert.throws(testFunc, /Line 13: Unexpected token ,\n.*\.\.\.var baz = 3;,\.\.\./);
+                it('should output code preview when AST parser throws a parse error', () => {
+                    assert.throws(testFunc, /Line 13: Unexpected token \(13:28\)\n.*\.\.\.var baz = 3;,\.\.\./);
                 });
             });
         });
@@ -68,11 +68,11 @@ describe('EspreeFacade', () => {
             let testFunc: () => void;
 
             before(() => {
-                testFunc = () => EspreeFacade.parse(sourceCode, { ecmaVersion: 9 });
+                testFunc = () => ASTParserFacade.parse(sourceCode, { ecmaVersion: 9 });
             });
 
-            it('should output code preview when `espree` throws a parse error', () => {
-                assert.throws(testFunc, /Line 4: Unexpected token baz\n.*\.\.\.functin baz \(\) {\.\.\./);
+            it('should output code preview when AST parser throws a parse error', () => {
+                assert.throws(testFunc, /Line 4: Unexpected token \(4:24\)\n.*\.\.\.functin baz \(\) {\.\.\./);
             });
         });
     });

+ 0 - 19
yarn.lock

@@ -452,11 +452,6 @@ abbrev@1:
   resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8"
   integrity sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q==
 
-acorn-jsx@^5.1.0:
-  version "5.1.0"
-  resolved "https://registry.yarnpkg.com/acorn-jsx/-/acorn-jsx-5.1.0.tgz#294adb71b57398b0680015f0a38c563ee1db5384"
-  integrity sha512-tMUqwBWfLFbJbizRmEcWSLw6HnFzfdJs2sOJEOwwtVPMoH/0Ay+E703oZz78VSXZiiDcZrQ5XKjPIUQixhmgVw==
-
 acorn@^6.2.1:
   version "6.4.0"
   resolved "https://registry.yarnpkg.com/acorn/-/acorn-6.4.0.tgz#b659d2ffbafa24baf5db1cdbb2c94a983ecd2784"
@@ -1645,25 +1640,11 @@ eslint-scope@^4.0.3:
     esrecurse "^4.1.0"
     estraverse "^4.1.1"
 
-eslint-visitor-keys@^1.1.0:
-  version "1.1.0"
-  resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-1.1.0.tgz#e2a82cea84ff246ad6fb57f9bde5b46621459ec2"
-  integrity sha512-8y9YjtM1JBJU/A9Kc+SbaOV4y29sSWckBwMHa+FGtVj5gN/sbnKDf6xJUl+8g7FAij9LVaP8C24DUiH/f/2Z9A==
-
 esm@^3.2.25:
   version "3.2.25"
   resolved "https://registry.yarnpkg.com/esm/-/esm-3.2.25.tgz#342c18c29d56157688ba5ce31f8431fbb795cc10"
   integrity sha512-U1suiZ2oDVWv4zPO56S0NcR5QriEahGtdN2OR6FiOG4WJvcjBVFB0qI4+eKoWFH483PKGuLuu6V8Z4T5g63UVA==
 
[email protected]:
-  version "6.1.2"
-  resolved "https://registry.yarnpkg.com/espree/-/espree-6.1.2.tgz#6c272650932b4f91c3714e5e7b5f5e2ecf47262d"
-  integrity sha512-2iUPuuPP+yW1PZaMSDM9eyVf8D5P0Hi8h83YtZ5bPc/zHYjII5khoixIUTMO794NOY8F/ThF1Bo8ncZILarUTA==
-  dependencies:
-    acorn "^7.1.0"
-    acorn-jsx "^5.1.0"
-    eslint-visitor-keys "^1.1.0"
-
 esprima@^3.1.3:
   version "3.1.3"
   resolved "https://registry.yarnpkg.com/esprima/-/esprima-3.1.3.tgz#fdca51cee6133895e3c88d535ce49dbff62a4633"

Daži faili netika attēloti, jo izmaiņu fails ir pārāk liels