Fixed SyntaxError when obfuscating a class that extends a boolean literal (#1420)

This commit is contained in:
Sebastian Cao
2026-07-09 09:33:44 +04:00
committed by GitHub
parent 0044334665
commit fecc14506a
4 changed files with 32 additions and 0 deletions
+4
View File
@@ -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
@@ -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
@@ -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);
});
});
});