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): void { 29 ModuleSourceFile.getSourceFiles().forEach((sourceFile: ModuleSourceFile) => { 30 const id: string = sourceFile.getModuleId(); 31 const unixId: string = toUnixPath(id); 32 if (isJsSourceFile(id) && unixId.indexOf('/oh_modules/') === -1) { 33 const importMap = rollupObject.getModuleInfo(id).importedIdMaps; 34 Object.values(importMap).forEach((requestFile: string) => { 35 if (requestFile.endsWith(EXTNAME_ETS) || requestFile.endsWith(EXTNAME_D_ETS)) { 36 const errorMsg: string = compilerOptions.isCompatibleVersion ? 37 `ArkTS:WARN File: ${id}\n` + 38 `Importing ArkTS files in JS and TS files is about to be forbidden.\n` : 39 `ArkTS:ERROR ArkTS:ERROR File: ${id}\n` + 40 `Importing ArkTS files in JS and TS files is forbidden.\n`; 41 const logger: Object = rollupObject.share.getLogger(GEN_ABC_PLUGIN_NAME); 42 compilerOptions.isCompatibleVersion ? logger.warn(yellow + errorMsg) : logger.error(red + errorMsg); 43 } 44 } 45 ); 46 } 47 }); 48}