diff --git a/ets2panda/linter/lib/utils/TsUtils.ts b/ets2panda/linter/lib/utils/TsUtils.ts index 48c37b005f3faa26856be9c9a5532780df63a6c1..34c6184c05abb2a44af7bf1784a2011aefe4e8d9 100644 --- a/ets2panda/linter/lib/utils/TsUtils.ts +++ b/ets2panda/linter/lib/utils/TsUtils.ts @@ -517,8 +517,11 @@ export class TsUtils { if (!typeA.symbol?.declarations) { return false; } - + const isBISendable = TsUtils.isISendableInterface(typeB); for (const typeADecl of typeA.symbol.declarations) { + if (isBISendable && ts.isClassDeclaration(typeADecl) && TsUtils.hasSendableDecorator(typeADecl)) { + return true; + } if (!ts.isClassDeclaration(typeADecl) && !ts.isInterfaceDeclaration(typeADecl) || !typeADecl.heritageClauses) { continue; } diff --git a/ets2panda/linter/test/structural_identity.ts b/ets2panda/linter/test/structural_identity.ts index e2469d8936bf65749da78ca9c8a5d3c83bf497d1..b1355436b07be9a8724ccd6d2ff4d658ad8d56db 100644 --- a/ets2panda/linter/test/structural_identity.ts +++ b/ets2panda/linter/test/structural_identity.ts @@ -31,6 +31,8 @@ import { DynLibCI } from './dynamic_lib' +import type { lang } from './@arkts.lang'; + class A { getName(): string { return 'A'; } getType(): string { return 'class'; } @@ -470,3 +472,110 @@ function testUnionStructuralIdentityPositive(u1: U1, u2: U2, u3: U3, u4: U4) { u3 as C2[] | C2[] | C0[]; u3 as U3 | U4; } + + +/** + * Add the Sendable exception to the [arks-no-struct_typing] rule. + */ +@Sendable +class TC1 implements lang.ISendable { + name:string = 'tc1'; +} + +@Sendable +class TC2 { + name:string = 'tc2'; +} + +class TC3 { + name:string = 'tc3'; +} +const t1 = new TC1(); +const t2 = new TC2(); +const t3 = new TC3(); + + +// Appears on SyntaxKind.VariableDeclaration node +const a1: lang.ISendable = t1; // OK +const a2: lang.ISendable = t2; // OK , Sendable can be thought of as implement ISendable +const a3: lang.ISendable = t2 as lang.ISendable; // OK +const a4: lang.ISendable = t3; // ERROR +const a5: lang.ISendable = t3 as lang.ISendable; // ERROR + + +// Appears on SyntaxKind.BinaryExpression node +let b1:lang.ISendable; +b1 = t1; // OK +b1 = t2; // OK , Sendable can be thought of as implement ISendable +b1 = t2 as lang.ISendable; // OK +b1 = t3; // ERROR +b1 = t3 as lang.ISendable; // ERROR + + +// Appears on SyntaxKind.CallExpression node +function cf(value: lang.ISendable):void {} +cf(t1); // OK +cf(t2); // OK , Sendable can be thought of as implement ISendable +cf(t2 as lang.ISendable); // OK +cf(t3); // ERROR +cf(t3 as lang.ISendable); // ERROR + +function cfT(value: T):void {} +cfT(t1); // OK +cfT(t2); // OK , Sendable can be thought of as implement ISendable +cfT(t2 as lang.ISendable); // OK +cfT(t3); // ERROR +cfT(t3 as lang.ISendable); // ERROR + + +// Appears on SyntaxKind.handleNewExpression node +class DC1 { + name:string; + + constructor(value:lang.ISendable) { + this.name = value.name; + } +} +new DC1(t1); // OK +new DC1(t2); // OK , Sendable can be thought of as implement ISendable +new DC1(t2 as lang.ISendable); // OK +new DC1(t3); // ERROR +new DC1(t3 as lang.ISendable); // ERROR + + +// Appears on SyntaxKind.ObjectLiteralExpression/SyntaxKind.ArrayLiteralExpression node +interface EI1 { + sendable: lang.ISendable; +} +const e1: EI1 = { sendable:t1 }; // OK +const e2: EI1 = { sendable:t2 }; // OK , Sendable can be thought of as implement ISendable, avoid [arkts-no-untyped-obj-literals] rule +const e3: EI1 = { sendable:t2 as lang.ISendable }; // OK +const e4: EI1 = { sendable:t3 }; // ERROR +const e5: EI1 = { sendable:t3 as lang.ISendable }; // ERROR +const e1s: EI1[] = [{ sendable:t1 }]; // OK +const e2s: EI1[] = [{ sendable:t2 }]; // OK , Sendable can be thought of as implement ISendable. avoid [arkts-no-untyped-obj-literals] rule +const e3s: EI1[] = [{ sendable:t2 as lang.ISendable }]; // OK +const e4s: EI1[] = [{ sendable:t3 }]; // ERROR +const e5s: EI1[] = [{ sendable:t3 as lang.ISendable }]; // ERROR + +// // Appears on SyntaxKind.PropertyDeclaration node +// PropertyDeclaration does not do [arkts-no-structural-typing] check +// export class FC1 { +// prop1: lang.ISendable = t1; // OK +// prop2: lang.ISendable = t2; // OK +// prop3: lang.ISendable = t3; // OK +// } + + +// union +class GC1 { } +function ff1(value: lang.ISendable):void {} +function ff2(value: lang.ISendable | GC1):void {} +function ff3(value: TC1 | TC2): void { ff1(value); } // OK , Sendable can be thought of as implement ISendable +function ff4(value: TC1 | TC3): void { ff1(value); } // ERROR +function ff5(value: TC1): void { ff2(value); } // OK +function ff6(value: TC2): void { ff2(value); } // OK , Sendable can be thought of as implement ISendable +function ff7(value: TC3): void { ff2(value); } // ERROR +function ff8(value: TC1 | TC2): void { ff2(value); } // OK , Sendable can be thought of as implement ISendable +function ff9(value: TC1 | TC3): void { ff2(value); } // ERROR + diff --git a/ets2panda/linter/test/structural_identity.ts.json b/ets2panda/linter/test/structural_identity.ts.json index a662a8d73a4c6d78a64d0c0efb23b0f3a20ad510..70152349478458066ccb7f109b78c4207812f21a 100644 --- a/ets2panda/linter/test/structural_identity.ts.json +++ b/ets2panda/linter/test/structural_identity.ts.json @@ -15,49 +15,42 @@ ], "nodes": [ { - "line": 44, + "line": 46, "column": 5, "problem": "StructuralIdentity", "suggest": "", "rule": "Structural typing is not supported (arkts-no-structural-typing)" }, { - "line": 44, + "line": 46, "column": 19, "problem": "StructuralIdentity", "suggest": "", "rule": "Structural typing is not supported (arkts-no-structural-typing)" }, { - "line": 47, + "line": 49, "column": 1, "problem": "StructuralIdentity", "suggest": "", "rule": "Structural typing is not supported (arkts-no-structural-typing)" }, { - "line": 49, + "line": 51, "column": 5, "problem": "StructuralIdentity", "suggest": "", "rule": "Structural typing is not supported (arkts-no-structural-typing)" }, { - "line": 52, + "line": 54, "column": 3, "problem": "StructuralIdentity", "suggest": "", "rule": "Structural typing is not supported (arkts-no-structural-typing)" }, { - "line": 106, - "column": 4, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)" - }, - { - "line": 107, + "line": 108, "column": 4, "problem": "StructuralIdentity", "suggest": "", @@ -71,28 +64,21 @@ "rule": "Structural typing is not supported (arkts-no-structural-typing)" }, { - "line": 110, + "line": 111, "column": 4, "problem": "StructuralIdentity", "suggest": "", "rule": "Structural typing is not supported (arkts-no-structural-typing)" }, { - "line": 113, + "line": 112, "column": 4, "problem": "StructuralIdentity", "suggest": "", "rule": "Structural typing is not supported (arkts-no-structural-typing)" }, { - "line": 136, - "column": 4, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)" - }, - { - "line": 137, + "line": 115, "column": 4, "problem": "StructuralIdentity", "suggest": "", @@ -169,14 +155,14 @@ "rule": "Structural typing is not supported (arkts-no-structural-typing)" }, { - "line": 173, + "line": 148, "column": 4, "problem": "StructuralIdentity", "suggest": "", "rule": "Structural typing is not supported (arkts-no-structural-typing)" }, { - "line": 174, + "line": 149, "column": 4, "problem": "StructuralIdentity", "suggest": "", @@ -253,60 +239,200 @@ "rule": "Structural typing is not supported (arkts-no-structural-typing)" }, { - "line": 339, + "line": 185, + "column": 4, + "problem": "StructuralIdentity", + "suggest": "", + "rule": "Structural typing is not supported (arkts-no-structural-typing)" + }, + { + "line": 186, + "column": 4, + "problem": "StructuralIdentity", + "suggest": "", + "rule": "Structural typing is not supported (arkts-no-structural-typing)" + }, + { + "line": 341, "column": 3, "problem": "StructuralIdentity", "suggest": "", "rule": "Structural typing is not supported (arkts-no-structural-typing)" }, { - "line": 342, + "line": 344, "column": 3, "problem": "StructuralIdentity", "suggest": "", "rule": "Structural typing is not supported (arkts-no-structural-typing)" }, { - "line": 343, + "line": 345, "column": 3, "problem": "StructuralIdentity", "suggest": "", "rule": "Structural typing is not supported (arkts-no-structural-typing)" }, { - "line": 346, + "line": 348, "column": 3, "problem": "StructuralIdentity", "suggest": "", "rule": "Structural typing is not supported (arkts-no-structural-typing)" }, { - "line": 349, + "line": 351, "column": 3, "problem": "StructuralIdentity", "suggest": "", "rule": "Structural typing is not supported (arkts-no-structural-typing)" }, { - "line": 354, + "line": 356, "column": 3, "problem": "StructuralIdentity", "suggest": "", "rule": "Structural typing is not supported (arkts-no-structural-typing)" }, { - "line": 355, + "line": 357, "column": 3, "problem": "StructuralIdentity", "suggest": "", "rule": "Structural typing is not supported (arkts-no-structural-typing)" }, { - "line": 358, + "line": 360, "column": 3, "problem": "StructuralIdentity", "suggest": "", "rule": "Structural typing is not supported (arkts-no-structural-typing)" + }, + { + "line": 502, + "column": 7, + "problem": "StructuralIdentity", + "suggest": "", + "rule": "Structural typing is not supported (arkts-no-structural-typing)" + }, + { + "line": 503, + "column": 28, + "problem": "SendableAsExpr", + "suggest": "", + "rule": "Casting \"Non-sendable\" data to \"Sendable\" type is not allowed (arkts-sendable-as-expr)" + }, + { + "line": 511, + "column": 1, + "problem": "StructuralIdentity", + "suggest": "", + "rule": "Structural typing is not supported (arkts-no-structural-typing)" + }, + { + "line": 512, + "column": 6, + "problem": "SendableAsExpr", + "suggest": "", + "rule": "Casting \"Non-sendable\" data to \"Sendable\" type is not allowed (arkts-sendable-as-expr)" + }, + { + "line": 520, + "column": 4, + "problem": "StructuralIdentity", + "suggest": "", + "rule": "Structural typing is not supported (arkts-no-structural-typing)" + }, + { + "line": 521, + "column": 4, + "problem": "SendableAsExpr", + "suggest": "", + "rule": "Casting \"Non-sendable\" data to \"Sendable\" type is not allowed (arkts-sendable-as-expr)" + }, + { + "line": 527, + "column": 21, + "problem": "StructuralIdentity", + "suggest": "", + "rule": "Structural typing is not supported (arkts-no-structural-typing)" + }, + { + "line": 528, + "column": 21, + "problem": "SendableAsExpr", + "suggest": "", + "rule": "Casting \"Non-sendable\" data to \"Sendable\" type is not allowed (arkts-sendable-as-expr)" + }, + { + "line": 542, + "column": 9, + "problem": "StructuralIdentity", + "suggest": "", + "rule": "Structural typing is not supported (arkts-no-structural-typing)" + }, + { + "line": 543, + "column": 9, + "problem": "SendableAsExpr", + "suggest": "", + "rule": "Casting \"Non-sendable\" data to \"Sendable\" type is not allowed (arkts-sendable-as-expr)" + }, + { + "line": 553, + "column": 17, + "problem": "ObjectLiteralNoContextType", + "suggest": "", + "rule": "Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals)" + }, + { + "line": 554, + "column": 28, + "problem": "SendableAsExpr", + "suggest": "", + "rule": "Casting \"Non-sendable\" data to \"Sendable\" type is not allowed (arkts-sendable-as-expr)" + }, + { + "line": 558, + "column": 20, + "problem": "ArrayLiteralNoContextType", + "suggest": "", + "rule": "Array literals must contain elements of only inferrable types (arkts-no-noninferrable-arr-literals)" + }, + { + "line": 558, + "column": 21, + "problem": "ObjectLiteralNoContextType", + "suggest": "", + "rule": "Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals)" + }, + { + "line": 559, + "column": 32, + "problem": "SendableAsExpr", + "suggest": "", + "rule": "Casting \"Non-sendable\" data to \"Sendable\" type is not allowed (arkts-sendable-as-expr)" + }, + { + "line": 575, + "column": 44, + "problem": "StructuralIdentity", + "suggest": "", + "rule": "Structural typing is not supported (arkts-no-structural-typing)" + }, + { + "line": 578, + "column": 38, + "problem": "StructuralIdentity", + "suggest": "", + "rule": "Structural typing is not supported (arkts-no-structural-typing)" + }, + { + "line": 580, + "column": 44, + "problem": "StructuralIdentity", + "suggest": "", + "rule": "Structural typing is not supported (arkts-no-structural-typing)" } ] } \ No newline at end of file