diff --git a/compiler/src/validate_ui_syntax.ts b/compiler/src/validate_ui_syntax.ts index c57d75f02aa275523941d00697a68c7d7e589f08..4778eccfd027b60f5e245ea93d9c2287db92a1c3 100644 --- a/compiler/src/validate_ui_syntax.ts +++ b/compiler/src/validate_ui_syntax.ts @@ -802,11 +802,17 @@ function collectExtend(component: string, attribute: string, parameter: string): export function processSystemApi(content: string, isProcessWhiteList: boolean = false): string { let REG_SYSTEM: RegExp; - REG_SYSTEM = - /import\s+(.+)\s+from\s+['"]@(system|ohos)\.(\S+)['"]|import\s+(.+)\s*=\s*require\(\s*['"]@(system|ohos)\.(\S+)['"]\s*\)/g; + if (isProcessWhiteList) { + REG_SYSTEM = + /(import|const)\s+(.+)\s*=\s*(\_\_importDefault\()?require\(\s*['"]@(system|ohos)\.(\S+)['"]\s*\)(\))?/g; + } else { + REG_SYSTEM = + /import\s+(.+)\s+from\s+['"]@(system|ohos)\.(\S+)['"]|import\s+(.+)\s*=\s*require\(\s*['"]@(system|ohos)\.(\S+)['"]\s*\)/g; + } const REG_LIB_SO: RegExp = /import\s+(.+)\s+from\s+['"]lib(\S+)\.so['"]|import\s+(.+)\s*=\s*require\(\s*['"]lib(\S+)\.so['"]\s*\)/g; - return content.replace(REG_LIB_SO, (_, item1, item2, item3, item4) => { + const systemValueCollection: Set = new Set(); + let newContent = content.replace(REG_LIB_SO, (_, item1, item2, item3, item4) => { const libSoValue: string = item1 || item3; const libSoKey: string = item2 || item4; return `var ${libSoValue} = globalThis.requireNapi("${libSoKey}", true);`; @@ -814,6 +820,14 @@ export function processSystemApi(content: string, isProcessWhiteList: boolean = let moduleType: string = item2 || item5; let systemKey: string = item3 || item6; let systemValue: string = item1 || item4; + if (!isProcessWhiteList && validateWhiteListModule(moduleType, systemKey)) { + return item; + } else if (isProcessWhiteList) { + systemValue = item2; + moduleType = item4; + systemKey = item5; + systemValueCollection.add(systemValue) + } moduleCollection.add(`${moduleType}.${systemKey}`); if (NATIVE_MODULE.has(`${moduleType}.${systemKey}`)) { item = `var ${systemValue} = globalThis.requireNativeModule('${moduleType}.${systemKey}')`; @@ -827,11 +841,19 @@ export function processSystemApi(content: string, isProcessWhiteList: boolean = `? globalThis.systemplugin.${systemKey} : undefined)`; } return item; - }); + }) + + systemValueCollection.forEach(element => { + let target: string = element.trim()+'.default' + while(newContent.includes(target)) { + newContent = newContent.replace(target,element.trim()) + } + }); + return newContent } function validateWhiteListModule(moduleType: string, systemKey: string): boolean { - return moduleType === 'ohos' && /^application\./g.test(systemKey); + return moduleType === 'ohos' && /^(application|util)\./g.test(systemKey); } export function resetComponentCollection() {