diff --git a/compiler/src/process_kit_import.ts b/compiler/src/process_kit_import.ts index adf6f74c8dc7068c520d5312c506bb0e53a56fdd..8a098c5dc7899434323ea6032bf28156b87406dc 100644 --- a/compiler/src/process_kit_import.ts +++ b/compiler/src/process_kit_import.ts @@ -289,6 +289,12 @@ export class KitInfo { } static processImportDecl(kitNode: ts.ImportDeclaration, symbols: Record) { + if (!kitNode.importClause) { + // e.g. import "@kit.xxx" + this.currentKitInfo = new EmptyImportKitInfo(kitNode, symbols); + return; + } + if (kitNode.importClause!.namedBindings) { const namedBindings: ts.NamedImportBindings = kitNode.importClause.namedBindings; if (ts.isNamespaceImport(namedBindings)) { @@ -332,10 +338,11 @@ export class KitInfo { this.currentKitName = kitName; // do not handle an empty import - if (ts.isImportDeclaration(kitNode) && kitNode.importClause) { + if (ts.isImportDeclaration(kitNode)) { // case 1: import { ... } from '@kit.xxx' // case 2: import * as ns from '@kit.xxx' // case 3: import defalutValue from '@kit.xxx' + // case 4: import '@kit.xxx' this.processImportDecl(kitNode, symbols); } @@ -477,6 +484,29 @@ class ImportSpecifierKitInfo extends KitInfo { } } +class EmptyImportKitInfo extends KitInfo { + constructor(kitNode: ts.ImportDeclaration, symbols: Record) { + super(kitNode, symbols); + + /* + * Side-effect import can not be used by Kit since Kit actually has no spcific implementation. + * In general, a Kit may be imported in a Side-effect import statement by mistake. So we + * illustrate explicitly that Kit can not in Side-effect import to avoid misunderstanding + * of runtime's behavior. + */ + kitTransformLog.errors.push({ + type: LogType.ERROR, + message: `Can not use empty import(side-effect import) statement with Kit ` + + `'${(kitNode.moduleSpecifier as ts.StringLiteral).text.replace(/'|"/g, '')}', ` + + `Please specify imported symbols explicitly.`, + pos: kitNode.getStart() + }); + } + + transform(): void { + } +} + class ExportSpecifierKitInfo extends KitInfo { private namedBindings: ts.ExportSpecifier[] = []; diff --git a/compiler/test/ark_compiler_ut/common/process_kit_import.test.ts b/compiler/test/ark_compiler_ut/common/process_kit_import.test.ts index bdcdc07813b8d4b405464e25330622a5db721ede..3d24075b851844585e57229c750a5627f0efaadf 100644 --- a/compiler/test/ark_compiler_ut/common/process_kit_import.test.ts +++ b/compiler/test/ark_compiler_ut/common/process_kit_import.test.ts @@ -102,6 +102,11 @@ const KIT_USED_VALUE_IMPROT_CODE_EXPECT: string = 'appAccount.createAppAccountManager();\n' + '//# sourceMappingURL=kitTest.js.map' +const KIT_EMPTY_IMPORT_CODE: string = +'import { appAccount } from "@kit.BasicServicesKit";\n' + +'import "@kit.ArkUI";\n' + +'appAccount.createAppAccountManager();'; + const compilerOptions = ts.readConfigFile( path.resolve(__dirname, '../../../tsconfig.json'), ts.sys.readFile).config.compilerOptions; compilerOptions['moduleResolution'] = 'nodenext'; @@ -151,7 +156,6 @@ mocha.describe('process Kit Imports tests', function () { fileName: "kitTest.ts", transformers: { before: [ processKitImport() ] } }); - console.error(result.outputText); expect(result.outputText == KIT_USED_TYPE_IMPROT_CODE_EXPECT).to.be.true; }); @@ -170,7 +174,6 @@ mocha.describe('process Kit Imports tests', function () { fileName: "kitTest.ts", transformers: { before: [ processKitImport() ] } }); - console.error(result.outputText); expect(result.outputText == KIT_USED_VALUE_IMPROT_CODE_EXPECT).to.be.true; }); @@ -207,4 +210,17 @@ mocha.describe('process Kit Imports tests', function () { ); expect(hasError).to.be.true; }); + + mocha.it('the error message of empty import', function () { + ts.transpileModule(KIT_EMPTY_IMPORT_CODE, { + compilerOptions: compilerOptions, + fileName: "kitTest.ts", + transformers: { before: [ processKitImport() ] } + }); + const hasError = kitTransformLog.errors.some(error => + error.message.includes("Can not use empty import(side-effect import) statement with Kit '@kit.ArkUI', " + + "Please specify imported symbols explicitly.") + ); + expect(hasError).to.be.true; + }); });