• 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';
27import {
28  CommonLogger,
29  LogData,
30  LogDataFactory
31} from './logger';
32import {
33  ArkTSErrorDescription,
34  ErrorCode
35} from './error_code';
36
37export function checkIfJsImportingArkts(rollupObject: Object, moduleSourceFile?: ModuleSourceFile): void {
38  if (rollupObject.share.projectConfig.singleFileEmit && moduleSourceFile) {
39    checkAndLogArkTsFileImports(rollupObject, moduleSourceFile);
40  } else {
41    ModuleSourceFile.getSourceFiles().forEach((sourceFile: ModuleSourceFile) => {
42      checkAndLogArkTsFileImports(rollupObject, sourceFile);
43    });
44  }
45}
46
47function checkAndLogArkTsFileImports(rollupObject: Object, sourceFile: ModuleSourceFile): void {
48  const id: string = sourceFile.getModuleId();
49  const unixId: string = toUnixPath(id);
50  const logger: CommonLogger = CommonLogger.getInstance(rollupObject);
51  if (isJsSourceFile(id) && unixId.indexOf('/oh_modules/') === -1) {
52    const importMap = rollupObject.getModuleInfo(id).importedIdMaps;
53    Object.values(importMap).forEach((requestFile: string) => {
54      logIfFileEndsWithArkts(requestFile, id, logger);
55    }
56    );
57  }
58}
59
60function logIfFileEndsWithArkts(requestFile: string, id: string, logger: CommonLogger): void {
61  if (requestFile.endsWith(EXTNAME_ETS) || requestFile.endsWith(EXTNAME_D_ETS)) {
62    if (compilerOptions.isCompatibleVersion) {
63      const warnMsg: string = `ArkTS:WARN File: ${id}\n` +
64        `Importing ArkTS files in JS and TS files is about to be forbidden.\n`;
65      logger.warn(yellow + warnMsg);
66    } else {
67      const errInfo: LogData = LogDataFactory.newInstance(
68        ErrorCode.ETS2BUNDLE_EXTERNAL_FORBIDDEN_IMPORT_ARKTS_FILE,
69        ArkTSErrorDescription,
70        'Importing ArkTS files in JS and TS files is forbidden.',
71        '',
72        [`Please remove the import statement of the ArkTS file in ${id}.`]
73      );
74      logger.printError(errInfo);
75    }
76  }
77}