1/* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { KeepInfo } from './ProjectCollections'; 17 18// This records the collections related to property obfuscation. 19export namespace PropCollections { 20 // global mangled properties table used by all files in a project 21 export let globalMangledTable: Map<string, string> = new Map(); 22 // used for property cache 23 export let historyMangledTable: Map<string, string> = undefined; 24 // the white list of property 25 export let reservedProperties: Set<string> = new Set(); 26 export let universalReservedProperties: RegExp[] = []; 27 // In incremental compilation, save the globally obfuscated names generated during the last compilation. 28 // When generating new variable or property obfuscated names, 29 // it is necessary to prevent duplication with the names in this set. 30 export let globalMangledNamesInCache: Set<string> = new Set(); 31 32 // When the module is compiled, call this function to clear the collections associated with property obfuscation. 33 export function clearPropsCollections(): void { 34 globalMangledTable.clear(); 35 historyMangledTable?.clear(); 36 reservedProperties.clear(); 37 universalReservedProperties = []; 38 globalMangledNamesInCache.clear(); 39 } 40} 41 42// This records the collections related to whitelists 43export namespace UnobfuscationCollections { 44 // whitelist 45 export let reservedSdkApiForProp: Set<string> = new Set(); 46 export let reservedSdkApiForGlobal: Set<string> = new Set(); 47 export let reservedSdkApiForLocal: Set<string> = new Set(); 48 export let reservedStruct: Set<string> = new Set(); 49 export let reservedLangForProperty: Set<string> = new Set(); 50 // declare global {} 51 // In the above syntax, 'global' is named '__global' in tsc. 52 export let reservedLangForTopLevel: Set<string> = new Set(['__global', 'default']); // Will not add new elements anymore 53 export let reservedExportName: Set<string> = new Set(); 54 export let reservedExportNameAndProp: Set<string> = new Set(); 55 export let reservedStrProp: Set<string> = new Set(); 56 export let reservedEnum: Set<string> = new Set(); 57 58 // The mapping between the unobfuscated names and their reasons. 59 export let unobfuscatedPropMap: Map<string, Set<string>> = new Map(); 60 export let unobfuscatedNamesMap: Map<string, Set<string>> = new Map(); 61 62 // The mapping between wildcards and regular expressions 63 export let reservedWildcardMap: Map<RegExp, string> = new Map(); 64 65 export function clear(): void { 66 reservedSdkApiForProp.clear(); 67 reservedSdkApiForGlobal.clear(); 68 reservedSdkApiForLocal.clear(); 69 reservedStruct.clear(); 70 reservedLangForProperty.clear(); 71 reservedExportName.clear(); 72 reservedExportNameAndProp.clear(); 73 reservedStrProp.clear(); 74 reservedEnum.clear(); 75 unobfuscatedPropMap.clear(); 76 unobfuscatedNamesMap.clear(); 77 reservedWildcardMap.clear(); 78 } 79} 80 81export namespace LocalVariableCollections { 82 export let reservedConfig: Set<string> = new Set(); // Obtain the name from the user-configured .d.ts file 83 export let reservedLangForLocal: Set<string> = new Set(['this', '__global']); // Will not add new elements anymore 84 85 export function clear(): void { 86 reservedConfig.clear(); 87 } 88} 89 90export namespace AtKeepCollections { 91 export let keepSymbol: KeepInfo = { 92 propertyNames: new Set<string>(), 93 globalNames: new Set<string>() 94 }; 95 96 export let keepAsConsumer: KeepInfo = { 97 propertyNames: new Set<string>(), 98 globalNames: new Set<string>() 99 }; 100 101 export function clear(): void { 102 keepSymbol.globalNames.clear(); 103 keepSymbol.propertyNames.clear(); 104 keepAsConsumer.globalNames.clear(); 105 keepAsConsumer.propertyNames.clear(); 106 }; 107} 108 109export namespace AtIntentCollections{ 110 export let propertyNames: Set<string> = new Set(); 111 export let globalNames: Set<string> = new Set(); 112 113 export function clear(): void{ 114 propertyNames.clear(); 115 globalNames.clear(); 116 } 117} 118 119export namespace BytecodeObfuscationCollections { 120 export let decoratorProp: Set<string> = new Set(); 121 122 export function clear(): void { 123 decoratorProp.clear(); 124 } 125}