From c570e4e7633380dfc71d6466ce3ea4b847e5a9ac Mon Sep 17 00:00:00 2001 From: sniperc96 Date: Fri, 8 Aug 2025 09:22:47 +0800 Subject: [PATCH] fix bug of import Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICRZ5Z Signed-off-by: sniperc96 --- .../linter/src/lib/autofixes/Autofixer.ts | 27 +++++++--- .../src/lib/utils/consts/ArkuiConstants.ts | 2 + .../linter/test/main/interface_import_6.ets | 24 +++++++++ .../main/interface_import_6.ets.args.json | 21 ++++++++ .../main/interface_import_6.ets.arkts2.json | 38 ++++++++++++++ .../main/interface_import_6.ets.autofix.json | 49 +++++++++++++++++++ .../test/main/interface_import_6.ets.json | 28 +++++++++++ .../main/interface_import_6.ets.migrate.ets | 26 ++++++++++ .../main/interface_import_6.ets.migrate.json | 38 ++++++++++++++ 9 files changed, 246 insertions(+), 7 deletions(-) create mode 100644 ets2panda/linter/test/main/interface_import_6.ets create mode 100644 ets2panda/linter/test/main/interface_import_6.ets.args.json create mode 100644 ets2panda/linter/test/main/interface_import_6.ets.arkts2.json create mode 100644 ets2panda/linter/test/main/interface_import_6.ets.autofix.json create mode 100644 ets2panda/linter/test/main/interface_import_6.ets.json create mode 100644 ets2panda/linter/test/main/interface_import_6.ets.migrate.ets create mode 100644 ets2panda/linter/test/main/interface_import_6.ets.migrate.json diff --git a/ets2panda/linter/src/lib/autofixes/Autofixer.ts b/ets2panda/linter/src/lib/autofixes/Autofixer.ts index 3789d7c2b9..db74cffd60 100644 --- a/ets2panda/linter/src/lib/autofixes/Autofixer.ts +++ b/ets2panda/linter/src/lib/autofixes/Autofixer.ts @@ -42,7 +42,8 @@ import { PROVIDE_ALLOW_OVERRIDE_PROPERTY_NAME, NEW_PROP_DECORATOR_SUFFIX, VIRTUAL_SCROLL_IDENTIFIER, - DISABLE_VIRTUAL_SCROLL_IDENTIFIER + DISABLE_VIRTUAL_SCROLL_IDENTIFIER, + USE_STATIC_STATEMENT } from '../utils/consts/ArkuiConstants'; import { ES_VALUE } from '../utils/consts/ESObject'; import type { IncrementDecrementNodeInfo } from '../utils/consts/InteropAPI'; @@ -3693,7 +3694,7 @@ export class Autofixer { fixInterfaceImport( interfacesNeedToImport: Set, interfacesAlreadyImported: Set, - sourceFile: ts.SourceFile + file: ts.SourceFile ): Autofix[] { const importSpecifiers: ts.ImportSpecifier[] = []; interfacesNeedToImport.forEach((interfaceName) => { @@ -3710,25 +3711,33 @@ export class Autofixer { undefined ); - const leadingComments = ts.getLeadingCommentRanges(sourceFile.getFullText(), 0); + const leadingComments = ts.getLeadingCommentRanges(file.getFullText(), 0); let annotationEndLine = 0; let annotationEndPos = 0; if (leadingComments && leadingComments.length > 0) { annotationEndPos = leadingComments[leadingComments.length - 1].end; - annotationEndLine = sourceFile.getLineAndCharacterOfPosition(annotationEndPos).line; + annotationEndLine = file.getLineAndCharacterOfPosition(annotationEndPos).line; } + const stmt = file?.statements[0]; + const isUseStaticAtStart = stmt ? Autofixer.checkUseStaticAtStart(stmt) : false; + annotationEndLine = isUseStaticAtStart ? file.getLineAndCharacterOfPosition(stmt.end).line : annotationEndLine; + annotationEndPos = isUseStaticAtStart ? stmt.end : annotationEndPos; + let text = Autofixer.formatImportStatement( - this.printer.printNode(ts.EmitHint.Unspecified, importDeclaration, sourceFile) + this.printer.printNode(ts.EmitHint.Unspecified, importDeclaration, file) ); if (annotationEndPos !== 0) { - text = this.getNewLine() + this.getNewLine() + text; + text = this.getNewLine() + (isUseStaticAtStart ? '' : this.getNewLine()) + text; } - const codeStartLine = sourceFile.getLineAndCharacterOfPosition(sourceFile.getStart()).line; + const codeStartLine = isUseStaticAtStart ? + annotationEndLine + 1 : + file.getLineAndCharacterOfPosition(file.getStart()).line; for (let i = 2; i > codeStartLine - annotationEndLine; i--) { text = text + this.getNewLine(); } + return [{ start: annotationEndPos, end: annotationEndPos, replacementText: text }]; } @@ -3750,6 +3759,10 @@ export class Autofixer { }); } + private static checkUseStaticAtStart(stmt: ts.Statement): boolean { + return stmt.getText().trim().replace(/^'|'$/g, '').endsWith(USE_STATIC_STATEMENT); + } + fixStylesDecoratorGlobal( funcDecl: ts.FunctionDeclaration, calls: ts.Identifier[], diff --git a/ets2panda/linter/src/lib/utils/consts/ArkuiConstants.ts b/ets2panda/linter/src/lib/utils/consts/ArkuiConstants.ts index 40063205da..b21cd87951 100644 --- a/ets2panda/linter/src/lib/utils/consts/ArkuiConstants.ts +++ b/ets2panda/linter/src/lib/utils/consts/ArkuiConstants.ts @@ -104,3 +104,5 @@ export const STATE_MANAGEMENT_MODULE = '@ohos.arkui.StateManagement'; export const BUILDERNODE_D_TS = 'BuilderNode.d.ts'; export const NESTING_BUILDER_SUPPORTED = 'nestingBuilderSupported'; + +export const USE_STATIC_STATEMENT = 'use static'; diff --git a/ets2panda/linter/test/main/interface_import_6.ets b/ets2panda/linter/test/main/interface_import_6.ets new file mode 100644 index 0000000000..881ce6f547 --- /dev/null +++ b/ets2panda/linter/test/main/interface_import_6.ets @@ -0,0 +1,24 @@ +/* + * 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. + */ + +'use static' +import { Component } from '@kit.ArkUI'; + +@Entry +@Component +struct Index { + build() { + } +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/interface_import_6.ets.args.json b/ets2panda/linter/test/main/interface_import_6.ets.args.json new file mode 100644 index 0000000000..ef3938e967 --- /dev/null +++ b/ets2panda/linter/test/main/interface_import_6.ets.args.json @@ -0,0 +1,21 @@ +{ + "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." + ], + "mode": { + "arkts2": "", + "autofix": "--arkts-2", + "migrate": "--arkts-2" + } +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/interface_import_6.ets.arkts2.json b/ets2panda/linter/test/main/interface_import_6.ets.arkts2.json new file mode 100644 index 0000000000..f1ac486b52 --- /dev/null +++ b/ets2panda/linter/test/main/interface_import_6.ets.arkts2.json @@ -0,0 +1,38 @@ +{ + "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": 17, + "column": 1, + "endLine": 17, + "endColumn": 40, + "problem": "ImportAfterStatement", + "suggest": "", + "rule": "\"import\" statements after other statements are not allowed (arkts-no-misplaced-imports)", + "severity": "ERROR" + }, + { + "line": 19, + "column": 2, + "endLine": 19, + "endColumn": 7, + "problem": "UIInterfaceImport", + "suggest": "", + "rule": "The ArkUI interface \"Entry\" should be imported before it is used (arkui-modular-interface)", + "severity": "ERROR" + } + ] +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/interface_import_6.ets.autofix.json b/ets2panda/linter/test/main/interface_import_6.ets.autofix.json new file mode 100644 index 0000000000..2b63a553ae --- /dev/null +++ b/ets2panda/linter/test/main/interface_import_6.ets.autofix.json @@ -0,0 +1,49 @@ +{ + "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": 17, + "column": 1, + "endLine": 17, + "endColumn": 40, + "problem": "ImportAfterStatement", + "suggest": "", + "rule": "\"import\" statements after other statements are not allowed (arkts-no-misplaced-imports)", + "severity": "ERROR" + }, + { + "line": 19, + "column": 2, + "endLine": 19, + "endColumn": 7, + "problem": "UIInterfaceImport", + "autofix": [ + { + "start": 617, + "end": 617, + "replacementText": "\nimport { Entry } from '@kit.ArkUI';\n", + "line": 19, + "column": 2, + "endLine": 19, + "endColumn": 7 + } + ], + "suggest": "", + "rule": "The ArkUI interface \"Entry\" should be imported before it is used (arkui-modular-interface)", + "severity": "ERROR" + } + ] +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/interface_import_6.ets.json b/ets2panda/linter/test/main/interface_import_6.ets.json new file mode 100644 index 0000000000..0cdb08cc16 --- /dev/null +++ b/ets2panda/linter/test/main/interface_import_6.ets.json @@ -0,0 +1,28 @@ +{ + "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": 17, + "column": 1, + "endLine": 17, + "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/interface_import_6.ets.migrate.ets b/ets2panda/linter/test/main/interface_import_6.ets.migrate.ets new file mode 100644 index 0000000000..12599d4583 --- /dev/null +++ b/ets2panda/linter/test/main/interface_import_6.ets.migrate.ets @@ -0,0 +1,26 @@ +/* + * 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. + */ + +'use static' +import { Entry } from '@kit.ArkUI'; + +import { Component } from '@kit.ArkUI'; + +@Entry +@Component +struct Index { + build() { + } +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/interface_import_6.ets.migrate.json b/ets2panda/linter/test/main/interface_import_6.ets.migrate.json new file mode 100644 index 0000000000..93115465bb --- /dev/null +++ b/ets2panda/linter/test/main/interface_import_6.ets.migrate.json @@ -0,0 +1,38 @@ +{ + "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": 17, + "column": 1, + "endLine": 17, + "endColumn": 36, + "problem": "ImportAfterStatement", + "suggest": "", + "rule": "\"import\" statements after other statements are not allowed (arkts-no-misplaced-imports)", + "severity": "ERROR" + }, + { + "line": 19, + "column": 1, + "endLine": 19, + "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