diff --git a/ets2panda/linter/src/lib/TypeScriptLinter.ts b/ets2panda/linter/src/lib/TypeScriptLinter.ts index 0b7067a939d4e9b2293ad9df1c856b5943891888..4607f067f2e759e0007e2746f4e25bd317312f31 100644 --- a/ets2panda/linter/src/lib/TypeScriptLinter.ts +++ b/ets2panda/linter/src/lib/TypeScriptLinter.ts @@ -22,7 +22,7 @@ import { Autofixer } from './autofixes/Autofixer'; import { PROMISE_METHODS, SYMBOL, SYMBOL_CONSTRUCTOR, TsUtils } from './utils/TsUtils'; import { FUNCTION_HAS_NO_RETURN_ERROR_CODE } from './utils/consts/FunctionHasNoReturnErrorCode'; import { LIMITED_STANDARD_UTILITY_TYPES } from './utils/consts/LimitedStandardUtilityTypes'; -import { LIKE_FUNCTION } from './utils/consts/LikeFunction'; +import { LIKE_FUNCTION, LIKE_FUNCTION_CONSTRUCTOR } from './utils/consts/LikeFunction'; import { METHOD_DECLARATION } from './utils/consts/MethodDeclaration'; import { METHOD_SIGNATURE } from './utils/consts/MethodSignature'; import { OPTIONAL_METHOD } from './utils/consts/OptionalMethod'; @@ -4547,10 +4547,21 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { const expression = callExpr.expression; const type = this.tsTypeChecker.getTypeAtLocation(expression); const typeText = this.tsTypeChecker.typeToString(type); - if (typeText === LIKE_FUNCTION) { + + if (LIKE_FUNCTION !== typeText) { + return; + } + if (ts.isNewExpression(expression) || ts.isCallExpression(expression)) { + const exprIndentifier = expression.expression; + const typeExprIndent = this.tsTypeChecker.getTypeAtLocation(exprIndentifier); + const typeTextExprIndent = this.tsTypeChecker.typeToString(typeExprIndent); + if (typeTextExprIndent === LIKE_FUNCTION_CONSTRUCTOR) { + this.incrementCounters(expression, FaultID.ExplicitFunctionType); + } + } else { const autofix = this.autofixer?.fixNoTsLikeFunctionCall(callExpr); this.incrementCounters(expression, FaultID.ExplicitFunctionType, autofix); - } + } } private handleAppStorageCallExpression(tsCallExpr: ts.CallExpression): void { diff --git a/ets2panda/linter/src/lib/utils/consts/LikeFunction.ts b/ets2panda/linter/src/lib/utils/consts/LikeFunction.ts index 669b24fd7b6c0af4fcac10613a84daf547c58aa8..38a013ece2d59a515c1962bb37e7041da0a0fa43 100755 --- a/ets2panda/linter/src/lib/utils/consts/LikeFunction.ts +++ b/ets2panda/linter/src/lib/utils/consts/LikeFunction.ts @@ -14,3 +14,5 @@ */ export const LIKE_FUNCTION = 'Function'; + +export const LIKE_FUNCTION_CONSTRUCTOR = 'FunctionConstructor'; diff --git a/ets2panda/linter/test/main/explicit_function_type.ets b/ets2panda/linter/test/main/explicit_function_type.ets index c42f90e3fbebdbd28ff66b836d2e4dad6853d178..9896f87a360f0fa363d365f9396d8edd0a6de033 100755 --- a/ets2panda/linter/test/main/explicit_function_type.ets +++ b/ets2panda/linter/test/main/explicit_function_type.ets @@ -177,4 +177,8 @@ fn29[1](); SysApiWrapper.setTimeout = (handler: Function | string, delay?: number): number => { (handler as Function)?.(); return Math.random(); -} \ No newline at end of file +} + +new Function('return 42')(); + +Function('return 42')(); \ No newline at end of file diff --git a/ets2panda/linter/test/main/explicit_function_type.ets.arkts2.json b/ets2panda/linter/test/main/explicit_function_type.ets.arkts2.json index 34273528033e6cc174e71dd6df5e600dc2e94486..1bafbdd1de860aa30d2aa4fd3815959c549155ea 100755 --- a/ets2panda/linter/test/main/explicit_function_type.ets.arkts2.json +++ b/ets2panda/linter/test/main/explicit_function_type.ets.arkts2.json @@ -353,6 +353,26 @@ "suggest": "", "rule": "The function type should be explicit (arkts-no-ts-like-function-call)", "severity": "ERROR" + }, + { + "line": 182, + "column": 1, + "endLine": 182, + "endColumn": 26, + "problem": "ExplicitFunctionType", + "suggest": "", + "rule": "The function type should be explicit (arkts-no-ts-like-function-call)", + "severity": "ERROR" + }, + { + "line": 184, + "column": 1, + "endLine": 184, + "endColumn": 22, + "problem": "ExplicitFunctionType", + "suggest": "", + "rule": "The function type should be explicit (arkts-no-ts-like-function-call)", + "severity": "ERROR" } ] } \ No newline at end of file diff --git a/ets2panda/linter/test/main/explicit_function_type.ets.autofix.json b/ets2panda/linter/test/main/explicit_function_type.ets.autofix.json index 9512b29aac946435a720b46d28464e7f675028ab..23f1af49211cac7790ce911cd388ed100ef0d475 100644 --- a/ets2panda/linter/test/main/explicit_function_type.ets.autofix.json +++ b/ets2panda/linter/test/main/explicit_function_type.ets.autofix.json @@ -573,6 +573,26 @@ "suggest": "", "rule": "The function type should be explicit (arkts-no-ts-like-function-call)", "severity": "ERROR" + }, + { + "line": 182, + "column": 1, + "endLine": 182, + "endColumn": 26, + "problem": "ExplicitFunctionType", + "suggest": "", + "rule": "The function type should be explicit (arkts-no-ts-like-function-call)", + "severity": "ERROR" + }, + { + "line": 184, + "column": 1, + "endLine": 184, + "endColumn": 22, + "problem": "ExplicitFunctionType", + "suggest": "", + "rule": "The function type should be explicit (arkts-no-ts-like-function-call)", + "severity": "ERROR" } ] } \ No newline at end of file diff --git a/ets2panda/linter/test/main/explicit_function_type.ets.migrate.ets b/ets2panda/linter/test/main/explicit_function_type.ets.migrate.ets index c5ecffb429d302f3dbed81aa57b822a0e37cfc5c..b9ac8ffe17c966926bb0ebaa801d4f790a66addb 100644 --- a/ets2panda/linter/test/main/explicit_function_type.ets.migrate.ets +++ b/ets2panda/linter/test/main/explicit_function_type.ets.migrate.ets @@ -177,4 +177,8 @@ fn29[1].unsafeCall(); SysApiWrapper.setTimeout = (handler: Function | string, delay?: number): number => { (handler as Function)?.unsafeCall(); return Math.random(); -} \ No newline at end of file +} + +new Function('return 42')(); + +Function('return 42')(); \ No newline at end of file diff --git a/ets2panda/linter/test/main/explicit_function_type.ets.migrate.json b/ets2panda/linter/test/main/explicit_function_type.ets.migrate.json index b4e8db486eeaedea242edff205286dd79e155a9f..e49a4f78fdfa8c381a28ca22ba63549747ac8016 100644 --- a/ets2panda/linter/test/main/explicit_function_type.ets.migrate.json +++ b/ets2panda/linter/test/main/explicit_function_type.ets.migrate.json @@ -153,6 +153,26 @@ "suggest": "", "rule": "Array bound not checked. (arkts-runtime-array-check)", "severity": "ERROR" + }, + { + "line": 182, + "column": 1, + "endLine": 182, + "endColumn": 26, + "problem": "ExplicitFunctionType", + "suggest": "", + "rule": "The function type should be explicit (arkts-no-ts-like-function-call)", + "severity": "ERROR" + }, + { + "line": 184, + "column": 1, + "endLine": 184, + "endColumn": 22, + "problem": "ExplicitFunctionType", + "suggest": "", + "rule": "The function type should be explicit (arkts-no-ts-like-function-call)", + "severity": "ERROR" } ] } \ No newline at end of file