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

v5.4.5
---
* Fixed `SyntaxError` when obfuscating a class that extends a boolean literal (e.g. `class C extends true {}`). Fixes https://github.com/javascript-obfuscator/javascript-obfuscator/issues/1131

v5.4.4
---
* Fixed `Invalid regular expression` error when obfuscating code that uses ES2025 RegExp pattern modifiers (e.g. `/(?i:abc)/`). Fixes https://github.com/javascript-obfuscator/javascript-obfuscator/issues/1410
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ export class BooleanLiteralTransformer extends AbstractNodeTransformer {
return literalNode;
}

/**
* A class heritage (`extends`) must be a `LeftHandSideExpression`, so replacing a
* boolean literal there with a unary expression (`class Foo extends ![] {}`) would
* produce a `SyntaxError`. Keep the original literal in that position.
*/
if ('superClass' in parentNode && parentNode.superClass === literalNode) {
return literalNode;
}

const literalValue: ESTree.SimpleLiteral['value'] = literalNode.value;

const unaryExpressionNode: ESTree.UnaryExpression = literalValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,22 @@ describe('BooleanLiteralTransformer', () => {
assert.match(obfuscatedCode, regExp);
});
});

describe('boolean literal as a class super class', () => {
const regExp: RegExp = /^class Foo extends true *{}$/;

let obfuscatedCode: string;

before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/super-class-value.js');

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

it('should not transform a boolean literal used as a class heritage to avoid a `SyntaxError`', () => {
assert.match(obfuscatedCode, regExp);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class Foo extends true {}
Loading