diff --git a/linter/lib/Autofixer.ts b/linter/lib/Autofixer.ts index 58f6cec9a904e8e5d6c9b5ff2dff6987b93ee288..c2aaf55c80bb257602a5720ae529db5f3af228fb 100644 --- a/linter/lib/Autofixer.ts +++ b/linter/lib/Autofixer.ts @@ -125,32 +125,6 @@ export function dropTypeOnVarDecl(varDecl: ts.VariableDeclaration): Autofix { return { start: varDecl.getStart(), end: varDecl.getEnd(), replacementText: text }; } -export function dropTypeOnlyFlag( - impExpNode: ts.ImportClause | ts.ImportSpecifier | ts.ExportDeclaration | ts.ExportSpecifier -): Autofix { - let text: string; - if (ts.isImportClause(impExpNode)) { - const newImportClause = ts.factory.createImportClause(false, impExpNode.name, impExpNode.namedBindings); - text = printer.printNode(ts.EmitHint.Unspecified, newImportClause, impExpNode.getSourceFile()); - } else if (ts.isImportSpecifier(impExpNode)) { - const newImportSpec = ts.factory.createImportSpecifier(false, impExpNode.propertyName, impExpNode.name); - text = printer.printNode(ts.EmitHint.Unspecified, newImportSpec, impExpNode.getSourceFile()); - } else if (ts.isExportDeclaration(impExpNode)) { - const newExportDecl = ts.factory.createExportDeclaration( - impExpNode.modifiers, - false, - impExpNode.exportClause, - impExpNode.moduleSpecifier, - impExpNode.assertClause - ); - text = printer.printNode(ts.EmitHint.Unspecified, newExportDecl, impExpNode.getSourceFile()); - } else { - const newExportSpec = ts.factory.createExportSpecifier(false, impExpNode.propertyName, impExpNode.name); - text = printer.printNode(ts.EmitHint.Unspecified, newExportSpec, impExpNode.getSourceFile()); - } - return { start: impExpNode.getStart(), end: impExpNode.getEnd(), replacementText: text }; -} - export function fixDefaultImport( importClause: ts.ImportClause, defaultSpec: ts.ImportSpecifier, diff --git a/linter/lib/LinterRunner.ts b/linter/lib/LinterRunner.ts index 48254ef0f4db5a8bd6e9851288a29f46cd3dee26..6144b0bb46f032d98f9643dfef69df62a2d022cc 100644 --- a/linter/lib/LinterRunner.ts +++ b/linter/lib/LinterRunner.ts @@ -111,7 +111,6 @@ export function lint(options: LintOptions): LintRunResult { tsProgram.getTypeChecker(), new AutofixInfoSet(cmdOptions.autofixInfo), !!cmdOptions.strictMode, - cmdOptions.warningsAsErrors, cancellationToken, options.incrementalLintInfo, tscStrictDiagnostics, diff --git a/linter/lib/TypeScriptLinter.ts b/linter/lib/TypeScriptLinter.ts index 97c357ecb72cb6397dd631746acba00471a552a5..0733d61555a7ed11daaca1b88d381258993f02c8 100644 --- a/linter/lib/TypeScriptLinter.ts +++ b/linter/lib/TypeScriptLinter.ts @@ -104,7 +104,6 @@ export class TypeScriptLinter { private readonly tsTypeChecker: ts.TypeChecker, private readonly autofixesInfo: AutofixInfoSet, public strictMode: boolean, - public warningsAsErrors: boolean, private readonly cancellationToken?: ts.CancellationToken, private readonly incrementalLintInfo?: IncrementalLintInfo, private readonly tscStrictDiagnostics?: Map, @@ -1853,8 +1852,7 @@ export class TypeScriptLinter { * spread element is allowed only for arrays as rest parameter */ if (ts.isSpreadElement(node)) { - const spreadElemNode = node; - const spreadExprType = this.tsTypeChecker.getTypeAtLocation(spreadElemNode.expression); + const spreadExprType = this.tsTypeChecker.getTypeAtLocation(node.expression); if (spreadExprType) { if (ts.isCallLikeExpression(node.parent) || ts.isArrayLiteralExpression(node.parent)) { if (this.tsUtils.isOrDerivedFrom(spreadExprType, this.tsUtils.isArray)) { diff --git a/linter/lib/autofixes/ReportAutofixCallback.ts b/linter/lib/autofixes/ReportAutofixCallback.ts index 81f0358911e7eacea0e2a1394c55fad91caf345d..62e03bff42bbc8263f2bd82e94b5354ddbc249e5 100644 --- a/linter/lib/autofixes/ReportAutofixCallback.ts +++ b/linter/lib/autofixes/ReportAutofixCallback.ts @@ -16,9 +16,3 @@ import type { ProblemInfo } from '../ProblemInfo'; export type ReportAutofixCallback = (p: ProblemInfo) => void; - -export function getDefaultCallback(withAutofixes: ProblemInfo[]): ReportAutofixCallback { - return (p: ProblemInfo) => { - withAutofixes.push(p); - }; -} diff --git a/linter/lib/utils/TsUtils.ts b/linter/lib/utils/TsUtils.ts index e3cd75be8fbcdcce1d4790197bf0c41c8e3a9e17..2ea649b93b46da2eb72a7d12c52d31dbd4f3584c 100644 --- a/linter/lib/utils/TsUtils.ts +++ b/linter/lib/utils/TsUtils.ts @@ -34,13 +34,6 @@ export class TsUtils { private readonly advancedClassChecks: boolean ) {} - isType(tsType: ts.TypeNode | undefined, checkType: string): boolean { - if (tsType === undefined || !ts.isTypeReferenceNode(tsType)) { - return false; - } - return this.entityNameToString(tsType.typeName) === checkType; - } - entityNameToString(name: ts.EntityName): string { if (ts.isIdentifier(name)) { return name.escapedText.toString(); @@ -64,63 +57,6 @@ export class TsUtils { return (tsType.getFlags() & ts.TypeFlags.BooleanLike) !== 0; } - static isStringLikeType(tsType: ts.Type): boolean { - if (tsType.isUnion()) { - for (const tsCompType of tsType.types) { - if ((tsCompType.flags & ts.TypeFlags.StringLike) === 0) { - return false; - } - } - return true; - } - return (tsType.getFlags() & ts.TypeFlags.StringLike) !== 0; - } - - static isStringType(type: ts.Type): boolean { - return (type.getFlags() & ts.TypeFlags.String) !== 0; - } - - static isPrimitiveEnumType(type: ts.Type, primitiveType: ts.TypeFlags): boolean { - const isNonPrimitive = (type.flags & ts.TypeFlags.NonPrimitive) !== 0; - if (!TsUtils.isEnumType(type) || !type.isUnion() || isNonPrimitive) { - return false; - } - for (const t of type.types) { - if ((t.flags & primitiveType) === 0) { - return false; - } - } - return true; - } - - static isPrimitiveEnumMemberType(type: ts.Type, primitiveType: ts.TypeFlags): boolean { - const isNonPrimitive = (type.flags & ts.TypeFlags.NonPrimitive) !== 0; - if (!TsUtils.isEnumMemberType(type) || isNonPrimitive) { - return false; - } - return (type.flags & primitiveType) !== 0; - } - - static unwrapParenthesizedType(tsType: ts.TypeNode): ts.TypeNode { - while (ts.isParenthesizedTypeNode(tsType)) { - tsType = tsType.type; - } - return tsType; - } - - static findParentIf(asExpr: ts.AsExpression): ts.IfStatement | null { - let node = asExpr.parent; - while (node) { - if (node.kind === ts.SyntaxKind.IfStatement) { - return node as ts.IfStatement; - } - - node = node.parent; - } - - return null; - } - static isDestructuringAssignmentLHS(tsExpr: ts.ArrayLiteralExpression | ts.ObjectLiteralExpression): boolean { /* @@ -162,23 +98,6 @@ export class TsUtils { return tsType.symbol && (tsType.symbol.flags & ts.SymbolFlags.Enum) !== 0; } - static isEnumMemberType(tsType: ts.Type): boolean { - - /* - * Note: For some reason, test (tsType.flags & ts.TypeFlags.Enum) != 0 doesn't work here. - * Must use SymbolFlags to figure out if this is an enum type. - */ - return tsType.symbol && (tsType.symbol.flags & ts.SymbolFlags.EnumMember) !== 0; - } - - static isObjectLiteralType(tsType: ts.Type): boolean { - return tsType.symbol && (tsType.symbol.flags & ts.SymbolFlags.ObjectLiteral) !== 0; - } - - static isNumberLikeType(tsType: ts.Type): boolean { - return (tsType.getFlags() & ts.TypeFlags.NumberLike) !== 0; - } - static hasModifier(tsModifiers: readonly ts.Modifier[] | undefined, tsModifierKind: number): boolean { if (!tsModifiers) { return false; @@ -266,19 +185,6 @@ export class TsUtils { return false; } - static isReferenceType(tsType: ts.Type): boolean { - const f = tsType.getFlags(); - return ( - (f & ts.TypeFlags.InstantiableNonPrimitive) !== 0 || - (f & ts.TypeFlags.Object) !== 0 || - (f & ts.TypeFlags.Boolean) !== 0 || - (f & ts.TypeFlags.Enum) !== 0 || - (f & ts.TypeFlags.NonPrimitive) !== 0 || - (f & ts.TypeFlags.Number) !== 0 || - (f & ts.TypeFlags.String) !== 0 - ); - } - static isPrimitiveType(type: ts.Type): boolean { const f = type.getFlags(); return ( @@ -367,14 +273,6 @@ export class TsUtils { ); } - static isNullType(tsTypeNode: ts.TypeNode): boolean { - return ts.isLiteralTypeNode(tsTypeNode) && tsTypeNode.literal.kind === ts.SyntaxKind.NullKeyword; - } - - static isThisOrSuperExpr(tsExpr: ts.Expression): boolean { - return tsExpr.kind === ts.SyntaxKind.ThisKeyword || tsExpr.kind === ts.SyntaxKind.SuperKeyword; - } - static isPrototypeSymbol(symbol: ts.Symbol | undefined): boolean { return !!symbol && !!symbol.flags && (symbol.flags & ts.SymbolFlags.Prototype) !== 0; } @@ -417,12 +315,6 @@ export class TsUtils { return false; } - static isFunctionOrMethod(tsSymbol: ts.Symbol | undefined): boolean { - return ( - !!tsSymbol && ((tsSymbol.flags & ts.SymbolFlags.Function) !== 0 || (tsSymbol.flags & ts.SymbolFlags.Method) !== 0) - ); - } - static isMethodAssignment(tsSymbol: ts.Symbol | undefined): boolean { return ( !!tsSymbol && (tsSymbol.flags & ts.SymbolFlags.Method) !== 0 && (tsSymbol.flags & ts.SymbolFlags.Assignment) !== 0 @@ -846,18 +738,6 @@ export class TsUtils { return undefined; } - checkTypeSet(uType: ts.Type, predicate: (t: ts.Type) => boolean): boolean { - if (!uType.isUnionOrIntersection()) { - return predicate(uType); - } - for (const elemType of uType.types) { - if (!this.checkTypeSet(elemType, predicate)) { - return false; - } - } - return true; - } - static getNonNullableType(t: ts.Type): ts.Type { if (TsUtils.isNullableUnionType(t)) { return t.getNonNullableType(); @@ -922,11 +802,6 @@ export class TsUtils { ); } - static isFunctionalType(type: ts.Type): boolean { - const callSigns = type.getCallSignatures(); - return callSigns && callSigns.length > 0; - } - private isDynamicObjectAssignedToStdType(lhsType: ts.Type, rhsExpr: ts.Expression): boolean { if (isStdLibraryType(lhsType) || TsUtils.isPrimitiveType(lhsType)) { const rhsSym = ts.isCallExpression(rhsExpr) ? @@ -1081,11 +956,6 @@ export class TsUtils { return !!parentName && (parentName === 'Symbol' || parentName === 'SymbolConstructor'); } - isStdSymbol(symbol: ts.Symbol): boolean { - const name = this.tsTypeChecker.getFullyQualifiedName(symbol); - return name === 'Symbol' && this.isGlobalSymbol(symbol); - } - isSymbolIterator(symbol: ts.Symbol): boolean { return this.isSymbolAPI(symbol) && symbol.name === 'iterator'; } @@ -1419,36 +1289,6 @@ export class TsUtils { return undefined; } - // has to be re-implemented with local loop detection - typeIsRecursive(topType: ts.Type, type: ts.Type | undefined = undefined): boolean { - if (type === undefined) { - type = topType; - } else if (type === topType) { - return true; - } else if (type.aliasSymbol) { - return false; - } - - if (type.isUnion()) { - for (const unionElem of type.types) { - if (this.typeIsRecursive(topType, unionElem)) { - return true; - } - } - } - if (type.flags & ts.TypeFlags.Object && (type as ts.ObjectType).objectFlags & ts.ObjectFlags.Reference) { - const typeArgs = this.tsTypeChecker.getTypeArguments(type as ts.TypeReference); - if (typeArgs) { - for (const typeArg of typeArgs) { - if (this.typeIsRecursive(topType, typeArg)) { - return true; - } - } - } - } - return false; - } - static isClassValueType(type: ts.Type): boolean { if ( (type.flags & ts.TypeFlags.Object) === 0 || diff --git a/linter/lib/utils/functions/GetScriptKind.ts b/linter/lib/utils/functions/GetScriptKind.ts deleted file mode 100644 index 98643872d1a7e4a7e3cd87e03034ac073bd21fbc..0000000000000000000000000000000000000000 --- a/linter/lib/utils/functions/GetScriptKind.ts +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import * as ts from 'typescript'; -import * as path from 'node:path'; - -export function getScriptKind(srcFile: ts.SourceFile): ts.ScriptKind { - const fileName = srcFile.fileName; - const ext = path.extname(fileName).toLowerCase(); - switch (ext) { - case ts.Extension.Ts: - return ts.ScriptKind.TS; - case ts.Extension.Tsx: - return ts.ScriptKind.TSX; - case ts.Extension.Js: - return ts.ScriptKind.JS; - case ts.Extension.Jsx: - return ts.ScriptKind.JSX; - case ts.Extension.Json: - return ts.ScriptKind.JSON; - default: - return ts.ScriptKind.Unknown; - } -} diff --git a/linter/src/LinterInfo.ts b/linter/src/LinterInfo.ts index c13b1030df8008636288e8307508afb0374f966a..8f7bff013f10a9e7b94ac6acfdd1880d2e9674c0 100644 --- a/linter/src/LinterInfo.ts +++ b/linter/src/LinterInfo.ts @@ -13,13 +13,8 @@ * limitations under the License. */ -import type { ProblemInfo } from '../lib/ProblemInfo'; import type { AutofixInfo } from '../lib/autofixes/AutofixInfo'; -export function encodeProblemInfo(problem: ProblemInfo): string { - return `${problem.problem}%${problem.start}%${problem.end}`; -} - export function decodeAutofixInfo(info: string): AutofixInfo { const infos = info.split('%'); return {