From 0d63d8a670698d48228b72db4fac58cdef91c187 Mon Sep 17 00:00:00 2001 From: sniperc96 Date: Sat, 7 Jun 2025 17:55:29 +0800 Subject: [PATCH] fix bug:arkui-no-bidirectional-data-binding Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICDDJO Signed-off-by: sniperc96 --- ets2panda/linter/src/lib/TypeScriptLinter.ts | 46 ++--- .../test/main/double_excla_binding_3.ets | 32 ++++ .../main/double_excla_binding_3.ets.args.json | 21 +++ .../double_excla_binding_3.ets.arkts2.json | 88 ++++++++++ .../double_excla_binding_3.ets.autofix.json | 165 ++++++++++++++++++ .../test/main/double_excla_binding_3.ets.json | 17 ++ .../double_excla_binding_3.ets.migrate.ets | 34 ++++ .../double_excla_binding_3.ets.migrate.json | 17 ++ 8 files changed, 400 insertions(+), 20 deletions(-) create mode 100644 ets2panda/linter/test/main/double_excla_binding_3.ets create mode 100644 ets2panda/linter/test/main/double_excla_binding_3.ets.args.json create mode 100644 ets2panda/linter/test/main/double_excla_binding_3.ets.arkts2.json create mode 100644 ets2panda/linter/test/main/double_excla_binding_3.ets.autofix.json create mode 100644 ets2panda/linter/test/main/double_excla_binding_3.ets.json create mode 100644 ets2panda/linter/test/main/double_excla_binding_3.ets.migrate.ets create mode 100644 ets2panda/linter/test/main/double_excla_binding_3.ets.migrate.json diff --git a/ets2panda/linter/src/lib/TypeScriptLinter.ts b/ets2panda/linter/src/lib/TypeScriptLinter.ts index ec2c5e4463..19ff79d1f8 100644 --- a/ets2panda/linter/src/lib/TypeScriptLinter.ts +++ b/ets2panda/linter/src/lib/TypeScriptLinter.ts @@ -6576,6 +6576,7 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { !ts.isNonNullExpression(node) || !ts.isNonNullExpression(node.expression) || ts.isNonNullExpression(node.parent) || + ts.isPropertyAccessExpression(node.parent) || ts.isNonNullExpression(node.expression.expression) ) { return; @@ -6583,26 +6584,7 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { const statement = ts.findAncestor(node, ts.isExpressionStatement); if (statement && this.isCustomComponent(statement)) { - let currentParam: ts.Identifier | undefined; - if (ts.isPropertyAccessExpression(node.expression.expression)) { - currentParam = node.expression.expression.name as ts.Identifier; - } - - let customParam: ts.Identifier | undefined; - if (ts.isPropertyAssignment(node.parent)) { - customParam = node.parent.name as ts.Identifier; - } - - if (!currentParam || !customParam) { - return; - } - - const originalExpr = node.parent.parent; - if (!ts.isObjectLiteralExpression(originalExpr)) { - return; - } - const autofix = this.autofixer?.fixCustomBidirectionalBinding(originalExpr, currentParam, customParam); - this.incrementCounters(node, FaultID.DoubleExclaBindingNotSupported, autofix); + this.handleCustomBidirectionalBinding(node, node.expression); } else { const autofix = this.autofixer?.fixNativeBidirectionalBinding(node, this.interfacesNeedToImport); this.incrementCounters(node, FaultID.DoubleExclaBindingNotSupported, autofix); @@ -6631,6 +6613,30 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { return this.interfacesAlreadyImported.has(callExpr.expression.getText()); } + private handleCustomBidirectionalBinding(firstExpr: ts.NonNullExpression, secondExpr: ts.NonNullExpression): void { + let currentParam: ts.Identifier | undefined; + if (ts.isPropertyAccessExpression(secondExpr.expression)) { + currentParam = secondExpr.expression.name as ts.Identifier; + } + + let customParam: ts.Identifier | undefined; + if (ts.isPropertyAssignment(firstExpr.parent)) { + customParam = firstExpr.parent.name as ts.Identifier; + } + + if (!currentParam || !customParam) { + return; + } + + const originalExpr = firstExpr.parent.parent; + if (!ts.isObjectLiteralExpression(originalExpr)) { + return; + } + + const autofix = this.autofixer?.fixCustomBidirectionalBinding(originalExpr, currentParam, customParam); + this.incrementCounters(firstExpr, FaultID.DoubleExclaBindingNotSupported, autofix); + } + private handleDoubleDollar(node: ts.Node): void { if (!this.options.arkts2) { return; diff --git a/ets2panda/linter/test/main/double_excla_binding_3.ets b/ets2panda/linter/test/main/double_excla_binding_3.ets new file mode 100644 index 0000000000..8c9dcc1e87 --- /dev/null +++ b/ets2panda/linter/test/main/double_excla_binding_3.ets @@ -0,0 +1,32 @@ +/* + * 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. + */ + +class User { + name: string = "Jack" +} + +@Entry +@Component +struct MyComponent { + @State user: User = new User() + + build() { + Row() { + Text(this.user!.name) + Text(this.user!!.name) + Text(this.user!!!.name) + } + } +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/double_excla_binding_3.ets.args.json b/ets2panda/linter/test/main/double_excla_binding_3.ets.args.json new file mode 100644 index 0000000000..30973c00a2 --- /dev/null +++ b/ets2panda/linter/test/main/double_excla_binding_3.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/double_excla_binding_3.ets.arkts2.json b/ets2panda/linter/test/main/double_excla_binding_3.ets.arkts2.json new file mode 100644 index 0000000000..592f7eb23d --- /dev/null +++ b/ets2panda/linter/test/main/double_excla_binding_3.ets.arkts2.json @@ -0,0 +1,88 @@ +{ + "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": 2, + "endLine": 20, + "endColumn": 7, + "problem": "UIInterfaceImport", + "suggest": "", + "rule": "ArkUI interface should be imported before using (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 21, + "column": 2, + "endLine": 21, + "endColumn": 11, + "problem": "UIInterfaceImport", + "suggest": "", + "rule": "ArkUI interface should be imported before using (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 23, + "column": 4, + "endLine": 23, + "endColumn": 9, + "problem": "UIInterfaceImport", + "suggest": "", + "rule": "ArkUI interface should be imported before using (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 26, + "column": 5, + "endLine": 26, + "endColumn": 8, + "problem": "UIInterfaceImport", + "suggest": "", + "rule": "ArkUI interface should be imported before using (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 27, + "column": 7, + "endLine": 27, + "endColumn": 11, + "problem": "UIInterfaceImport", + "suggest": "", + "rule": "ArkUI interface should be imported before using (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 28, + "column": 7, + "endLine": 28, + "endColumn": 11, + "problem": "UIInterfaceImport", + "suggest": "", + "rule": "ArkUI interface should be imported before using (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 29, + "column": 7, + "endLine": 29, + "endColumn": 11, + "problem": "UIInterfaceImport", + "suggest": "", + "rule": "ArkUI interface should be imported before using (arkui-modular-interface)", + "severity": "ERROR" + } + ] +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/double_excla_binding_3.ets.autofix.json b/ets2panda/linter/test/main/double_excla_binding_3.ets.autofix.json new file mode 100644 index 0000000000..31a6345741 --- /dev/null +++ b/ets2panda/linter/test/main/double_excla_binding_3.ets.autofix.json @@ -0,0 +1,165 @@ +{ + "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": 2, + "endLine": 20, + "endColumn": 7, + "problem": "UIInterfaceImport", + "autofix": [ + { + "start": 603, + "end": 603, + "replacementText": "\n\nimport { Entry, Component, State, Row, Text } from '@kit.ArkUI';", + "line": 29, + "column": 7, + "endLine": 29, + "endColumn": 11 + } + ], + "suggest": "", + "rule": "ArkUI interface should be imported before using (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 21, + "column": 2, + "endLine": 21, + "endColumn": 11, + "problem": "UIInterfaceImport", + "autofix": [ + { + "start": 603, + "end": 603, + "replacementText": "\n\nimport { Entry, Component, State, Row, Text } from '@kit.ArkUI';", + "line": 29, + "column": 7, + "endLine": 29, + "endColumn": 11 + } + ], + "suggest": "", + "rule": "ArkUI interface should be imported before using (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 23, + "column": 4, + "endLine": 23, + "endColumn": 9, + "problem": "UIInterfaceImport", + "autofix": [ + { + "start": 603, + "end": 603, + "replacementText": "\n\nimport { Entry, Component, State, Row, Text } from '@kit.ArkUI';", + "line": 29, + "column": 7, + "endLine": 29, + "endColumn": 11 + } + ], + "suggest": "", + "rule": "ArkUI interface should be imported before using (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 26, + "column": 5, + "endLine": 26, + "endColumn": 8, + "problem": "UIInterfaceImport", + "autofix": [ + { + "start": 603, + "end": 603, + "replacementText": "\n\nimport { Entry, Component, State, Row, Text } from '@kit.ArkUI';", + "line": 29, + "column": 7, + "endLine": 29, + "endColumn": 11 + } + ], + "suggest": "", + "rule": "ArkUI interface should be imported before using (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 27, + "column": 7, + "endLine": 27, + "endColumn": 11, + "problem": "UIInterfaceImport", + "autofix": [ + { + "start": 603, + "end": 603, + "replacementText": "\n\nimport { Entry, Component, State, Row, Text } from '@kit.ArkUI';", + "line": 29, + "column": 7, + "endLine": 29, + "endColumn": 11 + } + ], + "suggest": "", + "rule": "ArkUI interface should be imported before using (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 28, + "column": 7, + "endLine": 28, + "endColumn": 11, + "problem": "UIInterfaceImport", + "autofix": [ + { + "start": 603, + "end": 603, + "replacementText": "\n\nimport { Entry, Component, State, Row, Text } from '@kit.ArkUI';", + "line": 29, + "column": 7, + "endLine": 29, + "endColumn": 11 + } + ], + "suggest": "", + "rule": "ArkUI interface should be imported before using (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 29, + "column": 7, + "endLine": 29, + "endColumn": 11, + "problem": "UIInterfaceImport", + "autofix": [ + { + "start": 603, + "end": 603, + "replacementText": "\n\nimport { Entry, Component, State, Row, Text } from '@kit.ArkUI';", + "line": 29, + "column": 7, + "endLine": 29, + "endColumn": 11 + } + ], + "suggest": "", + "rule": "ArkUI interface should be imported before using (arkui-modular-interface)", + "severity": "ERROR" + } + ] +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/double_excla_binding_3.ets.json b/ets2panda/linter/test/main/double_excla_binding_3.ets.json new file mode 100644 index 0000000000..b7a8809e02 --- /dev/null +++ b/ets2panda/linter/test/main/double_excla_binding_3.ets.json @@ -0,0 +1,17 @@ +{ + "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": [] +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/double_excla_binding_3.ets.migrate.ets b/ets2panda/linter/test/main/double_excla_binding_3.ets.migrate.ets new file mode 100644 index 0000000000..b646a64c49 --- /dev/null +++ b/ets2panda/linter/test/main/double_excla_binding_3.ets.migrate.ets @@ -0,0 +1,34 @@ +/* + * 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. + */ + +import { Entry, Component, State, Row, Text } from '@kit.ArkUI'; + +class User { + name: string = "Jack" +} + +@Entry +@Component +struct MyComponent { + @State user: User = new User() + + build() { + Row() { + Text(this.user!.name) + Text(this.user!!.name) + Text(this.user!!!.name) + } + } +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/double_excla_binding_3.ets.migrate.json b/ets2panda/linter/test/main/double_excla_binding_3.ets.migrate.json new file mode 100644 index 0000000000..b7a8809e02 --- /dev/null +++ b/ets2panda/linter/test/main/double_excla_binding_3.ets.migrate.json @@ -0,0 +1,17 @@ +{ + "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": [] +} \ No newline at end of file -- Gitee