From 1922c64d0b36ff5936dc272e45c60f213f44eda6 Mon Sep 17 00:00:00 2001 From: c30058867 Date: Sun, 15 Jun 2025 19:19:24 +0800 Subject: [PATCH] Add automic repair for ason Issue:https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICF9QQ Signed-off-by: caiy --- ets2panda/linter/src/lib/TypeScriptLinter.ts | 25 ++++-- .../src/lib/utils/consts/ArkTSUtilsAPI.ts | 2 + .../linter/test/main/arktsutils_module.ets | 2 + .../main/arktsutils_module.ets.arkts2.json | 10 +++ .../main/arktsutils_module.ets.autofix.json | 76 +++++++++++++++++++ .../main/arktsutils_module.ets.migrate.ets | 14 ++-- .../main/arktsutils_module.ets.migrate.json | 56 +------------- 7 files changed, 118 insertions(+), 67 deletions(-) diff --git a/ets2panda/linter/src/lib/TypeScriptLinter.ts b/ets2panda/linter/src/lib/TypeScriptLinter.ts index 3328315b27..c43dbc874f 100644 --- a/ets2panda/linter/src/lib/TypeScriptLinter.ts +++ b/ets2panda/linter/src/lib/TypeScriptLinter.ts @@ -80,7 +80,7 @@ import { DEFAULT_DECORATOR_WHITE_LIST } from './utils/consts/DefaultDecoratorWhi import { INVALID_IDENTIFIER_KEYWORDS } from './utils/consts/InValidIndentifierKeywords'; import { WORKER_MODULES, WORKER_TEXT } from './utils/consts/WorkerAPI'; import { COLLECTIONS_TEXT, COLLECTIONS_MODULES } from './utils/consts/CollectionsAPI'; -import { ASON_TEXT, ASON_MODULES, ARKTS_UTILS_TEXT } from './utils/consts/ArkTSUtilsAPI'; +import { ASON_TEXT, ASON_MODULES, ARKTS_UTILS_TEXT, JSON_TEXT, ASON_WHITE_SET } from './utils/consts/ArkTSUtilsAPI'; import { interanlFunction } from './utils/consts/InternalFunction'; import { ETS_PART, PATH_SEPARATOR } from './utils/consts/OhmUrl'; import { @@ -7046,8 +7046,11 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { if (parent.right.text !== node.text) { return; } - this.checkAsonUsage(parent.left); - + if (ts.isQualifiedName(parent.parent) && ASON_WHITE_SET.has(parent.parent.right.text)) { + this.checkAsonUsage(parent.left, undefined); + } else { + this.checkAsonUsage(parent.left, this.autofixer); + } break; case ts.SyntaxKind.PropertyAccessExpression: if (!ts.isPropertyAccessExpression(parent)) { @@ -7056,20 +7059,25 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { if (parent.name.text !== node.text) { return; } - this.checkAsonUsage(parent.expression); - + if (ts.isPropertyAccessExpression(parent.parent) && ASON_WHITE_SET.has(parent.parent.name.text)) { + this.checkAsonUsage(parent.expression, undefined); + } else { + this.checkAsonUsage(parent.expression, this.autofixer); + } break; default: } } - private checkAsonUsage(nodeToCheck: ts.Node): void { + private checkAsonUsage(nodeToCheck: ts.Node, autofixer: Autofixer | undefined): void { if (!ts.isIdentifier(nodeToCheck)) { return; } + const declaration = this.tsUtils.getDeclarationNode(nodeToCheck); if (!declaration && nodeToCheck.text === ARKTS_UTILS_TEXT) { - this.incrementCounters(nodeToCheck, FaultID.LimitedStdLibNoASON); + const autofix = autofixer?.replaceNode(nodeToCheck.parent, JSON_TEXT); + this.incrementCounters(nodeToCheck, FaultID.LimitedStdLibNoASON, autofix); return; } @@ -7085,7 +7093,8 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { return fileName.startsWith(moduleName); }) ) { - this.incrementCounters(nodeToCheck, FaultID.LimitedStdLibNoASON); + const autofix = this.autofixer?.replaceNode(nodeToCheck.parent, JSON_TEXT); + this.incrementCounters(nodeToCheck, FaultID.LimitedStdLibNoASON, autofix); } } diff --git a/ets2panda/linter/src/lib/utils/consts/ArkTSUtilsAPI.ts b/ets2panda/linter/src/lib/utils/consts/ArkTSUtilsAPI.ts index 8218ffc511..e9ca2a1581 100644 --- a/ets2panda/linter/src/lib/utils/consts/ArkTSUtilsAPI.ts +++ b/ets2panda/linter/src/lib/utils/consts/ArkTSUtilsAPI.ts @@ -17,3 +17,5 @@ export const ASON_TEXT = 'ASON'; export const ASON_MODULES = ['@arkts.utils', '@kit.ArkTS']; export const JSON_TEXT = 'JSON'; export const ARKTS_UTILS_TEXT = 'ArkTSUtils'; + +export const ASON_WHITE_SET: Set = new Set(['ParseReturnType']); diff --git a/ets2panda/linter/test/main/arktsutils_module.ets b/ets2panda/linter/test/main/arktsutils_module.ets index 4e81dea82b..a8fbe4b65e 100644 --- a/ets2panda/linter/test/main/arktsutils_module.ets +++ b/ets2panda/linter/test/main/arktsutils_module.ets @@ -35,4 +35,6 @@ function tesCollectionsUsage() { type CreatedType = ArkTSUtils.ASON.SomeType; const someType: CreatedType = ArkTSUtils.ASON.SomeType; + + const map: number = ArkTSUtils.ASON.ParseReturnType.map; } diff --git a/ets2panda/linter/test/main/arktsutils_module.ets.arkts2.json b/ets2panda/linter/test/main/arktsutils_module.ets.arkts2.json index 6da6e6ae15..30c0ecdc72 100644 --- a/ets2panda/linter/test/main/arktsutils_module.ets.arkts2.json +++ b/ets2panda/linter/test/main/arktsutils_module.ets.arkts2.json @@ -113,6 +113,16 @@ "suggest": "", "rule": "ASON is not supported. (arkts-no-need-stdlib-ason)", "severity": "ERROR" + }, + { + "line": 39, + "column": 23, + "endLine": 39, + "endColumn": 33, + "problem": "LimitedStdLibNoASON", + "suggest": "", + "rule": "ASON is not supported. (arkts-no-need-stdlib-ason)", + "severity": "ERROR" } ] } diff --git a/ets2panda/linter/test/main/arktsutils_module.ets.autofix.json b/ets2panda/linter/test/main/arktsutils_module.ets.autofix.json index d6868a9cbf..9f6d6e0d9c 100644 --- a/ets2panda/linter/test/main/arktsutils_module.ets.autofix.json +++ b/ets2panda/linter/test/main/arktsutils_module.ets.autofix.json @@ -20,6 +20,17 @@ "endLine": 27, "endColumn": 31, "problem": "LimitedStdLibNoASON", + "autofix": [ + { + "start": 980, + "end": 990, + "replacementText": "JSON", + "line": 27, + "column": 26, + "endLine": 27, + "endColumn": 31 + } + ], "suggest": "", "rule": "ASON is not supported. (arkts-no-need-stdlib-ason)", "severity": "ERROR" @@ -51,6 +62,17 @@ "endLine": 29, "endColumn": 33, "problem": "LimitedStdLibNoASON", + "autofix": [ + { + "start": 1023, + "end": 1043, + "replacementText": "JSON", + "line": 29, + "column": 18, + "endLine": 29, + "endColumn": 33 + } + ], "suggest": "", "rule": "ASON is not supported. (arkts-no-need-stdlib-ason)", "severity": "ERROR" @@ -82,6 +104,17 @@ "endLine": 31, "endColumn": 31, "problem": "LimitedStdLibNoASON", + "autofix": [ + { + "start": 1076, + "end": 1094, + "replacementText": "JSON", + "line": 31, + "column": 18, + "endLine": 31, + "endColumn": 31 + } + ], "suggest": "", "rule": "ASON is not supported. (arkts-no-need-stdlib-ason)", "severity": "ERROR" @@ -113,6 +146,17 @@ "endLine": 33, "endColumn": 41, "problem": "LimitedStdLibNoASON", + "autofix": [ + { + "start": 1135, + "end": 1155, + "replacementText": "JSON", + "line": 33, + "column": 26, + "endLine": 33, + "endColumn": 41 + } + ], "suggest": "", "rule": "ASON is not supported. (arkts-no-need-stdlib-ason)", "severity": "ERROR" @@ -144,6 +188,17 @@ "endLine": 35, "endColumn": 32, "problem": "LimitedStdLibNoASON", + "autofix": [ + { + "start": 1192, + "end": 1207, + "replacementText": "JSON", + "line": 35, + "column": 22, + "endLine": 35, + "endColumn": 32 + } + ], "suggest": "", "rule": "ASON is not supported. (arkts-no-need-stdlib-ason)", "severity": "ERROR" @@ -154,6 +209,27 @@ "endLine": 37, "endColumn": 43, "problem": "LimitedStdLibNoASON", + "autofix": [ + { + "start": 1251, + "end": 1266, + "replacementText": "JSON", + "line": 37, + "column": 33, + "endLine": 37, + "endColumn": 43 + } + ], + "suggest": "", + "rule": "ASON is not supported. (arkts-no-need-stdlib-ason)", + "severity": "ERROR" + }, + { + "line": 39, + "column": 23, + "endLine": 39, + "endColumn": 33, + "problem": "LimitedStdLibNoASON", "suggest": "", "rule": "ASON is not supported. (arkts-no-need-stdlib-ason)", "severity": "ERROR" diff --git a/ets2panda/linter/test/main/arktsutils_module.ets.migrate.ets b/ets2panda/linter/test/main/arktsutils_module.ets.migrate.ets index 3b1d7e17b6..32dc4cb60d 100644 --- a/ets2panda/linter/test/main/arktsutils_module.ets.migrate.ets +++ b/ets2panda/linter/test/main/arktsutils_module.ets.migrate.ets @@ -24,15 +24,17 @@ export { utils } from './oh_modules/@arkts.utils'; function tesCollectionsUsage() { - const utils1: string = utils.ASON.stringify(1.0); + const utils1: string = JSON.stringify(1.0); - const utils2 = ArkTSUtilsAlias.ASON.stringify(1.0); + const utils2 = JSON.stringify(1.0); - const utils3 = kitArkTSUtils.ASON.stringify(1.0); + const utils3 = JSON.stringify(1.0); - const utils4: string = ArkTSUtilsAlias.ASON.stringify(1.0); + const utils4: string = JSON.stringify(1.0); - type CreatedType = ArkTSUtils.ASON.SomeType; + type CreatedType = JSON.SomeType; - const someType: CreatedType = ArkTSUtils.ASON.SomeType; + const someType: CreatedType = JSON.SomeType; + + const map: number = ArkTSUtils.ASON.ParseReturnType.map; } diff --git a/ets2panda/linter/test/main/arktsutils_module.ets.migrate.json b/ets2panda/linter/test/main/arktsutils_module.ets.migrate.json index 1b597eff5b..f4bff9a11a 100644 --- a/ets2panda/linter/test/main/arktsutils_module.ets.migrate.json +++ b/ets2panda/linter/test/main/arktsutils_module.ets.migrate.json @@ -15,64 +15,14 @@ ], "result": [ { - "line": 27, - "column": 26, - "endLine": 27, - "endColumn": 31, - "problem": "LimitedStdLibNoASON", - "suggest": "", - "rule": "ASON is not supported. (arkts-no-need-stdlib-ason)", - "severity": "ERROR" - }, - { - "line": 29, - "column": 18, - "endLine": 29, + "line": 39, + "column": 23, + "endLine": 39, "endColumn": 33, "problem": "LimitedStdLibNoASON", "suggest": "", "rule": "ASON is not supported. (arkts-no-need-stdlib-ason)", "severity": "ERROR" - }, - { - "line": 31, - "column": 18, - "endLine": 31, - "endColumn": 31, - "problem": "LimitedStdLibNoASON", - "suggest": "", - "rule": "ASON is not supported. (arkts-no-need-stdlib-ason)", - "severity": "ERROR" - }, - { - "line": 33, - "column": 26, - "endLine": 33, - "endColumn": 41, - "problem": "LimitedStdLibNoASON", - "suggest": "", - "rule": "ASON is not supported. (arkts-no-need-stdlib-ason)", - "severity": "ERROR" - }, - { - "line": 35, - "column": 22, - "endLine": 35, - "endColumn": 32, - "problem": "LimitedStdLibNoASON", - "suggest": "", - "rule": "ASON is not supported. (arkts-no-need-stdlib-ason)", - "severity": "ERROR" - }, - { - "line": 37, - "column": 33, - "endLine": 37, - "endColumn": 43, - "problem": "LimitedStdLibNoASON", - "suggest": "", - "rule": "ASON is not supported. (arkts-no-need-stdlib-ason)", - "severity": "ERROR" } ] } -- Gitee