Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Change Log
===
v0.14.1
---
* Temporary fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/181

v0.14.0
---
* **New option:** `identifiersPrefix` sets prefix for all generated identifiers.
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "javascript-obfuscator",
"version": "0.14.0",
"version": "0.14.1",
"description": "JavaScript obfuscator",
"keywords": [
"obfuscator",
Expand All @@ -19,10 +19,10 @@
"javascript-obfuscator": "./bin/javascript-obfuscator"
},
"dependencies": {
"chalk": "2.3.0",
"chalk": "2.3.1",
"chance": "1.0.13",
"class-validator": "0.8.1",
"commander": "2.14.0",
"commander": "2.14.1",
"escodegen-wallaby": "1.6.17",
"esprima": "4.0.0",
"estraverse": "4.2.0",
Expand All @@ -47,7 +47,7 @@
"@types/md5": "2.1.32",
"@types/mkdirp": "0.5.2",
"@types/mocha": "2.2.48",
"@types/node": "9.4.1",
"@types/node": "9.4.5",
"@types/rimraf": "2.0.2",
"@types/sinon": "4.1.3",
"@types/string-template": "1.0.2",
Expand All @@ -63,15 +63,15 @@
"mocha": "5.0.0",
"pre-commit": "1.2.2",
"rimraf": "2.6.2",
"sinon": "4.2.2",
"sinon": "4.3.0",
"threads": "0.10.1",
"ts-node": "4.1.0",
"tslint": "5.9.1",
"tslint-eslint-rules": "4.1.1",
"tslint-language-service": "0.9.8",
"tslint-webpack-plugin": "1.1.1",
"typescript": "2.7.1",
"webpack": "3.10.0",
"webpack": "3.11.0",
"webpack-node-externals": "1.6.0"
},
"repository": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { inject, injectable, } from 'inversify';
import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';

import * as estraverse from 'estraverse';
import * as ESTree from 'estree';

import { TStatement } from '../../types/node/TStatement';

import { IOptions } from '../../interfaces/options/IOptions';
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
import { IVisitor } from '../../interfaces/node-transformers/IVisitor';
Expand All @@ -12,6 +15,7 @@ import { TransformationStage } from '../../enums/node-transformers/Transformatio
import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
import { NodeGuards } from '../../node/NodeGuards';
import { Nodes } from '../../node/Nodes';
import { NodeUtils } from '../../node/NodeUtils';

/**
* Transform ES2015 template literals to ES5
Expand Down Expand Up @@ -47,6 +51,13 @@ export class TemplateLiteralTransformer extends AbstractNodeTransformer {
case TransformationStage.Converting:
return {
enter: (node: ESTree.Node, parentNode: ESTree.Node | null) => {
if (parentNode
&& NodeGuards.isExpressionStatementNode(node)
&& NodeGuards.isTemplateLiteralNode(node.expression)
) {
return this.fixEsprimaReturnStatementTemplateLiteralNode(node, node.expression);
}

if (parentNode && NodeGuards.isTemplateLiteralNode(node)) {
return this.transformNode(node, parentNode);
}
Expand Down Expand Up @@ -109,4 +120,35 @@ export class TemplateLiteralTransformer extends AbstractNodeTransformer {

return nodes[0];
}

/**
* @param {ExpressionStatement} expressionStatementNode
* @param {TemplateLiteral} templateLiteralNode
* @returns {Node | VisitorOption}
*/
private fixEsprimaReturnStatementTemplateLiteralNode (
expressionStatementNode: ESTree.ExpressionStatement,
templateLiteralNode: ESTree.TemplateLiteral
): ESTree.Node | estraverse.VisitorOption {
const previousSiblingStatementNode: TStatement | null = NodeUtils
.getPreviousSiblingStatementNode(expressionStatementNode);

if (
!previousSiblingStatementNode
|| !templateLiteralNode.parentNode
|| !NodeGuards.isReturnStatementNode(previousSiblingStatementNode)
|| previousSiblingStatementNode.argument !== null
) {
return expressionStatementNode;
}

previousSiblingStatementNode.argument = <ESTree.Expression>this.transformNode(
templateLiteralNode,
templateLiteralNode.parentNode
);

NodeUtils.parentizeNode(templateLiteralNode, previousSiblingStatementNode);

return estraverse.VisitorOption.Remove;
}
}
31 changes: 31 additions & 0 deletions src/node/NodeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ export class NodeUtils {
return NodeUtils.getBlockScopesOfNodeRecursive(targetNode);
}

/**
* @param {Statement} node
* @returns {TStatement | null}
*/
public static getNextSiblingStatementNode (node: ESTree.Statement): TStatement | null {
return NodeUtils.getSiblingStatementNodeByOffset(node, 1);
}

/**
* @param {Statement} node
* @returns {TStatement | null}
*/
public static getPreviousSiblingStatementNode (node: ESTree.Statement): TStatement | null {
return NodeUtils.getSiblingStatementNodeByOffset(node, -1);
}

/**
* @param {NodeGuards} node
* @returns {TNodeWithScope}
Expand Down Expand Up @@ -208,4 +224,19 @@ export class NodeUtils {

return blockScopes;
}

/**
* @param {Statement} node
* @param {number} offset
* @returns {TStatement | null}
*/
private static getSiblingStatementNodeByOffset (node: ESTree.Statement, offset: number): TStatement | null {
const scopeNode: TNodeWithScope = NodeUtils.getScopeOfNode(node);
const scopeBody: TStatement[] = !NodeGuards.isSwitchCaseNode(scopeNode)
? scopeNode.body
: scopeNode.consequent;
const indexInScope: number = scopeBody.indexOf(node);

return scopeBody[indexInScope + offset] || null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,48 @@ describe('TemplateLiteralTransformer', () => {
});
});

describe('variant #1: simple template literal with expression only', () => {
describe('variant #2: multiline template literals', () => {
it('variant #1: should transform es6 multiline template literal to es5', () => {
const code: string = readFileAsString(__dirname + '/fixtures/multiline-template-literal.js');
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
unicodeEscapeSequence: false
}
);

assert.match(obfuscationResult.getObfuscatedCode(), /^var *test *= *'foo\\x0abar';$/);
});

it('variant #2: should transform es6 multiline template literal inside return statement', () => {
const code: string = readFileAsString(__dirname + '/fixtures/multiline-template-literal-return-statement-1.js');
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
unicodeEscapeSequence: false
}
);

assert.match(obfuscationResult.getObfuscatedCode(), /{ *return *'foo\\x0abar'; *}$/);
});

it('variant #3: should transform es6 multiline template literal inside return statement', () => {
const code: string = readFileAsString(__dirname + '/fixtures/multiline-template-literal-return-statement-2.js');
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
unicodeEscapeSequence: false
}
);

assert.match(obfuscationResult.getObfuscatedCode(), /case *!!\[] *: *return *'foo\\x0abar'; *} *}$/);
});
});

describe('variant #3: simple template literal with expression only', () => {
it('should transform es6 template literal to es5 and add empty literal node before expression node', () => {
const code: string = readFileAsString(__dirname + '/fixtures/expression-only.js');
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
Expand All @@ -39,7 +80,7 @@ describe('TemplateLiteralTransformer', () => {
});
});

describe('variant #3: literal node inside expression', () => {
describe('variant #4: literal node inside expression', () => {
it('should transform es6 template literal to es5', () => {
const code: string = readFileAsString(__dirname + '/fixtures/literal-inside-expression.js');
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
Expand All @@ -54,7 +95,7 @@ describe('TemplateLiteralTransformer', () => {
});
});

describe('variant #4: multiple expressions', () => {
describe('variant #5: multiple expressions', () => {
it('should transform es6 template literal to es5', () => {
const code: string = readFileAsString(__dirname + '/fixtures/multiple-expressions.js');
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function foo() {
return `foo
bar`;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function hi() {
switch (true) {
case true:
return `foo
bar`;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var test = `foo
bar`;
Loading