Changed identifier names cache structure to store multiple identifier caches

This commit is contained in:
sanex
2021-05-30 18:43:36 +03:00
parent 51d4ea08be
commit f32bcf29a8
21 changed files with 274 additions and 158 deletions
+3 -3
View File
@@ -18,7 +18,7 @@ import {
ValidatorOptions
} from 'class-validator';
import { TIdentifierNamesCache } from '../types/storages/TIdentifierNamesCache';
import { TIdentifierNamesCache } from '../types/TIdentifierNamesCache';
import { TInputOptions } from '../types/options/TInputOptions';
import { TOptionsPreset } from '../types/options/TOptionsPreset';
import { TRenamePropertiesMode } from '../types/options/TRenamePropertiesMode';
@@ -46,7 +46,7 @@ import { HIGH_OBFUSCATION_PRESET } from './presets/HighObfuscation';
import { ValidationErrorsFormatter } from './ValidationErrorsFormatter';
import { IsAllowedForObfuscationTargets } from './validators/IsAllowedForObfuscationTargets';
import { IsPrimitiveDictionary } from './validators/IsPrimitiveDictionary';
import { IsIdentifierNamesCache } from './validators/IsIdentifierNamesCache';
@injectable()
export class Options implements IOptions {
@@ -147,7 +147,7 @@ export class Options implements IOptions {
/**
* @type {TIdentifierNamesCache}
*/
@IsPrimitiveDictionary('string')
@IsIdentifierNamesCache()
public readonly identifierNamesCache!: TIdentifierNamesCache;
/**
@@ -1,24 +1,53 @@
import equal from 'fast-deep-equal';
import { registerDecorator, ValidationArguments, ValidationOptions } from 'class-validator';
import { TIdentifierNamesCache } from '../../types/TIdentifierNamesCache';
import { TIdentifierNamesCacheDictionary } from '../../types/TIdentifierNamesCacheDictionary';
import { IOptions } from '../../interfaces/options/IOptions';
import { DEFAULT_PRESET } from '../presets/Default';
/**
* @param {"string" | "number"} valuesType
* @param value
* @returns {boolean}
*/
const validateDictionary = (value: unknown | TIdentifierNamesCacheDictionary): boolean => {
if (typeof value !== 'object') {
return false;
}
if (value === null) {
return false;
}
const objectValues: unknown[] = Object.values(value);
if (!objectValues.length) {
return true;
}
for (const objectValue of objectValues) {
if (typeof objectValue !== 'string') {
return false;
}
}
return true;
};
/**
* @param {ValidationOptions} validationOptions
* @returns {(options: IOptions, propertyName: keyof IOptions) => void}
*/
export function IsPrimitiveDictionary (
valuesType: 'string' | 'number',
export function IsIdentifierNamesCache (
validationOptions?: ValidationOptions
): (options: IOptions, propertyName: keyof IOptions) => void {
return (optionsObject: IOptions, propertyName: keyof IOptions): void => {
registerDecorator({
propertyName,
constraints: [valuesType],
name: 'IsPrimitiveDictionary',
constraints: [],
name: 'IsIdentifierNamesCache',
options: validationOptions,
target: optionsObject.constructor,
validator: {
@@ -27,7 +56,7 @@ export function IsPrimitiveDictionary (
* @param {ValidationArguments} validationArguments
* @returns {boolean}
*/
validate (value: IOptions[keyof IOptions], validationArguments: ValidationArguments): boolean {
validate (value: unknown, validationArguments: ValidationArguments): boolean {
const defaultValue: IOptions[keyof IOptions] | undefined = DEFAULT_PRESET[propertyName];
const isDefaultValue: boolean = equal(value, defaultValue);
@@ -39,26 +68,22 @@ export function IsPrimitiveDictionary (
return false;
}
const objectValues: unknown[] = Object.values<unknown>(value);
if (!objectValues.length) {
return true;
if (value === null) {
return false;
}
for (const objectValue of objectValues) {
if (typeof objectValue !== 'string') {
return false;
}
if (!validateDictionary((<TIdentifierNamesCache>value)?.globalIdentifiers)) {
return false;
}
return true;
return validateDictionary((<TIdentifierNamesCache>value)?.propertyIdentifiers);
},
/**
* @returns {string}
*/
defaultMessage (): string {
return `Passed value must be a dictionary with \`${valuesType}\` values or \`null\` value`;
return 'Passed value must be an identifier names cache object or `null` value';
}
}
});