mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
Fixed wrong scope of preserved identifiers of shorthand object pattern property
This commit is contained in:
+96
@@ -156,4 +156,100 @@ describe('VariablePreserveTransformer', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #4: destructed object property identifier name conflict with identifier name', () => {
|
||||
describe('Variant #1: `renameGlobals` option is disabled', () => {
|
||||
const variableDeclarationIdentifierName: RegExp = /var b *= *0x1;/;
|
||||
const destructedObjectPropertyIdentifierName: RegExp = /const { *a *} *= *{ *'a' *: *0x2 *};/;
|
||||
|
||||
let obfuscatedCode: string;
|
||||
|
||||
before(() => {
|
||||
const code: string = readFileAsString(__dirname + '/fixtures/destructed-object-property-identifier-name-1.js');
|
||||
|
||||
obfuscatedCode = JavaScriptObfuscator.obfuscate(
|
||||
code,
|
||||
{
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
identifierNamesGenerator: 'mangled',
|
||||
renameGlobals: false
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
});
|
||||
|
||||
it('should generate non-preserved name for variable name', () => {
|
||||
assert.match(obfuscatedCode, variableDeclarationIdentifierName);
|
||||
});
|
||||
|
||||
it('should keep the original name for destructed object property identifier', () => {
|
||||
assert.match(obfuscatedCode, destructedObjectPropertyIdentifierName);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #2: `renameGlobals` option is enabled', () => {
|
||||
const variableDeclarationIdentifierName: RegExp = /var b *= *0x1;/;
|
||||
const functionDeclarationIdentifierName: RegExp = /function c *\(\) *{/;
|
||||
const destructedObjectPropertyIdentifierName: RegExp = /const { *a *} *= *{ *'a' *: *0x2 *};/;
|
||||
|
||||
let obfuscatedCode: string;
|
||||
|
||||
before(() => {
|
||||
const code: string = readFileAsString(__dirname + '/fixtures/destructed-object-property-identifier-name-2.js');
|
||||
|
||||
obfuscatedCode = JavaScriptObfuscator.obfuscate(
|
||||
code,
|
||||
{
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
identifierNamesGenerator: 'mangled',
|
||||
renameGlobals: true
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
});
|
||||
|
||||
it('should generate non-preserved name for variable declaration', () => {
|
||||
assert.match(obfuscatedCode, variableDeclarationIdentifierName);
|
||||
});
|
||||
|
||||
it('should generate non-preserved name for function declaration', () => {
|
||||
assert.match(obfuscatedCode, functionDeclarationIdentifierName);
|
||||
});
|
||||
|
||||
it('should keep the original name for destructed object property identifier', () => {
|
||||
assert.match(obfuscatedCode, destructedObjectPropertyIdentifierName);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #3: function destructed object property', () => {
|
||||
const variableDeclarationIdentifierName: RegExp = /var b *= *0x1;/;
|
||||
const destructedObjectPropertyIdentifierName: RegExp = /return *\({ *a *}\) *=> *{/;
|
||||
const consoleLogIdentifierNames: RegExp = /console\['log']\(b, *a\);/;
|
||||
|
||||
let obfuscatedCode: string;
|
||||
|
||||
before(() => {
|
||||
const code: string = readFileAsString(__dirname + '/fixtures/destructed-object-property-identifier-name-3.js');
|
||||
|
||||
obfuscatedCode = JavaScriptObfuscator.obfuscate(
|
||||
code,
|
||||
{
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
identifierNamesGenerator: 'mangled',
|
||||
renameGlobals: false
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
});
|
||||
|
||||
it('should generate non-preserved name for variable declaration', () => {
|
||||
assert.match(obfuscatedCode, variableDeclarationIdentifierName);
|
||||
});
|
||||
|
||||
it('should keep the original name for destructed object property identifier', () => {
|
||||
assert.match(obfuscatedCode, destructedObjectPropertyIdentifierName);
|
||||
});
|
||||
|
||||
it('should generate valid identifier names for console.log call', () => {
|
||||
assert.match(obfuscatedCode, consoleLogIdentifierNames);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
function foo() {
|
||||
var bar = 1;
|
||||
|
||||
return () => {
|
||||
const { a } = {a: 2};
|
||||
|
||||
console.log(bar, a);
|
||||
};
|
||||
}
|
||||
foo();
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
var foo = 1;
|
||||
|
||||
function bar() {
|
||||
const { a } = {a: 2};
|
||||
|
||||
console.log(foo, a);
|
||||
}
|
||||
bar();
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
function foo() {
|
||||
var bar = 1;
|
||||
|
||||
return ({a}) => {
|
||||
console.log(bar, a);
|
||||
}
|
||||
}
|
||||
foo();
|
||||
Reference in New Issue
Block a user