diff --git a/ets2panda/linter/src/lib/TypeScriptLinter.ts b/ets2panda/linter/src/lib/TypeScriptLinter.ts index 90024f48809f9e480207510eb1298cec7a954b93..125ea7bb60cfa85be1f3d9c528afc6e8c6e3783f 100644 --- a/ets2panda/linter/src/lib/TypeScriptLinter.ts +++ b/ets2panda/linter/src/lib/TypeScriptLinter.ts @@ -1188,7 +1188,8 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { if (this.options.arkts2) { const importClause = importDeclNode.importClause; if (!importClause || !importClause.name && !importClause.namedBindings) { - this.incrementCounters(node, FaultID.NoSideEffectImport); + const autofix = this.autofixer?.fixSideEffectImport(importDeclNode); + this.incrementCounters(node, FaultID.NoSideEffectImport, autofix); } else { this.updateDataSdkJsonInfo(importDeclNode, importClause); } diff --git a/ets2panda/linter/src/lib/autofixes/Autofixer.ts b/ets2panda/linter/src/lib/autofixes/Autofixer.ts index dd88357f643f6c820f955aef75d578eac5fdf105..52855144625fada6b2e2aa01d860716ed6663a0d 100644 --- a/ets2panda/linter/src/lib/autofixes/Autofixer.ts +++ b/ets2panda/linter/src/lib/autofixes/Autofixer.ts @@ -5440,4 +5440,24 @@ export class Autofixer { return undefined; } + + fixSideEffectImport(importDeclNode: ts.ImportDeclaration): Autofix[] { + const initModuleCall = ts.factory.createCallExpression( + ts.factory.createIdentifier("initModule"), + undefined, + [importDeclNode.moduleSpecifier] + ); + const expressionStatement = ts.factory.createExpressionStatement(initModuleCall); + const replacedText = this.printer.printNode( + ts.EmitHint.Unspecified, + expressionStatement, + importDeclNode.getSourceFile() + ); + + return [{ + start: importDeclNode.getStart(), + end: importDeclNode.getEnd(), + replacementText: replacedText + }]; + } } diff --git a/ets2panda/linter/test/main/no_side_effect_import.ets.args.json b/ets2panda/linter/test/main/no_side_effect_import.ets.args.json index 4d93062f69db6d74420adeb506e0ca28c5580728..a89d885810708ad03d96e3e14bb6590efd1a7547 100755 --- a/ets2panda/linter/test/main/no_side_effect_import.ets.args.json +++ b/ets2panda/linter/test/main/no_side_effect_import.ets.args.json @@ -14,6 +14,8 @@ "limitations under the License." ], "mode": { - "arkts2": "" + "arkts2": "", + "autofix": "--arkts-2", + "migrate": "--arkts-2" } } diff --git a/ets2panda/linter/test/main/no_side_effect_import.ets.autofix.json b/ets2panda/linter/test/main/no_side_effect_import.ets.autofix.json new file mode 100644 index 0000000000000000000000000000000000000000..4723887320a710cda4191b750032bd80aae8e28a --- /dev/null +++ b/ets2panda/linter/test/main/no_side_effect_import.ets.autofix.json @@ -0,0 +1,110 @@ +{ + "copyright": [ + "Copyright (c) 2025 Huawei Device Co., Ltd.", + "Licensed under the Apache License, Version 2.0 (the 'License');", + "you may not use this file except in compliance with the License.", + "You may obtain a copy of the License at", + "", + "http://www.apache.org/licenses/LICENSE-2.0", + "", + "Unless required by applicable law or agreed to in writing, software", + "distributed under the License is distributed on an 'AS IS' BASIS,", + "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.", + "See the License for the specific language governing permissions and", + "limitations under the License." + ], + "result": [ + { + "line": 16, + "column": 1, + "endLine": 16, + "endColumn": 19, + "problem": "NoSideEffectImport", + "autofix": [ + { + "start": 610, + "end": 628, + "replacementText": "initModule(\"./logger\");", + "line": 16, + "column": 1, + "endLine": 16, + "endColumn": 19 + } + ], + "suggest": "", + "rule": "Import for side-effect only is prohibited.(arkts-no-side-effect-import)", + "severity": "ERROR" + }, + { + "line": 18, + "column": 1, + "endLine": 18, + "endColumn": 23, + "problem": "NoSideEffectImport", + "autofix": [ + { + "start": 669, + "end": 691, + "replacementText": "initModule(\"./utils/init\");", + "line": 18, + "column": 1, + "endLine": 18, + "endColumn": 23 + } + ], + "suggest": "", + "rule": "Import for side-effect only is prohibited.(arkts-no-side-effect-import)", + "severity": "ERROR" + }, + { + "line": 18, + "column": 1, + "endLine": 18, + "endColumn": 23, + "problem": "ImportAfterStatement", + "suggest": "", + "rule": "\"import\" statements after other statements are not allowed (arkts-no-misplaced-imports)", + "severity": "ERROR" + }, + { + "line": 20, + "column": 1, + "endLine": 20, + "endColumn": 45, + "problem": "ImportAfterStatement", + "suggest": "", + "rule": "\"import\" statements after other statements are not allowed (arkts-no-misplaced-imports)", + "severity": "ERROR" + }, + { + "line": 21, + "column": 1, + "endLine": 21, + "endColumn": 31, + "problem": "ImportAfterStatement", + "suggest": "", + "rule": "\"import\" statements after other statements are not allowed (arkts-no-misplaced-imports)", + "severity": "ERROR" + }, + { + "line": 22, + "column": 1, + "endLine": 22, + "endColumn": 34, + "problem": "ImportAfterStatement", + "suggest": "", + "rule": "\"import\" statements after other statements are not allowed (arkts-no-misplaced-imports)", + "severity": "ERROR" + }, + { + "line": 24, + "column": 1, + "endLine": 24, + "endColumn": 40, + "problem": "ImportAfterStatement", + "suggest": "", + "rule": "\"import\" statements after other statements are not allowed (arkts-no-misplaced-imports)", + "severity": "ERROR" + } + ] +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/no_side_effect_import.ets.migrate.ets b/ets2panda/linter/test/main/no_side_effect_import.ets.migrate.ets new file mode 100644 index 0000000000000000000000000000000000000000..a70297d4caea562168c338d773124639dca8b6ce --- /dev/null +++ b/ets2panda/linter/test/main/no_side_effect_import.ets.migrate.ets @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +initModule("./logger"); +console.log("Main program running..."); +initModule("./utils/init"); + +import { cookBookTag } from './CookBookMsg'; +import Logger from './logger'; +import * as Utils from './utils'; + +import { initApp } from './utils/init'; + + diff --git a/ets2panda/linter/test/main/no_side_effect_import.ets.migrate.json b/ets2panda/linter/test/main/no_side_effect_import.ets.migrate.json new file mode 100644 index 0000000000000000000000000000000000000000..e6817505079c74e6aa9b918b489be9036ce13b40 --- /dev/null +++ b/ets2panda/linter/test/main/no_side_effect_import.ets.migrate.json @@ -0,0 +1,58 @@ +{ + "copyright": [ + "Copyright (c) 2025 Huawei Device Co., Ltd.", + "Licensed under the Apache License, Version 2.0 (the 'License');", + "you may not use this file except in compliance with the License.", + "You may obtain a copy of the License at", + "", + "http://www.apache.org/licenses/LICENSE-2.0", + "", + "Unless required by applicable law or agreed to in writing, software", + "distributed under the License is distributed on an 'AS IS' BASIS,", + "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.", + "See the License for the specific language governing permissions and", + "limitations under the License." + ], + "result": [ + { + "line": 20, + "column": 1, + "endLine": 20, + "endColumn": 45, + "problem": "ImportAfterStatement", + "suggest": "", + "rule": "\"import\" statements after other statements are not allowed (arkts-no-misplaced-imports)", + "severity": "ERROR" + }, + { + "line": 21, + "column": 1, + "endLine": 21, + "endColumn": 31, + "problem": "ImportAfterStatement", + "suggest": "", + "rule": "\"import\" statements after other statements are not allowed (arkts-no-misplaced-imports)", + "severity": "ERROR" + }, + { + "line": 22, + "column": 1, + "endLine": 22, + "endColumn": 34, + "problem": "ImportAfterStatement", + "suggest": "", + "rule": "\"import\" statements after other statements are not allowed (arkts-no-misplaced-imports)", + "severity": "ERROR" + }, + { + "line": 24, + "column": 1, + "endLine": 24, + "endColumn": 40, + "problem": "ImportAfterStatement", + "suggest": "", + "rule": "\"import\" statements after other statements are not allowed (arkts-no-misplaced-imports)", + "severity": "ERROR" + } + ] +} \ No newline at end of file