mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
Some refactoring. Added IdentifierNamesCacheStorage tests
This commit is contained in:
-5
@@ -1,11 +1,6 @@
|
||||
import * as ESTree from 'estree';
|
||||
|
||||
export interface IThroughIdentifierReplacer {
|
||||
/**
|
||||
* @param {Identifier} identifierNode
|
||||
*/
|
||||
store (identifierNode: ESTree.Identifier): void;
|
||||
|
||||
/**
|
||||
* @param {Identifier} identifierNode
|
||||
* @returns {Identifier}
|
||||
|
||||
+4
-19
@@ -108,30 +108,15 @@ export class ScopeThroughIdentifiersTransformer extends AbstractNodeTransformer
|
||||
return;
|
||||
}
|
||||
|
||||
const identifier: ESTree.Identifier = reference.identifier;
|
||||
|
||||
this.storeIdentifierName(identifier);
|
||||
this.replaceIdentifierName(identifier, reference);
|
||||
this.replaceIdentifierName(reference);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Identifier} identifierNode
|
||||
*/
|
||||
protected storeIdentifierName (
|
||||
identifierNode: ESTree.Identifier
|
||||
): void {
|
||||
this.throughIdentifierReplacer.store(identifierNode);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Identifier} identifierNode
|
||||
* @param {Variable} reference
|
||||
*/
|
||||
protected replaceIdentifierName (
|
||||
identifierNode: ESTree.Identifier,
|
||||
reference: eslintScope.Reference
|
||||
): void {
|
||||
const newIdentifier: ESTree.Identifier = this.throughIdentifierReplacer.replace(identifierNode);
|
||||
protected replaceIdentifierName (reference: eslintScope.Reference): void {
|
||||
const identifier: ESTree.Identifier = reference.identifier;
|
||||
const newIdentifier: ESTree.Identifier = this.throughIdentifierReplacer.replace(identifier);
|
||||
|
||||
// rename of identifier
|
||||
reference.identifier.name = newIdentifier.name;
|
||||
|
||||
+3
-1
@@ -77,7 +77,9 @@ export class IdentifierReplacer implements IIdentifierReplacer {
|
||||
namesMap.set(identifierName, newIdentifierName);
|
||||
|
||||
// Have to write all global identifier names to the identifier names cache storage
|
||||
this.identifierNamesCacheStorage.set(identifierName, newIdentifierName);
|
||||
if (this.options.identifierNamesCache) {
|
||||
this.identifierNamesCacheStorage.set(identifierName, newIdentifierName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+5
-25
@@ -34,37 +34,17 @@ export class ThroughIdentifierReplacer implements IThroughIdentifierReplacer {
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store identifier node `name` of `through` identifiers as key in map with value from identifier names cache.
|
||||
* Reserved name will be ignored.
|
||||
*
|
||||
* @param {Node} identifierNode
|
||||
*/
|
||||
public store (identifierNode: ESTree.Identifier): void {
|
||||
const identifierName: string = identifierNode.name;
|
||||
|
||||
if (this.isReservedName(identifierName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const newIdentifierName: string | null = this.identifierNamesCacheStorage.get(identifierName) ?? null;
|
||||
|
||||
if (!newIdentifierName) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.identifierNamesCacheStorage.set(identifierName, newIdentifierName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Identifier} identifierNode
|
||||
* @returns {Identifier}
|
||||
*/
|
||||
public replace (identifierNode: ESTree.Identifier): ESTree.Identifier {
|
||||
const identifierName: string = this.identifierNamesCacheStorage.get(identifierNode.name)
|
||||
?? identifierNode.name;
|
||||
const identifierName: string = identifierNode.name;
|
||||
const newIdentifierName: string = this.options.identifierNamesCache && !this.isReservedName(identifierName)
|
||||
? this.identifierNamesCacheStorage.get(identifierName) ?? identifierName
|
||||
: identifierName;
|
||||
|
||||
return NodeFactory.identifierNode(identifierName);
|
||||
return NodeFactory.identifierNode(newIdentifierName);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -11,12 +11,7 @@ import { MapStorage } from '../MapStorage';
|
||||
|
||||
@injectable()
|
||||
export class IdentifierNamesCacheStorage extends MapStorage <string, string> implements IIdentifierNamesCacheStorage {
|
||||
/**
|
||||
* @type {boolean}
|
||||
*/
|
||||
private readonly shouldUseCache: boolean;
|
||||
|
||||
/**
|
||||
/**
|
||||
* @param {IRandomGenerator} randomGenerator
|
||||
* @param {IOptions} options
|
||||
*/
|
||||
@@ -25,60 +20,20 @@ export class IdentifierNamesCacheStorage extends MapStorage <string, string> imp
|
||||
@inject(ServiceIdentifiers.IOptions) options: IOptions
|
||||
) {
|
||||
super(randomGenerator, options);
|
||||
|
||||
this.shouldUseCache = !!options.identifierNamesCache;
|
||||
}
|
||||
|
||||
@postConstruct()
|
||||
public override initialize (): void {
|
||||
super.initialize();
|
||||
|
||||
if (this.options.identifierNamesCache) {
|
||||
this.storage = new Map(Object.entries(this.options.identifierNamesCache));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} key
|
||||
* @returns {string | undefined}
|
||||
*/
|
||||
public override get (key: string): string | undefined {
|
||||
if (!this.shouldUseCache) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return super.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} key
|
||||
* @returns {boolean}
|
||||
*/
|
||||
public override has (key: string): boolean {
|
||||
if (!this.shouldUseCache) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return super.has(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} key
|
||||
* @param {string} value
|
||||
*/
|
||||
public override set (key: string, value: string): void {
|
||||
if (!this.shouldUseCache) {
|
||||
return;
|
||||
}
|
||||
|
||||
super.set(key, value);
|
||||
this.storage = new Map(Object.entries(this.options.identifierNamesCache ?? {}));
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {TIdentifierNamesCache}
|
||||
*/
|
||||
public getCache (): TIdentifierNamesCache {
|
||||
if (!this.shouldUseCache) {
|
||||
if (!this.options.identifierNamesCache) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user