mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
Merge pull request #443 from javascript-obfuscator/feature/6.1.2-espree-version
Upgrade to latest 6.1.2 Espree version
This commit is contained in:
Vendored
+4
-4
File diff suppressed because one or more lines are too long
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+1
-1
File diff suppressed because one or more lines are too long
+1
-1
@@ -27,7 +27,7 @@
|
||||
"class-validator": "0.11.0",
|
||||
"commander": "4.0.1",
|
||||
"escodegen-wallaby": "1.6.27",
|
||||
"espree": "4.0.0",
|
||||
"espree": "^6.1.2",
|
||||
"estraverse": "4.3.0",
|
||||
"inversify": "5.0.1",
|
||||
"js-string-escape": "1.0.1",
|
||||
|
||||
@@ -28,12 +28,8 @@ export class JavaScriptObfuscator implements IJavaScriptObfuscator {
|
||||
* @type {Options}
|
||||
*/
|
||||
private static readonly espreeParseOptions: espree.ParseOptions = {
|
||||
attachComment: true,
|
||||
comment: true,
|
||||
ecmaFeatures: {
|
||||
experimentalObjectRestSpread: true
|
||||
},
|
||||
ecmaVersion: 9,
|
||||
ecmaVersion: 10,
|
||||
loc: true,
|
||||
range: true
|
||||
};
|
||||
@@ -160,6 +156,8 @@ export class JavaScriptObfuscator implements IJavaScriptObfuscator {
|
||||
* @returns {Program}
|
||||
*/
|
||||
private transformAstTree (astTree: ESTree.Program): ESTree.Program {
|
||||
astTree = this.runTransformationStage(astTree, TransformationStage.Initializing);
|
||||
|
||||
const isEmptyAstTree: boolean = NodeGuards.isProgramNode(astTree)
|
||||
&& !astTree.body.length
|
||||
&& !astTree.leadingComments
|
||||
|
||||
Vendored
+1
-3
@@ -10,15 +10,13 @@ declare module 'espree' {
|
||||
export type SourceType = 'script' | 'module';
|
||||
|
||||
export interface ParseOptions {
|
||||
attachComment?: boolean;
|
||||
comment?: boolean;
|
||||
ecmaFeatures?: {
|
||||
experimentalObjectRestSpread?: boolean;
|
||||
globalReturn?: boolean;
|
||||
impliedStrict?: boolean;
|
||||
jsx?: boolean;
|
||||
};
|
||||
ecmaVersion?: 3 | 5 | 6 | 7 | 8 | 9 | 2015 | 2016 | 2017 | 2018;
|
||||
ecmaVersion?: 3 | 5 | 6 | 7 | 8 | 9 | 10 | 2015 | 2016 | 2017 | 2018 | 2019;
|
||||
loc?: boolean;
|
||||
range?: boolean;
|
||||
sourceType?: SourceType;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
export enum TransformationStage {
|
||||
Initializing = 'Initializing',
|
||||
Preparing = 'Preparing',
|
||||
DeadCodeInjection = 'DeadCodeInjection',
|
||||
ControlFlowFlattening = 'ControlFlowFlattening',
|
||||
|
||||
@@ -2,6 +2,7 @@ import { inject, injectable, } from 'inversify';
|
||||
import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
|
||||
|
||||
import * as ESTree from 'estree';
|
||||
import * as estraverse from "estraverse";
|
||||
|
||||
import { IOptions } from '../../interfaces/options/IOptions';
|
||||
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
|
||||
@@ -11,6 +12,7 @@ import { TransformationStage } from '../../enums/node-transformers/Transformatio
|
||||
|
||||
import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
|
||||
import { NodeGuards } from '../../node/NodeGuards';
|
||||
import { ConditionalCommentObfuscatingGuard } from "./obfuscating-guards/ConditionalCommentObfuscatingGuard";
|
||||
|
||||
@injectable()
|
||||
export class CommentsTransformer extends AbstractNodeTransformer {
|
||||
@@ -39,11 +41,11 @@ export class CommentsTransformer extends AbstractNodeTransformer {
|
||||
*/
|
||||
public getVisitor (transformationStage: TransformationStage): IVisitor | null {
|
||||
switch (transformationStage) {
|
||||
case TransformationStage.Preparing:
|
||||
case TransformationStage.Initializing:
|
||||
return {
|
||||
leave: (node: ESTree.Node, parentNode: ESTree.Node | null) => {
|
||||
if (parentNode && NodeGuards.isNodeWithComments(node)) {
|
||||
return this.transformNode(node, parentNode);
|
||||
leave: (node: ESTree.Node) => {
|
||||
if (NodeGuards.isProgramNode(node)) {
|
||||
return this.transformNode(node);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -56,21 +58,37 @@ export class CommentsTransformer extends AbstractNodeTransformer {
|
||||
/**
|
||||
* Removes all comments from node except comments that contain
|
||||
* `@license`, `@preserve` or `javascript-obfuscator` words
|
||||
* Move comments to their nodes
|
||||
*
|
||||
* @param {Node} node
|
||||
* @param {Node} parentNode
|
||||
* @param {Node} programNode
|
||||
* @returns {NodeGuards}
|
||||
*/
|
||||
public transformNode (node: ESTree.Node, parentNode: ESTree.Node): ESTree.Node {
|
||||
if (node.leadingComments) {
|
||||
node.leadingComments = this.transformComments(node.leadingComments);
|
||||
public transformNode (programNode: ESTree.Program): ESTree.Node {
|
||||
if (programNode.comments) {
|
||||
const comments: ESTree.Comment[] = this.transformComments(programNode.comments);
|
||||
estraverse.traverse(programNode, {
|
||||
enter: (node: ESTree.Node): void => {
|
||||
if (comments.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const commentIdx: number = comments.findIndex((comment: ESTree.Comment) =>
|
||||
comment.range && node.range && comment.range[0] < node.range[0]
|
||||
);
|
||||
|
||||
if (commentIdx === -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
node.leadingComments = comments.splice(commentIdx, comments.length - commentIdx).reverse();
|
||||
}
|
||||
});
|
||||
if (comments.length > 0) {
|
||||
programNode.trailingComments = comments.reverse();
|
||||
}
|
||||
}
|
||||
|
||||
if (node.trailingComments) {
|
||||
node.trailingComments = this.transformComments(node.trailingComments);
|
||||
}
|
||||
|
||||
return node;
|
||||
return programNode;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,7 +98,8 @@ export class CommentsTransformer extends AbstractNodeTransformer {
|
||||
private transformComments (comments: ESTree.Comment[]): ESTree.Comment[] {
|
||||
return comments.filter((comment: ESTree.Comment) =>
|
||||
CommentsTransformer.preservedWords
|
||||
.some((preservedWord: string) => comment.value.includes(preservedWord))
|
||||
);
|
||||
.some((preservedWord: string) => comment.value.includes(preservedWord)) ||
|
||||
ConditionalCommentObfuscatingGuard.isConditionalComment(comment)
|
||||
).reverse();
|
||||
}
|
||||
}
|
||||
|
||||
+9
@@ -28,6 +28,15 @@ export class ConditionalCommentObfuscatingGuard implements IObfuscatingGuard {
|
||||
*/
|
||||
private obfuscationAllowedForNextNode: boolean | null = null;
|
||||
|
||||
/**
|
||||
* @param {Comment} comment
|
||||
* @returns {boolean}
|
||||
*/
|
||||
public static isConditionalComment (comment: ESTree.Comment): boolean {
|
||||
return ConditionalCommentObfuscatingGuard.obfuscationEnableCommentRegExp.test(comment.value) ||
|
||||
ConditionalCommentObfuscatingGuard.obfuscationDisableCommentRegExp.test(comment.value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {boolean}
|
||||
* @param node
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { assert } from 'chai';
|
||||
import { EspreeFacade } from '../../../src/EspreeFacade';
|
||||
|
||||
|
||||
describe('EspreeFacade', () => {
|
||||
describe(`parse`, () => {
|
||||
describe(`\`Unexpected token\` error code preview`, () => {
|
||||
@@ -16,7 +15,7 @@ describe('EspreeFacade', () => {
|
||||
let testFunc: () => void;
|
||||
|
||||
before(() => {
|
||||
testFunc = () => EspreeFacade.parse(sourceCode, {});
|
||||
testFunc = () => EspreeFacade.parse(sourceCode, { ecmaVersion: 9 });
|
||||
});
|
||||
|
||||
it('should output code preview when `espree` throws a parse error', () => {
|
||||
@@ -45,7 +44,7 @@ describe('EspreeFacade', () => {
|
||||
let testFunc: () => void;
|
||||
|
||||
before(() => {
|
||||
testFunc = () => EspreeFacade.parse(sourceCode, {});
|
||||
testFunc = () => EspreeFacade.parse(sourceCode, { ecmaVersion: 9 });
|
||||
});
|
||||
|
||||
it('should output code preview when `espree` throws a parse error', () => {
|
||||
@@ -69,7 +68,7 @@ describe('EspreeFacade', () => {
|
||||
let testFunc: () => void;
|
||||
|
||||
before(() => {
|
||||
testFunc = () => EspreeFacade.parse(sourceCode, {});
|
||||
testFunc = () => EspreeFacade.parse(sourceCode, { ecmaVersion: 9 });
|
||||
});
|
||||
|
||||
it('should output code preview when `espree` throws a parse error', () => {
|
||||
|
||||
@@ -983,25 +983,21 @@ abbrev@1.0.x:
|
||||
version "1.0.9"
|
||||
resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.0.9.tgz#91b4792588a7738c25f35dd6f63752a2f8776135"
|
||||
|
||||
acorn-jsx@^4.1.1:
|
||||
version "4.1.1"
|
||||
resolved "https://registry.yarnpkg.com/acorn-jsx/-/acorn-jsx-4.1.1.tgz#e8e41e48ea2fe0c896740610ab6a4ffd8add225e"
|
||||
dependencies:
|
||||
acorn "^5.0.3"
|
||||
|
||||
acorn@^5.0.3:
|
||||
version "5.5.3"
|
||||
resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.5.3.tgz#f473dd47e0277a08e28e9bec5aeeb04751f0b8c9"
|
||||
|
||||
acorn@^5.6.0:
|
||||
version "5.7.1"
|
||||
resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.7.1.tgz#f095829297706a7c9776958c0afc8930a9b9d9d8"
|
||||
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"
|
||||
integrity sha512-gac8OEcQ2Li1dxIEWGZzsp2BitJxwkwcOm0zHAJLcPJaVvm58FRnk6RkuLRpU1EujipU2ZFODv2P9DLMfnV8mw==
|
||||
|
||||
acorn@^7.1.0:
|
||||
version "7.1.0"
|
||||
resolved "https://registry.yarnpkg.com/acorn/-/acorn-7.1.0.tgz#949d36f2c292535da602283586c2477c57eb2d6c"
|
||||
integrity sha512-kL5CuoXA/dgxlBbVrflsflzQ3PAas7RYZB52NOm/6839iVYJgKMJ3cQJD+t2i5+qFa8h3MDpEOJiS64E8JLnSQ==
|
||||
|
||||
ajv-errors@^1.0.0:
|
||||
version "1.0.1"
|
||||
resolved "https://registry.yarnpkg.com/ajv-errors/-/ajv-errors-1.0.1.tgz#f35986aceb91afadec4102fbd85014950cefa64d"
|
||||
@@ -2218,12 +2214,19 @@ eslint-scope@^4.0.3:
|
||||
esrecurse "^4.1.0"
|
||||
estraverse "^4.1.1"
|
||||
|
||||
espree@4.0.0:
|
||||
version "4.0.0"
|
||||
resolved "https://registry.yarnpkg.com/espree/-/espree-4.0.0.tgz#253998f20a0f82db5d866385799d912a83a36634"
|
||||
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==
|
||||
|
||||
espree@^6.1.2:
|
||||
version "6.1.2"
|
||||
resolved "https://registry.yarnpkg.com/espree/-/espree-6.1.2.tgz#6c272650932b4f91c3714e5e7b5f5e2ecf47262d"
|
||||
integrity sha512-2iUPuuPP+yW1PZaMSDM9eyVf8D5P0Hi8h83YtZ5bPc/zHYjII5khoixIUTMO794NOY8F/ThF1Bo8ncZILarUTA==
|
||||
dependencies:
|
||||
acorn "^5.6.0"
|
||||
acorn-jsx "^4.1.1"
|
||||
acorn "^7.1.0"
|
||||
acorn-jsx "^5.1.0"
|
||||
eslint-visitor-keys "^1.1.0"
|
||||
|
||||
esprima@^2.7.1:
|
||||
version "2.7.3"
|
||||
|
||||
Reference in New Issue
Block a user