Skip to content

Commit 7170a2b

Browse files
committed
Fixed wrong arrow function block scope detection
1 parent 520ed2d commit 7170a2b

7 files changed

Lines changed: 29143 additions & 15 deletions

File tree

dist/index.cli.js

Lines changed: 14904 additions & 1 deletion
Large diffs are not rendered by default.

dist/index.js

Lines changed: 14161 additions & 1 deletion
Large diffs are not rendered by default.

src/node/NodeGuards.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,12 @@ export class NodeGuards {
253253
* @returns {boolean}
254254
*/
255255
public static isNodeHasBlockScope (node: ESTree.Node, parentNode: ESTree.Node): node is TNodeWithBlockScope {
256-
return NodeGuards.isProgramNode(node) || (
257-
NodeGuards.isBlockStatementNode(node)
258-
&& NodeGuards.nodesWithBlockScope.includes(parentNode.type)
259-
);
256+
return NodeGuards.isProgramNode(node)
257+
/**
258+
* Should correctly check arrow functions with expression body
259+
*/
260+
|| (NodeGuards.isArrowFunctionExpressionNode(node) && !NodeGuards.isBlockStatementNode(node.body))
261+
|| (NodeGuards.isBlockStatementNode(node) && NodeGuards.nodesWithBlockScope.includes(parentNode.type));
260262
}
261263

262264
/**
@@ -265,6 +267,10 @@ export class NodeGuards {
265267
*/
266268
public static isNodeHasScope (node: ESTree.Node): node is TNodeWithScope {
267269
return NodeGuards.isProgramNode(node)
270+
/**
271+
* Should correctly check arrow functions with expression body
272+
*/
273+
|| (NodeGuards.isArrowFunctionExpressionNode(node) && !NodeGuards.isBlockStatementNode(node.body))
268274
|| NodeGuards.isBlockStatementNode(node)
269275
|| NodeGuards.isSwitchCaseNode(node);
270276
}

test/dev/dev.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,8 @@ import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNo
66

77
let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
88
`
9-
function foo (data, options) {
10-
function bar ({data, ...rest}) {
11-
function baz ({options}) {
12-
return data + options + rest;
13-
}
14-
}
15-
16-
return data;
17-
}
9+
const foo = 1;
10+
[].map(foo=>1).map(bar=>[foo]);
1811
`,
1912
{
2013
...NO_ADDITIONAL_NODES_PRESET,

test/functional-tests/node-transformers/obfuscating-transformers/function-transformer/FunctionTransformer.spec.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,4 +436,60 @@ describe('FunctionTransformer', () => {
436436
});
437437
});
438438
});
439+
440+
describe('correct block scope detection of arrow function expression', () => {
441+
describe('Variant #1: block statement body', () => {
442+
const regExpMatch: string = `` +
443+
`\\[]` +
444+
`\\['map']\\(_0x[a-f0-9]{4,6} *=> *\\{ *return 0x1; *\\}\\)` +
445+
`\\['map']\\(_0x[a-f0-9]{4,6} *=> *\\[foo]\\);` +
446+
``;
447+
const regExp: RegExp = new RegExp(regExpMatch);
448+
449+
let obfuscatedCode: string;
450+
451+
before(() => {
452+
const code: string = readFileAsString(__dirname + '/fixtures/arrow-function-with-expression-body-block-scope-detection-1.js');
453+
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
454+
code,
455+
{
456+
...NO_ADDITIONAL_NODES_PRESET
457+
}
458+
);
459+
460+
obfuscatedCode = obfuscationResult.getObfuscatedCode();
461+
});
462+
463+
it('should transform identifiers in arrow function expression body', () => {
464+
assert.match(obfuscatedCode, regExp);
465+
});
466+
});
467+
468+
describe('Variant #1: expression statement body', () => {
469+
const regExpMatch: string = `` +
470+
`\\[]` +
471+
`\\['map']\\(_0x[a-f0-9]{4,6} *=> *0x1\\)` +
472+
`\\['map']\\(_0x[a-f0-9]{4,6} *=> *\\[foo]\\);` +
473+
``;
474+
const regExp: RegExp = new RegExp(regExpMatch);
475+
476+
let obfuscatedCode: string;
477+
478+
before(() => {
479+
const code: string = readFileAsString(__dirname + '/fixtures/arrow-function-with-expression-body-block-scope-detection-2.js');
480+
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
481+
code,
482+
{
483+
...NO_ADDITIONAL_NODES_PRESET
484+
}
485+
);
486+
487+
obfuscatedCode = obfuscationResult.getObfuscatedCode();
488+
});
489+
490+
it('should transform identifiers in arrow function expression body', () => {
491+
assert.match(obfuscatedCode, regExp);
492+
});
493+
});
494+
});
439495
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const foo = 1;
2+
[]
3+
.map(foo => {
4+
return 1;
5+
})
6+
.map(bar => [foo]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const foo = 1;
2+
[]
3+
.map(foo => 1)
4+
.map(bar => [foo]);

0 commit comments

Comments
 (0)