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 { ApiExtractor } from '../../../src/common/ApiExtractor'; 17import { assert, expect } from 'chai'; 18import { initScanProjectConfigByMergeConfig, readProjectPropertiesByCollectedPaths, scanProjectConfig } from '../../../src/common/ApiReader'; 19import { NameGeneratorType } from '../../../src/generator/NameFactory'; 20import { MergedConfig, ProjectCollections } from '../../../src/ArkObfuscator'; 21import { clearProjectWhiteListManager, FileWhiteList, projectWhiteListManager, initProjectWhiteListManager } from '../../../src/utils/ProjectCollections'; 22import { AtKeepCollections } from '../../../src/utils/CommonCollections'; 23import { IOptions } from '../../../src/configs/IOptions'; 24 25function collectApi(apiPath: string, apiType: ApiExtractor.ApiType): void { 26 clearAll(); 27 ApiExtractor.traverseApiFiles(apiPath, apiType); 28} 29 30function clearAll(): void { 31 ApiExtractor.mPropertySet.clear(); 32 ApiExtractor.mSystemExportSet.clear(); 33} 34 35describe('test for ApiExtractor', function () { 36 describe('test for visitExport', function () { 37 it('export {ExportDeclarationClass1, ExportDeclarationClass2}', function () { 38 let exportDeclarationAst: string = 'test/ut/utils/apiTest_visitExport/exportDeclarationAst.d.ts'; 39 collectApi(exportDeclarationAst, ApiExtractor.ApiType.API); 40 assert.strictEqual(ApiExtractor.mSystemExportSet.has('ExportDeclarationClass1'), true); 41 assert.strictEqual(ApiExtractor.mSystemExportSet.has('ExportDeclarationClass2'), true); 42 clearAll(); 43 }); 44 45 it('export {ExportDeclarationClass1 as class1, ExportDeclarationClass2} from `./exportDeclarationFrom`', 46 function () { 47 let exportDeclarationFromAst: string = 'test/ut/utils/apiTest_visitExport/exportDeclarationFrom.d.ts'; 48 collectApi(exportDeclarationFromAst, ApiExtractor.ApiType.API); 49 assert.strictEqual(ApiExtractor.mSystemExportSet.has('ExportDeclarationClass1'), false); 50 assert.strictEqual(ApiExtractor.mSystemExportSet.has('class1'), true); 51 assert.strictEqual(ApiExtractor.mSystemExportSet.has('ExportDeclarationClass2'), true); 52 clearAll(); 53 }); 54 55 it('export {default as name1, ExportDeclarationClass2, exportName} from `./exportDefaultDeclarationAst`', 56 function () { 57 let exportDeclarationDefault: string = 'test/ut/utils/apiTest_visitExport/exportDeclarationDefault.d.ts'; 58 collectApi(exportDeclarationDefault, ApiExtractor.ApiType.API); 59 assert.strictEqual(ApiExtractor.mSystemExportSet.has('default'), false); 60 assert.strictEqual(ApiExtractor.mSystemExportSet.has('name1'), true); 61 assert.strictEqual(ApiExtractor.mSystemExportSet.has('ExportDeclarationClass2'), true); 62 assert.strictEqual(ApiExtractor.mSystemExportSet.has('exportName'), true); 63 clearAll(); 64 }); 65 66 it('export {ExportDeclarationClass1 as exportName, ExportDeclarationClass2, ExportDeclarationClass3 as default}', 67 function () { 68 let exportDefaultDeclarationAst: string = 'test/ut/utils/apiTest_visitExport/exportDefaultDeclarationAst.d.ts'; 69 collectApi(exportDefaultDeclarationAst, ApiExtractor.ApiType.API); 70 assert.strictEqual(ApiExtractor.mSystemExportSet.has('ExportDeclarationClass1'), false); 71 assert.strictEqual(ApiExtractor.mSystemExportSet.has('exportName'), true); 72 assert.strictEqual(ApiExtractor.mSystemExportSet.has('ExportDeclarationClass2'), true); 73 assert.strictEqual(ApiExtractor.mSystemExportSet.has('ExportDeclarationClass3'), false); 74 assert.strictEqual(ApiExtractor.mSystemExportSet.has('default'), true); 75 clearAll(); 76 }); 77 78 it('export * as name1', function () { 79 let exportAllAst: string = 'test/ut/utils/apiTest_visitExport/exportAll.d.ts'; 80 collectApi(exportAllAst, ApiExtractor.ApiType.API); 81 assert.strictEqual(ApiExtractor.mSystemExportSet.has('name1'), true); 82 clearAll(); 83 }); 84 85 it('export * from `export*.ts`', function () { 86 let exportFile: string = 'test/ut/utils/apiTest_visitExport/export.d.ts'; 87 collectApi(exportFile, ApiExtractor.ApiType.API); 88 assert.strictEqual(ApiExtractor.mSystemExportSet.size === 0, true); 89 assert.strictEqual(ApiExtractor.mPropertySet.size === 0, true); 90 clearAll(); 91 }); 92 93 it('doubleUnderscoreTest', function () { 94 let doubleUnderscoreTestAst: string = 'test/ut/utils/apiTest_visitExport/doubleUnderscoreTest.d.ts'; 95 collectApi(doubleUnderscoreTestAst, ApiExtractor.ApiType.API); 96 assert.strictEqual(ApiExtractor.mPropertySet.has('__Admin'), true); 97 assert.strictEqual(ApiExtractor.mPropertySet.has('___Admin'), false); 98 assert.strictEqual(ApiExtractor.mPropertySet.has('__Moderator'), true); 99 assert.strictEqual(ApiExtractor.mPropertySet.has('___Moderator'), false); 100 assert.strictEqual(ApiExtractor.mPropertySet.has('__User'), true); 101 assert.strictEqual(ApiExtractor.mPropertySet.has('___User'), false); 102 assert.strictEqual(ApiExtractor.mPropertySet.has('__name'), true); 103 assert.strictEqual(ApiExtractor.mPropertySet.has('___name'), false); 104 assert.strictEqual(ApiExtractor.mPropertySet.has('__Admin'), true); 105 assert.strictEqual(ApiExtractor.mPropertySet.has('___Admin'), false); 106 assert.strictEqual(ApiExtractor.mPropertySet.has('__age'), true); 107 assert.strictEqual(ApiExtractor.mPropertySet.has('___age'), false); 108 assert.strictEqual(ApiExtractor.mPropertySet.has('__greet'), true); 109 assert.strictEqual(ApiExtractor.mPropertySet.has('___greet'), false); 110 assert.strictEqual(ApiExtractor.mPropertySet.has('__typeProp1'), true); 111 assert.strictEqual(ApiExtractor.mPropertySet.has('___typeProp1'), false); 112 assert.strictEqual(ApiExtractor.mPropertySet.has('__typeProp2'), true); 113 assert.strictEqual(ApiExtractor.mPropertySet.has('___typeProp2'), false); 114 assert.strictEqual(ApiExtractor.mPropertySet.has('__speak'), true); 115 assert.strictEqual(ApiExtractor.mPropertySet.has('___speak'), false); 116 assert.strictEqual(ApiExtractor.mPropertySet.has('__appName'), true); 117 assert.strictEqual(ApiExtractor.mPropertySet.has('___appName'), false); 118 assert.strictEqual(ApiExtractor.mPropertySet.has('__version'), true); 119 assert.strictEqual(ApiExtractor.mPropertySet.has('___version'), false); 120 assert.strictEqual(ApiExtractor.mPropertySet.has('__logDetails'), true); 121 assert.strictEqual(ApiExtractor.mPropertySet.has('___logDetails'), false); 122 clearAll(); 123 }); 124 }); 125 126 describe('test for visitPropertyAndName', function () { 127 it('Class Test', function () { 128 let filePath: string = 'test/ut/utils/apiTest_visitPropertyAndName/classTest.d.ts'; 129 let config: MergedConfig = new MergedConfig(); 130 config.options.stripSystemApiArgs = false; 131 initScanProjectConfigByMergeConfig(config); 132 133 collectApi(filePath, ApiExtractor.ApiType.API); 134 assert.strictEqual(ApiExtractor.mSystemExportSet.has('TestClass1'), true); 135 assert.strictEqual(ApiExtractor.mSystemExportSet.has('TestClass2'), false); 136 assert.strictEqual(ApiExtractor.mSystemExportSet.has('AbstractClass'), false); 137 assert.strictEqual(ApiExtractor.mPropertySet.has('prop1'), true); 138 assert.strictEqual(ApiExtractor.mPropertySet.has('prop2'), true); 139 assert.strictEqual(ApiExtractor.mPropertySet.has('abstractProp'), true); 140 assert.strictEqual(ApiExtractor.mPropertySet.has('foo1'), true); 141 assert.strictEqual(ApiExtractor.mPropertySet.has('foo2'), true); 142 assert.strictEqual(ApiExtractor.mPropertySet.has('param1'), true); 143 assert.strictEqual(ApiExtractor.mPropertySet.has('param2'), false); 144 clearAll(); 145 }); 146 147 it('Interface Test', function () { 148 let filePath: string = 'test/ut/utils/apiTest_visitPropertyAndName/interfaceTest.d.ts'; 149 let config: MergedConfig = new MergedConfig(); 150 config.options.stripSystemApiArgs = false; 151 initScanProjectConfigByMergeConfig(config); 152 153 collectApi(filePath, ApiExtractor.ApiType.API); 154 assert.strictEqual(ApiExtractor.mSystemExportSet.has('TestInterface1'), true); 155 assert.strictEqual(ApiExtractor.mSystemExportSet.has('TestInterface2'), false); 156 assert.strictEqual(ApiExtractor.mPropertySet.has('prop1'), true); 157 assert.strictEqual(ApiExtractor.mPropertySet.has('prop2'), true); 158 assert.strictEqual(ApiExtractor.mPropertySet.has('foo1'), true); 159 assert.strictEqual(ApiExtractor.mPropertySet.has('foo2'), true); 160 assert.strictEqual(ApiExtractor.mPropertySet.has('param1'), true); 161 assert.strictEqual(ApiExtractor.mPropertySet.has('param2'), false); 162 clearAll(); 163 }); 164 165 it('TypeLiteral Test', function () { 166 let filePath: string = 'test/ut/utils/apiTest_visitPropertyAndName/typeLiteralTest.d.ts'; 167 let config: MergedConfig = new MergedConfig(); 168 config.options.stripSystemApiArgs = false; 169 initScanProjectConfigByMergeConfig(config); 170 171 collectApi(filePath, ApiExtractor.ApiType.API); 172 assert.strictEqual(ApiExtractor.mSystemExportSet.has('TestType1'), true); 173 assert.strictEqual(ApiExtractor.mSystemExportSet.has('TestType2'), false); 174 assert.strictEqual(ApiExtractor.mPropertySet.has('collectProp1'), true); 175 assert.strictEqual(ApiExtractor.mPropertySet.has('collectProp2'), true); 176 assert.strictEqual(ApiExtractor.mPropertySet.has('testFunc1'), true); 177 assert.strictEqual(ApiExtractor.mPropertySet.has('testFunc2'), true); 178 assert.strictEqual(ApiExtractor.mPropertySet.has('message1'), true); 179 assert.strictEqual(ApiExtractor.mPropertySet.has('message2'), false); 180 clearAll(); 181 }); 182 183 it('Enum Test', function () { 184 let filePath: string = 'test/ut/utils/apiTest_visitPropertyAndName/enumTest.d.ts'; 185 let config: MergedConfig = new MergedConfig(); 186 config.options.stripSystemApiArgs = false; 187 initScanProjectConfigByMergeConfig(config); 188 189 collectApi(filePath, ApiExtractor.ApiType.API); 190 assert.strictEqual(ApiExtractor.mSystemExportSet.has('TestEnum1'), true); 191 assert.strictEqual(ApiExtractor.mSystemExportSet.has('TestEnum2'), false); 192 assert.strictEqual(ApiExtractor.mPropertySet.has('PARAM1'), true); 193 assert.strictEqual(ApiExtractor.mPropertySet.has('PARAM2'), true); 194 clearAll(); 195 }); 196 197 it('ObjectLiteral Test', function () { 198 let filePath: string = 'test/ut/utils/apiTest_visitPropertyAndName/objectLiteral.d.ts'; 199 let config: MergedConfig = new MergedConfig(); 200 config.options.stripSystemApiArgs = false; 201 initScanProjectConfigByMergeConfig(config); 202 203 collectApi(filePath, ApiExtractor.ApiType.API); 204 assert.strictEqual(ApiExtractor.mSystemExportSet.has('obj1'), true); 205 assert.strictEqual(ApiExtractor.mSystemExportSet.has('obj2'), false); 206 assert.strictEqual(ApiExtractor.mPropertySet.has('prop1'), true); 207 assert.strictEqual(ApiExtractor.mPropertySet.has('prop2'), true); 208 assert.strictEqual(ApiExtractor.mPropertySet.has('prop3'), true); 209 assert.strictEqual(ApiExtractor.mPropertySet.has('prop4'), true); 210 assert.strictEqual(ApiExtractor.mPropertySet.has('innerProp1'), true); 211 assert.strictEqual(ApiExtractor.mPropertySet.has('innerProp2'), true); 212 assert.strictEqual(ApiExtractor.mPropertySet.has('innerProp3'), true); 213 assert.strictEqual(ApiExtractor.mPropertySet.has('innerProp4'), true); 214 clearAll(); 215 }); 216 217 it('Module Test', function () { 218 let filePath: string = 'test/ut/utils/apiTest_visitPropertyAndName/moduleTest.d.ts'; 219 let config: MergedConfig = new MergedConfig(); 220 config.options.stripSystemApiArgs = false; 221 initScanProjectConfigByMergeConfig(config); 222 223 collectApi(filePath, ApiExtractor.ApiType.API); 224 assert.strictEqual(ApiExtractor.mSystemExportSet.has('ns1'), true); 225 assert.strictEqual(ApiExtractor.mSystemExportSet.has('ns2'), false); 226 assert.strictEqual(ApiExtractor.mPropertySet.has('TestClass1'), true); 227 assert.strictEqual(ApiExtractor.mPropertySet.has('TestInterface1'), true); 228 assert.strictEqual(ApiExtractor.mPropertySet.has('prop1'), true); 229 assert.strictEqual(ApiExtractor.mPropertySet.has('prop2'), true); 230 assert.strictEqual(ApiExtractor.mPropertySet.has('prop3'), true); 231 assert.strictEqual(ApiExtractor.mPropertySet.has('TestClass2'), false); 232 assert.strictEqual(ApiExtractor.mPropertySet.has('TestInterface2'), false); 233 assert.strictEqual(ApiExtractor.mPropertySet.has('prop4'), true); 234 assert.strictEqual(ApiExtractor.mPropertySet.has('prop5'), true); 235 clearAll(); 236 }); 237 238 it('When "-extra-options strip-system-api-args" option is enabled, no function parameters are collected', function () { 239 let filePath: string = 'test/ut/utils/apiTest_visitPropertyAndName/systemApiArgsTest.d.ts'; 240 let config: MergedConfig = new MergedConfig(); 241 config.options.stripSystemApiArgs = true; 242 initScanProjectConfigByMergeConfig(config); 243 244 collectApi(filePath, ApiExtractor.ApiType.API); 245 assert.strictEqual(ApiExtractor.mSystemExportSet.has('TestClass1'), true); 246 assert.strictEqual(ApiExtractor.mPropertySet.has('prop1'), true); 247 assert.strictEqual(ApiExtractor.mPropertySet.has('param1'), true); 248 assert.strictEqual(ApiExtractor.mPropertySet.has('param2'), false); 249 assert.strictEqual(ApiExtractor.mPropertySet.has('foo1'), true); 250 assert.strictEqual(ApiExtractor.mPropertySet.has('param5'), false); 251 assert.strictEqual(ApiExtractor.mSystemExportSet.has('TestClass2'), false); 252 assert.strictEqual(ApiExtractor.mPropertySet.has('prop2'), true); 253 assert.strictEqual(ApiExtractor.mPropertySet.has('param3'), true); 254 assert.strictEqual(ApiExtractor.mPropertySet.has('param4'), false); 255 assert.strictEqual(ApiExtractor.mPropertySet.has('foo2'), true); 256 assert.strictEqual(ApiExtractor.mPropertySet.has('param6'), false); 257 clearAll(); 258 }); 259 260 it('When "-extra-options strip-system-api-args" option is not enabled, function parameters are collected', function () { 261 let filePath: string = 'test/ut/utils/apiTest_visitPropertyAndName/systemApiArgsTest.d.ts'; 262 let config: MergedConfig = new MergedConfig(); 263 config.options.stripSystemApiArgs = false; 264 initScanProjectConfigByMergeConfig(config); 265 266 collectApi(filePath, ApiExtractor.ApiType.API); 267 assert.strictEqual(ApiExtractor.mSystemExportSet.has('TestClass1'), true); 268 assert.strictEqual(ApiExtractor.mPropertySet.has('prop1'), true); 269 assert.strictEqual(ApiExtractor.mPropertySet.has('param1'), true); 270 assert.strictEqual(ApiExtractor.mPropertySet.has('param2'), true); 271 assert.strictEqual(ApiExtractor.mPropertySet.has('foo1'), true); 272 assert.strictEqual(ApiExtractor.mPropertySet.has('param5'), true); 273 assert.strictEqual(ApiExtractor.mSystemExportSet.has('TestClass2'), false); 274 assert.strictEqual(ApiExtractor.mPropertySet.has('prop2'), true); 275 assert.strictEqual(ApiExtractor.mPropertySet.has('param3'), true); 276 assert.strictEqual(ApiExtractor.mPropertySet.has('param4'), false); 277 assert.strictEqual(ApiExtractor.mPropertySet.has('foo2'), true); 278 assert.strictEqual(ApiExtractor.mPropertySet.has('param6'), false); 279 clearAll(); 280 }); 281 }); 282 283 describe('test for visitProjectExport', function () { 284 const fileList: Set<string> = new Set([ 285 "test/ut/utils/module_exports_test/exportFile1.js", 286 "test/ut/utils/oh_modules/exportFile.js" 287 ]); 288 it('test for module.exports(property)', function () { 289 let projectAndLibs = readProjectPropertiesByCollectedPaths(fileList, 290 { 291 mNameObfuscation: { 292 mEnable: true, 293 mReservedProperties: [], 294 mRenameProperties: true, 295 mKeepStringProperty: false, 296 mNameGeneratorType: NameGeneratorType.ORDERED, 297 mReservedNames: [], 298 mReservedToplevelNames: [] 299 }, 300 mExportObfuscation: false, 301 mKeepFileSourceCode: { 302 mKeepSourceOfPaths: new Set(), 303 mkeepFilesAndDependencies: new Set(), 304 } 305 }, true); 306 let reservedProperties = projectAndLibs.exportNameAndPropSet == undefined? new Set<string> : projectAndLibs.exportNameAndPropSet; 307 let reservedExportNames = projectAndLibs.exportNameSet == undefined? new Set<string> : projectAndLibs.exportNameSet; 308 assert.strictEqual(reservedProperties.has('projectPropertyAssignment1'), true); 309 assert.strictEqual(reservedProperties.has('projectPropertyAssignment2'), true); 310 assert.strictEqual(reservedProperties.has('projectPropertyAssignment3'), true); 311 assert.strictEqual(reservedProperties.has('projectPropertyAssignment4'), true); 312 assert.strictEqual(reservedProperties.has('projectIndirectObj'), false); 313 assert.strictEqual(reservedProperties.has('indirectProp1'), true); 314 assert.strictEqual(reservedProperties.has('projectShorthand'), true); 315 assert.strictEqual(reservedProperties.has('projectShorthandProp'), true); 316 assert.strictEqual(reservedProperties.has('projectMethod1'), true); 317 assert.strictEqual(reservedProperties.has('projectMethod2'), true); 318 assert.strictEqual(reservedProperties.has('projectMethod3'), true); 319 assert.strictEqual(reservedProperties.has('projectGetProp1'), true); 320 assert.strictEqual(reservedProperties.has('projectGetProp2'), true); 321 assert.strictEqual(reservedProperties.has('projectGetProp3'), true); 322 assert.strictEqual(reservedProperties.has('projectSetProp1'), true); 323 assert.strictEqual(reservedProperties.has('projectSetProp2'), true); 324 assert.strictEqual(reservedProperties.has('projectSetProp3'), true); 325 assert.strictEqual(reservedProperties.has('projectExportElement1'), true); 326 assert.strictEqual(reservedProperties.has('projectExportElement2'), true); 327 assert.strictEqual(reservedProperties.has('indirectClass1'), false); 328 assert.strictEqual(reservedProperties.has('indirectProp2'), true); 329 assert.strictEqual(reservedProperties.has('projectExportElement3'), true); 330 assert.strictEqual(reservedProperties.has('indirectClass2'), false); 331 assert.strictEqual(reservedProperties.has('indirectProp3'), true); 332 assert.strictEqual(reservedProperties.has('projectExportElement4'), true); 333 assert.strictEqual(reservedProperties.has('indirectProp4'), true); 334 assert.strictEqual(reservedProperties.has('projectExportElement5'), true); 335 assert.strictEqual(reservedProperties.has('projectExportElement6'), true); 336 assert.strictEqual(reservedProperties.has('projectExportElement7'), true); 337 assert.strictEqual(reservedProperties.has('indirectClass3'), false); 338 assert.strictEqual(reservedProperties.has('indirectProp5'), true); 339 assert.strictEqual(reservedProperties.has('projectExportElement8'), true); 340 assert.strictEqual(reservedProperties.has('indirectClass4'), false); 341 assert.strictEqual(reservedProperties.has('indirectProp6'), true); 342 assert.strictEqual(reservedProperties.has('projectExportElement9'), true); 343 assert.strictEqual(reservedProperties.has('indirectProp7'), true); 344 assert.strictEqual(reservedProperties.has('ohPropertyAssignment1'), true); 345 assert.strictEqual(reservedProperties.has('ohPropertyAssignment2'), true); 346 assert.strictEqual(reservedProperties.has('ohPropertyAssignment3'), true); 347 assert.strictEqual(reservedProperties.has('ohPropertyAssignment4'), true); 348 assert.strictEqual(reservedProperties.has('ohIndirectObj'), false); 349 assert.strictEqual(reservedProperties.has('ohIndirectProp1'), true); 350 assert.strictEqual(reservedProperties.has('ohShorthand'), true); 351 assert.strictEqual(reservedProperties.has('ohShorthandProp'), true); 352 assert.strictEqual(reservedProperties.has('ohMethod1'), true); 353 assert.strictEqual(reservedProperties.has('ohMethod2'), true); 354 assert.strictEqual(reservedProperties.has('ohMethod3'), true); 355 assert.strictEqual(reservedProperties.has('ohGetProp1'), true); 356 assert.strictEqual(reservedProperties.has('ohGetProp2'), true); 357 assert.strictEqual(reservedProperties.has('ohGetProp3'), true); 358 assert.strictEqual(reservedProperties.has('ohSetProp1'), true); 359 assert.strictEqual(reservedProperties.has('ohSetProp2'), true); 360 assert.strictEqual(reservedProperties.has('ohSetProp3'), true); 361 assert.strictEqual(reservedProperties.has('ohExportElement1'), true); 362 assert.strictEqual(reservedProperties.has('ohExportElement2'), true); 363 assert.strictEqual(reservedProperties.has('ohIndirectClass1'), false); 364 assert.strictEqual(reservedProperties.has('ohIndirectProp2'), true); 365 assert.strictEqual(reservedProperties.has('ohExportElement3'), true); 366 assert.strictEqual(reservedProperties.has('ohIndirectClass2'), false); 367 assert.strictEqual(reservedProperties.has('ohIndirectProp3'), true); 368 assert.strictEqual(reservedProperties.has('ohExportElement4'), true); 369 assert.strictEqual(reservedProperties.has('ohIndirectProp4'), true); 370 assert.strictEqual(reservedProperties.has('ohExportElement5'), true); 371 assert.strictEqual(reservedProperties.has('ohExportElement6'), true); 372 assert.strictEqual(reservedProperties.has('ohExportElement7'), true); 373 assert.strictEqual(reservedProperties.has('ohIndirectClass3'), false); 374 assert.strictEqual(reservedProperties.has('ohIndirectProp5'), true); 375 assert.strictEqual(reservedProperties.has('ohExportElement8'), true); 376 assert.strictEqual(reservedProperties.has('ohIndirectClass4'), false); 377 assert.strictEqual(reservedProperties.has('ohIndirectProp6'), true); 378 assert.strictEqual(reservedExportNames.size === 0, true); 379 }); 380 381 it('test for module.exports(export)', function () { 382 let projectAndLibs = readProjectPropertiesByCollectedPaths(fileList, 383 { 384 mNameObfuscation: { 385 mEnable: true, 386 mReservedProperties: [], 387 mRenameProperties: false, 388 mKeepStringProperty: false, 389 mNameGeneratorType: NameGeneratorType.ORDERED, 390 mReservedNames: [], 391 mReservedToplevelNames: [] 392 }, 393 mExportObfuscation: true, 394 mKeepFileSourceCode: { 395 mKeepSourceOfPaths: new Set(), 396 mkeepFilesAndDependencies: new Set(["test/ut/utils/module_exports_test/exportFile3.js"]), 397 } 398 }, true); 399 let reservedProperties = projectAndLibs.exportNameAndPropSet == undefined? new Set<string> : projectAndLibs.exportNameAndPropSet; 400 let reservedExportNames = projectAndLibs.exportNameSet == undefined? new Set<string> : projectAndLibs.exportNameSet; 401 assert.strictEqual(reservedExportNames.has('projectPropertyAssignment1'), false); 402 assert.strictEqual(reservedExportNames.has('projectPropertyAssignment2'), false); 403 assert.strictEqual(reservedExportNames.has('projectPropertyAssignment3'), false); 404 assert.strictEqual(reservedExportNames.has('projectPropertyAssignment4'), false); 405 assert.strictEqual(reservedExportNames.has('projectIndirectObj'), false); 406 assert.strictEqual(reservedExportNames.has('indirectProp1'), false); 407 assert.strictEqual(reservedExportNames.has('projectShorthand'), false); 408 assert.strictEqual(reservedExportNames.has('projectShorthandProp'), false); 409 assert.strictEqual(reservedExportNames.has('projectMethod1'), false); 410 assert.strictEqual(reservedExportNames.has('projectMethod2'), false); 411 assert.strictEqual(reservedExportNames.has('projectMethod3'), false); 412 assert.strictEqual(reservedExportNames.has('projectGetProp1'), false); 413 assert.strictEqual(reservedExportNames.has('projectGetProp2'), false); 414 assert.strictEqual(reservedExportNames.has('projectGetProp3'), false); 415 assert.strictEqual(reservedExportNames.has('projectSetProp1'), false); 416 assert.strictEqual(reservedExportNames.has('projectSetProp2'), false); 417 assert.strictEqual(reservedExportNames.has('projectSetProp3'), false); 418 assert.strictEqual(reservedExportNames.has('projectExportElement1'), false); 419 assert.strictEqual(reservedExportNames.has('projectExportElement2'), false); 420 assert.strictEqual(reservedExportNames.has('indirectClass1'), false); 421 assert.strictEqual(reservedExportNames.has('indirectProp2'), false); 422 assert.strictEqual(reservedExportNames.has('projectExportElement3'), false); 423 assert.strictEqual(reservedExportNames.has('indirectClass2'), false); 424 assert.strictEqual(reservedExportNames.has('indirectProp3'), false); 425 assert.strictEqual(reservedExportNames.has('projectExportElement4'), false); 426 assert.strictEqual(reservedExportNames.has('indirectProp4'), false); 427 assert.strictEqual(reservedExportNames.has('projectExportElement5'), false); 428 assert.strictEqual(reservedExportNames.has('projectExportElement6'), false); 429 assert.strictEqual(reservedExportNames.has('projectExportElement7'), false); 430 assert.strictEqual(reservedExportNames.has('indirectClass3'), false); 431 assert.strictEqual(reservedExportNames.has('indirectProp5'), false); 432 assert.strictEqual(reservedExportNames.has('projectExportElement8'), false); 433 assert.strictEqual(reservedExportNames.has('indirectClass4'), false); 434 assert.strictEqual(reservedExportNames.has('indirectProp6'), false); 435 assert.strictEqual(reservedExportNames.has('projectExportElement9'), false); 436 assert.strictEqual(reservedExportNames.has('indirectProp7'), false); 437 assert.strictEqual(reservedExportNames.has('ohPropertyAssignment1'), true); 438 assert.strictEqual(reservedExportNames.has('ohPropertyAssignment2'), true); 439 assert.strictEqual(reservedExportNames.has('ohPropertyAssignment3'), true); 440 assert.strictEqual(reservedExportNames.has('ohPropertyAssignment4'), true); 441 assert.strictEqual(reservedExportNames.has('ohIndirectObj'), false); 442 assert.strictEqual(reservedExportNames.has('ohIndirectProp1'), false); 443 assert.strictEqual(reservedExportNames.has('ohShorthand'), true); 444 assert.strictEqual(reservedExportNames.has('ohShorthandProp'), false); 445 assert.strictEqual(reservedExportNames.has('ohMethod1'), true); 446 assert.strictEqual(reservedExportNames.has('ohMethod2'), true); 447 assert.strictEqual(reservedExportNames.has('ohMethod3'), true); 448 assert.strictEqual(reservedExportNames.has('ohGetProp1'), true); 449 assert.strictEqual(reservedExportNames.has('ohGetProp2'), true); 450 assert.strictEqual(reservedExportNames.has('ohGetProp3'), true); 451 assert.strictEqual(reservedExportNames.has('ohSetProp1'), true); 452 assert.strictEqual(reservedExportNames.has('ohSetProp2'), true); 453 assert.strictEqual(reservedExportNames.has('ohSetProp3'), true); 454 assert.strictEqual(reservedExportNames.has('ohExportElement1'), true); 455 assert.strictEqual(reservedExportNames.has('ohExportElement2'), true); 456 assert.strictEqual(reservedExportNames.has('ohIndirectClass1'), true); 457 assert.strictEqual(reservedExportNames.has('ohIndirectProp2'), false); 458 assert.strictEqual(reservedExportNames.has('ohExportElement3'), true); 459 assert.strictEqual(reservedExportNames.has('ohIndirectClass2'), false); 460 assert.strictEqual(reservedExportNames.has('ohIndirectProp3'), false); 461 assert.strictEqual(reservedExportNames.has('ohExportElement4'), true); 462 assert.strictEqual(reservedExportNames.has('ohIndirectProp4'), false); 463 assert.strictEqual(reservedExportNames.has('ohExportElement5'), true); 464 assert.strictEqual(reservedExportNames.has('ohExportElement6'), true); 465 assert.strictEqual(reservedExportNames.has('ohExportElement7'), true); 466 assert.strictEqual(reservedExportNames.has('ohIndirectClass3'), true); 467 assert.strictEqual(reservedExportNames.has('ohIndirectProp5'), false); 468 assert.strictEqual(reservedExportNames.has('ohExportElement8'), true); 469 assert.strictEqual(reservedExportNames.has('ohIndirectClass4'), false); 470 assert.strictEqual(reservedExportNames.has('ohIndirectProp6'), false); 471 assert.strictEqual(reservedExportNames.has('ohExportElement9'), true); 472 assert.strictEqual(reservedExportNames.has('ohIndirectProp7'), false); 473 assert.strictEqual(reservedProperties.size === 0, true); 474 }); 475 476 it('test for NamespaceExport(export)', function () { 477 const fileList: Set<string> = new Set([ 478 'test/ut/utils/oh_modules/NamespaceExport.d.ts' 479 ]); 480 let projectAndLibs = readProjectPropertiesByCollectedPaths(fileList, 481 { 482 mNameObfuscation: { 483 mEnable: true, 484 mReservedProperties: [], 485 mRenameProperties: false, 486 mKeepStringProperty: false, 487 mNameGeneratorType: NameGeneratorType.ORDERED, 488 mReservedNames: [], 489 mReservedToplevelNames: [] 490 }, 491 mExportObfuscation: true, 492 }, true); 493 let reservedProperties = projectAndLibs.exportNameAndPropSet == undefined? new Set<string> : projectAndLibs.exportNameAndPropSet; 494 let reservedExportNames = projectAndLibs.exportNameSet == undefined? new Set<string> : projectAndLibs.exportNameSet; 495 assert.strictEqual(reservedExportNames.has('Utils'), true); 496 assert.strictEqual(reservedExportNames.size === 1, true); 497 assert.strictEqual(reservedProperties.size === 0, true); 498 }); 499 500 it('test for module.exports(property + export)', function () { 501 let projectAndLibs = readProjectPropertiesByCollectedPaths(fileList, 502 { 503 mNameObfuscation: { 504 mEnable: true, 505 mReservedProperties: [], 506 mRenameProperties: true, 507 mKeepStringProperty: false, 508 mNameGeneratorType: NameGeneratorType.ORDERED, 509 mReservedNames: [], 510 mReservedToplevelNames: [] 511 }, 512 mExportObfuscation: true, 513 mKeepFileSourceCode: { 514 mKeepSourceOfPaths: new Set(), 515 mkeepFilesAndDependencies: new Set(["test/ut/utils/module_exports_test/exportFile3.js"]), 516 } 517 }, true); 518 let reservedProperties = projectAndLibs.exportNameAndPropSet == undefined? new Set<string> : projectAndLibs.exportNameAndPropSet; 519 let reservedExportNames = projectAndLibs.exportNameSet == undefined? new Set<string> : projectAndLibs.exportNameSet; 520 assert.strictEqual(reservedProperties.has('projectPropertyAssignment1'), false); 521 assert.strictEqual(reservedProperties.has('projectPropertyAssignment2'), false); 522 assert.strictEqual(reservedProperties.has('projectPropertyAssignment3'), false); 523 assert.strictEqual(reservedProperties.has('projectPropertyAssignment4'), false); 524 assert.strictEqual(reservedProperties.has('projectIndirectObj'), false); 525 assert.strictEqual(reservedProperties.has('indirectProp1'), false); 526 assert.strictEqual(reservedProperties.has('projectShorthand'), false); 527 assert.strictEqual(reservedProperties.has('projectShorthandProp'), false); 528 assert.strictEqual(reservedProperties.has('projectMethod1'), false); 529 assert.strictEqual(reservedProperties.has('projectMethod2'), false); 530 assert.strictEqual(reservedProperties.has('projectMethod3'), false); 531 assert.strictEqual(reservedProperties.has('projectGetProp1'), false); 532 assert.strictEqual(reservedProperties.has('projectGetProp2'), false); 533 assert.strictEqual(reservedProperties.has('projectGetProp3'), false); 534 assert.strictEqual(reservedProperties.has('projectSetProp1'), false); 535 assert.strictEqual(reservedProperties.has('projectSetProp2'), false); 536 assert.strictEqual(reservedProperties.has('projectSetProp3'), false); 537 assert.strictEqual(reservedProperties.has('projectExportElement1'), false); 538 assert.strictEqual(reservedProperties.has('projectExportElement2'), false); 539 assert.strictEqual(reservedProperties.has('indirectClass1'), false); 540 assert.strictEqual(reservedProperties.has('indirectProp2'), false); 541 assert.strictEqual(reservedProperties.has('projectExportElement3'), false); 542 assert.strictEqual(reservedProperties.has('indirectClass2'), false); 543 assert.strictEqual(reservedProperties.has('indirectProp3'), false); 544 assert.strictEqual(reservedProperties.has('projectExportElement4'), false); 545 assert.strictEqual(reservedProperties.has('indirectProp4'), false); 546 assert.strictEqual(reservedProperties.has('projectExportElement5'), false); 547 assert.strictEqual(reservedProperties.has('projectExportElement6'), false); 548 assert.strictEqual(reservedProperties.has('projectExportElement7'), false); 549 assert.strictEqual(reservedProperties.has('indirectClass3'), false); 550 assert.strictEqual(reservedProperties.has('indirectProp5'), false); 551 assert.strictEqual(reservedProperties.has('projectExportElement8'), false); 552 assert.strictEqual(reservedProperties.has('indirectClass4'), false); 553 assert.strictEqual(reservedProperties.has('indirectProp6'), false); 554 assert.strictEqual(reservedProperties.has('projectExportElement9'), false); 555 assert.strictEqual(reservedProperties.has('indirectProp7'), false); 556 assert.strictEqual(reservedProperties.has('ohPropertyAssignment1'), true); 557 assert.strictEqual(reservedProperties.has('ohPropertyAssignment2'), true); 558 assert.strictEqual(reservedProperties.has('ohPropertyAssignment3'), true); 559 assert.strictEqual(reservedProperties.has('ohPropertyAssignment4'), true); 560 assert.strictEqual(reservedProperties.has('ohIndirectObj'), false); 561 assert.strictEqual(reservedProperties.has('ohIndirectProp1'), true); 562 assert.strictEqual(reservedProperties.has('ohShorthand'), true); 563 assert.strictEqual(reservedProperties.has('ohShorthandProp'), true); 564 assert.strictEqual(reservedProperties.has('ohMethod1'), true); 565 assert.strictEqual(reservedProperties.has('ohMethod2'), true); 566 assert.strictEqual(reservedProperties.has('ohMethod3'), true); 567 assert.strictEqual(reservedProperties.has('ohGetProp1'), true); 568 assert.strictEqual(reservedProperties.has('ohGetProp2'), true); 569 assert.strictEqual(reservedProperties.has('ohGetProp3'), true); 570 assert.strictEqual(reservedProperties.has('ohSetProp1'), true); 571 assert.strictEqual(reservedProperties.has('ohSetProp2'), true); 572 assert.strictEqual(reservedProperties.has('ohSetProp3'), true); 573 assert.strictEqual(reservedProperties.has('ohExportElement1'), true); 574 assert.strictEqual(reservedProperties.has('ohExportElement2'), true); 575 assert.strictEqual(reservedProperties.has('ohIndirectClass1'), false); 576 assert.strictEqual(reservedProperties.has('ohIndirectProp2'), true); 577 assert.strictEqual(reservedProperties.has('ohExportElement3'), true); 578 assert.strictEqual(reservedProperties.has('ohIndirectClass2'), false); 579 assert.strictEqual(reservedProperties.has('ohIndirectProp3'), true); 580 assert.strictEqual(reservedProperties.has('ohExportElement4'), true); 581 assert.strictEqual(reservedProperties.has('ohIndirectProp4'), true); 582 assert.strictEqual(reservedProperties.has('ohExportElement5'), true); 583 assert.strictEqual(reservedProperties.has('ohExportElement6'), true); 584 assert.strictEqual(reservedProperties.has('ohExportElement7'), true); 585 assert.strictEqual(reservedProperties.has('ohIndirectClass3'), false); 586 assert.strictEqual(reservedProperties.has('ohIndirectProp5'), true); 587 assert.strictEqual(reservedProperties.has('ohExportElement8'), true); 588 assert.strictEqual(reservedProperties.has('ohIndirectClass4'), false); 589 assert.strictEqual(reservedProperties.has('ohIndirectProp6'), true); 590 assert.strictEqual(reservedExportNames.has('projectPropertyAssignment1'), false); 591 assert.strictEqual(reservedExportNames.has('projectPropertyAssignment2'), false); 592 assert.strictEqual(reservedExportNames.has('projectPropertyAssignment3'), false); 593 assert.strictEqual(reservedExportNames.has('projectPropertyAssignment4'), false); 594 assert.strictEqual(reservedExportNames.has('projectIndirectObj'), false); 595 assert.strictEqual(reservedExportNames.has('indirectProp1'), false); 596 assert.strictEqual(reservedExportNames.has('projectShorthand'), false); 597 assert.strictEqual(reservedExportNames.has('projectShorthandProp'), false); 598 assert.strictEqual(reservedExportNames.has('projectMethod1'), false); 599 assert.strictEqual(reservedExportNames.has('projectMethod2'), false); 600 assert.strictEqual(reservedExportNames.has('projectMethod3'), false); 601 assert.strictEqual(reservedExportNames.has('projectGetProp1'), false); 602 assert.strictEqual(reservedExportNames.has('projectGetProp2'), false); 603 assert.strictEqual(reservedExportNames.has('projectGetProp3'), false); 604 assert.strictEqual(reservedExportNames.has('projectSetProp1'), false); 605 assert.strictEqual(reservedExportNames.has('projectSetProp2'), false); 606 assert.strictEqual(reservedExportNames.has('projectSetProp3'), false); 607 assert.strictEqual(reservedExportNames.has('projectExportElement1'), false); 608 assert.strictEqual(reservedExportNames.has('projectExportElement2'), false); 609 assert.strictEqual(reservedExportNames.has('indirectClass1'), false); 610 assert.strictEqual(reservedExportNames.has('indirectProp2'), false); 611 assert.strictEqual(reservedExportNames.has('projectExportElement3'), false); 612 assert.strictEqual(reservedExportNames.has('indirectClass2'), false); 613 assert.strictEqual(reservedExportNames.has('indirectProp3'), false); 614 assert.strictEqual(reservedExportNames.has('projectExportElement4'), false); 615 assert.strictEqual(reservedExportNames.has('indirectProp4'), false); 616 assert.strictEqual(reservedExportNames.has('projectExportElement5'), false); 617 assert.strictEqual(reservedExportNames.has('projectExportElement6'), false); 618 assert.strictEqual(reservedExportNames.has('projectExportElement7'), false); 619 assert.strictEqual(reservedExportNames.has('indirectClass3'), false); 620 assert.strictEqual(reservedExportNames.has('indirectProp5'), false); 621 assert.strictEqual(reservedExportNames.has('projectExportElement8'), false); 622 assert.strictEqual(reservedExportNames.has('indirectClass4'), false); 623 assert.strictEqual(reservedExportNames.has('indirectProp6'), false); 624 assert.strictEqual(reservedExportNames.has('projectExportElement9'), false); 625 assert.strictEqual(reservedExportNames.has('indirectProp7'), false); 626 assert.strictEqual(reservedExportNames.has('ohPropertyAssignment1'), true); 627 assert.strictEqual(reservedExportNames.has('ohPropertyAssignment2'), true); 628 assert.strictEqual(reservedExportNames.has('ohPropertyAssignment3'), true); 629 assert.strictEqual(reservedExportNames.has('ohPropertyAssignment4'), true); 630 assert.strictEqual(reservedExportNames.has('ohIndirectObj'), false); 631 assert.strictEqual(reservedExportNames.has('ohIndirectProp1'), false); 632 assert.strictEqual(reservedExportNames.has('ohShorthand'), true); 633 assert.strictEqual(reservedExportNames.has('ohShorthandProp'), false); 634 assert.strictEqual(reservedExportNames.has('ohMethod1'), true); 635 assert.strictEqual(reservedExportNames.has('ohMethod2'), true); 636 assert.strictEqual(reservedExportNames.has('ohMethod3'), true); 637 assert.strictEqual(reservedExportNames.has('ohGetProp1'), true); 638 assert.strictEqual(reservedExportNames.has('ohGetProp2'), true); 639 assert.strictEqual(reservedExportNames.has('ohGetProp3'), true); 640 assert.strictEqual(reservedExportNames.has('ohSetProp1'), true); 641 assert.strictEqual(reservedExportNames.has('ohSetProp2'), true); 642 assert.strictEqual(reservedExportNames.has('ohSetProp3'), true); 643 assert.strictEqual(reservedExportNames.has('ohExportElement1'), true); 644 assert.strictEqual(reservedExportNames.has('ohExportElement2'), true); 645 assert.strictEqual(reservedExportNames.has('ohIndirectClass1'), true); 646 assert.strictEqual(reservedExportNames.has('ohIndirectProp2'), false); 647 assert.strictEqual(reservedExportNames.has('ohExportElement3'), true); 648 assert.strictEqual(reservedExportNames.has('ohIndirectClass2'), false); 649 assert.strictEqual(reservedExportNames.has('ohIndirectProp3'), false); 650 assert.strictEqual(reservedExportNames.has('ohExportElement4'), true); 651 assert.strictEqual(reservedExportNames.has('ohIndirectProp4'), false); 652 assert.strictEqual(reservedExportNames.has('ohExportElement5'), true); 653 assert.strictEqual(reservedExportNames.has('ohExportElement6'), true); 654 assert.strictEqual(reservedExportNames.has('ohExportElement7'), true); 655 assert.strictEqual(reservedExportNames.has('ohIndirectClass3'), true); 656 assert.strictEqual(reservedExportNames.has('ohIndirectProp5'), false); 657 assert.strictEqual(reservedExportNames.has('ohExportElement8'), true); 658 assert.strictEqual(reservedExportNames.has('ohIndirectClass4'), false); 659 assert.strictEqual(reservedExportNames.has('ohIndirectProp6'), false); 660 assert.strictEqual(reservedExportNames.has('ohExportElement9'), true); 661 assert.strictEqual(reservedExportNames.has('ohIndirectProp7'), false); 662 }); 663 }); 664 665 describe('test for visitNodeForConstructorProperty', function () { 666 it('should collect constructor properties', function () { 667 ApiExtractor.mConstructorPropertySet = new Set(); 668 let constructorPropertyAst: string = 'test/ut/utils/apiTest_visitConstructorProperty/constructorProperty.ts'; 669 let cachePath = 'test/ut/utils/obfuscation'; 670 initProjectWhiteListManager(cachePath, false, false); 671 collectApi(constructorPropertyAst, ApiExtractor.ApiType.CONSTRUCTOR_PROPERTY); 672 const fileWhiteList: FileWhiteList | undefined = projectWhiteListManager?.getFileWhiteListMap().get(constructorPropertyAst); 673 projectWhiteListManager?.createOrUpdateWhiteListCaches(); 674 expect(fileWhiteList!.fileReservedInfo.propertyParams.has('para1')).to.be.true; 675 expect(fileWhiteList!.fileReservedInfo.propertyParams.has('para2')).to.be.true; 676 expect(fileWhiteList!.fileReservedInfo.propertyParams.has('para3')).to.be.true; 677 expect(fileWhiteList!.fileReservedInfo.propertyParams.has('para4')).to.be.true; 678 expect(ApiExtractor.mConstructorPropertySet.has('para1')).to.be.true; 679 expect(ApiExtractor.mConstructorPropertySet.has('para2')).to.be.true; 680 expect(ApiExtractor.mConstructorPropertySet.has('para3')).to.be.true; 681 expect(ApiExtractor.mConstructorPropertySet.has('para4')).to.be.true; 682 clearAll(); 683 ApiExtractor.mConstructorPropertySet.clear(); 684 }) 685 }) 686 687 describe('test for visitEnumMembers', function () { 688 it('should collect enum members', function () { 689 let enumMembersAst: string = 'test/ut/utils/apiTest_visitEnumMembers/enumMembers.ts'; 690 let cachePath = 'test/ut/utils/obfuscation'; 691 initProjectWhiteListManager(cachePath, false, false); 692 collectApi(enumMembersAst, ApiExtractor.ApiType.PROJECT); 693 const fileWhiteList: FileWhiteList | undefined = projectWhiteListManager?.getFileWhiteListMap().get(enumMembersAst); 694 projectWhiteListManager?.createOrUpdateWhiteListCaches(); 695 expect(fileWhiteList!.fileReservedInfo.enumProperties.has('A1')).to.be.false; 696 expect(fileWhiteList!.fileReservedInfo.enumProperties.has('A2')).to.be.false; 697 expect(fileWhiteList!.fileReservedInfo.enumProperties.has('A3')).to.be.false; 698 expect(fileWhiteList!.fileReservedInfo.enumProperties.has('B1')).to.be.true; 699 expect(fileWhiteList!.fileReservedInfo.enumProperties.has('B2')).to.be.true; 700 expect(fileWhiteList!.fileReservedInfo.enumProperties.has('B3')).to.be.true; 701 expect(fileWhiteList!.fileReservedInfo.enumProperties.has('C1')).to.be.true; 702 expect(fileWhiteList!.fileReservedInfo.enumProperties.has('C2')).to.be.true; 703 expect(fileWhiteList!.fileReservedInfo.enumProperties.has('C3')).to.be.true; 704 expect(ApiExtractor.mEnumMemberSet.has('A1')).to.be.false; 705 expect(ApiExtractor.mEnumMemberSet.has('A2')).to.be.false; 706 expect(ApiExtractor.mEnumMemberSet.has('A3')).to.be.false; 707 expect(ApiExtractor.mEnumMemberSet.has('B1')).to.be.true; 708 expect(ApiExtractor.mEnumMemberSet.has('B2')).to.be.true; 709 expect(ApiExtractor.mEnumMemberSet.has('B3')).to.be.true; 710 expect(ApiExtractor.mEnumMemberSet.has('C1')).to.be.true; 711 expect(ApiExtractor.mEnumMemberSet.has('C2')).to.be.true; 712 expect(ApiExtractor.mEnumMemberSet.has('C3')).to.be.true; 713 clearAll(); 714 ApiExtractor.mEnumMemberSet.clear(); 715 }) 716 it('should not collect enum members of js file', function () { 717 let enumMembersAst: string = 'test/ut/utils/apiTest_visitEnumMembers/enumMembers.js'; 718 collectApi(enumMembersAst, ApiExtractor.ApiType.PROJECT); 719 expect(ApiExtractor.mEnumMemberSet.size === 0).to.be.true; 720 }) 721 }) 722 723 describe('test for collectNamesWithAtKeep', function () { 724 beforeEach(() => { 725 let cachePath = 'test/ut/utils/obfuscation'; 726 initProjectWhiteListManager(cachePath, false, true); 727 }) 728 afterEach(() => { 729 clearProjectWhiteListManager(); 730 }) 731 732 it('should not collect atKeepNames if not enabled', function () { 733 let filePath: string = 'test/ut/utils/apiTest_visitAtKeepNames/atKeepClass01.ts'; 734 AtKeepCollections.clear(); 735 scanProjectConfig.mEnableAtKeep = false; 736 collectApi(filePath, ApiExtractor.ApiType.PROJECT); 737 expect(AtKeepCollections.keepSymbol.globalNames.size).to.be.equal(0); 738 expect(AtKeepCollections.keepSymbol.propertyNames.size).to.be.equal(0); 739 expect(AtKeepCollections.keepAsConsumer.globalNames.size).to.be.equal(0); 740 expect(AtKeepCollections.keepAsConsumer.propertyNames.size).to.be.equal(0); 741 }) 742 it('should collect keepSymbol names from class if enabled', function () { 743 let filePath: string = 'test/ut/utils/apiTest_visitAtKeepNames/atKeepClass01.ts'; 744 AtKeepCollections.clear(); 745 scanProjectConfig.mEnableAtKeep = true; 746 collectApi(filePath, ApiExtractor.ApiType.PROJECT); 747 expect(AtKeepCollections.keepSymbol.globalNames.size).to.be.equal(7); 748 expect(AtKeepCollections.keepSymbol.globalNames.has('MyClass01')).to.be.true; 749 expect(AtKeepCollections.keepSymbol.globalNames.has('MyClass02')).to.be.true; 750 expect(AtKeepCollections.keepSymbol.globalNames.has('MyClass03')).to.be.true; 751 expect(AtKeepCollections.keepSymbol.globalNames.has('MyClass04')).to.be.true; 752 expect(AtKeepCollections.keepSymbol.globalNames.has('MyClass05')).to.be.true; 753 expect(AtKeepCollections.keepSymbol.globalNames.has('MyClass06')).to.be.true; 754 expect(AtKeepCollections.keepSymbol.globalNames.has('MyClass07')).to.be.true; 755 expect(AtKeepCollections.keepSymbol.globalNames.has('MyClass08')).to.be.false; 756 expect(AtKeepCollections.keepSymbol.propertyNames.size).to.be.equal(22); 757 expect(AtKeepCollections.keepSymbol.propertyNames.has('staticProperty01')).to.be.true; 758 expect(AtKeepCollections.keepSymbol.propertyNames.has('property02')).to.be.true; 759 expect(AtKeepCollections.keepSymbol.propertyNames.has('property03')).to.be.true; 760 expect(AtKeepCollections.keepSymbol.propertyNames.has('myMethod01')).to.be.true; 761 expect(AtKeepCollections.keepSymbol.propertyNames.has('myStaticMethod')).to.be.true; 762 expect(AtKeepCollections.keepSymbol.propertyNames.has('myGetter')).to.be.true; 763 expect(AtKeepCollections.keepSymbol.propertyNames.has('mySetter')).to.be.true; 764 expect(AtKeepCollections.keepSymbol.propertyNames.has('MyClass04')).to.be.true; 765 expect(AtKeepCollections.keepSymbol.propertyNames.has('staticProperty05_01')).to.be.true; 766 expect(AtKeepCollections.keepSymbol.propertyNames.has('property05_02')).to.be.true; 767 expect(AtKeepCollections.keepSymbol.propertyNames.has('myMethod05_01')).to.be.true; 768 expect(AtKeepCollections.keepSymbol.propertyNames.has('myStaticMethod05')).to.be.true; 769 expect(AtKeepCollections.keepSymbol.propertyNames.has('myGetter05')).to.be.true; 770 expect(AtKeepCollections.keepSymbol.propertyNames.has('mySetter05')).to.be.true; 771 expect(AtKeepCollections.keepSymbol.propertyNames.has('MyClass06')).to.be.true; 772 expect(AtKeepCollections.keepSymbol.propertyNames.has('property06')).to.be.true; 773 expect(AtKeepCollections.keepSymbol.propertyNames.has('MyClass07')).to.be.true; 774 expect(AtKeepCollections.keepSymbol.propertyNames.has('property07_01')).to.be.false; 775 expect(AtKeepCollections.keepSymbol.propertyNames.has('property07_02')).to.be.false; 776 expect(AtKeepCollections.keepSymbol.propertyNames.has('property07_03')).to.be.false; 777 expect(AtKeepCollections.keepSymbol.propertyNames.has('property07_04')).to.be.false; 778 expect(AtKeepCollections.keepSymbol.propertyNames.has('property07_05')).to.be.true; 779 expect(AtKeepCollections.keepSymbol.propertyNames.has('property07_06')).to.be.true; 780 expect(AtKeepCollections.keepSymbol.propertyNames.has('property07_07')).to.be.true; 781 expect(AtKeepCollections.keepSymbol.propertyNames.has('property07_08')).to.be.true; 782 expect(AtKeepCollections.keepSymbol.propertyNames.has('aa')).to.be.false; 783 expect(AtKeepCollections.keepSymbol.propertyNames.has('11')).to.be.false; 784 expect(AtKeepCollections.keepSymbol.propertyNames.has('myMethod')).to.be.false; 785 expect(AtKeepCollections.keepAsConsumer.globalNames.size).to.be.equal(0); 786 expect(AtKeepCollections.keepAsConsumer.propertyNames.size).to.be.equal(0); 787 }) 788 it('should collect keepAsConsumer names from class if enabled', function () { 789 let filePath: string = 'test/ut/utils/apiTest_visitAtKeepNames/atKeepClass02.ts'; 790 AtKeepCollections.clear(); 791 scanProjectConfig.mEnableAtKeep = true; 792 collectApi(filePath, ApiExtractor.ApiType.PROJECT); 793 expect(AtKeepCollections.keepAsConsumer.globalNames.size).to.be.equal(6); 794 expect(AtKeepCollections.keepAsConsumer.globalNames.has('MyClass01')).to.be.true; 795 expect(AtKeepCollections.keepAsConsumer.globalNames.has('MyClass02')).to.be.true; 796 expect(AtKeepCollections.keepAsConsumer.globalNames.has('MyClass03')).to.be.true; 797 expect(AtKeepCollections.keepAsConsumer.globalNames.has('MyClass04')).to.be.true; 798 expect(AtKeepCollections.keepAsConsumer.globalNames.has('MyClass05')).to.be.true; 799 expect(AtKeepCollections.keepAsConsumer.globalNames.has('MyClass06')).to.be.true; 800 expect(AtKeepCollections.keepAsConsumer.propertyNames.size).to.be.equal(17); 801 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('staticProperty01')).to.be.true; 802 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('property02')).to.be.true; 803 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('property03')).to.be.true; 804 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('myMethod01')).to.be.true; 805 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('myStaticMethod')).to.be.true; 806 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('myGetter')).to.be.true; 807 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('mySetter')).to.be.true; 808 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('MyClass04')).to.be.true; 809 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('staticProperty05_01')).to.be.true; 810 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('property05_02')).to.be.true; 811 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('myMethod05_01')).to.be.true; 812 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('myStaticMethod05')).to.be.true; 813 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('myGetter05')).to.be.true; 814 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('mySetter05')).to.be.true; 815 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('MyClass06')).to.be.true; 816 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('property06')).to.be.true; 817 expect(AtKeepCollections.keepSymbol.globalNames.size).to.be.equal(0); 818 expect(AtKeepCollections.keepSymbol.propertyNames.size).to.be.equal(0); 819 }) 820 it('should collect keepSymbol names from interface if enabled', function () { 821 let filePath: string = 'test/ut/utils/apiTest_visitAtKeepNames/atKeepInterface01.ts'; 822 AtKeepCollections.clear(); 823 scanProjectConfig.mEnableAtKeep = true; 824 collectApi(filePath, ApiExtractor.ApiType.PROJECT); 825 expect(AtKeepCollections.keepSymbol.globalNames.size).to.be.equal(4); 826 expect(AtKeepCollections.keepSymbol.globalNames.has('MyInterface01')).to.be.true; 827 expect(AtKeepCollections.keepSymbol.globalNames.has('MyInterface02')).to.be.true; 828 expect(AtKeepCollections.keepSymbol.globalNames.has('MyInterface03')).to.be.true; 829 expect(AtKeepCollections.keepSymbol.globalNames.has('MyInterface04')).to.be.true; 830 expect(AtKeepCollections.keepSymbol.propertyNames.size).to.be.equal(10); 831 expect(AtKeepCollections.keepSymbol.propertyNames.has('interfaceProperty01')).to.be.true; 832 expect(AtKeepCollections.keepSymbol.propertyNames.has('interfaceMethod01')).to.be.true; 833 expect(AtKeepCollections.keepSymbol.propertyNames.has('interfaceProperty02')).to.be.true; 834 expect(AtKeepCollections.keepSymbol.propertyNames.has('interfaceMethod02')).to.be.true; 835 expect(AtKeepCollections.keepSymbol.propertyNames.has('MyInterface02')).to.be.true; 836 expect(AtKeepCollections.keepSymbol.propertyNames.has('interfaceProperty03')).to.be.true; 837 expect(AtKeepCollections.keepSymbol.propertyNames.has('interfaceMethod03')).to.be.true; 838 expect(AtKeepCollections.keepSymbol.propertyNames.has('interfaceProperty04')).to.be.true; 839 expect(AtKeepCollections.keepSymbol.propertyNames.has('interfaceMethod04')).to.be.true; 840 expect(AtKeepCollections.keepSymbol.propertyNames.has('MyInterface04')).to.be.true; 841 expect(AtKeepCollections.keepAsConsumer.globalNames.size).to.be.equal(0); 842 expect(AtKeepCollections.keepAsConsumer.propertyNames.size).to.be.equal(0); 843 }) 844 it('should collect keepAsConsumer names from interface if enabled', function () { 845 let filePath: string = 'test/ut/utils/apiTest_visitAtKeepNames/atKeepInterface02.ts'; 846 AtKeepCollections.clear(); 847 scanProjectConfig.mEnableAtKeep = true; 848 collectApi(filePath, ApiExtractor.ApiType.PROJECT); 849 expect(AtKeepCollections.keepAsConsumer.globalNames.size).to.be.equal(4); 850 expect(AtKeepCollections.keepAsConsumer.globalNames.has('MyInterface01')).to.be.true; 851 expect(AtKeepCollections.keepAsConsumer.globalNames.has('MyInterface02')).to.be.true; 852 expect(AtKeepCollections.keepAsConsumer.globalNames.has('MyInterface03')).to.be.true; 853 expect(AtKeepCollections.keepAsConsumer.globalNames.has('MyInterface04')).to.be.true; 854 expect(AtKeepCollections.keepAsConsumer.propertyNames.size).to.be.equal(10); 855 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('interfaceProperty01')).to.be.true; 856 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('interfaceMethod01')).to.be.true; 857 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('interfaceProperty02')).to.be.true; 858 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('interfaceMethod02')).to.be.true; 859 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('MyInterface02')).to.be.true; 860 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('interfaceProperty03')).to.be.true; 861 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('interfaceMethod03')).to.be.true; 862 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('interfaceProperty04')).to.be.true; 863 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('interfaceMethod04')).to.be.true; 864 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('MyInterface04')).to.be.true; 865 expect(AtKeepCollections.keepSymbol.globalNames.size).to.be.equal(0); 866 expect(AtKeepCollections.keepSymbol.propertyNames.size).to.be.equal(0); 867 }) 868 it('should collect keepSymbol names from enum if enabled', function () { 869 let filePath: string = 'test/ut/utils/apiTest_visitAtKeepNames/atKeepEnum01.ts'; 870 AtKeepCollections.clear(); 871 scanProjectConfig.mEnableAtKeep = true; 872 collectApi(filePath, ApiExtractor.ApiType.PROJECT); 873 expect(AtKeepCollections.keepSymbol.globalNames.size).to.be.equal(4); 874 expect(AtKeepCollections.keepSymbol.globalNames.has('Color01')).to.be.true; 875 expect(AtKeepCollections.keepSymbol.globalNames.has('Color02')).to.be.true; 876 expect(AtKeepCollections.keepSymbol.globalNames.has('Color03')).to.be.true; 877 expect(AtKeepCollections.keepSymbol.globalNames.has('Color04')).to.be.true; 878 expect(AtKeepCollections.keepSymbol.globalNames.has('Color05')).to.be.false; 879 expect(AtKeepCollections.keepSymbol.propertyNames.size).to.be.equal(6); 880 expect(AtKeepCollections.keepSymbol.propertyNames.has('RED01')).to.be.true; 881 expect(AtKeepCollections.keepSymbol.propertyNames.has('RED02')).to.be.true; 882 expect(AtKeepCollections.keepSymbol.propertyNames.has('Color02')).to.be.true; 883 expect(AtKeepCollections.keepSymbol.propertyNames.has('RED03')).to.be.true; 884 expect(AtKeepCollections.keepSymbol.propertyNames.has('BLUE04')).to.be.true; 885 expect(AtKeepCollections.keepSymbol.propertyNames.has('Color04')).to.be.true; 886 expect(AtKeepCollections.keepSymbol.propertyNames.has('RED05')).to.be.false; 887 expect(AtKeepCollections.keepAsConsumer.globalNames.size).to.be.equal(0); 888 expect(AtKeepCollections.keepAsConsumer.propertyNames.size).to.be.equal(0); 889 }) 890 it('should collect keepAsConsumer names from enum if enabled', function () { 891 let filePath: string = 'test/ut/utils/apiTest_visitAtKeepNames/atKeepEnum02.ts'; 892 AtKeepCollections.clear(); 893 scanProjectConfig.mEnableAtKeep = true; 894 collectApi(filePath, ApiExtractor.ApiType.PROJECT); 895 expect(AtKeepCollections.keepAsConsumer.globalNames.size).to.be.equal(4); 896 expect(AtKeepCollections.keepAsConsumer.globalNames.has('Color01')).to.be.true; 897 expect(AtKeepCollections.keepAsConsumer.globalNames.has('Color02')).to.be.true; 898 expect(AtKeepCollections.keepAsConsumer.globalNames.has('Color03')).to.be.true; 899 expect(AtKeepCollections.keepAsConsumer.globalNames.has('Color04')).to.be.true; 900 expect(AtKeepCollections.keepAsConsumer.propertyNames.size).to.be.equal(6); 901 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('RED01')).to.be.true; 902 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('RED02')).to.be.true; 903 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('Color02')).to.be.true; 904 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('RED03')).to.be.true; 905 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('BLUE04')).to.be.true; 906 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('Color04')).to.be.true; 907 expect(AtKeepCollections.keepSymbol.globalNames.size).to.be.equal(0); 908 expect(AtKeepCollections.keepSymbol.propertyNames.size).to.be.equal(0); 909 }) 910 it('should collect atKeep names from function if enabled', function () { 911 let filePath: string = 'test/ut/utils/apiTest_visitAtKeepNames/atKeepFunction.ts'; 912 AtKeepCollections.clear(); 913 scanProjectConfig.mEnableAtKeep = true; 914 collectApi(filePath, ApiExtractor.ApiType.PROJECT); 915 expect(AtKeepCollections.keepSymbol.globalNames.size).to.be.equal(2); 916 expect(AtKeepCollections.keepSymbol.globalNames.has('addNumbers01')).to.be.true; 917 expect(AtKeepCollections.keepSymbol.globalNames.has('addNumbers02')).to.be.true; 918 expect(AtKeepCollections.keepSymbol.propertyNames.size).to.be.equal(1); 919 expect(AtKeepCollections.keepSymbol.propertyNames.has('addNumbers02')).to.be.true; 920 expect(AtKeepCollections.keepAsConsumer.globalNames.size).to.be.equal(2); 921 expect(AtKeepCollections.keepAsConsumer.globalNames.has('addNumbers03')).to.be.true; 922 expect(AtKeepCollections.keepAsConsumer.globalNames.has('addNumbers04')).to.be.true; 923 expect(AtKeepCollections.keepAsConsumer.propertyNames.size).to.be.equal(1); 924 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('addNumbers04')).to.be.true; 925 }) 926 it('should collect keepSymbol names from namespace if enabled', function () { 927 let filePath: string = 'test/ut/utils/apiTest_visitAtKeepNames/atKeepNamespace01.ts'; 928 AtKeepCollections.clear(); 929 scanProjectConfig.mEnableAtKeep = true; 930 collectApi(filePath, ApiExtractor.ApiType.PROJECT); 931 // globalNames: 932 expect(AtKeepCollections.keepSymbol.globalNames.size).to.be.equal(26); 933 // from MyNamespace01 934 expect(AtKeepCollections.keepSymbol.globalNames.has('MyNamespace01')).to.be.true; 935 expect(AtKeepCollections.keepSymbol.globalNames.has('nsConstValue01')).to.be.true; 936 expect(AtKeepCollections.keepSymbol.globalNames.has('nsFunction01')).to.be.true; 937 expect(AtKeepCollections.keepSymbol.globalNames.has('nsClass01')).to.be.true; 938 expect(AtKeepCollections.keepSymbol.globalNames.has('nsInterface01')).to.be.true; 939 expect(AtKeepCollections.keepSymbol.globalNames.has('nsEnum01')).to.be.true; 940 expect(AtKeepCollections.keepSymbol.globalNames.has('InnerNamespace01')).to.be.true; 941 expect(AtKeepCollections.keepSymbol.globalNames.has('innerConstValue01')).to.be.true; 942 expect(AtKeepCollections.keepSymbol.globalNames.has('nsType01')).to.be.true; 943 // from MyNamespace02 944 expect(AtKeepCollections.keepSymbol.globalNames.has('nsConstValue02')).to.be.true; 945 expect(AtKeepCollections.keepSymbol.globalNames.has('nsFunction02')).to.be.true; 946 expect(AtKeepCollections.keepSymbol.globalNames.has('nsClass02')).to.be.true; 947 expect(AtKeepCollections.keepSymbol.globalNames.has('nsClass02_01')).to.be.true; 948 expect(AtKeepCollections.keepSymbol.globalNames.has('nsClass02_02')).to.be.true; 949 expect(AtKeepCollections.keepSymbol.globalNames.has('nsInterface02')).to.be.true; 950 expect(AtKeepCollections.keepSymbol.globalNames.has('nsInterface02_01')).to.be.true; 951 expect(AtKeepCollections.keepSymbol.globalNames.has('nsEnum02')).to.be.true; 952 expect(AtKeepCollections.keepSymbol.globalNames.has('nsEnum02_01')).to.be.true; 953 expect(AtKeepCollections.keepSymbol.globalNames.has('InnerNamespace02')).to.be.true; 954 expect(AtKeepCollections.keepSymbol.globalNames.has('innerConstValue02')).to.be.true; 955 // from MyNamespace04 956 expect(AtKeepCollections.keepSymbol.globalNames.has('MyNamespace04')).to.be.true; 957 expect(AtKeepCollections.keepSymbol.globalNames.has('MyNamespace05')).to.be.true; 958 expect(AtKeepCollections.keepSymbol.globalNames.has('MyNamespace06')).to.be.true; 959 expect(AtKeepCollections.keepSymbol.globalNames.has('MyNamespace08Class')).to.be.true; 960 expect(AtKeepCollections.keepSymbol.globalNames.has('MyNamespace09')).to.be.true; 961 expect(AtKeepCollections.keepSymbol.globalNames.has('MyNamespace09Class')).to.be.true; 962 // propertyNames: 963 expect(AtKeepCollections.keepSymbol.propertyNames.size).to.be.equal(37); 964 // from MyNamespace01 965 expect(AtKeepCollections.keepSymbol.propertyNames.has('nsConstValue01')).to.be.true; 966 expect(AtKeepCollections.keepSymbol.propertyNames.has('nsFunction01')).to.be.true; 967 expect(AtKeepCollections.keepSymbol.propertyNames.has('nsClass01')).to.be.true; 968 expect(AtKeepCollections.keepSymbol.propertyNames.has('nsInterface01')).to.be.true; 969 expect(AtKeepCollections.keepSymbol.propertyNames.has('nsEnum01')).to.be.true; 970 expect(AtKeepCollections.keepSymbol.propertyNames.has('InnerNamespace01')).to.be.true; 971 expect(AtKeepCollections.keepSymbol.propertyNames.has('innerConstValue01')).to.be.true; 972 expect(AtKeepCollections.keepSymbol.propertyNames.has('nsType01')).to.be.true; 973 // from MyNamespace02 974 expect(AtKeepCollections.keepSymbol.propertyNames.has('nsConstValue02')).to.be.true; 975 expect(AtKeepCollections.keepSymbol.propertyNames.has('nsFunction02')).to.be.true; 976 expect(AtKeepCollections.keepSymbol.propertyNames.has('nsClass02')).to.be.true; 977 expect(AtKeepCollections.keepSymbol.propertyNames.has('classProp02')).to.be.true; 978 expect(AtKeepCollections.keepSymbol.propertyNames.has('nsClass02_01')).to.be.true; 979 expect(AtKeepCollections.keepSymbol.propertyNames.has('nsClass02_02')).to.be.true; 980 expect(AtKeepCollections.keepSymbol.propertyNames.has('classProp02_02')).to.be.true; 981 expect(AtKeepCollections.keepSymbol.propertyNames.has('nsInterface02')).to.be.true; 982 expect(AtKeepCollections.keepSymbol.propertyNames.has('interfaceProp02')).to.be.true; 983 expect(AtKeepCollections.keepSymbol.propertyNames.has('nsInterface02_01')).to.be.true; 984 expect(AtKeepCollections.keepSymbol.propertyNames.has('interfaceProp02_01')).to.be.true; 985 expect(AtKeepCollections.keepSymbol.propertyNames.has('nsEnum02')).to.be.true; 986 expect(AtKeepCollections.keepSymbol.propertyNames.has('nsEnum02_01')).to.be.true; 987 expect(AtKeepCollections.keepSymbol.propertyNames.has('RED02')).to.be.true; 988 expect(AtKeepCollections.keepSymbol.propertyNames.has('RED02_01')).to.be.true; 989 expect(AtKeepCollections.keepSymbol.propertyNames.has('InnerNamespace02')).to.be.true; 990 expect(AtKeepCollections.keepSymbol.propertyNames.has('innerConstValue02')).to.be.true; 991 // from MyNamespace03 992 expect(AtKeepCollections.keepSymbol.propertyNames.has('classProp03')).to.be.true; 993 expect(AtKeepCollections.keepSymbol.propertyNames.has('classProp03_02')).to.be.true; 994 expect(AtKeepCollections.keepSymbol.propertyNames.has('interfaceProp03')).to.be.true; 995 expect(AtKeepCollections.keepSymbol.propertyNames.has('interfaceProp03_01')).to.be.true; 996 expect(AtKeepCollections.keepSymbol.propertyNames.has('RED03')).to.be.true; 997 expect(AtKeepCollections.keepSymbol.propertyNames.has('RED03_01')).to.be.true; 998 // from MyNamespace04 999 expect(AtKeepCollections.keepSymbol.propertyNames.has('MyNamespace04')).to.be.true; 1000 expect(AtKeepCollections.keepSymbol.propertyNames.has('MyNamespace05')).to.be.true; 1001 expect(AtKeepCollections.keepSymbol.propertyNames.has('MyNamespace06')).to.be.true; 1002 expect(AtKeepCollections.keepSymbol.propertyNames.has('MyNamespace08Class')).to.be.true; 1003 expect(AtKeepCollections.keepSymbol.propertyNames.has('MyNamespace09')).to.be.true; 1004 expect(AtKeepCollections.keepSymbol.propertyNames.has('MyNamespace09Class')).to.be.true; 1005 expect(AtKeepCollections.keepAsConsumer.globalNames.size).to.be.equal(0); 1006 expect(AtKeepCollections.keepAsConsumer.propertyNames.size).to.be.equal(0); 1007 }) 1008 it('should collect keepAsConsumer names from namespace if enabled', function () { 1009 let filePath: string = 'test/ut/utils/apiTest_visitAtKeepNames/atKeepNamespace02.ts'; 1010 AtKeepCollections.clear(); 1011 scanProjectConfig.mEnableAtKeep = true; 1012 collectApi(filePath, ApiExtractor.ApiType.PROJECT); 1013 // globalNames: 1014 expect(AtKeepCollections.keepAsConsumer.globalNames.size).to.be.equal(26); 1015 // from MyNamespace01 1016 expect(AtKeepCollections.keepAsConsumer.globalNames.has('MyNamespace01')).to.be.true; 1017 expect(AtKeepCollections.keepAsConsumer.globalNames.has('nsConstValue01')).to.be.true; 1018 expect(AtKeepCollections.keepAsConsumer.globalNames.has('nsFunction01')).to.be.true; 1019 expect(AtKeepCollections.keepAsConsumer.globalNames.has('nsClass01')).to.be.true; 1020 expect(AtKeepCollections.keepAsConsumer.globalNames.has('nsInterface01')).to.be.true; 1021 expect(AtKeepCollections.keepAsConsumer.globalNames.has('nsEnum01')).to.be.true; 1022 expect(AtKeepCollections.keepAsConsumer.globalNames.has('InnerNamespace01')).to.be.true; 1023 expect(AtKeepCollections.keepAsConsumer.globalNames.has('innerConstValue01')).to.be.true; 1024 expect(AtKeepCollections.keepAsConsumer.globalNames.has('nsType01')).to.be.true; 1025 // from MyNamespace02 1026 expect(AtKeepCollections.keepAsConsumer.globalNames.has('nsConstValue02')).to.be.true; 1027 expect(AtKeepCollections.keepAsConsumer.globalNames.has('nsFunction02')).to.be.true; 1028 expect(AtKeepCollections.keepAsConsumer.globalNames.has('nsClass02')).to.be.true; 1029 expect(AtKeepCollections.keepAsConsumer.globalNames.has('nsClass02_01')).to.be.true; 1030 expect(AtKeepCollections.keepAsConsumer.globalNames.has('nsClass02_02')).to.be.true; 1031 expect(AtKeepCollections.keepAsConsumer.globalNames.has('nsInterface02')).to.be.true; 1032 expect(AtKeepCollections.keepAsConsumer.globalNames.has('nsInterface02_01')).to.be.true; 1033 expect(AtKeepCollections.keepAsConsumer.globalNames.has('nsEnum02')).to.be.true; 1034 expect(AtKeepCollections.keepAsConsumer.globalNames.has('nsEnum02_01')).to.be.true; 1035 expect(AtKeepCollections.keepAsConsumer.globalNames.has('InnerNamespace02')).to.be.true; 1036 expect(AtKeepCollections.keepAsConsumer.globalNames.has('innerConstValue02')).to.be.true; 1037 // from MyNamespace04 1038 expect(AtKeepCollections.keepAsConsumer.globalNames.has('MyNamespace04')).to.be.true; 1039 expect(AtKeepCollections.keepAsConsumer.globalNames.has('MyNamespace05')).to.be.true; 1040 expect(AtKeepCollections.keepAsConsumer.globalNames.has('MyNamespace06')).to.be.true; 1041 expect(AtKeepCollections.keepAsConsumer.globalNames.has('MyNamespace08Class')).to.be.true; 1042 expect(AtKeepCollections.keepAsConsumer.globalNames.has('MyNamespace09')).to.be.true; 1043 expect(AtKeepCollections.keepAsConsumer.globalNames.has('MyNamespace09Class')).to.be.true; 1044 // propertyNames: 1045 expect(AtKeepCollections.keepAsConsumer.propertyNames.size).to.be.equal(37); 1046 // from MyNamespace01 1047 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('nsConstValue01')).to.be.true; 1048 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('nsFunction01')).to.be.true; 1049 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('nsClass01')).to.be.true; 1050 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('nsInterface01')).to.be.true; 1051 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('nsEnum01')).to.be.true; 1052 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('InnerNamespace01')).to.be.true; 1053 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('innerConstValue01')).to.be.true; 1054 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('nsType01')).to.be.true; 1055 // from MyNamespace02 1056 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('nsConstValue02')).to.be.true; 1057 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('nsFunction02')).to.be.true; 1058 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('nsClass02')).to.be.true; 1059 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('classProp02')).to.be.true; 1060 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('nsClass02_01')).to.be.true; 1061 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('nsClass02_02')).to.be.true; 1062 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('classProp02_02')).to.be.true; 1063 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('nsInterface02')).to.be.true; 1064 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('interfaceProp02')).to.be.true; 1065 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('nsInterface02_01')).to.be.true; 1066 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('interfaceProp02_01')).to.be.true; 1067 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('nsEnum02')).to.be.true; 1068 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('nsEnum02_01')).to.be.true; 1069 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('RED02')).to.be.true; 1070 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('RED02_01')).to.be.true; 1071 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('InnerNamespace02')).to.be.true; 1072 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('innerConstValue02')).to.be.true; 1073 // from MyNamespace03 1074 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('classProp03')).to.be.true; 1075 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('classProp03_02')).to.be.true; 1076 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('interfaceProp03')).to.be.true; 1077 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('interfaceProp03_01')).to.be.true; 1078 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('RED03')).to.be.true; 1079 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('RED03_01')).to.be.true; 1080 // from MyNamespace04 1081 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('MyNamespace04')).to.be.true; 1082 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('MyNamespace05')).to.be.true; 1083 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('MyNamespace06')).to.be.true; 1084 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('MyNamespace08Class')).to.be.true; 1085 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('MyNamespace09')).to.be.true; 1086 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('MyNamespace09Class')).to.be.true; 1087 expect(AtKeepCollections.keepSymbol.globalNames.size).to.be.equal(0); 1088 expect(AtKeepCollections.keepSymbol.propertyNames.size).to.be.equal(0); 1089 }) 1090 it('should collect atKeep names from global variable if enabled', function () { 1091 let filePath: string = 'test/ut/utils/apiTest_visitAtKeepNames/atKeepGlobalVar.ts'; 1092 AtKeepCollections.clear(); 1093 scanProjectConfig.mEnableAtKeep = true; 1094 collectApi(filePath, ApiExtractor.ApiType.PROJECT); 1095 expect(AtKeepCollections.keepSymbol.globalNames.size).to.be.equal(6); 1096 expect(AtKeepCollections.keepSymbol.globalNames.has('globalVar01')).to.be.true; 1097 expect(AtKeepCollections.keepSymbol.globalNames.has('globalFunc01')).to.be.true; 1098 expect(AtKeepCollections.keepSymbol.globalNames.has('globalMyClass01')).to.be.true; 1099 expect(AtKeepCollections.keepSymbol.globalNames.has('globalVar02')).to.be.true; 1100 expect(AtKeepCollections.keepSymbol.globalNames.has('globalFunc02')).to.be.true; 1101 expect(AtKeepCollections.keepSymbol.globalNames.has('globalMyClass02')).to.be.true; 1102 expect(AtKeepCollections.keepSymbol.propertyNames.size).to.be.equal(3); 1103 expect(AtKeepCollections.keepSymbol.propertyNames.has('globalVar02')).to.be.true; 1104 expect(AtKeepCollections.keepSymbol.propertyNames.has('globalFunc02')).to.be.true; 1105 expect(AtKeepCollections.keepSymbol.propertyNames.has('globalMyClass02')).to.be.true; 1106 expect(AtKeepCollections.keepAsConsumer.globalNames.size).to.be.equal(6); 1107 expect(AtKeepCollections.keepAsConsumer.globalNames.has('globalVar03')).to.be.true; 1108 expect(AtKeepCollections.keepAsConsumer.globalNames.has('globalFunc03')).to.be.true; 1109 expect(AtKeepCollections.keepAsConsumer.globalNames.has('globalMyClass03')).to.be.true; 1110 expect(AtKeepCollections.keepAsConsumer.globalNames.has('globalVar04')).to.be.true; 1111 expect(AtKeepCollections.keepAsConsumer.globalNames.has('globalFunc04')).to.be.true; 1112 expect(AtKeepCollections.keepAsConsumer.globalNames.has('globalMyClass04')).to.be.true; 1113 expect(AtKeepCollections.keepAsConsumer.propertyNames.size).to.be.equal(3); 1114 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('globalVar04')).to.be.true; 1115 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('globalFunc04')).to.be.true; 1116 expect(AtKeepCollections.keepAsConsumer.propertyNames.has('globalMyClass04')).to.be.true; 1117 }) 1118 it('should collect atKeep names from annotation declarations if enabled', function () { 1119 let filePath: string = 'test/ut/utils/apiTest_visitAtKeepNames/atKeepAnnotation.ets'; 1120 AtKeepCollections.clear(); 1121 scanProjectConfig.mEnableAtKeep = true; 1122 collectApi(filePath, ApiExtractor.ApiType.PROJECT); 1123 expect(AtKeepCollections.keepSymbol.globalNames.size).to.be.equal(2); 1124 expect(AtKeepCollections.keepSymbol.globalNames.has('MyAnnotation01')).to.be.true; 1125 expect(AtKeepCollections.keepSymbol.globalNames.has('MyAnnotation02')).to.be.true; 1126 expect(AtKeepCollections.keepSymbol.propertyNames.size).to.be.equal(1); 1127 expect(AtKeepCollections.keepSymbol.propertyNames.has('MyAnnotation02')).to.be.true; 1128 expect(AtKeepCollections.keepAsConsumer.globalNames.size).to.be.equal(2); 1129 expect(AtKeepCollections.keepAsConsumer.globalNames.has('MyAnnotation04')).to.be.true; 1130 expect(AtKeepCollections.keepAsConsumer.globalNames.has('MyAnnotation05')).to.be.true; 1131 expect(AtKeepCollections.keepAsConsumer.propertyNames.size).to.be.equal(1); 1132 expect(AtKeepCollections.keepAsConsumer.globalNames.has('MyAnnotation05')).to.be.true; 1133 }) 1134 it('should collect atKeep names from .ets', function () { 1135 let filePath: string = 'test/ut/utils/apiTest_visitAtKeepNames/atKeepTest01.ets'; 1136 AtKeepCollections.clear(); 1137 scanProjectConfig.mEnableAtKeep = true; 1138 collectApi(filePath, ApiExtractor.ApiType.PROJECT); 1139 expect(AtKeepCollections.keepSymbol.globalNames.size).to.be.equal(1); 1140 expect(AtKeepCollections.keepSymbol.globalNames.has('MyClass01')).to.be.true; 1141 expect(AtKeepCollections.keepSymbol.propertyNames.size).to.be.equal(2); 1142 expect(AtKeepCollections.keepSymbol.propertyNames.has('property01')).to.be.true; 1143 expect(AtKeepCollections.keepSymbol.propertyNames.has('MyClass01')).to.be.true; 1144 expect(AtKeepCollections.keepAsConsumer.globalNames.size).to.be.equal(2); 1145 expect(AtKeepCollections.keepAsConsumer.globalNames.has('MyVar01')).to.be.true; 1146 expect(AtKeepCollections.keepAsConsumer.globalNames.has('MyVar02')).to.be.true; 1147 expect(AtKeepCollections.keepAsConsumer.propertyNames.size).to.be.equal(0); 1148 }) 1149 it('should not collect atKeep names from .js', function () { 1150 let filePath: string = 'test/ut/utils/apiTest_visitAtKeepNames/atKeepTest02.js'; 1151 AtKeepCollections.clear(); 1152 scanProjectConfig.mEnableAtKeep = true; 1153 collectApi(filePath, ApiExtractor.ApiType.PROJECT); 1154 expect(AtKeepCollections.keepSymbol.globalNames.size).to.be.equal(0); 1155 expect(AtKeepCollections.keepSymbol.propertyNames.size).to.be.equal(0); 1156 expect(AtKeepCollections.keepAsConsumer.globalNames.size).to.be.equal(0); 1157 expect(AtKeepCollections.keepAsConsumer.propertyNames.size).to.be.equal(0); 1158 }) 1159 it('should not collect atKeep names from .d.ts', function () { 1160 let filePath: string = 'test/ut/utils/apiTest_visitAtKeepNames/atKeepTest03.d.ts'; 1161 AtKeepCollections.clear(); 1162 scanProjectConfig.mEnableAtKeep = true; 1163 collectApi(filePath, ApiExtractor.ApiType.PROJECT); 1164 expect(AtKeepCollections.keepSymbol.globalNames.size).to.be.equal(0); 1165 expect(AtKeepCollections.keepSymbol.propertyNames.size).to.be.equal(0); 1166 expect(AtKeepCollections.keepAsConsumer.globalNames.size).to.be.equal(0); 1167 expect(AtKeepCollections.keepAsConsumer.propertyNames.size).to.be.equal(0); 1168 }) 1169 it('should not collect atKeep names from .d.ets', function () { 1170 let filePath: string = 'test/ut/utils/apiTest_visitAtKeepNames/atKeepTest04.d.ets'; 1171 AtKeepCollections.clear(); 1172 scanProjectConfig.mEnableAtKeep = true; 1173 collectApi(filePath, ApiExtractor.ApiType.PROJECT); 1174 expect(AtKeepCollections.keepSymbol.globalNames.size).to.be.equal(0); 1175 expect(AtKeepCollections.keepSymbol.propertyNames.size).to.be.equal(0); 1176 expect(AtKeepCollections.keepAsConsumer.globalNames.size).to.be.equal(0); 1177 expect(AtKeepCollections.keepAsConsumer.propertyNames.size).to.be.equal(0); 1178 }) 1179 }) 1180 1181 describe('test for collect fileWhiteList', function () { 1182 beforeEach(() => { 1183 let cachePath = 'test/ut/utils/obfuscation'; 1184 initProjectWhiteListManager(cachePath, false, true); 1185 }) 1186 1187 afterEach(() => { 1188 clearProjectWhiteListManager(); 1189 }) 1190 1191 it('should collect structProperties, stringProperties, enumProperties if propertyObf is enabled', function () { 1192 let filePath: string = 'test/ut/utils/apiTest_collectFileWhiteList/collectFileWhiteList01.ets'; 1193 scanProjectConfig.mPropertyObfuscation = true; 1194 scanProjectConfig.mKeepStringProperty = true; 1195 scanProjectConfig.isHarCompiled = true; 1196 collectApi(filePath, ApiExtractor.ApiType.PROJECT); 1197 const fileWhiteList: FileWhiteList = projectWhiteListManager?.getFileWhiteListMap().get(filePath)!; 1198 expect(fileWhiteList.fileKeepInfo.enumProperties.has('RED01')).to.be.true; 1199 expect(fileWhiteList.fileKeepInfo.enumProperties.has('RED02')).to.be.true; 1200 expect(fileWhiteList.fileKeepInfo.enumProperties.has('BLUE02')).to.be.true; 1201 expect(fileWhiteList.fileKeepInfo.exported.propertyNames.has('MyEnum')).to.be.true; 1202 expect(fileWhiteList.fileKeepInfo.exported.propertyNames.has('MyClass')).to.be.true; 1203 expect(fileWhiteList.fileKeepInfo.exported.propertyNames.has('obj01')).to.be.true; 1204 expect(fileWhiteList.fileKeepInfo.exported.propertyNames.has('RED01')).to.be.true; 1205 expect(fileWhiteList.fileKeepInfo.exported.propertyNames.has('myProp01')).to.be.true; 1206 expect(fileWhiteList.fileKeepInfo.exported.propertyNames.has('objProp')).to.be.true; 1207 expect(fileWhiteList.fileKeepInfo.stringProperties.has('name')).to.be.true; 1208 expect(fileWhiteList.fileKeepInfo.stringProperties.has('age')).to.be.true; 1209 expect(fileWhiteList.fileKeepInfo.structProperties.has('myStructProp')).to.be.true; 1210 }) 1211 it('should not collect stringProperties if mKeepStringProperty is not enabled', function () { 1212 let filePath: string = 'test/ut/utils/apiTest_collectFileWhiteList/collectFileWhiteList01.ets'; 1213 scanProjectConfig.mPropertyObfuscation = true; 1214 scanProjectConfig.mKeepStringProperty = false; 1215 scanProjectConfig.isHarCompiled = true; 1216 collectApi(filePath, ApiExtractor.ApiType.PROJECT); 1217 const fileWhiteList: FileWhiteList = projectWhiteListManager?.getFileWhiteListMap().get(filePath)!; 1218 expect(fileWhiteList.fileKeepInfo.enumProperties.has('RED01')).to.be.true; 1219 expect(fileWhiteList.fileKeepInfo.enumProperties.has('RED02')).to.be.true; 1220 expect(fileWhiteList.fileKeepInfo.enumProperties.has('BLUE02')).to.be.true; 1221 expect(fileWhiteList.fileKeepInfo.exported.propertyNames.has('MyEnum')).to.be.true; 1222 expect(fileWhiteList.fileKeepInfo.exported.propertyNames.has('MyClass')).to.be.true; 1223 expect(fileWhiteList.fileKeepInfo.exported.propertyNames.has('obj01')).to.be.true; 1224 expect(fileWhiteList.fileKeepInfo.exported.propertyNames.has('RED01')).to.be.true; 1225 expect(fileWhiteList.fileKeepInfo.exported.propertyNames.has('myProp01')).to.be.true; 1226 expect(fileWhiteList.fileKeepInfo.exported.propertyNames.has('objProp')).to.be.true; 1227 expect(fileWhiteList.fileKeepInfo.stringProperties.has('name')).to.be.false; 1228 expect(fileWhiteList.fileKeepInfo.stringProperties.has('age')).to.be.false; 1229 expect(fileWhiteList.fileKeepInfo.structProperties.has('myStructProp')).to.be.true; 1230 }) 1231 it('should not collect structProperties, stringProperties, enumProperties if propertyObf is not enabled', function () { 1232 let filePath: string = 'test/ut/utils/apiTest_collectFileWhiteList/collectFileWhiteList01.ets'; 1233 scanProjectConfig.mPropertyObfuscation = false; 1234 scanProjectConfig.mKeepStringProperty = true; 1235 scanProjectConfig.isHarCompiled = true; 1236 collectApi(filePath, ApiExtractor.ApiType.PROJECT); 1237 const fileWhiteList: FileWhiteList = projectWhiteListManager?.getFileWhiteListMap().get(filePath)!; 1238 expect(fileWhiteList.fileKeepInfo.enumProperties.size).to.be.equal(0); 1239 expect(fileWhiteList.fileKeepInfo.exported.propertyNames.size).to.be.equal(0); 1240 expect(fileWhiteList.fileKeepInfo.stringProperties.size).to.be.equal(0); 1241 expect(fileWhiteList.fileKeepInfo.structProperties.size).to.be.equal(0); 1242 }) 1243 it('should collect decoratorMap if need scanDecorator', function () { 1244 let filePath: string = 'test/ut/utils/apiTest_collectFileWhiteList/collectFileWhiteList02.ets'; 1245 scanProjectConfig.scanDecorator = true; 1246 collectApi(filePath, ApiExtractor.ApiType.PROJECT); 1247 const fileWhiteList: FileWhiteList = projectWhiteListManager?.getFileWhiteListMap().get(filePath)!; 1248 if (fileWhiteList?.bytecodeObfuscateKeepInfo?.decoratorMap) { 1249 const decoratorKeys = Object.keys(fileWhiteList.bytecodeObfuscateKeepInfo.decoratorMap); 1250 expect(decoratorKeys.length).to.be.greaterThan(0); 1251 } 1252 }) 1253 }) 1254});