From cc147bce8b82a5a42d2d1cd88e4ac6eec91f3cb0 Mon Sep 17 00:00:00 2001 From: chenyiyuan Date: Tue, 2 Apr 2024 12:11:25 +0000 Subject: [PATCH] test cases add of `TypeParameter Default / Extends Var / ISendable / Initialization` cover by tests in ArkTS linter Signed-off-by: chenyiyuan Change-Id: I4bb3b175dbc9fa4c7fae5f550f9cee92d08d53c6 --- .../linter/test/sendable_class_inheritance.ts | 51 ++++++++------ .../test/sendable_class_initialization.ts | 23 +++++-- ...dable_class_initialization.ts.autofix.json | 66 +++++++++++++++++-- .../sendable_class_initialization.ts.json | 58 ++++++++++++++-- .../linter/test/sendable_generic_types.ts | 13 ++++ .../sendable_generic_types.ts.autofix.json | 41 +++++++++--- .../test/sendable_generic_types.ts.json | 38 ++++++++--- 7 files changed, 240 insertions(+), 50 deletions(-) diff --git a/ets2panda/linter/test/sendable_class_inheritance.ts b/ets2panda/linter/test/sendable_class_inheritance.ts index 00a183cc0d..cab5f51353 100644 --- a/ets2panda/linter/test/sendable_class_inheritance.ts +++ b/ets2panda/linter/test/sendable_class_inheritance.ts @@ -91,30 +91,30 @@ class sendableClass8 extends localNonSendableClassB {} // ERROR // class + interface -// case1: extends local interface -// == case1.1: extends sendable +// case1: implements local interface +// == case1.1: implements sendable @Sendable class sendableClass9 implements localSendableInterface {} // OK -// == case1.2: extends non-sendable +// == case1.2: implements non-sendable @Sendable class sendableClass10 implements localNonSendableInterface {} // OK -// case2: extends import interface -// == case2.1: extends sendable +// case2: implements import interface +// == case2.1: implements sendable @Sendable class sendableClass11 implements sendableInterface {} // OK -// == case2.2: extends non-sendable +// == case2.2: implements non-sendable @Sendable class sendableClass12 implements nonSendableInterface {} // OK -// case3: extends type alias -// == case3.1: extends sendable +// case3: implements type alias +// == case3.1: implements sendable @Sendable class sendableClass13 implements localSendableInterfaceAlias {} // OK -// == case3.2: extends non-sendable +// == case3.2: implements non-sendable @Sendable class sendableClass14 implements localNonSendableInterfaceAlias {} // OK @@ -150,23 +150,36 @@ class sendableClass22 extends localNonSendableClassB {} // OK // class + interface -// case1: extends local interface -// == case1.1: extends sendable +// case1: implements local interface +// == case1.1: implements sendable class sendableClass23 implements localSendableInterface {} // ERROR -// == case1.2: extends non-sendable +// == case1.2: implements non-sendable class sendableClass24 implements localNonSendableInterface {} // OK -// case2: extends import interface -// == case2.1: extends sendable +// case2: implements import interface +// == case2.1: implements sendable class sendableClass25 implements sendableInterface {} // ERROR -// == case2.2: extends non-sendable +// == case2.2: implements non-sendable class sendableClass26 implements nonSendableInterface {} // OK -// case3: extends type alias -// == case4.1: extends sendable +// case3: implements type alias +// == case4.1: implements sendable class sendableClass27 implements localSendableInterfaceAlias {} // ERROR -// == case4.2: extends non-sendable -class sendableClass28 implements localNonSendableInterfaceAlias {} // OK \ No newline at end of file +// == case4.2: implements non-sendable +class sendableClass28 implements localNonSendableInterfaceAlias {} // OK + +// ISendable created by developer is not a sendable interface + +interface ISendable {} + +interface fakeSendableInterface extends ISendable {} + +// fake sendable interface type alias +type fakeSendableInterfaceAlias = fakeSendableInterface + +class sendableClass29 implements fakeSendableInterface {} // OK + +class sendableClass30 implements fakeSendableInterfaceAlias {} // OK \ No newline at end of file diff --git a/ets2panda/linter/test/sendable_class_initialization.ts b/ets2panda/linter/test/sendable_class_initialization.ts index 8ec142319e..0d9c3476c1 100644 --- a/ets2panda/linter/test/sendable_class_initialization.ts +++ b/ets2panda/linter/test/sendable_class_initialization.ts @@ -13,11 +13,24 @@ * limitations under the License. */ +class nonSendableClass9 {} + +@Sendable +class SendableClass9 {} + @Sendable -class SendableClass9 { - prop1: SendableClass9 = {a: 1}; // ERROR, the initialization for "Sendable" objects is limited - prop2: SendableClass9 = [1, 2]; // ERROR, the initialization for "Sendable" objects is limited +class SendableClass10 { + prop1: number | null = {a: 1}; // OK + prop2: string | bigint | null = [1, 2]; // OK + prop3: SendableClass9 = {a: 1}; // ERROR, the initialization for "Sendable" objects is limited + prop4: SendableClass9 = [1, 2]; // ERROR, the initialization for "Sendable" objects is limited + prop5: SendableClass9 | T | BigInt = {a: 1}; // ERROR, the initialization for "Sendable" objects is limited + prop6: SendableClass9 | T | BigInt = [1, 2]; // ERROR, the initialization for "Sendable" objects is limited } -let v0: SendableClass9 = {b: 1}; // ERROR, the initialization for "Sendable" objects is limited -let v1: SendableClass9 = [1, 2]; // ERROR, the initialization for "Sendable" objects is limited \ No newline at end of file +let v0: string | nonSendableClass9 | undefined | null = {a: 1}; // OK +let v1: undefined | nonSendableClass9 | null = [1, 2]; // OK +let v2: SendableClass9 = {a: 1}; // ERROR, the initialization for "Sendable" objects is limited +let v3: SendableClass9 = [1, 2]; // ERROR, the initialization for "Sendable" objects is limited +let v4: SendableClass9 | nonSendableClass9 | BigInt = [1, 2]; // ERROR, the initialization for "Sendable" objects is limited +let v5: SendableClass9 | nonSendableClass9 | null = [1, 2]; // ERROR, the initialization for "Sendable" objects is limited \ No newline at end of file diff --git a/ets2panda/linter/test/sendable_class_initialization.ts.autofix.json b/ets2panda/linter/test/sendable_class_initialization.ts.autofix.json index 93897de7af..106c96bd67 100644 --- a/ets2panda/linter/test/sendable_class_initialization.ts.autofix.json +++ b/ets2panda/linter/test/sendable_class_initialization.ts.autofix.json @@ -15,32 +15,90 @@ ], "nodes": [ { - "line": 18, + "line": 23, + "column": 26, + "problem": "ObjectLiteralNoContextType", + "autofixable": false, + "suggest": "", + "rule": "Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals)" + }, + { + "line": 25, "column": 27, "problem": "SendableObjectInitialization", "autofixable": false, "rule": "Objects of \"Sendable\" type can not be initialized using object literal or array literal (arkts-sendable-obj-init)" }, { - "line": 19, + "line": 26, "column": 27, "problem": "SendableObjectInitialization", "autofixable": false, "rule": "Objects of \"Sendable\" type can not be initialized using object literal or array literal (arkts-sendable-obj-init)" }, { - "line": 22, + "line": 27, + "column": 3, + "problem": "SendablePropType", + "autofixable": false, + "rule": "Properties in \"Sendable\" classes and interfaces must have a Sendable data type (arkts-sendable-prop-types)" + }, + { + "line": 27, + "column": 40, + "problem": "SendableObjectInitialization", + "autofixable": false, + "rule": "Objects of \"Sendable\" type can not be initialized using object literal or array literal (arkts-sendable-obj-init)" + }, + { + "line": 28, + "column": 3, + "problem": "SendablePropType", + "autofixable": false, + "rule": "Properties in \"Sendable\" classes and interfaces must have a Sendable data type (arkts-sendable-prop-types)" + }, + { + "line": 28, + "column": 41, + "problem": "SendableObjectInitialization", + "autofixable": false, + "rule": "Objects of \"Sendable\" type can not be initialized using object literal or array literal (arkts-sendable-obj-init)" + }, + { + "line": 31, + "column": 57, + "problem": "ObjectLiteralNoContextType", + "autofixable": false, + "suggest": "", + "rule": "Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals)" + }, + { + "line": 33, "column": 26, "problem": "SendableObjectInitialization", "autofixable": false, "rule": "Objects of \"Sendable\" type can not be initialized using object literal or array literal (arkts-sendable-obj-init)" }, { - "line": 23, + "line": 34, "column": 26, "problem": "SendableObjectInitialization", "autofixable": false, "rule": "Objects of \"Sendable\" type can not be initialized using object literal or array literal (arkts-sendable-obj-init)" + }, + { + "line": 35, + "column": 55, + "problem": "SendableObjectInitialization", + "autofixable": false, + "rule": "Objects of \"Sendable\" type can not be initialized using object literal or array literal (arkts-sendable-obj-init)" + }, + { + "line": 36, + "column": 53, + "problem": "SendableObjectInitialization", + "autofixable": false, + "rule": "Objects of \"Sendable\" type can not be initialized using object literal or array literal (arkts-sendable-obj-init)" } ] } \ No newline at end of file diff --git a/ets2panda/linter/test/sendable_class_initialization.ts.json b/ets2panda/linter/test/sendable_class_initialization.ts.json index 48fe2decca..e04cc6365b 100644 --- a/ets2panda/linter/test/sendable_class_initialization.ts.json +++ b/ets2panda/linter/test/sendable_class_initialization.ts.json @@ -15,28 +15,78 @@ ], "nodes": [ { - "line": 18, + "line": 23, + "column": 26, + "problem": "ObjectLiteralNoContextType", + "suggest": "", + "rule": "Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals)" + }, + { + "line": 25, "column": 27, "problem": "SendableObjectInitialization", "rule": "Objects of \"Sendable\" type can not be initialized using object literal or array literal (arkts-sendable-obj-init)" }, { - "line": 19, + "line": 26, "column": 27, "problem": "SendableObjectInitialization", "rule": "Objects of \"Sendable\" type can not be initialized using object literal or array literal (arkts-sendable-obj-init)" }, { - "line": 22, + "line": 27, + "column": 3, + "problem": "SendablePropType", + "rule": "Properties in \"Sendable\" classes and interfaces must have a Sendable data type (arkts-sendable-prop-types)" + }, + { + "line": 27, + "column": 40, + "problem": "SendableObjectInitialization", + "rule": "Objects of \"Sendable\" type can not be initialized using object literal or array literal (arkts-sendable-obj-init)" + }, + { + "line": 28, + "column": 3, + "problem": "SendablePropType", + "rule": "Properties in \"Sendable\" classes and interfaces must have a Sendable data type (arkts-sendable-prop-types)" + }, + { + "line": 28, + "column": 41, + "problem": "SendableObjectInitialization", + "rule": "Objects of \"Sendable\" type can not be initialized using object literal or array literal (arkts-sendable-obj-init)" + }, + { + "line": 31, + "column": 57, + "problem": "ObjectLiteralNoContextType", + "suggest": "", + "rule": "Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals)" + }, + { + "line": 33, "column": 26, "problem": "SendableObjectInitialization", "rule": "Objects of \"Sendable\" type can not be initialized using object literal or array literal (arkts-sendable-obj-init)" }, { - "line": 23, + "line": 34, "column": 26, "problem": "SendableObjectInitialization", "rule": "Objects of \"Sendable\" type can not be initialized using object literal or array literal (arkts-sendable-obj-init)" + }, + { + "line": 35, + "column": 55, + "problem": "SendableObjectInitialization", + "rule": "Objects of \"Sendable\" type can not be initialized using object literal or array literal (arkts-sendable-obj-init)" + }, + { + "line": 36, + "column": 53, + "problem": "SendableObjectInitialization", + "rule": "Objects of \"Sendable\" type can not be initialized using object literal or array literal (arkts-sendable-obj-init)" } ] } \ No newline at end of file diff --git a/ets2panda/linter/test/sendable_generic_types.ts b/ets2panda/linter/test/sendable_generic_types.ts index 4d7fab52b0..ca1d76ce49 100644 --- a/ets2panda/linter/test/sendable_generic_types.ts +++ b/ets2panda/linter/test/sendable_generic_types.ts @@ -15,12 +15,25 @@ class NonSendableClass3 {} +@Sendable +class SendableClass3 {} + @Sendable class SendableClass5 { prop1: T; prop2: U; } +@Sendable +class SendableClass6 { // ERROR, sendable class generic type cannot be non-sendable-class + prop1: T; +} + +@Sendable +class SendableClass7 { // OK + prop1: T; +} + let ins1 = new SendableClass5; // OK let ins2 = new SendableClass5; // ERROR, sendable class generic type cannot be non-sendable-class let ins3 = new SendableClass5; // ERROR, sendable class generic type can only be sendable data type diff --git a/ets2panda/linter/test/sendable_generic_types.ts.autofix.json b/ets2panda/linter/test/sendable_generic_types.ts.autofix.json index f3a191b3e5..a7f76f22a0 100644 --- a/ets2panda/linter/test/sendable_generic_types.ts.autofix.json +++ b/ets2panda/linter/test/sendable_generic_types.ts.autofix.json @@ -15,28 +15,35 @@ ], "nodes": [ { - "line": 25, + "line": 28, + "column": 24, + "problem": "SendableGenericTypes", + "autofixable": false, + "rule": "Type arguments of generic \"Sendable\" type must be a \"Sendable\" data type (arkts-sendable-generic-types)" + }, + { + "line": 38, "column": 39, "problem": "SendableGenericTypes", "autofixable": false, "rule": "Type arguments of generic \"Sendable\" type must be a \"Sendable\" data type (arkts-sendable-generic-types)" }, { - "line": 26, + "line": 39, "column": 31, "problem": "SendableGenericTypes", "autofixable": false, "rule": "Type arguments of generic \"Sendable\" type must be a \"Sendable\" data type (arkts-sendable-generic-types)" }, { - "line": 26, + "line": 39, "column": 41, "problem": "SendableGenericTypes", "autofixable": false, "rule": "Type arguments of generic \"Sendable\" type must be a \"Sendable\" data type (arkts-sendable-generic-types)" }, { - "line": 28, + "line": 41, "column": 9, "problem": "ClassAsObject", "autofixable": false, @@ -44,7 +51,7 @@ "rule": "Classes cannot be used as objects (arkts-no-classes-as-obj)" }, { - "line": 31, + "line": 44, "column": 29, "problem": "ClassAsObject", "autofixable": false, @@ -52,7 +59,7 @@ "rule": "Classes cannot be used as objects (arkts-no-classes-as-obj)" }, { - "line": 31, + "line": 44, "column": 46, "problem": "ClassAsObject", "autofixable": false, @@ -60,14 +67,14 @@ "rule": "Classes cannot be used as objects (arkts-no-classes-as-obj)" }, { - "line": 33, + "line": 46, "column": 26, "problem": "SendableGenericTypes", "autofixable": false, "rule": "Type arguments of generic \"Sendable\" type must be a \"Sendable\" data type (arkts-sendable-generic-types)" }, { - "line": 20, + "line": 23, "column": 3, "problem": "StrictDiagnostic", "autofixable": false, @@ -75,12 +82,28 @@ "rule": "Property 'prop1' has no initializer and is not definitely assigned in the constructor." }, { - "line": 21, + "line": 24, "column": 3, "problem": "StrictDiagnostic", "autofixable": false, "suggest": "Property 'prop2' has no initializer and is not definitely assigned in the constructor.", "rule": "Property 'prop2' has no initializer and is not definitely assigned in the constructor." + }, + { + "line": 29, + "column": 3, + "problem": "StrictDiagnostic", + "autofixable": false, + "suggest": "Property 'prop1' has no initializer and is not definitely assigned in the constructor.", + "rule": "Property 'prop1' has no initializer and is not definitely assigned in the constructor." + }, + { + "line": 34, + "column": 3, + "problem": "StrictDiagnostic", + "autofixable": false, + "suggest": "Property 'prop1' has no initializer and is not definitely assigned in the constructor.", + "rule": "Property 'prop1' has no initializer and is not definitely assigned in the constructor." } ] } \ No newline at end of file diff --git a/ets2panda/linter/test/sendable_generic_types.ts.json b/ets2panda/linter/test/sendable_generic_types.ts.json index ef26fcd373..1e0272547e 100644 --- a/ets2panda/linter/test/sendable_generic_types.ts.json +++ b/ets2panda/linter/test/sendable_generic_types.ts.json @@ -15,63 +15,83 @@ ], "nodes": [ { - "line": 25, + "line": 28, + "column": 24, + "problem": "SendableGenericTypes", + "rule": "Type arguments of generic \"Sendable\" type must be a \"Sendable\" data type (arkts-sendable-generic-types)" + }, + { + "line": 38, "column": 39, "problem": "SendableGenericTypes", "rule": "Type arguments of generic \"Sendable\" type must be a \"Sendable\" data type (arkts-sendable-generic-types)" }, { - "line": 26, + "line": 39, "column": 31, "problem": "SendableGenericTypes", "rule": "Type arguments of generic \"Sendable\" type must be a \"Sendable\" data type (arkts-sendable-generic-types)" }, { - "line": 26, + "line": 39, "column": 41, "problem": "SendableGenericTypes", "rule": "Type arguments of generic \"Sendable\" type must be a \"Sendable\" data type (arkts-sendable-generic-types)" }, { - "line": 28, + "line": 41, "column": 9, "problem": "ClassAsObject", "suggest": "", "rule": "Classes cannot be used as objects (arkts-no-classes-as-obj)" }, { - "line": 31, + "line": 44, "column": 29, "problem": "ClassAsObject", "suggest": "", "rule": "Classes cannot be used as objects (arkts-no-classes-as-obj)" }, { - "line": 31, + "line": 44, "column": 46, "problem": "ClassAsObject", "suggest": "", "rule": "Classes cannot be used as objects (arkts-no-classes-as-obj)" }, { - "line": 33, + "line": 46, "column": 26, "problem": "SendableGenericTypes", "rule": "Type arguments of generic \"Sendable\" type must be a \"Sendable\" data type (arkts-sendable-generic-types)" }, { - "line": 20, + "line": 23, "column": 3, "problem": "StrictDiagnostic", "suggest": "Property 'prop1' has no initializer and is not definitely assigned in the constructor.", "rule": "Property 'prop1' has no initializer and is not definitely assigned in the constructor." }, { - "line": 21, + "line": 24, "column": 3, "problem": "StrictDiagnostic", "suggest": "Property 'prop2' has no initializer and is not definitely assigned in the constructor.", "rule": "Property 'prop2' has no initializer and is not definitely assigned in the constructor." + }, + { + "line": 29, + "column": 3, + "problem": "StrictDiagnostic", + "suggest": "Property 'prop1' has no initializer and is not definitely assigned in the constructor.", + "rule": "Property 'prop1' has no initializer and is not definitely assigned in the constructor." + }, + { + "line": 34, + "column": 3, + "problem": "StrictDiagnostic", + "suggest": "Property 'prop1' has no initializer and is not definitely assigned in the constructor.", + "rule": "Property 'prop1' has no initializer and is not definitely assigned in the constructor." } ] } \ No newline at end of file -- Gitee