diff --git a/ets2panda/linter/src/lib/TypeScriptLinter.ts b/ets2panda/linter/src/lib/TypeScriptLinter.ts index ca04dbcfcd70cd084c01b8f28c23f983dd279534..7f80b8cebd3290a991b746ad3ad158f64a082212 100644 --- a/ets2panda/linter/src/lib/TypeScriptLinter.ts +++ b/ets2panda/linter/src/lib/TypeScriptLinter.ts @@ -5080,7 +5080,7 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { this.handleSharedArrayBuffer(tsNewExpr); this.handleSdkGlobalApi(tsNewExpr); this.checkCreatingPrimitiveTypes(tsNewExpr); - + this.checkPromiseReturnType(tsNewExpr); if (this.options.advancedClassChecks || this.options.arkts2) { const calleeExpr = tsNewExpr.expression; const calleeType = this.tsTypeChecker.getTypeAtLocation(calleeExpr); @@ -5110,6 +5110,35 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { this.handlePromiseNeedVoidResolve(tsNewExpr); } + private checkPromiseReturnType(newExpr: ts.NewExpression): void { + if (!ts.isIdentifier(newExpr.expression)) { return }; + if (newExpr.expression.text !== 'Promise') { return }; + + const callback = newExpr.arguments?.[0]; + if (!callback || !ts.isFunctionLike(callback)) { return }; + + const callbackReturnType = this.tsTypeChecker.getTypeAtLocation(callback); + const callSignatures = callbackReturnType.getCallSignatures(); + if (callSignatures.length === 0) { return }; + + const actualReturnType = callSignatures[0].getReturnType(); + + const voidType = this.tsTypeChecker.getTypeAtLocation( + ts.factory.createVoidZero()); + if (!this.areTypesEqual(actualReturnType, voidType)) { + this.incrementCounters(callback, FaultID.IncompationbleFunctionType); + } + } + + private areTypesEqual(type1: ts.Type, type2: ts.Type): boolean { + if (type1.flags !== type2.flags) { return false }; + + const type1Str = this.tsTypeChecker.typeToString(type1); + const type2Str = this.tsTypeChecker.typeToString(type2); + + return type1Str === type2Str; + } + handlePromiseNeedVoidResolve(newExpr: ts.NewExpression): void { if (!this.options.arkts2) { return; diff --git a/ets2panda/linter/test/main/arkts_promise_need_void_resolve.ets.arkts2.json b/ets2panda/linter/test/main/arkts_promise_need_void_resolve.ets.arkts2.json index 83f403ba1f5ef5fd64ebbb44235a11c5ef6c754a..88c5e5728c72ed6c4de772086a8aaf7df509e0cd 100755 --- a/ets2panda/linter/test/main/arkts_promise_need_void_resolve.ets.arkts2.json +++ b/ets2panda/linter/test/main/arkts_promise_need_void_resolve.ets.arkts2.json @@ -14,6 +14,16 @@ "limitations under the License." ], "result": [ + { + "line": 17, + "column": 31, + "endLine": 17, + "endColumn": 58, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + }, { "line": 17, "column": 40, diff --git a/ets2panda/linter/test/main/arkts_promise_need_void_resolve.ets.json b/ets2panda/linter/test/main/arkts_promise_need_void_resolve.ets.json index 127a98a334a7471028ae0d660051f086a7d261be..d4d6e1cb05bd9f253cc2f069e80915ec03e53ffe 100755 --- a/ets2panda/linter/test/main/arkts_promise_need_void_resolve.ets.json +++ b/ets2panda/linter/test/main/arkts_promise_need_void_resolve.ets.json @@ -13,5 +13,16 @@ "See the License for the specific language governing permissions and", "limitations under the License." ], - "result": [] + "result": [ + { + "line": 17, + "column": 31, + "endLine": 17, + "endColumn": 58, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + } + ] } \ No newline at end of file diff --git a/ets2panda/linter/test/main/func_inferred_type_args.ets.arkts2.json b/ets2panda/linter/test/main/func_inferred_type_args.ets.arkts2.json index eee42f58f0450a05faf2cd75d35139f35e77c1cd..bea02a402017f9f5aef3dbed4aa246234bbdf017 100755 --- a/ets2panda/linter/test/main/func_inferred_type_args.ets.arkts2.json +++ b/ets2panda/linter/test/main/func_inferred_type_args.ets.arkts2.json @@ -434,6 +434,16 @@ "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)", "severity": "ERROR" }, + { + "line": 92, + "column": 29, + "endLine": 92, + "endColumn": 44, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + }, { "line": 92, "column": 16, diff --git a/ets2panda/linter/test/main/func_inferred_type_args.ets.json b/ets2panda/linter/test/main/func_inferred_type_args.ets.json index 2d86c954b2772f916aefe6ee54dbcd73100e9674..6e35f7f6ebfaec9847428e2e2ff929605e1bc8a8 100644 --- a/ets2panda/linter/test/main/func_inferred_type_args.ets.json +++ b/ets2panda/linter/test/main/func_inferred_type_args.ets.json @@ -224,6 +224,16 @@ "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)", "severity": "ERROR" }, + { + "line": 92, + "column": 29, + "endLine": 92, + "endColumn": 44, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + }, { "line": 92, "column": 16, diff --git a/ets2panda/linter/test/main/func_inferred_type_args_2.ets.arkts2.json b/ets2panda/linter/test/main/func_inferred_type_args_2.ets.arkts2.json index d1ca60d4ff31e83b6b4d74b04b596d8d7099ea38..d80ed23dda9084a8e8ea90ec0e3289a20471c013 100644 --- a/ets2panda/linter/test/main/func_inferred_type_args_2.ets.arkts2.json +++ b/ets2panda/linter/test/main/func_inferred_type_args_2.ets.arkts2.json @@ -244,6 +244,16 @@ "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", "severity": "ERROR" }, + { + "line": 41, + "column": 46, + "endLine": 41, + "endColumn": 66, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + }, { "line": 41, "column": 34, @@ -474,6 +484,16 @@ "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", "severity": "ERROR" }, + { + "line": 105, + "column": 27, + "endLine": 105, + "endColumn": 42, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + }, { "line": 105, "column": 14, diff --git a/ets2panda/linter/test/main/func_inferred_type_args_2.ets.autofix.json b/ets2panda/linter/test/main/func_inferred_type_args_2.ets.autofix.json index c53b7da247fd5afc1070851dfb394632b7dedcc7..920bace1fcca957e8cdcb04047057845635da46b 100644 --- a/ets2panda/linter/test/main/func_inferred_type_args_2.ets.autofix.json +++ b/ets2panda/linter/test/main/func_inferred_type_args_2.ets.autofix.json @@ -354,6 +354,16 @@ "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", "severity": "ERROR" }, + { + "line": 41, + "column": 46, + "endLine": 41, + "endColumn": 66, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + }, { "line": 41, "column": 34, @@ -727,6 +737,16 @@ "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", "severity": "ERROR" }, + { + "line": 105, + "column": 27, + "endLine": 105, + "endColumn": 42, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + }, { "line": 105, "column": 14, diff --git a/ets2panda/linter/test/main/func_inferred_type_args_2.ets.json b/ets2panda/linter/test/main/func_inferred_type_args_2.ets.json index c3cf571d47c001259b422014701fc4184dc4faea..0bfd2339e08d30c239acc5678e1c74ef721dc117 100644 --- a/ets2panda/linter/test/main/func_inferred_type_args_2.ets.json +++ b/ets2panda/linter/test/main/func_inferred_type_args_2.ets.json @@ -84,6 +84,16 @@ "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", "severity": "ERROR" }, + { + "line": 41, + "column": 46, + "endLine": 41, + "endColumn": 66, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + }, { "line": 52, "column": 10, @@ -124,6 +134,16 @@ "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", "severity": "ERROR" }, + { + "line": 105, + "column": 27, + "endLine": 105, + "endColumn": 42, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + }, { "line": 103, "column": 22, diff --git a/ets2panda/linter/test/main/func_inferred_type_args_2.ets.migrate.json b/ets2panda/linter/test/main/func_inferred_type_args_2.ets.migrate.json index 0b10990af1e4f586fff32414b1e2111eb5fe7f15..6280062327153821cb32277f5da7e3e009f36a23 100644 --- a/ets2panda/linter/test/main/func_inferred_type_args_2.ets.migrate.json +++ b/ets2panda/linter/test/main/func_inferred_type_args_2.ets.migrate.json @@ -144,6 +144,16 @@ "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", "severity": "ERROR" }, + { + "line": 48, + "column": 54, + "endLine": 48, + "endColumn": 74, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + }, { "line": 59, "column": 10, @@ -244,6 +254,16 @@ "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", "severity": "ERROR" }, + { + "line": 112, + "column": 35, + "endLine": 112, + "endColumn": 50, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + }, { "line": 118, "column": 22, diff --git a/ets2panda/linter/test/main/incompatible_function.ets b/ets2panda/linter/test/main/incompatible_function.ets index e137fe5ecc7c1d95332b75831e1e407433c031df..412909e603bd6d3bfe1f4cf581b3c14c81317221 100644 --- a/ets2panda/linter/test/main/incompatible_function.ets +++ b/ets2panda/linter/test/main/incompatible_function.ets @@ -79,4 +79,25 @@ type FuncTypeNoParams = () => void; let fNoParams: FuncTypeNoParams = () => { return 0; -}; \ No newline at end of file +}; + +function sleep(time: number): Promise { + return new Promise((resolve) => setTimeout(resolve, time)); +} +sleep(100) + +const caseName = 'PromiseAnyTest0200'; +console.info(${caseName} test start); +const startTime = Date.now(); +const promises: Promise[] = []; +const totalPromises = 1000; +const delay = 100; +for (let i = 0; i < totalPromises; i++) { + promises.push( + new Promise((_, reject) => + setTimeout(() => { + reject(new Error(Failure ${i})); + }, delay) + ) + ); +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/incompatible_function.ets.arkts2.json b/ets2panda/linter/test/main/incompatible_function.ets.arkts2.json index da908095d3d48a520457fd8c8455423a513ff7ff..080087fe96a93664aad5d137b2eb04f007b96614 100644 --- a/ets2panda/linter/test/main/incompatible_function.ets.arkts2.json +++ b/ets2panda/linter/test/main/incompatible_function.ets.arkts2.json @@ -203,6 +203,136 @@ "suggest": "", "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", "severity": "ERROR" + }, + { + "line": 85, + "column": 23, + "endLine": 85, + "endColumn": 30, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + }, + { + "line": 85, + "column": 10, + "endLine": 85, + "endColumn": 61, + "problem": "GenericCallNoTypeArgs", + "suggest": "", + "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", + "severity": "ERROR" + }, + { + "line": 85, + "column": 55, + "endLine": 85, + "endColumn": 59, + "problem": "NoTsLikeSmartType", + "suggest": "", + "rule": "Smart type differences (arkts-no-ts-like-smart-type)", + "severity": "ERROR" + }, + { + "line": 87, + "column": 7, + "endLine": 87, + "endColumn": 10, + "problem": "NumericSemantics", + "suggest": "", + "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", + "severity": "ERROR" + }, + { + "line": 91, + "column": 7, + "endLine": 91, + "endColumn": 29, + "problem": "NumericSemantics", + "suggest": "", + "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", + "severity": "ERROR" + }, + { + "line": 93, + "column": 7, + "endLine": 93, + "endColumn": 27, + "problem": "NumericSemantics", + "suggest": "", + "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", + "severity": "ERROR" + }, + { + "line": 93, + "column": 23, + "endLine": 93, + "endColumn": 27, + "problem": "NumericSemantics", + "suggest": "", + "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", + "severity": "ERROR" + }, + { + "line": 94, + "column": 7, + "endLine": 94, + "endColumn": 18, + "problem": "NumericSemantics", + "suggest": "", + "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", + "severity": "ERROR" + }, + { + "line": 94, + "column": 15, + "endLine": 94, + "endColumn": 18, + "problem": "NumericSemantics", + "suggest": "", + "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", + "severity": "ERROR" + }, + { + "line": 95, + "column": 10, + "endLine": 95, + "endColumn": 15, + "problem": "NumericSemantics", + "suggest": "", + "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", + "severity": "ERROR" + }, + { + "line": 95, + "column": 14, + "endLine": 95, + "endColumn": 15, + "problem": "NumericSemantics", + "suggest": "", + "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", + "severity": "ERROR" + }, + { + "line": 97, + "column": 18, + "endLine": 97, + "endColumn": 27, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + }, + { + "line": 97, + "column": 5, + "endLine": 101, + "endColumn": 6, + "problem": "GenericCallNoTypeArgs", + "suggest": "", + "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", + "severity": "ERROR" } ] } \ No newline at end of file diff --git a/ets2panda/linter/test/main/incompatible_function.ets.json b/ets2panda/linter/test/main/incompatible_function.ets.json index 2844fc25ba579f7e073c0d91f47b203c80c09683..f031ea64684de0338976eb0d2e54920d911f96c4 100644 --- a/ets2panda/linter/test/main/incompatible_function.ets.json +++ b/ets2panda/linter/test/main/incompatible_function.ets.json @@ -13,5 +13,46 @@ "See the License for the specific language governing permissions and", "limitations under the License." ], - "result": [] + "result": [ + { + "line": 85, + "column": 23, + "endLine": 85, + "endColumn": 30, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + }, + { + "line": 85, + "column": 10, + "endLine": 85, + "endColumn": 61, + "problem": "GenericCallNoTypeArgs", + "suggest": "", + "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", + "severity": "ERROR" + }, + { + "line": 97, + "column": 18, + "endLine": 97, + "endColumn": 27, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + }, + { + "line": 97, + "column": 5, + "endLine": 101, + "endColumn": 6, + "problem": "GenericCallNoTypeArgs", + "suggest": "", + "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", + "severity": "ERROR" + } + ] } \ No newline at end of file diff --git a/ets2panda/linter/test/main/structural_identity_promise.ets.arkts2.json b/ets2panda/linter/test/main/structural_identity_promise.ets.arkts2.json index ca88f857e960b437dcf767c0ac40be998c8f1236..20fe94b977154c076bf348f1182d283951380a83 100644 --- a/ets2panda/linter/test/main/structural_identity_promise.ets.arkts2.json +++ b/ets2panda/linter/test/main/structural_identity_promise.ets.arkts2.json @@ -13,5 +13,16 @@ "See the License for the specific language governing permissions and", "limitations under the License." ], - "result": [] + "result": [ + { + "line": 19, + "column": 31, + "endLine": 19, + "endColumn": 40, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + } + ] } \ No newline at end of file diff --git a/ets2panda/linter/test/main/structural_identity_promise.ets.json b/ets2panda/linter/test/main/structural_identity_promise.ets.json index ca88f857e960b437dcf767c0ac40be998c8f1236..20fe94b977154c076bf348f1182d283951380a83 100644 --- a/ets2panda/linter/test/main/structural_identity_promise.ets.json +++ b/ets2panda/linter/test/main/structural_identity_promise.ets.json @@ -13,5 +13,16 @@ "See the License for the specific language governing permissions and", "limitations under the License." ], - "result": [] + "result": [ + { + "line": 19, + "column": 31, + "endLine": 19, + "endColumn": 40, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + } + ] } \ No newline at end of file