Fixed transformObjectKeys with mangled identifier generator causing variable shadowing when extracted object variable name matched an existing inner scope variable

This commit is contained in:
sanex3339
2026-01-27 23:34:45 +04:00
parent 05aacd9dfe
commit 999e93c084
7 changed files with 61 additions and 3 deletions
@@ -0,0 +1 @@
[].forEach(a => a === { a: 1 });
@@ -0,0 +1,32 @@
import { assert } from 'chai';
import { NO_ADDITIONAL_NODES_PRESET } from '../../../src/options/presets/NoCustomNodes';
import { readFileAsString } from '../../helpers/readFileAsString';
import { JavaScriptObfuscator } from '../../../src/JavaScriptObfuscatorFacade';
//
// https://github.com/javascript-obfuscator/javascript-obfuscator/issues/1232
//
describe('Issue #1232', () => {
describe('transformObjectKeys should not cause variable shadowing with mangled identifiers', () => {
let obfuscatedCode: string;
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/issue1232.js');
obfuscatedCode = JavaScriptObfuscator.obfuscate(code, {
...NO_ADDITIONAL_NODES_PRESET,
identifierNamesGenerator: 'mangled',
transformObjectKeys: true
}).getObfuscatedCode();
});
it('should not rename extracted object variable to same name as function parameter', () => {
const shadowingPattern = /(\w+)\s*===\s*\1[)\s;,]/;
assert.isFalse(
shadowingPattern.test(obfuscatedCode),
`Variable shadowing detected in obfuscated code: ${obfuscatedCode}`
);
});
});
});