• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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');
38const {
39  partialUpdateConfig
40} = require('../main');
41
42function expectActual(name, filePath) {
43  const content = require(filePath);
44  const source = content.source;
45  process.env.compiler = BUILD_ON;
46  const afterProcess = sourceReplace(source);
47  validateUISyntax(source, afterProcess.content, `${name}.ts`);
48  const compilerOptions = ts.readConfigFile(
49    path.resolve(__dirname, '../tsconfig.json'), ts.sys.readFile).config.compilerOptions;
50    Object.assign(compilerOptions, {
51      "sourceMap": false,
52    });
53  const result = ts.transpileModule(afterProcess.content, {
54    compilerOptions: compilerOptions,
55    fileName: `${name}.ets`,
56    transformers: { before: [processUISyntax(null, true)] }
57  });
58  componentInfo.id = 0;
59  componentCollection.customComponents.clear();
60  resetComponentCollection();
61  expect(result.outputText).eql(content.expectResult);
62}
63
64mocha.describe('compiler', () => {
65  let utPath = path.resolve(__dirname, './ut');
66  if (process.argv.includes('--partialUpdate')) {
67    partialUpdateConfig.partialUpdateMode = true;
68    utPath = path.resolve(__dirname, './utForPartialUpdate');
69  }
70  const utFiles = [];
71  readFile(utPath, utFiles);
72  utFiles.forEach((item) => {
73    const fileName = path.basename(item, '.ts');
74    mocha.it(fileName, () => {
75      expectActual(fileName, item);
76    })
77  })
78})
79
80function sourceReplace(source) {
81  let content = source;
82  const log = [];
83  content = preprocessExtend(content);
84  content = processSystemApi(content);
85  return {
86    content: content,
87    log: log
88  };
89}
90
91function processSystemApi(content) {
92  const REG_SYSTEM =
93    /import\s+(.+)\s+from\s+['"]@(system|ohos)\.(\S+)['"]|import\s+(.+)\s*=\s*require\(\s*['"]@(system|ohos)\.(\S+)['"]\s*\)/g;
94  const REG_LIB_SO =
95    /import\s+(.+)\s+from\s+['"]lib(\S+)\.so['"]|import\s+(.+)\s*=\s*require\(\s*['"]lib(\S+)\.so['"]\s*\)/g;
96  const newContent = content.replace(REG_LIB_SO, (_, item1, item2, item3, item4) => {
97    const libSoValue = item1 || item3;
98    const libSoKey = item2 || item4;
99    return `var ${libSoValue} = globalThis.requireNapi("${libSoKey}", true);`;
100  }).replace(REG_SYSTEM, (item, item1, item2, item3, item4, item5, item6, item7) => {
101    let moduleType = item2 || item5;
102    let systemKey = item3 || item6;
103    let systemValue = item1 || item4;
104    if (NATIVE_MODULE.has(`${moduleType}.${systemKey}`)) {
105      item = `var ${systemValue} = globalThis.requireNativeModule('${moduleType}.${systemKey}')`;
106    } else if (moduleType === SYSTEM_PLUGIN) {
107      item = `var ${systemValue} = isSystemplugin('${systemKey}', '${SYSTEM_PLUGIN}') ? ` +
108        `globalThis.systemplugin.${systemKey} : globalThis.requireNapi('${systemKey}')`;
109    } else if (moduleType === OHOS_PLUGIN) {
110      item = `var ${systemValue} = globalThis.requireNapi('${systemKey}') || ` +
111        `(isSystemplugin('${systemKey}', '${OHOS_PLUGIN}') ? ` +
112        `globalThis.ohosplugin.${systemKey} : isSystemplugin('${systemKey}', '${SYSTEM_PLUGIN}') ` +
113        `? globalThis.systemplugin.${systemKey} : undefined)`;
114    }
115    return item;
116  });
117  return newContent;
118}