1/* 2 * Copyright (c) 2021 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 16const ts = require('typescript'); 17const path = require('path'); 18const chai = require('chai'); 19const mocha = require('mocha'); 20const expect = chai.expect; 21const { processUISyntax } = require('../lib/process_ui_syntax'); 22const { 23 validateUISyntax, 24 preprocessExtend, 25 resetComponentCollection, 26 componentCollection 27} = require('../lib/validate_ui_syntax'); 28const { 29 componentInfo, 30 readFile 31} = require('../lib/utils'); 32const { 33 BUILD_ON, 34 OHOS_PLUGIN, 35 NATIVE_MODULE, 36 SYSTEM_PLUGIN 37} = require('../lib/pre_define'); 38 39function expectActual(name, filePath) { 40 const content = require(filePath); 41 const source = content.source; 42 process.env.compiler = BUILD_ON; 43 const afterProcess = sourceReplace(source); 44 validateUISyntax(source, afterProcess.content, `${name}.ts`); 45 const compilerOptions = ts.readConfigFile( 46 path.resolve(__dirname, '../tsconfig.json'), ts.sys.readFile).config.compilerOptions; 47 Object.assign(compilerOptions, { 48 "sourceMap": false, 49 }); 50 const result = ts.transpileModule(afterProcess.content, { 51 compilerOptions: compilerOptions, 52 fileName: `${name}.ets`, 53 transformers: { before: [processUISyntax(null, true)] } 54 }); 55 componentInfo.id = 0; 56 componentCollection.customComponents.clear(); 57 resetComponentCollection(); 58 expect(result.outputText).eql(content.expectResult); 59} 60 61mocha.describe('compiler', () => { 62 const utPath = path.resolve(__dirname, './ut'); 63 const utFiles = []; 64 readFile(utPath, utFiles); 65 utFiles.forEach((item) => { 66 const fileName = path.basename(item, '.ts'); 67 mocha.it(fileName, () => { 68 expectActual(fileName, item); 69 }) 70 }) 71}) 72 73function sourceReplace(source) { 74 let content = source; 75 const log = []; 76 content = preprocessExtend(content); 77 content = processSystemApi(content); 78 return { 79 content: content, 80 log: log 81 }; 82} 83 84function processSystemApi(content) { 85 const REG_SYSTEM = 86 /import\s+(.+)\s+from\s+['"]@(system|ohos)\.(\S+)['"]|import\s+(.+)\s*=\s*require\(\s*['"]@(system|ohos)\.(\S+)['"]\s*\)/g; 87 const REG_LIB_SO = 88 /import\s+(.+)\s+from\s+['"]lib(\S+)\.so['"]|import\s+(.+)\s*=\s*require\(\s*['"]lib(\S+)\.so['"]\s*\)/g; 89 const newContent = content.replace(REG_LIB_SO, (_, item1, item2, item3, item4) => { 90 const libSoValue = item1 || item3; 91 const libSoKey = item2 || item4; 92 return `var ${libSoValue} = globalThis.requireNapi("${libSoKey}", true);`; 93 }).replace(REG_SYSTEM, (item, item1, item2, item3, item4, item5, item6, item7) => { 94 let moduleType = item2 || item5; 95 let systemKey = item3 || item6; 96 let systemValue = item1 || item4; 97 if (NATIVE_MODULE.has(`${moduleType}.${systemKey}`)) { 98 item = `var ${systemValue} = globalThis.requireNativeModule('${moduleType}.${systemKey}')`; 99 } else if (moduleType === SYSTEM_PLUGIN) { 100 item = `var ${systemValue} = isSystemplugin('${systemKey}', '${SYSTEM_PLUGIN}') ? ` + 101 `globalThis.systemplugin.${systemKey} : globalThis.requireNapi('${systemKey}')`; 102 } else if (moduleType === OHOS_PLUGIN) { 103 item = `var ${systemValue} = globalThis.requireNapi('${systemKey}') || ` + 104 `(isSystemplugin('${systemKey}', '${OHOS_PLUGIN}') ? ` + 105 `globalThis.ohosplugin.${systemKey} : isSystemplugin('${systemKey}', '${SYSTEM_PLUGIN}') ` + 106 `? globalThis.systemplugin.${systemKey} : undefined)`; 107 } 108 return item; 109 }); 110 return newContent; 111}