From 33a07740ad0e4c8985c0826dd38a1856e9c1f63a Mon Sep 17 00:00:00 2001 From: Igor Rossinski Date: Thu, 26 Oct 2023 02:48:24 +0300 Subject: [PATCH] [ArkTS Linter] fix #13851 Signed-off-by: Igor Rossinski --- linter-4.2/src/TypeScriptLinter.ts | 16 +++++++++------- linter-4.2/test/ts_ignore.ts.relax.json | 5 ----- linter-4.2/test/ts_ignore.ts.strict.json | 5 ----- linter/src/TypeScriptLinter.ts | 23 +++++++++++++---------- linter/test/ts_ignore.ts.relax.json | 5 ----- linter/test/ts_ignore.ts.strict.json | 5 ----- 6 files changed, 22 insertions(+), 37 deletions(-) diff --git a/linter-4.2/src/TypeScriptLinter.ts b/linter-4.2/src/TypeScriptLinter.ts index 27442da9f..dc58f202d 100644 --- a/linter-4.2/src/TypeScriptLinter.ts +++ b/linter-4.2/src/TypeScriptLinter.ts @@ -677,7 +677,7 @@ export class TypeScriptLinter { if (!!baseExprSym && this.tsUtils.symbolHasEsObjectType(baseExprSym)) { this.incrementCounters(propertyAccessNode, FaultID.EsObjectType); } - if (this.isPrototypePropertyAccess(propertyAccessNode, exprSym, baseExprSym, baseExprType)) { + if (this.isPrototypePropertyAccess(propertyAccessNode, exprSym, baseExprSym, baseExprType)) { this.incrementCounters(propertyAccessNode.name, FaultID.Prototype); } } @@ -759,7 +759,7 @@ export class TypeScriptLinter { ts.isIdentifier(x.expression.expression) ) decoratorName = x.expression.expression.text; - + // special case for property of type CustomDialogController of the @CustomDialog-decorated class if (expectedDecorators.includes(TsUtils.NON_INITIALIZABLE_PROPERTY_CLASS_DECORATORS[0])) { return expectedDecorators.includes(decoratorName) && propType === 'CustomDialogController' @@ -1596,7 +1596,7 @@ export class TypeScriptLinter { ); const checkClassOrInterface = tsElemAccessBaseExprType.isClassOrInterface() && !this.tsUtils.isGenericArrayType(tsElemAccessBaseExprType) && - !this.tsUtils.isDerivedFrom(tsElemAccessBaseExprType, CheckType.Array); + !this.tsUtils.isDerivedFrom(tsElemAccessBaseExprType, CheckType.Array); const checkThisOrSuper = this.tsUtils.isThisOrSuperExpr(tsElementAccessExpr.expression) && !this.tsUtils.isDerivedFrom(tsElemAccessBaseExprType, CheckType.Array); @@ -1699,7 +1699,7 @@ export class TypeScriptLinter { this.handleImportCall(tsCallExpr); this.handleRequireCall(tsCallExpr); - if (!!calleeSym) { + if (!!calleeSym) { if (this.tsUtils.symbolHasEsObjectType(calleeSym)) { this.incrementCounters(tsCallExpr, FaultID.EsObjectType); } @@ -2050,8 +2050,10 @@ export class TypeScriptLinter { comment.kind === ts.SyntaxKind.MultiLineCommentTrivia ? srcText.slice(comment.pos + 2, comment.end - 2) : srcText.slice(comment.pos + 2, comment.end); - - let trimmedContent = commentContent.trim(); + //if comment is multiline end closing '*/' is not at the same line as '@ts-xxx' - do nothing (see #13851) + if (commentContent.endsWith('\n')) + return; + const trimmedContent = commentContent.trim(); if ( trimmedContent.startsWith("@ts-ignore") || trimmedContent.startsWith("@ts-nocheck") || @@ -2115,7 +2117,7 @@ export class TypeScriptLinter { // option is enabled, compiler attempts to infer type from variable references: // see https://github.com/microsoft/TypeScript/pull/11263. // In this case, we still want to report the error, since ArkTS doesn't allow - // to omit both type annotation and initializer. + // to omit both type annotation and initializer. if (((ts.isVariableDeclaration(decl) && ts.isVariableStatement(decl.parent.parent)) || ts.isPropertyDeclaration(decl)) && !decl.initializer) { this.incrementCounters(decl, FaultID.AnyType); diff --git a/linter-4.2/test/ts_ignore.ts.relax.json b/linter-4.2/test/ts_ignore.ts.relax.json index d1f517671..787d4b0e1 100644 --- a/linter-4.2/test/ts_ignore.ts.relax.json +++ b/linter-4.2/test/ts_ignore.ts.relax.json @@ -34,11 +34,6 @@ "column": 9, "problem": "ErrorSuppression" }, - { - "line": 33, - "column": 2, - "problem": "ErrorSuppression" - }, { "line": 43, "column": 9, diff --git a/linter-4.2/test/ts_ignore.ts.strict.json b/linter-4.2/test/ts_ignore.ts.strict.json index d1f517671..787d4b0e1 100644 --- a/linter-4.2/test/ts_ignore.ts.strict.json +++ b/linter-4.2/test/ts_ignore.ts.strict.json @@ -34,11 +34,6 @@ "column": 9, "problem": "ErrorSuppression" }, - { - "line": 33, - "column": 2, - "problem": "ErrorSuppression" - }, { "line": 43, "column": 9, diff --git a/linter/src/TypeScriptLinter.ts b/linter/src/TypeScriptLinter.ts index 15d018ed5..2ae868924 100644 --- a/linter/src/TypeScriptLinter.ts +++ b/linter/src/TypeScriptLinter.ts @@ -582,7 +582,7 @@ export class TypeScriptLinter { if (!!baseExprSym && this.tsUtils.symbolHasEsObjectType(baseExprSym)) { this.incrementCounters(propertyAccessNode, FaultID.EsObjectType); } - if (this.isPrototypePropertyAccess(propertyAccessNode, exprSym, baseExprSym, baseExprType)) { + if (this.isPrototypePropertyAccess(propertyAccessNode, exprSym, baseExprSym, baseExprType)) { this.incrementCounters(propertyAccessNode.name, FaultID.Prototype); } if (TypeScriptLinter.advancedClassChecks && this.tsUtils.isClassObjectExpression(propertyAccessNode.expression)) { @@ -617,7 +617,7 @@ export class TypeScriptLinter { this.handleDecorators(decorators); this.filterOutDecoratorsDiagnostics(decorators, NON_INITIALIZABLE_PROPERTY_DECORATORS, {begin: propName.getStart(), end: propName.getStart()}, PROPERTY_HAS_NO_INITIALIZER_ERROR_CODE); - + const classDecorators = ts.getDecorators(node.parent); const propType = (node as ts.PropertyDeclaration).type?.getText(); this.filterOutDecoratorsDiagnostics(classDecorators, NON_INITIALIZABLE_PROPERTY_CLASS_DECORATORS, @@ -1273,7 +1273,7 @@ export class TypeScriptLinter { private handleRestrictedValues(tsIdentifier: ts.Identifier, tsIdentSym: ts.Symbol) { const illegalValues = ts.SymbolFlags.ConstEnum | ts.SymbolFlags.RegularEnum | ts.SymbolFlags.ValueModule | (TypeScriptLinter.advancedClassChecks? 0 : ts.SymbolFlags.Class); - + // If module name is duplicated by another declaration, this increases the possibility // of finding a lot of false positives. Thus, do not check further in that case. if ((tsIdentSym.flags & ts.SymbolFlags.ValueModule) != 0) { @@ -1307,7 +1307,7 @@ export class TypeScriptLinter { const checkClassOrInterface = tsElemAccessBaseExprType.isClassOrInterface() && !this.tsUtils.isGenericArrayType(tsElemAccessBaseExprType) && - !this.tsUtils.isDerivedFrom(tsElemAccessBaseExprType, CheckType.Array); + !this.tsUtils.isDerivedFrom(tsElemAccessBaseExprType, CheckType.Array); const checkThisOrSuper = this.tsUtils.isThisOrSuperExpr(tsElementAccessExpr.expression) && !this.tsUtils.isDerivedFrom(tsElemAccessBaseExprType, CheckType.Array); @@ -1391,7 +1391,7 @@ export class TypeScriptLinter { this.handleImportCall(tsCallExpr); this.handleRequireCall(tsCallExpr); - if (!!calleeSym) { + if (!!calleeSym) { if (this.tsUtils.symbolHasEsObjectType(calleeSym)) { this.incrementCounters(tsCallExpr, FaultID.EsObjectType); } @@ -1442,8 +1442,8 @@ export class TypeScriptLinter { private handleGenericCallWithNoTypeArgs(callLikeExpr: ts.CallExpression | ts.NewExpression, callSignature: ts.Signature) { // Note: The PR!716 has led to a significant performance degradation. // Since initial problem was fixed in a more general way, this change - // became redundant. Therefore, it was reverted. See #13721 comments - // for a detailed analysis. + // became redundant. Therefore, it was reverted. See #13721 comments + // for a detailed analysis. const tsSyntaxKind = ts.isNewExpression(callLikeExpr) ? ts.SyntaxKind.Constructor : ts.SyntaxKind.FunctionDeclaration; const signFlags = ts.NodeBuilderFlags.WriteTypeArgumentsOfSignature | ts.NodeBuilderFlags.IgnoreErrors; @@ -1721,9 +1721,12 @@ export class TypeScriptLinter { const commentContent = comment.kind === ts.SyntaxKind.MultiLineCommentTrivia ? srcText.slice(comment.pos + 2, comment.end - 2) : srcText.slice(comment.pos + 2, comment.end); - const trimmedContent = commentContent.trim() + //if comment is multiline end closing '*/' is not at the same line as '@ts-xxx' - do nothing (see #13851) + if (commentContent.endsWith('\n')) + return; + const trimmedContent = commentContent.trim(); if (trimmedContent.startsWith('@ts-ignore') || - trimmedContent.startsWith('@ts-nocheck') || + trimmedContent.startsWith('@ts-nocheck') || trimmedContent.startsWith('@ts-expect-error')) this.incrementCounters(comment, FaultID.ErrorSuppression); } @@ -1778,7 +1781,7 @@ export class TypeScriptLinter { // option is enabled, compiler attempts to infer type from variable references: // see https://github.com/microsoft/TypeScript/pull/11263. // In this case, we still want to report the error, since ArkTS doesn't allow - // to omit both type annotation and initializer. + // to omit both type annotation and initializer. if (((ts.isVariableDeclaration(decl) && ts.isVariableStatement(decl.parent.parent)) || ts.isPropertyDeclaration(decl)) && !decl.initializer) { this.incrementCounters(decl, FaultID.AnyType); diff --git a/linter/test/ts_ignore.ts.relax.json b/linter/test/ts_ignore.ts.relax.json index d1f517671..787d4b0e1 100644 --- a/linter/test/ts_ignore.ts.relax.json +++ b/linter/test/ts_ignore.ts.relax.json @@ -34,11 +34,6 @@ "column": 9, "problem": "ErrorSuppression" }, - { - "line": 33, - "column": 2, - "problem": "ErrorSuppression" - }, { "line": 43, "column": 9, diff --git a/linter/test/ts_ignore.ts.strict.json b/linter/test/ts_ignore.ts.strict.json index d1f517671..787d4b0e1 100644 --- a/linter/test/ts_ignore.ts.strict.json +++ b/linter/test/ts_ignore.ts.strict.json @@ -34,11 +34,6 @@ "column": 9, "problem": "ErrorSuppression" }, - { - "line": 33, - "column": 2, - "problem": "ErrorSuppression" - }, { "line": 43, "column": 9, -- Gitee