From 915194fedc465705df48ca0eb08c09881d2f8fb2 Mon Sep 17 00:00:00 2001 From: muratcimen_9be2 Date: Fri, 27 Jun 2025 16:42:20 +0300 Subject: [PATCH] Added invalid export alias check Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICIBNF Signed-off-by: muratcimen_9be2 --- ets2panda/linter/src/lib/TypeScriptLinter.ts | 66 +++++++----- .../consts/InValidIndentifierKeywords.ts | 4 +- .../linter/test/main/invalid_identifier.ets | 24 ++++- .../main/invalid_identifier.ets.arkts2.json | 100 ++++++++++++++++++ .../linter/test/rules/rule207.ets.arkts2.json | 12 ++- 5 files changed, 175 insertions(+), 31 deletions(-) diff --git a/ets2panda/linter/src/lib/TypeScriptLinter.ts b/ets2panda/linter/src/lib/TypeScriptLinter.ts index 2ba0b44631..a32406e056 100644 --- a/ets2panda/linter/src/lib/TypeScriptLinter.ts +++ b/ets2panda/linter/src/lib/TypeScriptLinter.ts @@ -3465,12 +3465,12 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { continue; } - const baseMethodDecl = baseMethod.declarations?.find( - (d) => { - return (ts.isMethodDeclaration(d) || ts.isMethodSignature(d)) && - this.tsTypeChecker.getTypeAtLocation(d.parent) === baseType; - } - ) as ts.MethodDeclaration | ts.MethodSignature; + const baseMethodDecl = baseMethod.declarations?.find((d) => { + return ( + (ts.isMethodDeclaration(d) || ts.isMethodSignature(d)) && + this.tsTypeChecker.getTypeAtLocation(d.parent) === baseType + ); + }) as ts.MethodDeclaration | ts.MethodSignature; if (!baseMethodDecl) { continue; @@ -5255,18 +5255,19 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { const resolveParam = executor.parameters[0]; if (resolveParam?.type) { - if (ts.isFunctionTypeNode(resolveParam.type) && - resolveParam.type.parameters.length === 0) { - this.incrementCounters(resolveParam.type,FaultID.PromiseVoidNeedResolveArg); + if (ts.isFunctionTypeNode(resolveParam.type) && resolveParam.type.parameters.length === 0) { + this.incrementCounters(resolveParam.type, FaultID.PromiseVoidNeedResolveArg); } } if (executor.body) { - ts.forEachChild(executor.body, node => { - if (ts.isCallExpression(node) && + ts.forEachChild(executor.body, (node) => { + if ( + ts.isCallExpression(node) && ts.isIdentifier(node.expression) && node.expression.text === 'resolve' && - node.arguments.length === 0) { - this.incrementCounters(node,FaultID.PromiseVoidNeedResolveArg); + node.arguments.length === 0 + ) { + this.incrementCounters(node, FaultID.PromiseVoidNeedResolveArg); } }); } @@ -6173,6 +6174,8 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { private handleExportDeclaration(node: ts.Node): void { const exportDecl = node as ts.ExportDeclaration; + this.handleInvalidIdentifier(exportDecl); + if (this.isExportedEntityDeclaredInJs(exportDecl)) { this.incrementCounters(node, FaultID.InteropJsObjectExport); return; @@ -7084,18 +7087,17 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { | ts.EnumMember | ts.ModuleDeclaration | ts.InterfaceDeclaration + | ts.ExportDeclaration ): void { if (!this.options.arkts2) { return; } - const checkIdentifier = (identifier: ts.Identifier | undefined): void => { const text = identifier && ts.isIdentifier(identifier) ? identifier.text : ''; if (identifier && text && INVALID_IDENTIFIER_KEYWORDS.includes(text)) { this.incrementCounters(identifier, FaultID.InvalidIdentifier); } }; - if (ts.isImportDeclaration(decl)) { const importClause = decl.importClause; if (importClause?.namedBindings && ts.isNamedImports(importClause?.namedBindings)) { @@ -7104,6 +7106,12 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { }); } checkIdentifier(importClause?.name); + } else if (ts.isExportDeclaration(decl)) { + if (decl.exportClause && ts.isNamedExports(decl.exportClause)) { + for (const exportSpecifier of decl.exportClause.elements) { + checkIdentifier(exportSpecifier.name); + } + } } else if (isStructDeclaration(decl)) { checkIdentifier((decl as ts.StructDeclaration).name); } else { @@ -8097,7 +8105,7 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { if (matchedApi) { this.incrementCounters(errorNode, faultId); } - } + } } private isIdentifierFromSDK(node: ts.Node): boolean { @@ -8114,18 +8122,20 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { 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)) { + if ( + ts.isVariableDeclaration(declaration) || + ts.isTypeAliasDeclaration(declaration) || + ts.isClassDeclaration(declaration) || + ts.isInterfaceDeclaration(declaration) || + ts.isFunctionDeclaration(declaration) || + ts.isEnumDeclaration(declaration) + ) { isLocal = true; - break + break; } } - if(isLocal) { + if (isLocal) { return false; } @@ -8208,7 +8218,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); } } @@ -9809,7 +9819,7 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { this.incrementCounters(tsAsExpr, FaultID.NoTsLikeSmartType); } } - + private handleAssignmentNotsLikeSmartType(tsBinaryExpr: ts.BinaryExpression): void { if (!this.options.arkts2) { return; @@ -9826,9 +9836,9 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { } // Handle both regular assignment and 'as' type assertion - let right: ts.Expression = ts.isAsExpression(node.right) ? node.right.expression : node.right; + const right: ts.Expression = ts.isAsExpression(node.right) ? node.right.expression : node.right; if (!ts.isPropertyAccessExpression(right)) { - return false; + return false; } const propertyName = right.name; diff --git a/ets2panda/linter/src/lib/utils/consts/InValidIndentifierKeywords.ts b/ets2panda/linter/src/lib/utils/consts/InValidIndentifierKeywords.ts index 072f4a88c4..8a802da782 100755 --- a/ets2panda/linter/src/lib/utils/consts/InValidIndentifierKeywords.ts +++ b/ets2panda/linter/src/lib/utils/consts/InValidIndentifierKeywords.ts @@ -120,5 +120,7 @@ export const INVALID_IDENTIFIER_KEYWORDS = [ 'AsyncIterator', 'NewableFunction', 'CallableFunction', - 'PropertyDescriptor' + 'PropertyDescriptor', + 'arguments', + 'eval' ]; diff --git a/ets2panda/linter/test/main/invalid_identifier.ets b/ets2panda/linter/test/main/invalid_identifier.ets index ade4397a20..43962333ff 100755 --- a/ets2panda/linter/test/main/invalid_identifier.ets +++ b/ets2panda/linter/test/main/invalid_identifier.ets @@ -218,4 +218,26 @@ namespace quarantine { interface test { int: int } -} \ No newline at end of file +} + +let _if = null; +let _import = null; +let _export = null; +let _await = null; +let _arguments = null; +let _eval = null; +let _default = null; +let _as = null; + +export { +_if as if, +_import as import, +_export as export, +_await as await, +_arguments as arguments, +_eval as eval, +_default as default, +_as as as +}; + +let a = 1; \ No newline at end of file diff --git a/ets2panda/linter/test/main/invalid_identifier.ets.arkts2.json b/ets2panda/linter/test/main/invalid_identifier.ets.arkts2.json index 8d294f2636..73ba326aa5 100644 --- a/ets2panda/linter/test/main/invalid_identifier.ets.arkts2.json +++ b/ets2panda/linter/test/main/invalid_identifier.ets.arkts2.json @@ -864,6 +864,106 @@ "rule": "This keyword cannot be used as identifiers (arkts-invalid-identifier)", "severity": "ERROR" }, + { + "line": 233, + "column": 8, + "endLine": 233, + "endColumn": 10, + "problem": "InvalidIdentifier", + "suggest": "", + "rule": "This keyword cannot be used as identifiers (arkts-invalid-identifier)", + "severity": "ERROR" + }, + { + "line": 234, + "column": 12, + "endLine": 234, + "endColumn": 18, + "problem": "InvalidIdentifier", + "suggest": "", + "rule": "This keyword cannot be used as identifiers (arkts-invalid-identifier)", + "severity": "ERROR" + }, + { + "line": 235, + "column": 12, + "endLine": 235, + "endColumn": 18, + "problem": "InvalidIdentifier", + "suggest": "", + "rule": "This keyword cannot be used as identifiers (arkts-invalid-identifier)", + "severity": "ERROR" + }, + { + "line": 236, + "column": 11, + "endLine": 236, + "endColumn": 16, + "problem": "InvalidIdentifier", + "suggest": "", + "rule": "This keyword cannot be used as identifiers (arkts-invalid-identifier)", + "severity": "ERROR" + }, + { + "line": 237, + "column": 15, + "endLine": 237, + "endColumn": 24, + "problem": "InvalidIdentifier", + "suggest": "", + "rule": "This keyword cannot be used as identifiers (arkts-invalid-identifier)", + "severity": "ERROR" + }, + { + "line": 238, + "column": 10, + "endLine": 238, + "endColumn": 14, + "problem": "InvalidIdentifier", + "suggest": "", + "rule": "This keyword cannot be used as identifiers (arkts-invalid-identifier)", + "severity": "ERROR" + }, + { + "line": 239, + "column": 13, + "endLine": 239, + "endColumn": 20, + "problem": "InvalidIdentifier", + "suggest": "", + "rule": "This keyword cannot be used as identifiers (arkts-invalid-identifier)", + "severity": "ERROR" + }, + { + "line": 240, + "column": 8, + "endLine": 240, + "endColumn": 10, + "problem": "InvalidIdentifier", + "suggest": "", + "rule": "This keyword cannot be used as identifiers (arkts-invalid-identifier)", + "severity": "ERROR" + }, + { + "line": 243, + "column": 5, + "endLine": 243, + "endColumn": 10, + "problem": "NumericSemantics", + "suggest": "", + "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", + "severity": "ERROR" + }, + { + "line": 243, + "column": 9, + "endLine": 243, + "endColumn": 10, + "problem": "NumericSemantics", + "suggest": "", + "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", + "severity": "ERROR" + }, { "line": 172, "column": 2, diff --git a/ets2panda/linter/test/rules/rule207.ets.arkts2.json b/ets2panda/linter/test/rules/rule207.ets.arkts2.json index aa91c408bd..13570b31ed 100644 --- a/ets2panda/linter/test/rules/rule207.ets.arkts2.json +++ b/ets2panda/linter/test/rules/rule207.ets.arkts2.json @@ -1,5 +1,5 @@ { - "result": [ +"result": [ { "line": 17, "column": 17, @@ -260,6 +260,16 @@ "rule": "Special arguments object inside functions are not supported (arkts-no-arguments-obj)", "severity": "ERROR" }, + { + "line": 65, + "column": 3, + "endLine": 65, + "endColumn": 12, + "problem": "InvalidIdentifier", + "suggest": "", + "rule": "This keyword cannot be used as identifiers (arkts-invalid-identifier)", + "severity": "ERROR" + }, { "line": 68, "column": 22, -- Gitee