From 4e2e4cdb9a550711426e898a87ef2c0299220cd1 Mon Sep 17 00:00:00 2001 From: zengyuan Date: Sun, 20 Jul 2025 20:46:06 +0800 Subject: [PATCH] Implement auto-fix for 'no-side-effect-import' Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICNINH Signed-off-by: zengyuan Change-Id: Idaac3d70033ea2702e1d2eb310615342cd9583ec --- ets2panda/linter/src/lib/TypeScriptLinter.ts | 3 +- .../linter/src/lib/autofixes/Autofixer.ts | 20 ++++ .../main/no_side_effect_import.ets.args.json | 4 +- .../no_side_effect_import.ets.autofix.json | 110 ++++++++++++++++++ .../no_side_effect_import.ets.migrate.ets | 26 +++++ .../no_side_effect_import.ets.migrate.json | 58 +++++++++ 6 files changed, 219 insertions(+), 2 deletions(-) create mode 100644 ets2panda/linter/test/main/no_side_effect_import.ets.autofix.json create mode 100644 ets2panda/linter/test/main/no_side_effect_import.ets.migrate.ets create mode 100644 ets2panda/linter/test/main/no_side_effect_import.ets.migrate.json diff --git a/ets2panda/linter/src/lib/TypeScriptLinter.ts b/ets2panda/linter/src/lib/TypeScriptLinter.ts index 90024f4880..125ea7bb60 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 dd88357f64..5285514462 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 4d93062f69..a89d885810 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 0000000000..4723887320 --- /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 0000000000..a70297d4ca --- /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 0000000000..e681750507 --- /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 -- Gitee