diff --git a/ets2panda/linter/src/lib/TypeScriptLinter.ts b/ets2panda/linter/src/lib/TypeScriptLinter.ts index 84b5a362bffd31b3599d67e33eaa79e5b47b21d1..a7a275982d28a7aadee02e7b0aca60496203d381 100644 --- a/ets2panda/linter/src/lib/TypeScriptLinter.ts +++ b/ets2panda/linter/src/lib/TypeScriptLinter.ts @@ -146,6 +146,7 @@ import { globalApiAssociatedInfo } from './utils/consts/AssociatedInfo'; import { ARRAY_API_LIST } from './utils/consts/ArraysAPI'; import { ERROR_PROP_LIST } from './utils/consts/ErrorProp'; import { D_ETS, D_TS } from './utils/consts/TsSuffix'; +import { arkTsBuiltInTypeName } from './utils/consts/ArkuiImportList'; interface InterfaceSymbolTypeResult { propNames: string[]; @@ -8049,20 +8050,58 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { if (!hasSameApiName) { return; } - if (ts.isTypeReferenceNode(errorNode)) { errorNode = errorNode.typeName; } - const matchedApi = apiNamesArr.some((sdkInfo) => { const isSameName = sdkInfo.api_info.api_name === apiName; const isGlobal = sdkInfo.is_global; return isSameName && isGlobal; }); + const checkSymbol = this.isIdentifierFromSDK(errorNode); + const type = this.tsTypeChecker.getTypeAtLocation(errorNode); + const typeName = this.tsTypeChecker.typeToString(type); - if (matchedApi) { - this.incrementCounters(errorNode, faultId); + if (checkSymbol) { + if (arkTsBuiltInTypeName.has(typeName)) { + return; + } + if (matchedApi) { + this.incrementCounters(errorNode, faultId); + } + } + } + + private isIdentifierFromSDK(node: ts.Node): boolean { + const symbol = this.tsTypeChecker.getSymbolAtLocation(node); + if (!symbol) { + return true; } + + // Check if the symbol is from an SDK import + const declarations = symbol.getDeclarations(); + if (!declarations || declarations.length === 0) { + return true; + } + + let isLocal = false; + for (const declaration of declarations) { + if (ts.isVariableDeclaration(declaration) || + ts.isTypeAliasDeclaration(declaration) || + ts.isClassDeclaration(declaration) || + ts.isInterfaceDeclaration(declaration) || + ts.isFunctionDeclaration(declaration) || + ts.isEnumDeclaration(declaration)) { + isLocal = true; + break + } + } + + if(isLocal) { + return false; + } + + return true; } private handleSdkGlobalApi( @@ -8141,6 +8180,7 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { private checkCallExpressionForSdkGlobalApi(node: ts.CallExpression): void { if (ts.isPropertyAccessExpression(node.expression) && ts.isIdentifier(node.expression.expression)) { const expression = node.expression.expression; + this.processApiNodeSdkGlobalApi(expression.text, expression); } } diff --git a/ets2panda/linter/src/lib/utils/consts/ArkuiImportList.ts b/ets2panda/linter/src/lib/utils/consts/ArkuiImportList.ts index cfe9c6eff057e3fac27b0f0949c96b0dc31bb98a..3beae87577365b666ebf9f1722014fe7b1b260cf 100644 --- a/ets2panda/linter/src/lib/utils/consts/ArkuiImportList.ts +++ b/ets2panda/linter/src/lib/utils/consts/ArkuiImportList.ts @@ -1614,3 +1614,54 @@ export const arkuiImportList: Set = new Set([ 'sharedTransitionOptions', 'vp2px' ]); + +export const arkTsBuiltInTypeName: Set = new Set([ + 'Object', + 'Function', + 'Boolean', + 'Symbol', + 'Number', + 'BigInt', + 'Math', + 'Date', + 'String', + 'RegExp', + 'Array', + 'Int8Array', + 'Uint8Array', + 'Uint8ClampedArray', + 'Int16Array', + 'Uint16Array', + 'Int32Array', + 'Uint32Array', + 'Float32Array', + 'Float64Array', + 'BigInt64Array', + 'BigUint64Array', + 'Map', + 'Set', + 'WeakMap', + 'WeakSet', + 'ArrayBuffer', + 'SharedArrayBuffer', + 'DataView', + 'JSON', + 'Promise', + 'Generator', + 'GeneratorFunction', + 'AsyncFunction', + 'AsyncGenerator', + 'AsyncGeneratorFunction', + 'Reflect', + 'Proxy', + 'Error', + 'EvalError', + 'RangeError', + 'ReferenceError', + 'SyntaxError', + 'TypeError', + 'URIError', + 'AggregateError', + 'Intl', + 'WebAssembly' +]); \ No newline at end of file diff --git a/ets2panda/linter/test/sdkwhite/decl_with_duplicate_name_sdk.ets b/ets2panda/linter/test/sdkwhite/decl_with_duplicate_name_sdk.ets index 4584cafa7b4940bab8a405ecae1f87d510783d5a..43e59da7f5025136aa713e61673f1e4908030d96 100755 --- a/ets2panda/linter/test/sdkwhite/decl_with_duplicate_name_sdk.ets +++ b/ets2panda/linter/test/sdkwhite/decl_with_duplicate_name_sdk.ets @@ -14,15 +14,11 @@ */ 'use static' -import { TextStyle } from './sdk/declarations/styled_string' -import { HyperlinkAttribute, HyperlinkInterface } from './sdk/declarations/hyperlink' - @Entry @Component struct Index { fontStyleAttr1: TextStyle = new TextStyle({fontColor: Color.Blue}); // Error(2) fontStyleAttr2: TextStyle; // Error - @State styleList: Array = new Array(); // Error aboutToAppear() { for (let i = 15; i < 50; i++) @@ -47,14 +43,26 @@ class TextStyleDemo2 extends TextStyle { // Error } } -let hyperlinkAttr1: HyperlinkAttribute = HyperlinkInterface; -let hyperlinkAttr2: HyperlinkAttribute = HyperlinkInterface.color(''); +let hyperlinkAttr1: HyperlinkAttribute = HyperlinkInterface; // Error +let hyperlinkAttr2: HyperlinkAttribute = HyperlinkInterface.color(''); // Error class HyperlinkTest { - prop1: HyperlinkAttribute = HyperlinkInterface; - prop2: HyperlinkAttribute = HyperlinkInterface.color(''); + prop1: HyperlinkAttribute = HyperlinkInterface; // Error + prop2: HyperlinkAttribute = HyperlinkInterface.color(''); // Error } let hyperlinkAttr3: HyperlinkAttribute; -hyperlinkAttr3 = HyperlinkInterface; -function func(x = HyperlinkInterface) { +hyperlinkAttr3 = HyperlinkInterface; // Error +function func(x = HyperlinkInterface) { // Error + +} +class A { + onBackPress: Function; + TextStyle: Function; + constructor(onBackPress: Function, TextStyle: Function) { + this.onBackPress = onBackPress; // no Error + this.TextStyle = TextStyle; // no Error + } +} +function tt(a: A) { + a.onBackPress(); } diff --git a/ets2panda/linter/test/sdkwhite/decl_with_duplicate_name_sdk.ets.arkts2.json b/ets2panda/linter/test/sdkwhite/decl_with_duplicate_name_sdk.ets.arkts2.json index 64896441f073959258440bf2a73e3f2e1a65ec78..d351a537bd6a910bd2443741e098df05f14db2d7 100644 --- a/ets2panda/linter/test/sdkwhite/decl_with_duplicate_name_sdk.ets.arkts2.json +++ b/ets2panda/linter/test/sdkwhite/decl_with_duplicate_name_sdk.ets.arkts2.json @@ -15,29 +15,9 @@ ], "result": [ { - "line": 17, - "column": 1, - "endLine": 17, - "endColumn": 61, - "problem": "ImportAfterStatement", - "suggest": "", - "rule": "\"import\" statements after other statements are not allowed (arkts-no-misplaced-imports)", - "severity": "ERROR" - }, - { - "line": 18, - "column": 1, - "endLine": 18, - "endColumn": 86, - "problem": "ImportAfterStatement", - "suggest": "", - "rule": "\"import\" statements after other statements are not allowed (arkts-no-misplaced-imports)", - "severity": "ERROR" - }, - { - "line": 23, + "line": 20, "column": 19, - "endLine": 23, + "endLine": 20, "endColumn": 28, "problem": "DuplicateDeclNameFromSdk", "suggest": "", @@ -45,9 +25,9 @@ "severity": "ERROR" }, { - "line": 23, + "line": 20, "column": 35, - "endLine": 23, + "endLine": 20, "endColumn": 44, "problem": "DuplicateDeclNameFromSdk", "suggest": "", @@ -55,9 +35,19 @@ "severity": "ERROR" }, { - "line": 24, + "line": 20, + "column": 35, + "endLine": 20, + "endColumn": 44, + "problem": "DynamicCtorCall", + "suggest": "", + "rule": "\"new\" expression with dynamic constructor type is not supported (arkts-no-dynamic-ctor-call)", + "severity": "ERROR" + }, + { + "line": 21, "column": 19, - "endLine": 24, + "endLine": 21, "endColumn": 28, "problem": "DuplicateDeclNameFromSdk", "suggest": "", @@ -65,9 +55,9 @@ "severity": "ERROR" }, { - "line": 26, + "line": 22, "column": 27, - "endLine": 26, + "endLine": 22, "endColumn": 36, "problem": "DuplicateDeclNameFromSdk", "suggest": "", @@ -75,9 +65,9 @@ "severity": "ERROR" }, { - "line": 26, + "line": 22, "column": 40, - "endLine": 26, + "endLine": 22, "endColumn": 51, "problem": "GenericCallNoTypeArgs", "suggest": "", @@ -85,9 +75,9 @@ "severity": "ERROR" }, { - "line": 28, + "line": 24, "column": 14, - "endLine": 28, + "endLine": 24, "endColumn": 20, "problem": "NumericSemantics", "suggest": "", @@ -95,9 +85,9 @@ "severity": "ERROR" }, { - "line": 28, + "line": 24, "column": 18, - "endLine": 28, + "endLine": 24, "endColumn": 20, "problem": "NumericSemantics", "suggest": "", @@ -105,9 +95,9 @@ "severity": "ERROR" }, { - "line": 28, + "line": 24, "column": 26, - "endLine": 28, + "endLine": 24, "endColumn": 28, "problem": "NumericSemantics", "suggest": "", @@ -115,9 +105,9 @@ "severity": "ERROR" }, { - "line": 29, + "line": 25, "column": 31, - "endLine": 29, + "endLine": 25, "endColumn": 40, "problem": "DuplicateDeclNameFromSdk", "suggest": "", @@ -125,9 +115,19 @@ "severity": "ERROR" }, { - "line": 34, + "line": 25, + "column": 31, + "endLine": 25, + "endColumn": 40, + "problem": "DynamicCtorCall", + "suggest": "", + "rule": "\"new\" expression with dynamic constructor type is not supported (arkts-no-dynamic-ctor-call)", + "severity": "ERROR" + }, + { + "line": 30, "column": 38, - "endLine": 34, + "endLine": 30, "endColumn": 47, "problem": "DuplicateDeclNameFromSdk", "suggest": "", @@ -135,9 +135,9 @@ "severity": "ERROR" }, { - "line": 44, + "line": 40, "column": 30, - "endLine": 44, + "endLine": 40, "endColumn": 39, "problem": "DuplicateDeclNameFromSdk", "suggest": "", @@ -145,9 +145,9 @@ "severity": "ERROR" }, { - "line": 50, + "line": 46, "column": 42, - "endLine": 50, + "endLine": 46, "endColumn": 60, "problem": "DuplicateDeclNameFromSdk", "suggest": "", @@ -155,9 +155,9 @@ "severity": "ERROR" }, { - "line": 51, + "line": 47, "column": 42, - "endLine": 51, + "endLine": 47, "endColumn": 60, "problem": "DuplicateDeclNameFromSdk", "suggest": "", @@ -165,9 +165,9 @@ "severity": "ERROR" }, { - "line": 53, + "line": 49, "column": 31, - "endLine": 53, + "endLine": 49, "endColumn": 49, "problem": "DuplicateDeclNameFromSdk", "suggest": "", @@ -175,9 +175,9 @@ "severity": "ERROR" }, { - "line": 54, + "line": 50, "column": 31, - "endLine": 54, + "endLine": 50, "endColumn": 49, "problem": "DuplicateDeclNameFromSdk", "suggest": "", @@ -185,9 +185,9 @@ "severity": "ERROR" }, { - "line": 57, + "line": 53, "column": 18, - "endLine": 57, + "endLine": 53, "endColumn": 36, "problem": "DuplicateDeclNameFromSdk", "suggest": "", @@ -195,9 +195,19 @@ "severity": "ERROR" }, { - "line": 58, + "line": 54, + "column": 15, + "endLine": 54, + "endColumn": 37, + "problem": "AnyType", + "suggest": "", + "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)", + "severity": "ERROR" + }, + { + "line": 54, "column": 19, - "endLine": 58, + "endLine": 54, "endColumn": 37, "problem": "DuplicateDeclNameFromSdk", "suggest": "", @@ -205,9 +215,19 @@ "severity": "ERROR" }, { - "line": 20, + "line": 67, + "column": 3, + "endLine": 67, + "endColumn": 16, + "problem": "ExplicitFunctionType", + "suggest": "", + "rule": "The function type should be explicit (arkts-no-ts-like-function-call)", + "severity": "ERROR" + }, + { + "line": 17, "column": 2, - "endLine": 20, + "endLine": 17, "endColumn": 7, "problem": "UIInterfaceImport", "suggest": "", @@ -215,9 +235,9 @@ "severity": "ERROR" }, { - "line": 21, + "line": 18, "column": 2, - "endLine": 21, + "endLine": 18, "endColumn": 11, "problem": "UIInterfaceImport", "suggest": "", @@ -225,9 +245,29 @@ "severity": "ERROR" }, { - "line": 23, + "line": 20, + "column": 19, + "endLine": 20, + "endColumn": 28, + "problem": "UIInterfaceImport", + "suggest": "", + "rule": "The ArkUI interface \"TextStyle\" should be imported before it is used (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 20, + "column": 35, + "endLine": 20, + "endColumn": 44, + "problem": "UIInterfaceImport", + "suggest": "", + "rule": "The ArkUI interface \"TextStyle\" should be imported before it is used (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 20, "column": 57, - "endLine": 23, + "endLine": 20, "endColumn": 62, "problem": "UIInterfaceImport", "suggest": "", @@ -235,9 +275,19 @@ "severity": "ERROR" }, { - "line": 26, + "line": 21, + "column": 19, + "endLine": 21, + "endColumn": 28, + "problem": "UIInterfaceImport", + "suggest": "", + "rule": "The ArkUI interface \"TextStyle\" should be imported before it is used (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 22, "column": 4, - "endLine": 26, + "endLine": 22, "endColumn": 9, "problem": "UIInterfaceImport", "suggest": "", @@ -245,9 +295,29 @@ "severity": "ERROR" }, { - "line": 33, + "line": 22, + "column": 27, + "endLine": 22, + "endColumn": 36, + "problem": "UIInterfaceImport", + "suggest": "", + "rule": "The ArkUI interface \"TextStyle\" should be imported before it is used (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 25, + "column": 31, + "endLine": 25, + "endColumn": 40, + "problem": "UIInterfaceImport", + "suggest": "", + "rule": "The ArkUI interface \"TextStyle\" should be imported before it is used (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 29, "column": 5, - "endLine": 33, + "endLine": 29, "endColumn": 9, "problem": "UIInterfaceImport", "suggest": "", @@ -255,9 +325,9 @@ "severity": "ERROR" }, { - "line": 34, + "line": 30, "column": 7, - "endLine": 34, + "endLine": 30, "endColumn": 14, "problem": "UIInterfaceImport", "suggest": "", @@ -265,9 +335,19 @@ "severity": "ERROR" }, { - "line": 35, + "line": 30, + "column": 38, + "endLine": 30, + "endColumn": 47, + "problem": "UIInterfaceImport", + "suggest": "", + "rule": "The ArkUI interface \"TextStyle\" should be imported before it is used (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 31, "column": 9, - "endLine": 35, + "endLine": 31, "endColumn": 17, "problem": "UIInterfaceImport", "suggest": "", @@ -275,9 +355,9 @@ "severity": "ERROR" }, { - "line": 36, + "line": 32, "column": 11, - "endLine": 36, + "endLine": 32, "endColumn": 15, "problem": "UIInterfaceImport", "suggest": "", @@ -285,13 +365,63 @@ "severity": "ERROR" }, { - "line": 24, - "column": 3, - "endLine": 24, - "endColumn": 17, - "problem": "StrictDiagnostic", - "suggest": "Property 'fontStyleAttr2' has no initializer and is not definitely assigned in the constructor.", - "rule": "Property 'fontStyleAttr2' has no initializer and is not definitely assigned in the constructor.", + "line": 40, + "column": 30, + "endLine": 40, + "endColumn": 39, + "problem": "UIInterfaceImport", + "suggest": "", + "rule": "The ArkUI interface \"TextStyle\" should be imported before it is used (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 46, + "column": 21, + "endLine": 46, + "endColumn": 39, + "problem": "UIInterfaceImport", + "suggest": "", + "rule": "The ArkUI interface \"HyperlinkAttribute\" should be imported before it is used (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 47, + "column": 21, + "endLine": 47, + "endColumn": 39, + "problem": "UIInterfaceImport", + "suggest": "", + "rule": "The ArkUI interface \"HyperlinkAttribute\" should be imported before it is used (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 49, + "column": 10, + "endLine": 49, + "endColumn": 28, + "problem": "UIInterfaceImport", + "suggest": "", + "rule": "The ArkUI interface \"HyperlinkAttribute\" should be imported before it is used (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 50, + "column": 10, + "endLine": 50, + "endColumn": 28, + "problem": "UIInterfaceImport", + "suggest": "", + "rule": "The ArkUI interface \"HyperlinkAttribute\" should be imported before it is used (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 52, + "column": 21, + "endLine": 52, + "endColumn": 39, + "problem": "UIInterfaceImport", + "suggest": "", + "rule": "The ArkUI interface \"HyperlinkAttribute\" should be imported before it is used (arkui-modular-interface)", "severity": "ERROR" } ] diff --git a/ets2panda/linter/test/sdkwhite/decl_with_duplicate_name_sdk.ets.json b/ets2panda/linter/test/sdkwhite/decl_with_duplicate_name_sdk.ets.json index 489a1474b5a6f1db71bdf86fbf3b06b773ea21c8..4e18966ecf9476bb11e1fa1f33245984342a792d 100755 --- a/ets2panda/linter/test/sdkwhite/decl_with_duplicate_name_sdk.ets.json +++ b/ets2panda/linter/test/sdkwhite/decl_with_duplicate_name_sdk.ets.json @@ -15,33 +15,13 @@ ], "result": [ { - "line": 17, - "column": 1, - "endLine": 17, - "endColumn": 61, - "problem": "ImportAfterStatement", + "line": 54, + "column": 15, + "endLine": 54, + "endColumn": 37, + "problem": "AnyType", "suggest": "", - "rule": "\"import\" statements after other statements are not allowed (arkts-no-misplaced-imports)", - "severity": "ERROR" - }, - { - "line": 18, - "column": 1, - "endLine": 18, - "endColumn": 86, - "problem": "ImportAfterStatement", - "suggest": "", - "rule": "\"import\" statements after other statements are not allowed (arkts-no-misplaced-imports)", - "severity": "ERROR" - }, - { - "line": 24, - "column": 3, - "endLine": 24, - "endColumn": 17, - "problem": "StrictDiagnostic", - "suggest": "Property 'fontStyleAttr2' has no initializer and is not definitely assigned in the constructor.", - "rule": "Property 'fontStyleAttr2' has no initializer and is not definitely assigned in the constructor.", + "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)", "severity": "ERROR" } ]