mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
Added property cache implementation
This commit is contained in:
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+1
-1
File diff suppressed because one or more lines are too long
@@ -29,7 +29,7 @@ export enum ServiceIdentifiers {
|
||||
ICustomCodeHelperFormatter = 'ICustomCodeHelperFormatter',
|
||||
ICustomCodeHelperObfuscator = 'ICustomCodeHelperObfuscator',
|
||||
IEscapeSequenceEncoder = 'IEscapeSequenceEncoder',
|
||||
IIdentifierNamesCacheStorage = 'IIdentifierNamesCacheStorage',
|
||||
IGlobalIdentifierNamesCacheStorage = 'IGlobalIdentifierNamesCacheStorage',
|
||||
IIdentifierNamesGenerator = 'IIdentifierNamesGenerator',
|
||||
IIdentifierReplacer = 'IIdentifierReplacer',
|
||||
IJavaScriptObfuscator = 'IJavaScriptObfuscator',
|
||||
@@ -45,6 +45,7 @@ export enum ServiceIdentifiers {
|
||||
IOptions = 'IOptions',
|
||||
IOptionsNormalizer = 'IOptionsNormalizer',
|
||||
IPrevailingKindOfVariablesAnalyzer = 'IPrevailingKindOfVariablesAnalyzer',
|
||||
IPropertyIdentifierNamesCacheStorage = 'IPropertyIdentifierNamesCacheStorage',
|
||||
IObjectExpressionExtractor = 'IObjectExpressionExtractor',
|
||||
IRandomGenerator = 'IRandomGenerator',
|
||||
IRenamePropertiesReplacer = 'IRenamePropertiesReplacer',
|
||||
|
||||
@@ -4,9 +4,10 @@ import { ServiceIdentifiers } from '../../ServiceIdentifiers';
|
||||
import { TControlFlowStorage } from '../../../types/storages/TControlFlowStorage';
|
||||
import { TCustomCodeHelperGroupStorage } from '../../../types/storages/TCustomCodeHelperGroupStorage';
|
||||
|
||||
import { IIdentifierNamesCacheStorage } from '../../../interfaces/storages/identifier-names-cache/IIdentifierNamesCacheStorage';
|
||||
import { IGlobalIdentifierNamesCacheStorage } from '../../../interfaces/storages/identifier-names-cache/IGlobalIdentifierNamesCacheStorage';
|
||||
import { ILiteralNodesCacheStorage } from '../../../interfaces/storages/string-array-transformers/ILiteralNodesCacheStorage';
|
||||
import { IOptions } from '../../../interfaces/options/IOptions';
|
||||
import { IPropertyIdentifierNamesCacheStorage } from '../../../interfaces/storages/identifier-names-cache/IPropertyIdentifierNamesCacheStorage';
|
||||
import { IRandomGenerator } from '../../../interfaces/utils/IRandomGenerator';
|
||||
import { IStringArrayScopeCallsWrapperLexicalScopeDataStorage } from '../../../interfaces/storages/string-array-transformers/IStringArrayScopeCallsWrapperLexicalScopeDataStorage';
|
||||
import { IStringArrayScopeCallsWrapperNamesDataStorage } from '../../../interfaces/storages/string-array-transformers/IStringArrayScopeCallsWrapperNamesDataStorage';
|
||||
@@ -15,8 +16,9 @@ import { IVisitedLexicalScopeNodesStackStorage } from '../../../interfaces/stora
|
||||
|
||||
import { ControlFlowStorage } from '../../../storages/custom-nodes/ControlFlowStorage';
|
||||
import { CustomCodeHelperGroupStorage } from '../../../storages/custom-code-helpers/CustomCodeHelperGroupStorage';
|
||||
import { IdentifierNamesCacheStorage } from '../../../storages/identifier-names-cache/IdentifierNamesCacheStorage';
|
||||
import { GlobalIdentifierNamesCacheStorage } from '../../../storages/identifier-names-cache/GlobalIdentifierNamesCacheStorage';
|
||||
import { LiteralNodesCacheStorage } from '../../../storages/string-array-transformers/LiteralNodesCacheStorage';
|
||||
import { PropertyIdentifierNamesCacheStorage } from '../../../storages/identifier-names-cache/PropertyIdentifierNamesCacheStorage';
|
||||
import { StringArrayScopeCallsWrapperLexicalScopeDataStorage } from '../../../storages/string-array-transformers/StringArrayScopeCallsWrapperLexicalScopeDataStorage';
|
||||
import { StringArrayScopeCallsWrapperNamesDataStorage } from '../../../storages/string-array-transformers/StringArrayScopeCallsWrapperNamesDataStorage';
|
||||
import { StringArrayStorage } from '../../../storages/string-array-transformers/StringArrayStorage';
|
||||
@@ -28,14 +30,18 @@ export const storagesModule: interfaces.ContainerModule = new ContainerModule((b
|
||||
.to(CustomCodeHelperGroupStorage)
|
||||
.inSingletonScope();
|
||||
|
||||
bind<IIdentifierNamesCacheStorage>(ServiceIdentifiers.IIdentifierNamesCacheStorage)
|
||||
.to(IdentifierNamesCacheStorage)
|
||||
bind<IGlobalIdentifierNamesCacheStorage>(ServiceIdentifiers.IGlobalIdentifierNamesCacheStorage)
|
||||
.to(GlobalIdentifierNamesCacheStorage)
|
||||
.inSingletonScope();
|
||||
|
||||
bind<ILiteralNodesCacheStorage>(ServiceIdentifiers.ILiteralNodesCacheStorage)
|
||||
.to(LiteralNodesCacheStorage)
|
||||
.inSingletonScope();
|
||||
|
||||
bind<IPropertyIdentifierNamesCacheStorage>(ServiceIdentifiers.IPropertyIdentifierNamesCacheStorage)
|
||||
.to(PropertyIdentifierNamesCacheStorage)
|
||||
.inSingletonScope();
|
||||
|
||||
bind<IStringArrayStorage>(ServiceIdentifiers.IStringArrayStorage)
|
||||
.to(StringArrayStorage)
|
||||
.inSingletonScope();
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
import { IMapStorage } from '../IMapStorage';
|
||||
|
||||
// eslint-disable-next-line
|
||||
export interface IGlobalIdentifierNamesCacheStorage extends IMapStorage <string, string> {}
|
||||
@@ -1,4 +0,0 @@
|
||||
import { IMapStorage } from '../IMapStorage';
|
||||
|
||||
// eslint-disable-next-line
|
||||
export interface IIdentifierNamesCacheStorage extends IMapStorage <string, string> {}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { IMapStorage } from '../IMapStorage';
|
||||
|
||||
// eslint-disable-next-line
|
||||
export interface IPropertyIdentifierNamesCacheStorage extends IMapStorage <string, string> {}
|
||||
+6
-6
@@ -6,7 +6,7 @@ import * as ESTree from 'estree';
|
||||
import { TIdentifierNamesGeneratorFactory } from '../../../types/container/generators/TIdentifierNamesGeneratorFactory';
|
||||
import { TNodeWithLexicalScope } from '../../../types/node/TNodeWithLexicalScope';
|
||||
|
||||
import { IIdentifierNamesCacheStorage } from '../../../interfaces/storages/identifier-names-cache/IIdentifierNamesCacheStorage';
|
||||
import { IGlobalIdentifierNamesCacheStorage } from '../../../interfaces/storages/identifier-names-cache/IGlobalIdentifierNamesCacheStorage';
|
||||
import { IIdentifierNamesGenerator } from '../../../interfaces/generators/identifier-names-generators/IIdentifierNamesGenerator';
|
||||
import { IIdentifierReplacer } from '../../../interfaces/node-transformers/rename-identifiers-transformers/replacer/IIdentifierReplacer';
|
||||
import { IOptions } from '../../../interfaces/options/IOptions';
|
||||
@@ -16,9 +16,9 @@ import { NodeFactory } from '../../../node/NodeFactory';
|
||||
@injectable()
|
||||
export class IdentifierReplacer implements IIdentifierReplacer {
|
||||
/**
|
||||
* @type {IIdentifierNamesCacheStorage}
|
||||
* @type {IGlobalIdentifierNamesCacheStorage}
|
||||
*/
|
||||
private readonly identifierNamesCacheStorage: IIdentifierNamesCacheStorage;
|
||||
private readonly identifierNamesCacheStorage: IGlobalIdentifierNamesCacheStorage;
|
||||
|
||||
/**
|
||||
* @type {IIdentifierNamesGenerator}
|
||||
@@ -37,14 +37,14 @@ export class IdentifierReplacer implements IIdentifierReplacer {
|
||||
|
||||
/**
|
||||
* @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
|
||||
* @param {IIdentifierNamesCacheStorage} identifierNamesCacheStorage
|
||||
* @param {IGlobalIdentifierNamesCacheStorage} identifierNamesCacheStorage
|
||||
* @param {IOptions} options
|
||||
*/
|
||||
public constructor (
|
||||
@inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
|
||||
identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
|
||||
@inject(ServiceIdentifiers.IIdentifierNamesCacheStorage)
|
||||
identifierNamesCacheStorage: IIdentifierNamesCacheStorage,
|
||||
@inject(ServiceIdentifiers.IGlobalIdentifierNamesCacheStorage)
|
||||
identifierNamesCacheStorage: IGlobalIdentifierNamesCacheStorage,
|
||||
@inject(ServiceIdentifiers.IOptions) options: IOptions
|
||||
) {
|
||||
this.options = options;
|
||||
|
||||
+6
-6
@@ -3,7 +3,7 @@ import { ServiceIdentifiers } from '../../../container/ServiceIdentifiers';
|
||||
|
||||
import * as ESTree from 'estree';
|
||||
|
||||
import { IIdentifierNamesCacheStorage } from '../../../interfaces/storages/identifier-names-cache/IIdentifierNamesCacheStorage';
|
||||
import { IGlobalIdentifierNamesCacheStorage } from '../../../interfaces/storages/identifier-names-cache/IGlobalIdentifierNamesCacheStorage';
|
||||
import { IOptions } from '../../../interfaces/options/IOptions';
|
||||
import { IThroughIdentifierReplacer } from '../../../interfaces/node-transformers/rename-identifiers-transformers/replacer/IThroughIdentifierReplacer';
|
||||
|
||||
@@ -12,9 +12,9 @@ import { NodeFactory } from '../../../node/NodeFactory';
|
||||
@injectable()
|
||||
export class ThroughIdentifierReplacer implements IThroughIdentifierReplacer {
|
||||
/**
|
||||
* @type {IIdentifierNamesCacheStorage}
|
||||
* @type {IGlobalIdentifierNamesCacheStorage}
|
||||
*/
|
||||
private readonly identifierNamesCacheStorage: IIdentifierNamesCacheStorage;
|
||||
private readonly identifierNamesCacheStorage: IGlobalIdentifierNamesCacheStorage;
|
||||
|
||||
/**
|
||||
* @type {IOptions}
|
||||
@@ -22,12 +22,12 @@ export class ThroughIdentifierReplacer implements IThroughIdentifierReplacer {
|
||||
private readonly options: IOptions;
|
||||
|
||||
/**
|
||||
* @param {IIdentifierNamesCacheStorage} identifierNamesCacheStorage
|
||||
* @param {IGlobalIdentifierNamesCacheStorage} identifierNamesCacheStorage
|
||||
* @param {IOptions} options
|
||||
*/
|
||||
public constructor (
|
||||
@inject(ServiceIdentifiers.IIdentifierNamesCacheStorage)
|
||||
identifierNamesCacheStorage: IIdentifierNamesCacheStorage,
|
||||
@inject(ServiceIdentifiers.IGlobalIdentifierNamesCacheStorage)
|
||||
identifierNamesCacheStorage: IGlobalIdentifierNamesCacheStorage,
|
||||
@inject(ServiceIdentifiers.IOptions) options: IOptions
|
||||
) {
|
||||
this.identifierNamesCacheStorage = identifierNamesCacheStorage;
|
||||
|
||||
+22
-2
@@ -7,8 +7,9 @@ import * as ESTree from 'estree';
|
||||
import { TIdentifierNamesGeneratorFactory } from '../../../types/container/generators/TIdentifierNamesGeneratorFactory';
|
||||
|
||||
import { IIdentifierNamesGenerator } from '../../../interfaces/generators/identifier-names-generators/IIdentifierNamesGenerator';
|
||||
import { IRenamePropertiesReplacer } from '../../../interfaces/node-transformers/rename-properties-transformers/replacer/IRenamePropertiesReplacer';
|
||||
import { IOptions } from '../../../interfaces/options/IOptions';
|
||||
import { IPropertyIdentifierNamesCacheStorage } from '../../../interfaces/storages/identifier-names-cache/IPropertyIdentifierNamesCacheStorage';
|
||||
import { IRenamePropertiesReplacer } from '../../../interfaces/node-transformers/rename-properties-transformers/replacer/IRenamePropertiesReplacer';
|
||||
|
||||
// eslint-disable-next-line import/no-internal-modules
|
||||
import ReservedDomProperties from './ReservedDomProperties.json';
|
||||
@@ -38,6 +39,11 @@ export class RenamePropertiesReplacer implements IRenamePropertiesReplacer {
|
||||
*/
|
||||
private readonly excludedPropertyNames: Set<string> = new Set();
|
||||
|
||||
/**
|
||||
* @type {IPropertyIdentifierNamesCacheStorage}
|
||||
*/
|
||||
private readonly propertyIdentifierNamesCacheStorage: IPropertyIdentifierNamesCacheStorage;
|
||||
|
||||
/**
|
||||
* @type {Map<string, string>}
|
||||
* @private
|
||||
@@ -51,14 +57,18 @@ export class RenamePropertiesReplacer implements IRenamePropertiesReplacer {
|
||||
|
||||
/**
|
||||
* @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
|
||||
* @param {IPropertyIdentifierNamesCacheStorage} propertyIdentifierNamesCacheStorage
|
||||
* @param {IOptions} options
|
||||
*/
|
||||
public constructor (
|
||||
@inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
|
||||
identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
|
||||
@inject(ServiceIdentifiers.IPropertyIdentifierNamesCacheStorage)
|
||||
propertyIdentifierNamesCacheStorage: IPropertyIdentifierNamesCacheStorage,
|
||||
@inject(ServiceIdentifiers.IOptions) options: IOptions
|
||||
) {
|
||||
this.identifierNamesGenerator = identifierNamesGeneratorFactory(options);
|
||||
this.propertyIdentifierNamesCacheStorage = propertyIdentifierNamesCacheStorage;
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
@@ -99,7 +109,13 @@ export class RenamePropertiesReplacer implements IRenamePropertiesReplacer {
|
||||
return propertyName;
|
||||
}
|
||||
|
||||
let renamedPropertyName: string | null = this.propertyNamesMap.get(propertyName) ?? null;
|
||||
let renamedPropertyName: string | null = this.options.identifierNamesCache
|
||||
? this.propertyIdentifierNamesCacheStorage.get(propertyName) ?? null
|
||||
: null;
|
||||
|
||||
renamedPropertyName = renamedPropertyName
|
||||
?? this.propertyNamesMap.get(propertyName)
|
||||
?? null;
|
||||
|
||||
if (renamedPropertyName !== null) {
|
||||
return renamedPropertyName;
|
||||
@@ -108,6 +124,10 @@ export class RenamePropertiesReplacer implements IRenamePropertiesReplacer {
|
||||
renamedPropertyName = this.identifierNamesGenerator.generateNext();
|
||||
this.propertyNamesMap.set(propertyName, renamedPropertyName);
|
||||
|
||||
if (this.options.identifierNamesCache) {
|
||||
this.propertyIdentifierNamesCacheStorage.set(propertyName, renamedPropertyName);
|
||||
}
|
||||
|
||||
return renamedPropertyName;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,8 +4,9 @@ import { ServiceIdentifiers } from '../container/ServiceIdentifiers';
|
||||
import { TIdentifierNamesCache } from '../types/TIdentifierNamesCache';
|
||||
|
||||
import { ICryptUtils } from '../interfaces/utils/ICryptUtils';
|
||||
import { IIdentifierNamesCacheStorage } from '../interfaces/storages/identifier-names-cache/IIdentifierNamesCacheStorage';
|
||||
import { IGlobalIdentifierNamesCacheStorage } from '../interfaces/storages/identifier-names-cache/IGlobalIdentifierNamesCacheStorage';
|
||||
import { IObfuscationResult } from '../interfaces/source-code/IObfuscationResult';
|
||||
import { IPropertyIdentifierNamesCacheStorage } from '../interfaces/storages/identifier-names-cache/IPropertyIdentifierNamesCacheStorage';
|
||||
import { IOptions } from '../interfaces/options/IOptions';
|
||||
|
||||
import { initializable } from '../decorators/Initializable';
|
||||
@@ -31,9 +32,14 @@ export class ObfuscationResult implements IObfuscationResult {
|
||||
private readonly cryptUtils: ICryptUtils;
|
||||
|
||||
/**
|
||||
* @type {IIdentifierNamesCacheStorage}
|
||||
* @type {IGlobalIdentifierNamesCacheStorage}
|
||||
*/
|
||||
private readonly identifierNamesCacheStorage: IIdentifierNamesCacheStorage;
|
||||
private readonly globalIdentifierNamesCacheStorage: IGlobalIdentifierNamesCacheStorage;
|
||||
|
||||
/**
|
||||
* @type {IPropertyIdentifierNamesCacheStorage}
|
||||
*/
|
||||
private readonly propertyIdentifierNamesCacheStorage: IPropertyIdentifierNamesCacheStorage;
|
||||
|
||||
/**
|
||||
* @type {IOptions}
|
||||
@@ -42,17 +48,21 @@ export class ObfuscationResult implements IObfuscationResult {
|
||||
|
||||
/**
|
||||
* @param {ICryptUtils} cryptUtils
|
||||
* @param {IIdentifierNamesCacheStorage} identifierNamesCacheStorage
|
||||
* @param {IGlobalIdentifierNamesCacheStorage} globalIdentifierNamesCacheStorage
|
||||
* @param {IPropertyIdentifierNamesCacheStorage} propertyIdentifierNamesCacheStorage
|
||||
* @param {IOptions} options
|
||||
*/
|
||||
public constructor (
|
||||
@inject(ServiceIdentifiers.ICryptUtils) cryptUtils: ICryptUtils,
|
||||
@inject(ServiceIdentifiers.IIdentifierNamesCacheStorage)
|
||||
identifierNamesCacheStorage: IIdentifierNamesCacheStorage,
|
||||
@inject(ServiceIdentifiers.IGlobalIdentifierNamesCacheStorage)
|
||||
globalIdentifierNamesCacheStorage: IGlobalIdentifierNamesCacheStorage,
|
||||
@inject(ServiceIdentifiers.IPropertyIdentifierNamesCacheStorage)
|
||||
propertyIdentifierNamesCacheStorage: IPropertyIdentifierNamesCacheStorage,
|
||||
@inject(ServiceIdentifiers.IOptions) options: IOptions
|
||||
) {
|
||||
this.cryptUtils = cryptUtils;
|
||||
this.identifierNamesCacheStorage = identifierNamesCacheStorage;
|
||||
this.globalIdentifierNamesCacheStorage = globalIdentifierNamesCacheStorage;
|
||||
this.propertyIdentifierNamesCacheStorage = propertyIdentifierNamesCacheStorage;
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
@@ -74,8 +84,8 @@ export class ObfuscationResult implements IObfuscationResult {
|
||||
}
|
||||
|
||||
return {
|
||||
globalIdentifiers: this.identifierNamesCacheStorage.getStorageAsDictionary(),
|
||||
propertyIdentifiers: {}
|
||||
globalIdentifiers: this.globalIdentifierNamesCacheStorage.getStorageAsDictionary(),
|
||||
propertyIdentifiers: this.propertyIdentifierNamesCacheStorage.getStorageAsDictionary()
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -1,14 +1,14 @@
|
||||
import { inject, injectable, postConstruct } from 'inversify';
|
||||
import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
|
||||
|
||||
import { IIdentifierNamesCacheStorage } from '../../interfaces/storages/identifier-names-cache/IIdentifierNamesCacheStorage';
|
||||
import { IGlobalIdentifierNamesCacheStorage } from '../../interfaces/storages/identifier-names-cache/IGlobalIdentifierNamesCacheStorage';
|
||||
import { IOptions } from '../../interfaces/options/IOptions';
|
||||
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
|
||||
|
||||
import { MapStorage } from '../MapStorage';
|
||||
|
||||
@injectable()
|
||||
export class IdentifierNamesCacheStorage extends MapStorage <string, string> implements IIdentifierNamesCacheStorage {
|
||||
export class GlobalIdentifierNamesCacheStorage extends MapStorage <string, string> implements IGlobalIdentifierNamesCacheStorage {
|
||||
/**
|
||||
* @param {IRandomGenerator} randomGenerator
|
||||
* @param {IOptions} options
|
||||
@@ -0,0 +1,29 @@
|
||||
import { inject, injectable, postConstruct } from 'inversify';
|
||||
import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
|
||||
|
||||
import { IOptions } from '../../interfaces/options/IOptions';
|
||||
import { IPropertyIdentifierNamesCacheStorage } from '../../interfaces/storages/identifier-names-cache/IPropertyIdentifierNamesCacheStorage';
|
||||
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
|
||||
|
||||
import { MapStorage } from '../MapStorage';
|
||||
|
||||
@injectable()
|
||||
export class PropertyIdentifierNamesCacheStorage extends MapStorage <string, string> implements IPropertyIdentifierNamesCacheStorage {
|
||||
/**
|
||||
* @param {IRandomGenerator} randomGenerator
|
||||
* @param {IOptions} options
|
||||
*/
|
||||
public constructor (
|
||||
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
|
||||
@inject(ServiceIdentifiers.IOptions) options: IOptions
|
||||
) {
|
||||
super(randomGenerator, options);
|
||||
}
|
||||
|
||||
@postConstruct()
|
||||
public override initialize (): void {
|
||||
super.initialize();
|
||||
|
||||
this.storage = new Map(Object.entries(this.options.identifierNamesCache?.propertyIdentifiers ?? {}));
|
||||
}
|
||||
}
|
||||
+5
-4
@@ -6,20 +6,21 @@
|
||||
let obfuscationResult = JavaScriptObfuscator.obfuscate(
|
||||
`
|
||||
function foo() {
|
||||
var baz = 1;
|
||||
global.baz = 3;
|
||||
}
|
||||
|
||||
function bar(...args) {
|
||||
var bark = 2;
|
||||
console.log(2);
|
||||
}
|
||||
`,
|
||||
{
|
||||
compact: false,
|
||||
identifierNamesCache: {
|
||||
globalIdentifiers: { foo: '_0x5de86d', bar: '_0x2a943b' },
|
||||
globalIdentifiers: {},
|
||||
propertyIdentifiers: {}
|
||||
},
|
||||
renameGlobals: true
|
||||
renameGlobals: true,
|
||||
renameProperties: true
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
+2
-1
@@ -36,7 +36,8 @@ import './unit-tests/source-code/ObfuscationResult.spec';
|
||||
import './unit-tests/source-code/SourceCode.spec';
|
||||
import './unit-tests/storages/ArrayStorage.spec';
|
||||
import './unit-tests/storages/MapStorage.spec';
|
||||
import './unit-tests/storages/identifier-names-cache/IdentifierNamesCacheStorage.spec';
|
||||
import './unit-tests/storages/identifier-names-cache/GlobalIdentifierNamesCacheStorage.spec';
|
||||
import './unit-tests/storages/identifier-names-cache/PropertyIdentifierNamesCacheStorage.spec';
|
||||
import './unit-tests/storages/string-array-transformers/literal-nodes-cache/LiteralNodesCacheStorage.spec';
|
||||
import './unit-tests/storages/string-array-transformers/string-array/StringArrayStorage.spec';
|
||||
import './unit-tests/storages/string-array-transformers/visited-lexical-scope-nodes-stack/VisitedLexicalScopeNodesStackStorage.spec';
|
||||
|
||||
+7
-7
@@ -6,24 +6,24 @@ import { ServiceIdentifiers } from '../../../../src/container/ServiceIdentifiers
|
||||
|
||||
import { TDictionary } from '../../../../src/types/TDictionary';
|
||||
|
||||
import { IIdentifierNamesCacheStorage } from '../../../../src/interfaces/storages/identifier-names-cache/IIdentifierNamesCacheStorage';
|
||||
import { IGlobalIdentifierNamesCacheStorage } from '../../../../src/interfaces/storages/identifier-names-cache/IGlobalIdentifierNamesCacheStorage';
|
||||
import { IInversifyContainerFacade } from '../../../../src/interfaces/container/IInversifyContainerFacade';
|
||||
import { IOptions } from '../../../../src/interfaces/options/IOptions';
|
||||
import { IRandomGenerator } from '../../../../src/interfaces/utils/IRandomGenerator';
|
||||
|
||||
import { DEFAULT_PRESET } from '../../../../src/options/presets/Default';
|
||||
|
||||
import { IdentifierNamesCacheStorage } from '../../../../src/storages/identifier-names-cache/IdentifierNamesCacheStorage';
|
||||
import { GlobalIdentifierNamesCacheStorage } from '../../../../src/storages/identifier-names-cache/GlobalIdentifierNamesCacheStorage';
|
||||
import { InversifyContainerFacade } from '../../../../src/container/InversifyContainerFacade';
|
||||
|
||||
/**
|
||||
* @returns {IIdentifierNamesCacheStorage}
|
||||
* @returns {IGlobalIdentifierNamesCacheStorage}
|
||||
*/
|
||||
const getStorageInstance = <V>(options: Partial<IOptions> = DEFAULT_PRESET): IIdentifierNamesCacheStorage => {
|
||||
const getStorageInstance = <V>(options: Partial<IOptions> = DEFAULT_PRESET): IGlobalIdentifierNamesCacheStorage => {
|
||||
const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
|
||||
inversifyContainerFacade.load('', '', {});
|
||||
|
||||
const storage: IIdentifierNamesCacheStorage = new IdentifierNamesCacheStorage (
|
||||
const storage: IGlobalIdentifierNamesCacheStorage = new GlobalIdentifierNamesCacheStorage (
|
||||
inversifyContainerFacade.get<IRandomGenerator>(ServiceIdentifiers.IRandomGenerator),
|
||||
{
|
||||
...DEFAULT_PRESET,
|
||||
@@ -36,11 +36,11 @@ const getStorageInstance = <V>(options: Partial<IOptions> = DEFAULT_PRESET): IId
|
||||
return storage;
|
||||
};
|
||||
|
||||
describe('IdentifierNamesCacheStorage', () => {
|
||||
describe('GlobalIdentifierNamesCacheStorage', () => {
|
||||
const storageKey: string = 'foo';
|
||||
const storageValue: string = 'bar';
|
||||
|
||||
let storage: IIdentifierNamesCacheStorage;
|
||||
let storage: IGlobalIdentifierNamesCacheStorage;
|
||||
|
||||
describe('initialize', () => {
|
||||
describe('Variant #1: `identifierNamesCache` option values is object', () => {
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
import 'reflect-metadata';
|
||||
|
||||
import { assert } from 'chai';
|
||||
|
||||
import { ServiceIdentifiers } from '../../../../src/container/ServiceIdentifiers';
|
||||
|
||||
import { TDictionary } from '../../../../src/types/TDictionary';
|
||||
|
||||
import { IPropertyIdentifierNamesCacheStorage } from '../../../../src/interfaces/storages/identifier-names-cache/IPropertyIdentifierNamesCacheStorage';
|
||||
import { IInversifyContainerFacade } from '../../../../src/interfaces/container/IInversifyContainerFacade';
|
||||
import { IOptions } from '../../../../src/interfaces/options/IOptions';
|
||||
import { IRandomGenerator } from '../../../../src/interfaces/utils/IRandomGenerator';
|
||||
|
||||
import { DEFAULT_PRESET } from '../../../../src/options/presets/Default';
|
||||
|
||||
import { InversifyContainerFacade } from '../../../../src/container/InversifyContainerFacade';
|
||||
import { PropertyIdentifierNamesCacheStorage } from '../../../../src/storages/identifier-names-cache/PropertyIdentifierNamesCacheStorage';
|
||||
|
||||
/**
|
||||
* @returns {IPropertyIdentifierNamesCacheStorage}
|
||||
*/
|
||||
const getStorageInstance = <V>(options: Partial<IOptions> = DEFAULT_PRESET): IPropertyIdentifierNamesCacheStorage => {
|
||||
const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
|
||||
inversifyContainerFacade.load('', '', {});
|
||||
|
||||
const storage: IPropertyIdentifierNamesCacheStorage = new PropertyIdentifierNamesCacheStorage(
|
||||
inversifyContainerFacade.get<IRandomGenerator>(ServiceIdentifiers.IRandomGenerator),
|
||||
{
|
||||
...DEFAULT_PRESET,
|
||||
...options as IOptions
|
||||
}
|
||||
);
|
||||
|
||||
storage.initialize();
|
||||
|
||||
return storage;
|
||||
};
|
||||
|
||||
describe('PropertyIdentifierNamesCacheStorage', () => {
|
||||
const storageKey: string = 'foo';
|
||||
const storageValue: string = 'bar';
|
||||
|
||||
let storage: IPropertyIdentifierNamesCacheStorage;
|
||||
|
||||
describe('initialize', () => {
|
||||
describe('Variant #1: `identifierNamesCache` option values is object', () => {
|
||||
const expectedDictionary: TDictionary<string> = {
|
||||
[storageKey]: storageValue
|
||||
};
|
||||
|
||||
let dictionary: TDictionary<string>;
|
||||
|
||||
before(() => {
|
||||
storage = getStorageInstance({
|
||||
identifierNamesCache: {
|
||||
globalIdentifiers: {},
|
||||
propertyIdentifiers: {
|
||||
[storageKey]: storageValue
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
dictionary = storage.getStorageAsDictionary();
|
||||
});
|
||||
|
||||
it('should initialize storage with `identifierNamesStorage` option object', () => {
|
||||
assert.deepEqual(dictionary, expectedDictionary);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #2: `identifierNamesCache` option values is empty object', () => {
|
||||
const expectedDictionary: TDictionary<string> = {};
|
||||
|
||||
let dictionary: TDictionary<string>;
|
||||
|
||||
before(() => {
|
||||
storage = getStorageInstance({
|
||||
identifierNamesCache: {
|
||||
globalIdentifiers: {},
|
||||
propertyIdentifiers: {}
|
||||
}
|
||||
});
|
||||
|
||||
dictionary = storage.getStorageAsDictionary();
|
||||
});
|
||||
|
||||
it('should initialize storage with `identifierNamesStorage` option object', () => {
|
||||
assert.deepEqual(dictionary, expectedDictionary);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #3: `identifierNamesCache` option values is `null`', () => {
|
||||
const expectedDictionary: TDictionary<string> = {};
|
||||
|
||||
let dictionary: TDictionary<string>;
|
||||
|
||||
before(() => {
|
||||
storage = getStorageInstance({
|
||||
identifierNamesCache: null
|
||||
});
|
||||
|
||||
dictionary = storage.getStorageAsDictionary();
|
||||
});
|
||||
|
||||
it('should initialize storage with `identifierNamesStorage` option object', () => {
|
||||
assert.deepEqual(dictionary, expectedDictionary);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user