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

v0.24.1
---
* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/531

v0.24.0
---
* **Internal refactoring:** completely new mechanism to rename variable names
Expand Down
10 changes: 5 additions & 5 deletions dist/index.browser.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.cli.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "javascript-obfuscator",
"version": "0.24.0",
"version": "0.24.1",
"description": "JavaScript obfuscator",
"keywords": [
"obfuscator",
Expand Down
50 changes: 30 additions & 20 deletions src/analyzers/scope-analyzer/ScopeAnalyzer.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { inject, injectable, } from 'inversify';
import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
import { injectable, } from 'inversify';

import * as eslintScope from 'eslint-scope';
import * as estraverse from 'estraverse';
import * as ESTree from 'estree';

import { IOptions } from '../../interfaces/options/IOptions';
import { IScopeAnalyzer } from '../../interfaces/analyzers/scope-analyzer/IScopeAnalyzer';

import { ObfuscationTarget } from '../../enums/ObfuscationTarget';

import { ecmaVersion } from '../../constants/EcmaVersion';

import { NodeGuards } from '../../node/NodeGuards';
Expand Down Expand Up @@ -37,25 +33,11 @@ export class ScopeAnalyzer implements IScopeAnalyzer {
*/
private static readonly emptyRangeValue: number = 0;

/**
* @type {IOptions}
*/
private readonly options: IOptions;

/**
* @type {eslintScope.ScopeManager | null}
*/
private scopeManager: eslintScope.ScopeManager | null = null;

/**
* @param {IOptions} options
*/
constructor (
@inject(ServiceIdentifiers.IOptions) options: IOptions
) {
this.options = options;
}

/**
* `eslint-scope` reads `ranges` property of a nodes
* Should attach that property to the some custom nodes
Expand Down Expand Up @@ -97,7 +79,6 @@ export class ScopeAnalyzer implements IScopeAnalyzer {
try {
this.scopeManager = eslintScope.analyze(astTree, {
...ScopeAnalyzer.eslintScopeOptions,
nodejsScope: this.options.target === ObfuscationTarget.Node,
sourceType: ScopeAnalyzer.sourceTypes[i]
});

Expand Down Expand Up @@ -132,6 +113,35 @@ export class ScopeAnalyzer implements IScopeAnalyzer {
throw new Error('Cannot acquire scope for node');
}

this.sanitizeScopes(scope);

return scope;
}

/**
* @param {Scope} scope
*/
private sanitizeScopes (scope: eslintScope.Scope): void {
scope.childScopes.forEach((childScope: eslintScope.Scope) => {
// fix of class scopes
// trying to move class scope references to the parent scope
if (childScope.type === 'class' && childScope.upper) {
const upperVariable: eslintScope.Variable | undefined = childScope.upper.variables
.find((variable: eslintScope.Variable) => {
// class name variable is always first
const classNameVariable: eslintScope.Variable = childScope.variables[0];
const isValidClassNameVariable: boolean = classNameVariable.defs
.some((definition: eslintScope.Definition) => definition.type === 'ClassName');

return isValidClassNameVariable && variable.name === classNameVariable.name;
});

upperVariable?.references.push(...childScope.variables[0].references);
}
});

for (const childScope of scope.childScopes) {
this.sanitizeScopes(childScope);
}
}
}
26 changes: 23 additions & 3 deletions test/dev/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,33 @@ import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNo

let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
`
var passthrough = value => value;
var foo = 1, bar = {baz: passthrough(foo)}
class A {
foo() {
return A;
}

bar() {
var A = 1;
return A;
}
}

console.log(A);

function foo () {
class B {
foo() {
return B;
}
}

console.log(B);
}
`,
{
...NO_ADDITIONAL_NODES_PRESET,
compact: false,
transformObjectKeys: true
target: 'node'
}
).getObfuscatedCode();

Expand Down
Loading