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.25.4
---
* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/569

v0.25.3
---
* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/568
Expand Down
10 changes: 5 additions & 5 deletions dist/index.browser.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.cli.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "javascript-obfuscator",
"version": "0.25.3",
"version": "0.25.4",
"description": "JavaScript obfuscator",
"keywords": [
"obfuscator",
Expand Down
4 changes: 2 additions & 2 deletions src/analyzers/scope-analyzer/ScopeAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ export class ScopeAnalyzer implements IScopeAnalyzer {
enter: (node: ESTree.Node): ESTree.Node => {
if (!node.range) {
node.range = [
ScopeAnalyzer.emptyRangeValue,
ScopeAnalyzer.emptyRangeValue
node.parentNode?.range?.[0] ?? ScopeAnalyzer.emptyRangeValue,
node.parentNode?.range?.[1] ?? ScopeAnalyzer.emptyRangeValue
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { NodeAppender } from '../../../node/NodeAppender';
import { NodeFactory } from '../../../node/NodeFactory';
import { NodeGuards } from '../../../node/NodeGuards';
import { NodeStatementUtils } from '../../../node/NodeStatementUtils';
import { NodeUtils } from '../../../node/NodeUtils';

@injectable()
export class BasePropertiesExtractor implements IObjectExpressionExtractor {
Expand Down Expand Up @@ -110,6 +111,7 @@ export class BasePropertiesExtractor implements IObjectExpressionExtractor {

this.filterExtractedObjectExpressionProperties(objectExpressionNode, removablePropertyIds);
NodeAppender.insertAfter(hostNodeWithStatements, expressionStatements, hostStatement);
NodeUtils.parentizeAst(hostNodeWithStatements);

return {
nodeToReplace: objectExpressionNode,
Expand Down
6 changes: 6 additions & 0 deletions src/node/NodeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,16 @@ export class NodeUtils {
* @returns {T}
*/
public static parentizeAst <T extends ESTree.Node = ESTree.Node> (astTree: T): T {
const parentNode: ESTree.Node | null = astTree.parentNode ?? null;

estraverse.replace(astTree, {
enter: NodeUtils.parentizeNode
});

if (parentNode) {
astTree.parentNode = parentNode;
}

return astTree;
}

Expand Down
12 changes: 4 additions & 8 deletions test/dev/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,13 @@ import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNo

let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
`
(function(foo){
function foo () {

}

return new foo();
})();
(function () {
const foo = 'foo';
const bar = { [foo]: 'bar' };
})();
`,
{
...NO_ADDITIONAL_NODES_PRESET,
identifierNamesGenerator: 'mangled',
transformObjectKeys: true,
compact: false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1792,5 +1792,33 @@ describe('ObjectExpressionKeysTransformer', () => {
assert.match(obfuscatedCode, regExp);
});
});

describe('Variant #10: computed property key name', () => {
const match: string = `` +
`const ${variableMatch} *= *'foo';` +
`const ${variableMatch} *= *{};` +
`${variableMatch}\\[${variableMatch}] *= *'bar';` +
`const ${variableMatch} *= *${variableMatch};` +
``;
const regExp: RegExp = new RegExp(match);

let obfuscatedCode: string;

before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/computed-key-1.js');

obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
transformObjectKeys: true
}
).getObfuscatedCode();
});

it('should correctly generate name for the computed key identifier', () => {
assert.match(obfuscatedCode, regExp);
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(function () {
const foo = 'foo';
const bar = { [foo]: 'bar' };
})();
23 changes: 23 additions & 0 deletions test/unit-tests/node/node-utils/NodeUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,29 @@ describe('NodeUtils', () => {
assert.deepEqual(expressionStatementNode2.parentNode, ifStatementBlockStatementNode);
});
});

describe('Variant #3: parentize AST-tree and keep root node parent node', () => {
beforeEach(() => {
programNode = NodeFactory.programNode([
ifStatementNode
]);

ifStatementNode.parentNode = programNode;
ifStatementNode = NodeUtils.parentizeAst(ifStatementNode);
});

it('should parentize `ifStatement` node', () => {
assert.deepEqual(ifStatementNode.parentNode, programNode);
});

it('should parentize `ifStatement blockStatement` node', () => {
assert.deepEqual(ifStatementBlockStatementNode.parentNode, ifStatementNode);
});

afterEach(() => {
ifStatementNode.parentNode = undefined;
});
});
});

describe('parentizeNode', () => {
Expand Down