13af6ab5fSopenharmony_ci/*
23af6ab5fSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License.
53af6ab5fSopenharmony_ci * You may obtain a copy of the License at
63af6ab5fSopenharmony_ci *
73af6ab5fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
83af6ab5fSopenharmony_ci *
93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and
133af6ab5fSopenharmony_ci * limitations under the License.
143af6ab5fSopenharmony_ci */
153af6ab5fSopenharmony_ci
163af6ab5fSopenharmony_ci// This records the collections related to property obfuscation.
173af6ab5fSopenharmony_ciexport namespace PropCollections {
183af6ab5fSopenharmony_ci  // whether enable property obfuscation
193af6ab5fSopenharmony_ci  export let enablePropertyObfuscation: boolean = false;
203af6ab5fSopenharmony_ci  // global mangled properties table used by all files in a project
213af6ab5fSopenharmony_ci  export let globalMangledTable: Map<string, string> = new Map();
223af6ab5fSopenharmony_ci  // used for property cache
233af6ab5fSopenharmony_ci  export let historyMangledTable: Map<string, string> = undefined;
243af6ab5fSopenharmony_ci  // the white list of property
253af6ab5fSopenharmony_ci  export let reservedProperties: Set<string> = new Set();
263af6ab5fSopenharmony_ci  export let universalReservedProperties: RegExp[] = [];
273af6ab5fSopenharmony_ci  // saved generated property name
283af6ab5fSopenharmony_ci  export let newlyOccupiedMangledProps: Set<string> = new Set();
293af6ab5fSopenharmony_ci  export let mangledPropsInNameCache: Set<string> = new Set();
303af6ab5fSopenharmony_ci
313af6ab5fSopenharmony_ci  // When the module is compiled, call this function to clear the collections associated with property obfuscation.
323af6ab5fSopenharmony_ci  export function clearPropsCollections(): void {
333af6ab5fSopenharmony_ci    globalMangledTable.clear();
343af6ab5fSopenharmony_ci    historyMangledTable?.clear();
353af6ab5fSopenharmony_ci    reservedProperties.clear();
363af6ab5fSopenharmony_ci    universalReservedProperties = [];
373af6ab5fSopenharmony_ci    newlyOccupiedMangledProps.clear();
383af6ab5fSopenharmony_ci    mangledPropsInNameCache.clear();
393af6ab5fSopenharmony_ci  }
403af6ab5fSopenharmony_ci}
413af6ab5fSopenharmony_ci
423af6ab5fSopenharmony_ci// This records the collections related to whitelists
433af6ab5fSopenharmony_ciexport namespace UnobfuscationCollections {
443af6ab5fSopenharmony_ci  // printKeptName: by user configuration, it decides whether to print unobfuscation names and whitelists.
453af6ab5fSopenharmony_ci  export let printKeptName: boolean = false;
463af6ab5fSopenharmony_ci  // whitelist
473af6ab5fSopenharmony_ci  export let reservedSdkApiForProp: Set<string> = new Set();
483af6ab5fSopenharmony_ci  export let reservedSdkApiForGlobal: Set<string> = new Set();
493af6ab5fSopenharmony_ci  export let reservedSdkApiForLocal: Set<string> = new Set();
503af6ab5fSopenharmony_ci  export let reservedStruct: Set<string> = new Set();
513af6ab5fSopenharmony_ci  export let reservedLangForProperty: Set<string> = new Set();
523af6ab5fSopenharmony_ci  // declare global {}
533af6ab5fSopenharmony_ci  // In the above syntax, 'global' is named '__global' in tsc.
543af6ab5fSopenharmony_ci  export let reservedLangForTopLevel: Set<string> = new Set(['__global', 'default']); // Will not add new elements anymore
553af6ab5fSopenharmony_ci  export let reservedExportName: Set<string> = new Set();
563af6ab5fSopenharmony_ci  export let reservedExportNameAndProp: Set<string> = new Set();
573af6ab5fSopenharmony_ci  export let reservedStrProp: Set<string> = new Set();
583af6ab5fSopenharmony_ci  export let reservedEnum: Set<string> = new Set();
593af6ab5fSopenharmony_ci  
603af6ab5fSopenharmony_ci  // The mapping between the unobfuscated names and their reasons.
613af6ab5fSopenharmony_ci  export let unobfuscatedPropMap: Map<string, Set<string>> = new Map();
623af6ab5fSopenharmony_ci  export let unobfuscatedNamesMap: Map<string, Set<string>> = new Map();
633af6ab5fSopenharmony_ci
643af6ab5fSopenharmony_ci  // The mapping between wildcards and regular expressions
653af6ab5fSopenharmony_ci  export let reservedWildcardMap: Map<RegExp, string> = new Map();
663af6ab5fSopenharmony_ci
673af6ab5fSopenharmony_ci  export function clear(): void {
683af6ab5fSopenharmony_ci    printKeptName = false;
693af6ab5fSopenharmony_ci    reservedSdkApiForProp.clear();
703af6ab5fSopenharmony_ci    reservedSdkApiForGlobal.clear();
713af6ab5fSopenharmony_ci    reservedSdkApiForLocal.clear();
723af6ab5fSopenharmony_ci    reservedStruct.clear();
733af6ab5fSopenharmony_ci    reservedLangForProperty.clear();
743af6ab5fSopenharmony_ci    reservedExportName.clear();
753af6ab5fSopenharmony_ci    reservedExportNameAndProp.clear();
763af6ab5fSopenharmony_ci    reservedStrProp.clear();
773af6ab5fSopenharmony_ci    reservedEnum.clear();
783af6ab5fSopenharmony_ci    unobfuscatedPropMap.clear();
793af6ab5fSopenharmony_ci    unobfuscatedNamesMap.clear();
803af6ab5fSopenharmony_ci  }
813af6ab5fSopenharmony_ci}
823af6ab5fSopenharmony_ci
833af6ab5fSopenharmony_ciexport namespace LocalVariableCollections {
843af6ab5fSopenharmony_ci  export let reservedStruct: Set<string> = new Set(); 
853af6ab5fSopenharmony_ci  export let reservedConfig: Set<string> = new Set(); // Obtain the name from the user-configured .d.ts file
863af6ab5fSopenharmony_ci  export let reservedLangForLocal: Set<string> = new Set(['this', '__global']); // Will not add new elements anymore
873af6ab5fSopenharmony_ci
883af6ab5fSopenharmony_ci  export function clear(): void {
893af6ab5fSopenharmony_ci    reservedStruct.clear();
903af6ab5fSopenharmony_ci    reservedConfig.clear();
913af6ab5fSopenharmony_ci  }
923af6ab5fSopenharmony_ci}