From bb9ae8a3a17b883e108341d4c90866a6e6ae1724 Mon Sep 17 00:00:00 2001 From: Igor Rossinski Date: Thu, 16 Nov 2023 15:32:09 +0300 Subject: [PATCH] [ArkTS Linter] fix code style for linter-4.2 Signed-off-by: Igor Rossinski --- ets2panda/linter-4.2/src/DiagnosticChecker.ts | 2 +- .../src/LibraryTypeCallDiagnosticChecker.ts | 16 +++--- ets2panda/linter-4.2/src/TypeScriptLinter.ts | 45 ++++++++-------- ets2panda/linter-4.2/src/Utils.ts | 52 +++++++++++-------- 4 files changed, 62 insertions(+), 53 deletions(-) diff --git a/ets2panda/linter-4.2/src/DiagnosticChecker.ts b/ets2panda/linter-4.2/src/DiagnosticChecker.ts index 841d0d5b4f..dd327de111 100644 --- a/ets2panda/linter-4.2/src/DiagnosticChecker.ts +++ b/ets2panda/linter-4.2/src/DiagnosticChecker.ts @@ -13,7 +13,7 @@ * limitations under the License. */ -import * as ts from 'typescript'; +import type * as ts from 'typescript'; export interface DiagnosticChecker { checkDiagnosticMessage(msgText: string | ts.DiagnosticMessageChain): boolean; diff --git a/ets2panda/linter-4.2/src/LibraryTypeCallDiagnosticChecker.ts b/ets2panda/linter-4.2/src/LibraryTypeCallDiagnosticChecker.ts index 72778922bf..f34e4f8617 100644 --- a/ets2panda/linter-4.2/src/LibraryTypeCallDiagnosticChecker.ts +++ b/ets2panda/linter-4.2/src/LibraryTypeCallDiagnosticChecker.ts @@ -35,7 +35,7 @@ export class LibraryTypeCallDiagnosticChecker implements DiagnosticChecker { constructor(filteredDiagnosticMessages: Set) { this.filteredDiagnosticMessages = filteredDiagnosticMessages; } - + configure(inLibCall: boolean, diagnosticMessages: Array) { this.inLibCall = inLibCall; this.diagnosticMessages = diagnosticMessages; @@ -53,7 +53,7 @@ export class LibraryTypeCallDiagnosticChecker implements DiagnosticChecker { } checkMessageChain(chain: ts.DiagnosticMessageChain): boolean { - if (chain.code == TYPE_0_IS_NOT_ASSIGNABLE_TO_TYPE_1_ERROR_CODE) { + if (chain.code === TYPE_0_IS_NOT_ASSIGNABLE_TO_TYPE_1_ERROR_CODE) { if (chain.messageText.match(TYPE_UNKNOWN_IS_NOT_ASSIGNABLE_TO_TYPE_1_RE)) { return false; } @@ -64,11 +64,11 @@ export class LibraryTypeCallDiagnosticChecker implements DiagnosticChecker { return false; } } - return chain.next == undefined ? true : this.checkMessageChain(chain.next[0]); + return chain.next === undefined ? true : this.checkMessageChain(chain.next[0]); }; checkFilteredDiagnosticMessages(msgText: ts.DiagnosticMessageChain | string) { - if (this.filteredDiagnosticMessages.size == 0) { + if (this.filteredDiagnosticMessages.size === 0) { return true; } @@ -77,8 +77,8 @@ export class LibraryTypeCallDiagnosticChecker implements DiagnosticChecker { } for (const msgChain of this.filteredDiagnosticMessages) { - if (typeof msgText == 'string') { - if (msgText == msgChain.messageText) { + if (typeof msgText === 'string') { + if (msgText === msgChain.messageText) { return false; } continue; @@ -99,8 +99,8 @@ export class LibraryTypeCallDiagnosticChecker implements DiagnosticChecker { return true; } - curMsg = curMsg.next ? curMsg.next[0]: undefined; - curFilteredMsg = curFilteredMsg.next ? curFilteredMsg.next[0]: undefined; + curMsg = curMsg.next ? curMsg.next[0] : undefined; + curFilteredMsg = curFilteredMsg.next ? curFilteredMsg.next[0] : undefined; } return false; diff --git a/ets2panda/linter-4.2/src/TypeScriptLinter.ts b/ets2panda/linter-4.2/src/TypeScriptLinter.ts index 27442da9f6..706e3b1ceb 100644 --- a/ets2panda/linter-4.2/src/TypeScriptLinter.ts +++ b/ets2panda/linter-4.2/src/TypeScriptLinter.ts @@ -13,9 +13,9 @@ * limitations under the License. */ -import * as ts from "typescript"; -import * as path from "node:path"; -import { TsUtils, getNodeOrLineEnd, CheckType, isAssignmentOperator } from "./Utils"; +import type * as ts from "typescript"; +import type * as path from "node:path"; +import { TsUtils, getNodeOrLineEnd, CheckType, isAssignmentOperator } from './Utils'; import { FaultID, faultsAttrs } from "./Problems"; import { cookBookMsg, cookBookTag } from "./CookBookMsg"; import { LinterConfig } from "./TypeScriptLinterConfig"; @@ -24,12 +24,12 @@ import * as Autofixer from "./Autofixer"; import { ProblemInfo } from "./ProblemInfo"; import { ProblemSeverity } from "./ProblemSeverity"; import Logger from "../utils/logger"; -import { DiagnosticChecker } from "./DiagnosticChecker"; +import type { DiagnosticChecker } from './DiagnosticChecker'; import { ARGUMENT_OF_TYPE_0_IS_NOT_ASSIGNABLE_TO_PARAMETER_OF_TYPE_1_ERROR_CODE, TYPE_0_IS_NOT_ASSIGNABLE_TO_TYPE_1_ERROR_CODE, LibraryTypeCallDiagnosticChecker -} from "./LibraryTypeCallDiagnosticChecker"; +} from './LibraryTypeCallDiagnosticChecker'; const logger = Logger.getLogger(); @@ -597,8 +597,9 @@ export class TypeScriptLinter { this.incrementCounters(node, FaultID.InterfaceMerging); } - if (interfaceNode.heritageClauses) + if (interfaceNode.heritageClauses) { this.interfaceInheritanceLint(node, interfaceNode.heritageClauses); + } this.countDeclarationsWithDuplicateName(interfaceNode.name, interfaceNode); } @@ -677,7 +678,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 +760,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' @@ -939,7 +940,7 @@ export class TypeScriptLinter { // Note: Return type can't be inferred for function without body. if (ts.isMethodSignature(funcLikeDecl) || !funcLikeDecl.body) { // Ambient flag is not exposed, so we apply dirty hack to make it visible - const isAmbientDeclaration = !!(funcLikeDecl.flags & (ts.NodeFlags as any)['Ambient']); + const isAmbientDeclaration = !!(funcLikeDecl.flags & (ts.NodeFlags as any).Ambient); const isSignature = ts.isMethodSignature(funcLikeDecl); if ((isSignature || isAmbientDeclaration) && !funcLikeDecl.type) { this.incrementCounters(funcLikeDecl, FaultID.LimitedReturnTypeInference); @@ -1197,7 +1198,7 @@ export class TypeScriptLinter { this.incrementCounters(node, FaultID.VarDeclaration); } - private handleVariableDeclaration(node: ts.Node) { + private handleVariableDeclaration(node: ts.Node): void { let tsVarDecl = node as ts.VariableDeclaration; if ( ts.isArrayBindingPattern(tsVarDecl.name) || @@ -1237,7 +1238,7 @@ export class TypeScriptLinter { this.handleDefiniteAssignmentAssertion(tsVarDecl); } - private handleEsObjectDelaration(node: ts.VariableDeclaration) { + private handleEsObjectDelaration(node: ts.VariableDeclaration): void { const isDeclaredESObject = !!node.type && this.tsUtils.isEsObjectType(node.type); const initalizerTypeNode = node.initializer && this.tsUtils.getVariableDeclarationTypeNode(node.initializer); const isInitializedWithESObject = !!initalizerTypeNode && this.tsUtils.isEsObjectType(initalizerTypeNode); @@ -1440,7 +1441,7 @@ export class TypeScriptLinter { ]); } - private handleMethodDeclaration(node: ts.Node) { + private handleMethodDeclaration(node: ts.Node): void { const tsMethodDecl = node as ts.MethodDeclaration; const hasThis = this.scopeContainsThis(tsMethodDecl); let isStatic = false; @@ -1596,7 +1597,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 +1700,7 @@ export class TypeScriptLinter { this.handleImportCall(tsCallExpr); this.handleRequireCall(tsCallExpr); - if (!!calleeSym) { + if (!!calleeSym) { if (this.tsUtils.symbolHasEsObjectType(calleeSym)) { this.incrementCounters(tsCallExpr, FaultID.EsObjectType); } @@ -1739,7 +1740,7 @@ export class TypeScriptLinter { } } - private handleRequireCall(tsCallExpr: ts.CallExpression) { + private handleRequireCall(tsCallExpr: ts.CallExpression): void { if ( ts.isIdentifier(tsCallExpr.expression) && tsCallExpr.expression.text === "require" && @@ -1754,7 +1755,7 @@ export class TypeScriptLinter { } } - private handleGenericCallWithNoTypeArgs(callLikeExpr: ts.CallExpression | ts.NewExpression, callSignature: ts.Signature) { + private handleGenericCallWithNoTypeArgs(callLikeExpr: ts.CallExpression | ts.NewExpression, callSignature: ts.Signature): void { let tsSyntaxKind = ts.isNewExpression(callLikeExpr) ? ts.SyntaxKind.Constructor : ts.SyntaxKind.FunctionDeclaration; @@ -1776,7 +1777,7 @@ export class TypeScriptLinter { // 2. Compiler infer 'unknown' from arguments // We report error in both cases. It is ok because we cannot use 'unknown' // in ArkTS and already have separate check for it. - if (typeNode.kind == ts.SyntaxKind.UnknownKeyword) { + if (typeNode.kind === ts.SyntaxKind.UnknownKeyword) { this.incrementCounters(callLikeExpr, FaultID.GenericCallNoTypeArgs); break; } @@ -1784,7 +1785,7 @@ export class TypeScriptLinter { } } - private handleStructIdentAndUndefinedInArgs(tsCallOrNewExpr: ts.CallExpression | ts.NewExpression, callSignature: ts.Signature) { + private handleStructIdentAndUndefinedInArgs(tsCallOrNewExpr: ts.CallExpression | ts.NewExpression, callSignature: ts.Signature): void { if (!tsCallOrNewExpr.arguments) { return; } @@ -1797,7 +1798,7 @@ export class TypeScriptLinter { let tsArgType = this.tsTypeChecker.getTypeAtLocation(tsArg); if (!tsArgType) continue; - let paramIndex = argIndex < callSignature.parameters.length ? argIndex : callSignature.parameters.length-1; + let paramIndex = argIndex < callSignature.parameters.length ? argIndex : callSignature.parameters.length - 1; let tsParamSym = callSignature.parameters[paramIndex]; if (!tsParamSym) continue; @@ -2115,9 +2116,9 @@ 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. - if (((ts.isVariableDeclaration(decl) && ts.isVariableStatement(decl.parent.parent)) || ts.isPropertyDeclaration(decl)) - && !decl.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); return; } diff --git a/ets2panda/linter-4.2/src/Utils.ts b/ets2panda/linter-4.2/src/Utils.ts index 03674b19e9..ba940b1e77 100644 --- a/ets2panda/linter-4.2/src/Utils.ts +++ b/ets2panda/linter-4.2/src/Utils.ts @@ -18,7 +18,7 @@ import * as ts from 'typescript'; import { ProblemInfo } from './ProblemInfo'; import { AutofixInfo } from './AutofixInfo'; import { LinterConfig } from './TypeScriptLinterConfig' -import { FaultID } from "./Problems"; +import { FaultID } from './Problems'; export function logTscDiagnostic(diagnostics: readonly ts.Diagnostic[], log: (message: any, ...args: any[]) => void) { diagnostics.forEach((diagnostic) => { @@ -104,7 +104,7 @@ export enum CheckType { }; export class TsUtils { - static readonly ES_OBJECT = 'ESObject' + static readonly ES_OBJECT = 'ESObject'; private static readonly LIMITED_STD_ARRAYBUFFER_API = [ // properties @@ -211,7 +211,7 @@ export class TsUtils { 'eval', ]; - static readonly LIMITED_STD_API = new Map, fault: FaultID}> ([ + static readonly LIMITED_STD_API = new Map, fault: FaultID}>([ ['Object', {arr: TsUtils.LIMITED_STD_OBJECT_API, fault: FaultID.LimitedStdLibApi}], ['ObjectConstructor', {arr: TsUtils.LIMITED_STD_OBJECT_API, fault: FaultID.LimitedStdLibApi}], ['Reflect', {arr: TsUtils.LIMITED_STD_REFLECT_API, fault: FaultID.LimitedStdLibApi}], @@ -222,11 +222,11 @@ export class TsUtils { ['SymbolConstructor', {arr: TsUtils.LIMITED_STD_SYMBOL_API, fault: FaultID.SymbolType}], ['Function', {arr: TsUtils.LIMITED_STD_FUNCTION_API, fault: FaultID.FunctionApplyBindCall}], ['CallableFunction', {arr: TsUtils.LIMITED_STD_FUNCTION_API, fault: FaultID.FunctionApplyBindCall}], - ]) + ]); static readonly NON_INITIALIZABLE_PROPERTY_DECORATORS = ['Link', 'Consume', 'ObjectLink', 'Prop', 'BuilderParam']; - static readonly NON_INITIALIZABLE_PROPERTY_CLASS_DECORATORS = ['CustomDialog'] + static readonly NON_INITIALIZABLE_PROPERTY_CLASS_DECORATORS = ['CustomDialog']; static readonly NON_RETURN_FUNCTION_DECORATORS = ['AnimatableExtend', 'Builder', 'Extend', 'Styles']; @@ -787,8 +787,12 @@ export class TsUtils { if (this.isTypeReference(typeA) && typeA.target !== typeA) typeA = typeA.target; if (this.isTypeReference(typeB) && typeB.target !== typeB) typeB = typeB.target; - if (typeA === typeB || this.isObject(typeB)) return true; - if (!typeA.symbol || !typeA.symbol.declarations) return false; + if (typeA === typeB || this.isObject(typeB)) { + return true; + } + if (!typeA.symbol || !typeA.symbol.declarations) { + return false; + } for (let typeADecl of typeA.symbol.declarations) { if ( @@ -1082,7 +1086,7 @@ export class TsUtils { return true; } - + private validateField(type: ts.Type, prop: ts.PropertyAssignment): boolean { const propName = prop.name.getText(); const propSym = this.findProperty(type, propName); @@ -1095,7 +1099,7 @@ export class TsUtils { if (ts.isObjectLiteralExpression(initExpr)) { if (!this.isObjectLiteralAssignable(propType, initExpr)) { return false; - } + } } else { // Only check for structural sub-typing. if (this.needToDeduceStructuralIdentity(propType, this.tsTypeChecker.getTypeAtLocation(initExpr), initExpr)) { @@ -1144,8 +1148,12 @@ export class TsUtils { if (ts.isTupleTypeNode(typeNode)) { for (const elem of typeNode.elements) { - if (ts.isTypeNode(elem) && !this.isSupportedType(elem)) return false; - if (ts.isNamedTupleMember(elem) && !this.isSupportedType(elem.type)) return false; + if (ts.isTypeNode(elem) && !this.isSupportedType(elem)) { + return false; + } + if (ts.isNamedTupleMember(elem) && !this.isSupportedType(elem.type)) { + return false; + } } return true; } @@ -1169,7 +1177,7 @@ export class TsUtils { public isStructDeclarationKind(kind: ts.SyntaxKind) { return LinterConfig.tsSyntaxKindNames[kind] === 'StructDeclaration'; } - + public isStructDeclaration(node: ts.Node) { return this.isStructDeclarationKind(node.kind); } @@ -1220,7 +1228,7 @@ export class TsUtils { } public isStdSymbol(symbol: ts.Symbol): boolean { - const name = this.tsTypeChecker.getFullyQualifiedName(symbol) + const name = this.tsTypeChecker.getFullyQualifiedName(symbol); return name === 'Symbol'; } @@ -1267,12 +1275,12 @@ export class TsUtils { public isStdRequiredType(type: ts.Type): boolean { const sym = type.aliasSymbol; - return !!sym && sym.getName() === "Required" && this.isGlobalSymbol(sym); + return !!sym && sym.getName() === 'Required' && this.isGlobalSymbol(sym); } - + public isStdReadonlyType(type: ts.Type): boolean { const sym = type.aliasSymbol; - return !!sym && sym.getName() === "Readonly" && this.isGlobalSymbol(sym); + return !!sym && sym.getName() === 'Readonly' && this.isGlobalSymbol(sym); } public isLibraryType(type: ts.Type): boolean { @@ -1377,7 +1385,7 @@ export class TsUtils { if (type.isUnion()) { for (let compType of type.types) { - let isDynamic = this.isDynamicType(compType); + let isDynamic = this.isDynamicType(compType); if (isDynamic || isDynamic === undefined) { return isDynamic; } @@ -1397,7 +1405,7 @@ export class TsUtils { } public isObjectType(type: ts.Type): type is ts.ObjectType { - return !!(type.flags & ts.TypeFlags.Object) + return !!(type.flags & ts.TypeFlags.Object); } private isAnonymous(type: ts.Type): boolean { @@ -1417,7 +1425,7 @@ export class TsUtils { let curNode: ts.Node = expr; while (ts.isObjectLiteralExpression(curNode) || ts.isArrayLiteralExpression(curNode)) { const exprType = this.tsTypeChecker.getContextualType(curNode); - if (exprType !== undefined && !this.isAnonymous(exprType)) { + if (exprType !== undefined && !this.isAnonymous(exprType)) { const res = this.isDynamicType(exprType) if (res !== undefined) { return res; @@ -1478,7 +1486,7 @@ export class TsUtils { } public isInsideBlock(node: ts.Node): boolean { - let par = node.parent + let par = node.parent; while (par) { if (ts.isBlock(par)) { return true; @@ -1497,7 +1505,7 @@ export class TsUtils { return false; } const valueType = this.tsTypeChecker.getTypeAtLocation(node); - return this.isUnsupportedType(valueType) || this.isAnonymousType(valueType) + return this.isUnsupportedType(valueType) || this.isAnonymousType(valueType); } public getVariableDeclarationTypeNode(node: ts.Node): ts.TypeNode | undefined { @@ -1584,7 +1592,7 @@ export class TsUtils { } return false; } - + public isFunctionCalledRecursively(funcExpr: ts.FunctionExpression): boolean { if (!funcExpr.name) return false; -- Gitee