• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 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 { ModuleSourceFile } from './module/module_source_file';
17import { isJsSourceFile } from './utils';
18import { toUnixPath } from '../../utils';
19import { compilerOptions } from '../../ets_checker';
20import {
21  EXTNAME_D_ETS,
22  EXTNAME_ETS,
23  GEN_ABC_PLUGIN_NAME,
24  red,
25  yellow
26} from './common/ark_define';
27
28export function checkIfJsImportingArkts(rollupObject: Object, moduleSourceFile?: ModuleSourceFile): void {
29  if (rollupObject.share.projectConfig.singleFileEmit && moduleSourceFile) {
30    checkAndLogArkTsFileImports(rollupObject, moduleSourceFile);
31  } else {
32    ModuleSourceFile.getSourceFiles().forEach((sourceFile: ModuleSourceFile) => {
33      checkAndLogArkTsFileImports(rollupObject, sourceFile);
34    });
35  }
36}
37
38function checkAndLogArkTsFileImports(rollupObject: Object, sourceFile: ModuleSourceFile): void {
39  const id: string = sourceFile.getModuleId();
40  const unixId: string = toUnixPath(id);
41  if (isJsSourceFile(id) && unixId.indexOf('/oh_modules/') === -1) {
42    const importMap = rollupObject.getModuleInfo(id).importedIdMaps;
43    Object.values(importMap).forEach((requestFile: string) => {
44      if (requestFile.endsWith(EXTNAME_ETS) || requestFile.endsWith(EXTNAME_D_ETS)) {
45        const errorMsg: string = compilerOptions.isCompatibleVersion ?
46          `ArkTS:WARN File: ${id}\n` +
47          `Importing ArkTS files in JS and TS files is about to be forbidden.\n` :
48          `ArkTS:ERROR ArkTS:ERROR File: ${id}\n` +
49          `Importing ArkTS files in JS and TS files is forbidden.\n`;
50        const logger: Object = rollupObject.share.getLogger(GEN_ABC_PLUGIN_NAME);
51        compilerOptions.isCompatibleVersion ? logger.warn(yellow + errorMsg) : logger.error(red + errorMsg);
52      }
53    }
54    );
55  }
56}