From 5df7ae5f9d859577ec52af08888fa2b8187ca4b0 Mon Sep 17 00:00:00 2001 From: mmorozov Date: Thu, 21 Mar 2024 12:40:41 +0300 Subject: [PATCH] Add rule for sendable assignment Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I9AJ1D Tests: Tests for new rule were added Signed-off-by: mmorozov --- ets2panda/linter/lib/CookBookMsg.ts | 1 + ets2panda/linter/lib/FaultAttrs.ts | 1 + ets2panda/linter/lib/FaultDesc.ts | 1 + ets2panda/linter/lib/Problems.ts | 1 + ets2panda/linter/lib/TypeScriptLinter.ts | 13 ++++- ets2panda/linter/test_rules/rule155.ts | 31 ++++++++++ .../linter/test_rules/rule155.ts.autofix.skip | 14 +++++ ets2panda/linter/test_rules/rule155.ts.json | 58 +++++++++++++++++++ 8 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 ets2panda/linter/test_rules/rule155.ts create mode 100644 ets2panda/linter/test_rules/rule155.ts.autofix.skip create mode 100644 ets2panda/linter/test_rules/rule155.ts.json diff --git a/ets2panda/linter/lib/CookBookMsg.ts b/ets2panda/linter/lib/CookBookMsg.ts index b76ff8f1..2e46601e 100644 --- a/ets2panda/linter/lib/CookBookMsg.ts +++ b/ets2panda/linter/lib/CookBookMsg.ts @@ -178,3 +178,4 @@ cookBookTag[150] = '"import" statements after other statements are not allowed ( cookBookTag[151] = 'Usage of "ESObject" type is restricted (arkts-limited-esobj)'; cookBookTag[152] = '"Function.apply", "Function.call" are not supported (arkts-no-func-apply-call)'; cookBookTag[153] = 'The inheritance for "Sendable" classes is limited (arkts-sendable-class-inheritance)'; +cookBookTag[155] = 'Definite assignment assertion in not allowed in "Sendable" classes and interfaces (arkts-sendable-definite-assignment)'; diff --git a/ets2panda/linter/lib/FaultAttrs.ts b/ets2panda/linter/lib/FaultAttrs.ts index 3bdb392b..138856ca 100644 --- a/ets2panda/linter/lib/FaultAttrs.ts +++ b/ets2panda/linter/lib/FaultAttrs.ts @@ -106,3 +106,4 @@ faultsAttrs[FaultID.ImportAfterStatement] = new FaultAttributes(150); faultsAttrs[FaultID.EsObjectType] = new FaultAttributes(151, ProblemSeverity.WARNING); faultsAttrs[FaultID.FunctionApplyCall] = new FaultAttributes(152); faultsAttrs[FaultID.SendableClassInheritance] = new FaultAttributes(153); +faultsAttrs[FaultID.SendableDefiniteAssignment] = new FaultAttributes(155); diff --git a/ets2panda/linter/lib/FaultDesc.ts b/ets2panda/linter/lib/FaultDesc.ts index 073be58d..11b05a8a 100644 --- a/ets2panda/linter/lib/FaultDesc.ts +++ b/ets2panda/linter/lib/FaultDesc.ts @@ -98,3 +98,4 @@ faultDesc[FaultID.StrictDiagnostic] = 'Strict diagnostic'; faultDesc[FaultID.ImportAfterStatement] = 'Import declaration after other declaration or statement'; faultDesc[FaultID.EsObjectType] = 'Restricted "ESObject" type'; faultDesc[FaultID.SendableClassInheritance] = 'Sendable class inheritance'; +faultDesc[FaultID.SendableDefiniteAssignment] = 'Use of definite assignment assertin in "Sendable" class of interface'; diff --git a/ets2panda/linter/lib/Problems.ts b/ets2panda/linter/lib/Problems.ts index c8f528dd..5e5e15b7 100644 --- a/ets2panda/linter/lib/Problems.ts +++ b/ets2panda/linter/lib/Problems.ts @@ -95,6 +95,7 @@ export enum FaultID { ImportAfterStatement, EsObjectType, SendableClassInheritance, + SendableDefiniteAssignment, // this should always be last enum LAST_ID } diff --git a/ets2panda/linter/lib/TypeScriptLinter.ts b/ets2panda/linter/lib/TypeScriptLinter.ts index 67c5e211..306e301b 100644 --- a/ets2panda/linter/lib/TypeScriptLinter.ts +++ b/ets2panda/linter/lib/TypeScriptLinter.ts @@ -2068,9 +2068,18 @@ export class TypeScriptLinter { } private handleDefiniteAssignmentAssertion(decl: ts.VariableDeclaration | ts.PropertyDeclaration): void { - if (decl.exclamationToken !== undefined) { - this.incrementCounters(decl, FaultID.DefiniteAssignment); + if (decl.exclamationToken === undefined) { + return; + } + + if (decl.kind === ts.SyntaxKind.PropertyDeclaration) { + const parentDecl = decl.parent; + if (parentDecl.kind === ts.SyntaxKind.ClassDeclaration && TsUtils.hasSendableDecorator(parentDecl)) { + this.incrementCounters(decl, FaultID.SendableDefiniteAssignment); + return; + } } + this.incrementCounters(decl, FaultID.DefiniteAssignment); } private readonly validatedTypesSet = new Set(); diff --git a/ets2panda/linter/test_rules/rule155.ts b/ets2panda/linter/test_rules/rule155.ts new file mode 100644 index 00000000..38cfeb1d --- /dev/null +++ b/ets2panda/linter/test_rules/rule155.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2022-2024 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. + */ + +@Sendable +class A{ + a: number; + b: number = 1; + c!: number; + d!: number = 1; + e?: number; +} + +class B{ + a: number; + b: number = 1; + c!: number; + d!: number = 1; + e?: number; +} diff --git a/ets2panda/linter/test_rules/rule155.ts.autofix.skip b/ets2panda/linter/test_rules/rule155.ts.autofix.skip new file mode 100644 index 00000000..13982e9c --- /dev/null +++ b/ets2panda/linter/test_rules/rule155.ts.autofix.skip @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2023-2024 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. + */ diff --git a/ets2panda/linter/test_rules/rule155.ts.json b/ets2panda/linter/test_rules/rule155.ts.json new file mode 100644 index 00000000..a19d7994 --- /dev/null +++ b/ets2panda/linter/test_rules/rule155.ts.json @@ -0,0 +1,58 @@ +{ + "copyright": [ + "Copyright (c) 2024 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." + ], + "nodes": [ + { + "line": 20, + "column": 5, + "problem": "SendableDefiniteAssignment", + "rule": "Definite assignment assertion in not allowed in \"Sendable\" classes and interfaces (arkts-sendable-definite-assignment)" + }, + { + "line": 21, + "column": 5, + "problem": "SendableDefiniteAssignment", + "rule": "Definite assignment assertion in not allowed in \"Sendable\" classes and interfaces (arkts-sendable-definite-assignment)" + }, + { + "line": 28, + "column": 5, + "problem": "DefiniteAssignment", + "suggest": "", + "rule": "Definite assignment assertions are not supported (arkts-no-definite-assignment)" + }, + { + "line": 29, + "column": 5, + "problem": "DefiniteAssignment", + "suggest": "", + "rule": "Definite assignment assertions are not supported (arkts-no-definite-assignment)" + }, + { + "line": 18, + "column": 5, + "problem": "StrictDiagnostic", + "suggest": "Property 'a' has no initializer and is not definitely assigned in the constructor.", + "rule": "Property 'a' has no initializer and is not definitely assigned in the constructor." + }, + { + "line": 26, + "column": 5, + "problem": "StrictDiagnostic", + "suggest": "Property 'a' has no initializer and is not definitely assigned in the constructor.", + "rule": "Property 'a' has no initializer and is not definitely assigned in the constructor." + } + ] +} -- Gitee