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
16// This records the collections related to property obfuscation.
17export namespace PropCollections {
18  // whether enable property obfuscation
19  export let enablePropertyObfuscation: boolean = false;
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  // saved generated property name
28  export let newlyOccupiedMangledProps: Set<string> = new Set();
29  export let mangledPropsInNameCache: Set<string> = new Set();
30
31  // When the module is compiled, call this function to clear the collections associated with property obfuscation.
32  export function clearPropsCollections(): void {
33    globalMangledTable.clear();
34    historyMangledTable?.clear();
35    reservedProperties.clear();
36    universalReservedProperties = [];
37    newlyOccupiedMangledProps.clear();
38    mangledPropsInNameCache.clear();
39  }
40}
41
42// This records the collections related to whitelists
43export namespace UnobfuscationCollections {
44  // printKeptName: by user configuration, it decides whether to print unobfuscation names and whitelists.
45  export let printKeptName: boolean = false;
46  // whitelist
47  export let reservedSdkApiForProp: Set<string> = new Set();
48  export let reservedSdkApiForGlobal: Set<string> = new Set();
49  export let reservedSdkApiForLocal: Set<string> = new Set();
50  export let reservedStruct: Set<string> = new Set();
51  export let reservedLangForProperty: Set<string> = new Set();
52  // declare global {}
53  // In the above syntax, 'global' is named '__global' in tsc.
54  export let reservedLangForTopLevel: Set<string> = new Set(['__global', 'default']); // Will not add new elements anymore
55  export let reservedExportName: Set<string> = new Set();
56  export let reservedExportNameAndProp: Set<string> = new Set();
57  export let reservedStrProp: Set<string> = new Set();
58  export let reservedEnum: Set<string> = new Set();
59  
60  // The mapping between the unobfuscated names and their reasons.
61  export let unobfuscatedPropMap: Map<string, Set<string>> = new Map();
62  export let unobfuscatedNamesMap: Map<string, Set<string>> = new Map();
63
64  // The mapping between wildcards and regular expressions
65  export let reservedWildcardMap: Map<RegExp, string> = new Map();
66
67  export function clear(): void {
68    printKeptName = false;
69    reservedSdkApiForProp.clear();
70    reservedSdkApiForGlobal.clear();
71    reservedSdkApiForLocal.clear();
72    reservedStruct.clear();
73    reservedLangForProperty.clear();
74    reservedExportName.clear();
75    reservedExportNameAndProp.clear();
76    reservedStrProp.clear();
77    reservedEnum.clear();
78    unobfuscatedPropMap.clear();
79    unobfuscatedNamesMap.clear();
80  }
81}
82
83export namespace LocalVariableCollections {
84  export let reservedStruct: Set<string> = new Set(); 
85  export let reservedConfig: Set<string> = new Set(); // Obtain the name from the user-configured .d.ts file
86  export let reservedLangForLocal: Set<string> = new Set(['this', '__global']); // Will not add new elements anymore
87
88  export function clear(): void {
89    reservedStruct.clear();
90    reservedConfig.clear();
91  }
92}