From 7e768cc15594d848b940fd4197f5b4f8562e9514 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=92=9F=E6=9F=A0?= Date: Tue, 29 Jul 2025 15:30:02 +0800 Subject: [PATCH] fix issue for arkts-identifiers-as-prop-names MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICPGY3 Test scenarios: arkts-identifiers-as-prop-names Signed-off-by: 钟柠 --- .../linter/src/lib/autofixes/Autofixer.ts | 6 +- ets2panda/linter/src/lib/utils/TsUtils.ts | 7 - .../test/main/literals_as_prop_names.ets | 12 +- .../literals_as_prop_names.ets.arkts2.json | 20 ++ .../literals_as_prop_names.ets.autofix.json | 178 ++++++++++++++++++ .../literals_as_prop_names.ets.migrate.ets | 24 ++- .../literals_as_prop_names.ets.migrate.json | 70 +------ .../main/numeric_semantics2.ets.autofix.json | 51 +++++ .../main/numeric_semantics2.ets.migrate.ets | 6 +- .../main/numeric_semantics2.ets.migrate.json | 30 --- 10 files changed, 294 insertions(+), 110 deletions(-) diff --git a/ets2panda/linter/src/lib/autofixes/Autofixer.ts b/ets2panda/linter/src/lib/autofixes/Autofixer.ts index 9d3a0c1732..b2d74401d3 100644 --- a/ets2panda/linter/src/lib/autofixes/Autofixer.ts +++ b/ets2panda/linter/src/lib/autofixes/Autofixer.ts @@ -1033,8 +1033,10 @@ export class Autofixer { if (this.renameSymbolAsIdentifierCache.has(symbol)) { return this.renameSymbolAsIdentifierCache.get(symbol); } - - if (!TsUtils.isPropertyOfInternalClassOrInterface(symbol)) { + if ( + !TsUtils.isPropertyOfInternalClassOrInterface(symbol) && + !(enumMember?.parent && ts.isEnumDeclaration(enumMember.parent)) + ) { this.renameSymbolAsIdentifierCache.set(symbol, undefined); return undefined; } diff --git a/ets2panda/linter/src/lib/utils/TsUtils.ts b/ets2panda/linter/src/lib/utils/TsUtils.ts index 738928269d..318d5b22a7 100644 --- a/ets2panda/linter/src/lib/utils/TsUtils.ts +++ b/ets2panda/linter/src/lib/utils/TsUtils.ts @@ -2914,13 +2914,6 @@ export class TsUtils { if (TsUtils.hasModifier(ts.getModifiers(propDecl.parent), ts.SyntaxKind.ExportKeyword)) { return false; } - - if ( - ts.isStringLiteral(propDecl.name) && - (ts.isClassDeclaration(propDecl.parent) || ts.isInterfaceDeclaration(propDecl.parent)) - ) { - return false; - } } return true; diff --git a/ets2panda/linter/test/main/literals_as_prop_names.ets b/ets2panda/linter/test/main/literals_as_prop_names.ets index 0f2781e90b..e0fd0882c2 100755 --- a/ets2panda/linter/test/main/literals_as_prop_names.ets +++ b/ets2panda/linter/test/main/literals_as_prop_names.ets @@ -162,4 +162,14 @@ enum TEST2 { let de3 = TEST2[TEST.A] //error let de4 = TEST2[TEST.A| TEST.B] //error -let de5 = TEST[TEST.C] //ok \ No newline at end of file +let de5 = TEST[TEST.C] //ok + +export enum DeviceTypes12 { + PHONE = 'phone', + TABLET = 'tablet', + "2IN1" = '2in1', +} + +export class A{ + public static a= DeviceTypes12["2IN1"] +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/literals_as_prop_names.ets.arkts2.json b/ets2panda/linter/test/main/literals_as_prop_names.ets.arkts2.json index 25a3ca26f6..d7d3490723 100644 --- a/ets2panda/linter/test/main/literals_as_prop_names.ets.arkts2.json +++ b/ets2panda/linter/test/main/literals_as_prop_names.ets.arkts2.json @@ -754,6 +754,26 @@ "rule": "Enum cannot get member name by member value (arkts-enum-no-props-by-index)", "severity": "ERROR" }, + { + "line": 170, + "column": 3, + "endLine": 170, + "endColumn": 18, + "problem": "LiteralAsPropertyName", + "suggest": "", + "rule": "Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)", + "severity": "ERROR" + }, + { + "line": 174, + "column": 20, + "endLine": 174, + "endColumn": 41, + "problem": "UnsupportPropNameFromValue", + "suggest": "", + "rule": "Enum cannot get member name by member value (arkts-enum-no-props-by-index)", + "severity": "ERROR" + }, { "line": 115, "column": 2, diff --git a/ets2panda/linter/test/main/literals_as_prop_names.ets.autofix.json b/ets2panda/linter/test/main/literals_as_prop_names.ets.autofix.json index 127be23a53..bbe794fdbd 100644 --- a/ets2panda/linter/test/main/literals_as_prop_names.ets.autofix.json +++ b/ets2panda/linter/test/main/literals_as_prop_names.ets.autofix.json @@ -89,6 +89,35 @@ "endLine": 27, "endColumn": 8, "problem": "LiteralAsPropertyName", + "autofix": [ + { + "replacementText": "Two", + "start": 849, + "end": 854, + "line": 38, + "column": 13, + "endLine": 38, + "endColumn": 33 + }, + { + "replacementText": "Two", + "start": 940, + "end": 945, + "line": 38, + "column": 13, + "endLine": 38, + "endColumn": 33 + }, + { + "replacementText": "litAsPropName.Two", + "start": 1032, + "end": 1052, + "line": 38, + "column": 13, + "endLine": 38, + "endColumn": 33 + } + ], "suggest": "", "rule": "Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)", "severity": "ERROR" @@ -138,6 +167,35 @@ "endLine": 33, "endColumn": 8, "problem": "LiteralAsPropertyName", + "autofix": [ + { + "replacementText": "Two", + "start": 849, + "end": 854, + "line": 38, + "column": 13, + "endLine": 38, + "endColumn": 33 + }, + { + "replacementText": "Two", + "start": 940, + "end": 945, + "line": 38, + "column": 13, + "endLine": 38, + "endColumn": 33 + }, + { + "replacementText": "litAsPropName.Two", + "start": 1032, + "end": 1052, + "line": 38, + "column": 13, + "endLine": 38, + "endColumn": 33 + } + ], "suggest": "", "rule": "Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)", "severity": "ERROR" @@ -229,6 +287,35 @@ "endLine": 38, "endColumn": 33, "problem": "PropertyAccessByIndex", + "autofix": [ + { + "replacementText": "Two", + "start": 849, + "end": 854, + "line": 38, + "column": 13, + "endLine": 38, + "endColumn": 33 + }, + { + "replacementText": "Two", + "start": 940, + "end": 945, + "line": 38, + "column": 13, + "endLine": 38, + "endColumn": 33 + }, + { + "replacementText": "litAsPropName.Two", + "start": 1032, + "end": 1052, + "line": 38, + "column": 13, + "endLine": 38, + "endColumn": 33 + } + ], "suggest": "", "rule": "Indexed access is not supported for fields (arkts-no-props-by-index)", "severity": "ERROR" @@ -454,6 +541,26 @@ "endLine": 76, "endColumn": 8, "problem": "LiteralAsPropertyName", + "autofix": [ + { + "replacementText": "__2", + "start": 1739, + "end": 1744, + "line": 81, + "column": 3, + "endLine": 81, + "endColumn": 8 + }, + { + "replacementText": "__2", + "start": 1824, + "end": 1829, + "line": 81, + "column": 3, + "endLine": 81, + "endColumn": 8 + } + ], "suggest": "", "rule": "Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)", "severity": "ERROR" @@ -504,6 +611,26 @@ "endLine": 81, "endColumn": 8, "problem": "LiteralAsPropertyName", + "autofix": [ + { + "replacementText": "__2", + "start": 1739, + "end": 1744, + "line": 81, + "column": 3, + "endLine": 81, + "endColumn": 8 + }, + { + "replacementText": "__2", + "start": 1824, + "end": 1829, + "line": 81, + "column": 3, + "endLine": 81, + "endColumn": 8 + } + ], "suggest": "", "rule": "Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)", "severity": "ERROR" @@ -965,6 +1092,17 @@ "endLine": 132, "endColumn": 15, "problem": "LiteralAsPropertyName", + "autofix": [ + { + "replacementText": "age", + "start": 2466, + "end": 2471, + "line": 132, + "column": 10, + "endLine": 132, + "endColumn": 15 + } + ], "suggest": "", "rule": "Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)", "severity": "ERROR" @@ -1278,6 +1416,46 @@ "rule": "Enum cannot get member name by member value (arkts-enum-no-props-by-index)", "severity": "ERROR" }, + { + "line": 170, + "column": 3, + "endLine": 170, + "endColumn": 18, + "problem": "LiteralAsPropertyName", + "autofix": [ + { + "replacementText": "__2IN1", + "start": 3173, + "end": 3179, + "line": 170, + "column": 3, + "endLine": 170, + "endColumn": 18 + }, + { + "replacementText": "DeviceTypes12.__2IN1", + "start": 3228, + "end": 3249, + "line": 170, + "column": 3, + "endLine": 170, + "endColumn": 18 + } + ], + "suggest": "", + "rule": "Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)", + "severity": "ERROR" + }, + { + "line": 174, + "column": 20, + "endLine": 174, + "endColumn": 41, + "problem": "UnsupportPropNameFromValue", + "suggest": "", + "rule": "Enum cannot get member name by member value (arkts-enum-no-props-by-index)", + "severity": "ERROR" + }, { "line": 115, "column": 2, diff --git a/ets2panda/linter/test/main/literals_as_prop_names.ets.migrate.ets b/ets2panda/linter/test/main/literals_as_prop_names.ets.migrate.ets index 0cbab448d6..09312e6f0f 100644 --- a/ets2panda/linter/test/main/literals_as_prop_names.ets.migrate.ets +++ b/ets2panda/linter/test/main/literals_as_prop_names.ets.migrate.ets @@ -29,18 +29,18 @@ enum LiteralAsPropertyNameEnum { class LiteralAsPropertyName { public one: string = "1111111111"; private __2: string; - 'Two': number; + Two: number; } const litAsPropName: LiteralAsPropertyName = { one: "1", __2: 'two', - 'Two': 2.0, + Two: 2.0, }; console.log(litAsPropName.one); console.log(litAsPropName.__2); -console.log(litAsPropName["Two"]); +console.log(litAsPropName.Two); class LiteralAsPropertyName_fix { public one: string = "1111111111"; @@ -82,12 +82,12 @@ console.log(x_fix._2); interface litAsPropNameIface { one: string; ___2: string; - '__2': number; + __2: number; } const int: litAsPropNameIface = { one: '12321', ___2: 'weqwewq', - '__2': 123.0 + __2: 123.0 }; const imp: ExportLitAsPropName = { 1: 234.0 }; @@ -138,7 +138,7 @@ let a:A = { age: 30.0} class B { - public 'age': number = 1.0 // error in arkts2 + public age: number = 1.0 // error in arkts2 } let obj11: Record = { @@ -171,4 +171,14 @@ enum TEST2 { let de3 = TEST2[TEST.A] //error let de4 = TEST2[TEST.A| TEST.B] //error -let de5 = TEST[TEST.C] //ok \ No newline at end of file +let de5 = TEST[TEST.C] //ok + +export enum DeviceTypes12 { + PHONE = 'phone', + TABLET = 'tablet', + __2IN1 = '2in1', +} + +export class A{ + public static a= DeviceTypes12.__2IN1 +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/literals_as_prop_names.ets.migrate.json b/ets2panda/linter/test/main/literals_as_prop_names.ets.migrate.json index 3b5d627860..4f163766c4 100644 --- a/ets2panda/linter/test/main/literals_as_prop_names.ets.migrate.json +++ b/ets2panda/linter/test/main/literals_as_prop_names.ets.migrate.json @@ -14,36 +14,6 @@ "limitations under the License." ], "result": [ - { - "line": 32, - "column": 3, - "endLine": 32, - "endColumn": 8, - "problem": "LiteralAsPropertyName", - "suggest": "", - "rule": "Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)", - "severity": "ERROR" - }, - { - "line": 38, - "column": 3, - "endLine": 38, - "endColumn": 8, - "problem": "LiteralAsPropertyName", - "suggest": "", - "rule": "Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)", - "severity": "ERROR" - }, - { - "line": 43, - "column": 13, - "endLine": 43, - "endColumn": 33, - "problem": "PropertyAccessByIndex", - "suggest": "", - "rule": "Indexed access is not supported for fields (arkts-no-props-by-index)", - "severity": "ERROR" - }, { "line": 62, "column": 9, @@ -94,16 +64,6 @@ "rule": "Indexed access is not supported for fields (arkts-no-props-by-index)", "severity": "ERROR" }, - { - "line": 85, - "column": 3, - "endLine": 85, - "endColumn": 8, - "problem": "LiteralAsPropertyName", - "suggest": "", - "rule": "Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)", - "severity": "ERROR" - }, { "line": 87, "column": 7, @@ -114,16 +74,6 @@ "rule": "This keyword cannot be used as identifiers (arkts-invalid-identifier)", "severity": "ERROR" }, - { - "line": 90, - "column": 3, - "endLine": 90, - "endColumn": 8, - "problem": "LiteralAsPropertyName", - "suggest": "", - "rule": "Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)", - "severity": "ERROR" - }, { "line": 93, "column": 36, @@ -214,16 +164,6 @@ "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", "severity": "ERROR" }, - { - "line": 141, - "column": 10, - "endLine": 141, - "endColumn": 15, - "problem": "LiteralAsPropertyName", - "suggest": "", - "rule": "Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)", - "severity": "ERROR" - }, { "line": 145, "column": 3, @@ -354,6 +294,16 @@ "rule": "Property '__2' has no initializer and is not definitely assigned in the constructor.", "severity": "ERROR" }, + { + "line": 32, + "column": 3, + "endLine": 32, + "endColumn": 6, + "problem": "StrictDiagnostic", + "suggest": "Property 'Two' has no initializer and is not definitely assigned in the constructor.", + "rule": "Property 'Two' has no initializer and is not definitely assigned in the constructor.", + "severity": "ERROR" + }, { "line": 47, "column": 11, diff --git a/ets2panda/linter/test/main/numeric_semantics2.ets.autofix.json b/ets2panda/linter/test/main/numeric_semantics2.ets.autofix.json index f4e1b08a21..6b343da07c 100644 --- a/ets2panda/linter/test/main/numeric_semantics2.ets.autofix.json +++ b/ets2panda/linter/test/main/numeric_semantics2.ets.autofix.json @@ -207,6 +207,26 @@ "endLine": 35, "endColumn": 26, "problem": "LiteralAsPropertyName", + "autofix": [ + { + "replacementText": "name", + "start": 1117, + "end": 1123, + "line": 37, + "column": 18, + "endLine": 37, + "endColumn": 24 + }, + { + "replacementText": "name", + "start": 1210, + "end": 1216, + "line": 37, + "column": 18, + "endLine": 37, + "endColumn": 24 + } + ], "suggest": "", "rule": "Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)", "severity": "ERROR" @@ -256,6 +276,17 @@ "endLine": 36, "endColumn": 26, "problem": "LiteralAsPropertyName", + "autofix": [ + { + "replacementText": "name", + "start": 1164, + "end": 1170, + "line": 36, + "column": 20, + "endLine": 36, + "endColumn": 26 + } + ], "suggest": "", "rule": "Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)", "severity": "ERROR" @@ -266,6 +297,26 @@ "endLine": 37, "endColumn": 24, "problem": "LiteralAsPropertyName", + "autofix": [ + { + "replacementText": "name", + "start": 1117, + "end": 1123, + "line": 37, + "column": 18, + "endLine": 37, + "endColumn": 24 + }, + { + "replacementText": "name", + "start": 1210, + "end": 1216, + "line": 37, + "column": 18, + "endLine": 37, + "endColumn": 24 + } + ], "suggest": "", "rule": "Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)", "severity": "ERROR" diff --git a/ets2panda/linter/test/main/numeric_semantics2.ets.migrate.ets b/ets2panda/linter/test/main/numeric_semantics2.ets.migrate.ets index 81211a821a..a687cd7007 100644 --- a/ets2panda/linter/test/main/numeric_semantics2.ets.migrate.ets +++ b/ets2panda/linter/test/main/numeric_semantics2.ets.migrate.ets @@ -32,9 +32,9 @@ namespace NumericSemanticsDone { } namespace NoNumericSemantics { - interface X1 { "name": number, __2: number} - interface X2 { "name": number, _2: number} - let x: X1 = {"name": 20.0, __2: 30.0} // OK + interface X1 { name: number, __2: number} + interface X2 { name: number, _2: number} + let x: X1 = {name: 20.0, __2: 30.0} // OK console.log(x.__2); // OK let x_fix: X2 = {name: 20.0, _2: 20.0}; diff --git a/ets2panda/linter/test/main/numeric_semantics2.ets.migrate.json b/ets2panda/linter/test/main/numeric_semantics2.ets.migrate.json index 3f86f1a88b..d7fd5e18c9 100644 --- a/ets2panda/linter/test/main/numeric_semantics2.ets.migrate.json +++ b/ets2panda/linter/test/main/numeric_semantics2.ets.migrate.json @@ -54,36 +54,6 @@ "rule": "Non-declaration statements in namespaces are not supported (single semicolons are considered as empty non-declaration statements) (arkts-no-ns-statements)", "severity": "ERROR" }, - { - "line": 35, - "column": 20, - "endLine": 35, - "endColumn": 26, - "problem": "LiteralAsPropertyName", - "suggest": "", - "rule": "Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)", - "severity": "ERROR" - }, - { - "line": 36, - "column": 20, - "endLine": 36, - "endColumn": 26, - "problem": "LiteralAsPropertyName", - "suggest": "", - "rule": "Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)", - "severity": "ERROR" - }, - { - "line": 37, - "column": 18, - "endLine": 37, - "endColumn": 24, - "problem": "LiteralAsPropertyName", - "suggest": "", - "rule": "Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)", - "severity": "ERROR" - }, { "line": 42, "column": 17, -- Gitee