diff --git a/packages/eslint-plugin/README.md b/packages/eslint-plugin/README.md index 6f890d272562..1f72015199a4 100644 --- a/packages/eslint-plugin/README.md +++ b/packages/eslint-plugin/README.md @@ -110,7 +110,7 @@ Pro Tip: For larger codebases you may want to consider splitting our linting int | [`@typescript-eslint/consistent-type-imports`](./docs/rules/consistent-type-imports.md) | Enforces consistent usage of type imports | | :wrench: | | | [`@typescript-eslint/explicit-function-return-type`](./docs/rules/explicit-function-return-type.md) | Require explicit return types on functions and class methods | | | | | [`@typescript-eslint/explicit-member-accessibility`](./docs/rules/explicit-member-accessibility.md) | Require explicit accessibility modifiers on class properties and methods | | :wrench: | | -| [`@typescript-eslint/explicit-module-boundary-types`](./docs/rules/explicit-module-boundary-types.md) | Require explicit return and argument types on exported functions' and classes' public class methods | :white_check_mark: | | | +| [`@typescript-eslint/explicit-module-boundary-types`](./docs/rules/explicit-module-boundary-types.md) | Require explicit return and argument types on exported functions' and classes' public class methods | | | | | [`@typescript-eslint/member-delimiter-style`](./docs/rules/member-delimiter-style.md) | Require a specific member delimiter style for interfaces and type literals | | :wrench: | | | [`@typescript-eslint/member-ordering`](./docs/rules/member-ordering.md) | Require a consistent member declaration order | | | | | [`@typescript-eslint/method-signature-style`](./docs/rules/method-signature-style.md) | Enforces using a particular method signature syntax. | | :wrench: | | @@ -125,7 +125,6 @@ Pro Tip: For larger codebases you may want to consider splitting our linting int | [`@typescript-eslint/no-extraneous-class`](./docs/rules/no-extraneous-class.md) | Forbids the use of classes as namespaces | | | | | [`@typescript-eslint/no-floating-promises`](./docs/rules/no-floating-promises.md) | Requires Promise-like values to be handled appropriately | :white_check_mark: | | :thought_balloon: | | [`@typescript-eslint/no-for-in-array`](./docs/rules/no-for-in-array.md) | Disallow iterating over an array with a for-in loop | :white_check_mark: | | :thought_balloon: | -| [`@typescript-eslint/no-implicit-any-catch`](./docs/rules/no-implicit-any-catch.md) | Disallow usage of the implicit `any` type in catch clauses | | :wrench: | | | [`@typescript-eslint/no-inferrable-types`](./docs/rules/no-inferrable-types.md) | Disallows explicit type declarations for variables or parameters initialized to a number, string, or boolean | :white_check_mark: | :wrench: | | | [`@typescript-eslint/no-invalid-void-type`](./docs/rules/no-invalid-void-type.md) | Disallows usage of `void` type outside of generic or return types | | | | | [`@typescript-eslint/no-misused-new`](./docs/rules/no-misused-new.md) | Enforce valid definition of `new` and `constructor` | :white_check_mark: | | | @@ -142,8 +141,8 @@ Pro Tip: For larger codebases you may want to consider splitting our linting int | [`@typescript-eslint/no-unnecessary-qualifier`](./docs/rules/no-unnecessary-qualifier.md) | Warns when a namespace qualifier is unnecessary | | :wrench: | :thought_balloon: | | [`@typescript-eslint/no-unnecessary-type-arguments`](./docs/rules/no-unnecessary-type-arguments.md) | Enforces that type arguments will not be used if not required | | :wrench: | :thought_balloon: | | [`@typescript-eslint/no-unnecessary-type-assertion`](./docs/rules/no-unnecessary-type-assertion.md) | Warns if a type assertion does not change the type of an expression | :white_check_mark: | :wrench: | :thought_balloon: | -| [`@typescript-eslint/no-unnecessary-type-constraint`](./docs/rules/no-unnecessary-type-constraint.md) | Disallows unnecessary constraints on generic types | | :wrench: | | -| [`@typescript-eslint/no-unsafe-argument`](./docs/rules/no-unsafe-argument.md) | Disallows calling an function with an any type value | | | :thought_balloon: | +| [`@typescript-eslint/no-unnecessary-type-constraint`](./docs/rules/no-unnecessary-type-constraint.md) | Disallows unnecessary constraints on generic types | :white_check_mark: | :wrench: | | +| [`@typescript-eslint/no-unsafe-argument`](./docs/rules/no-unsafe-argument.md) | Disallows calling an function with an any type value | :white_check_mark: | | :thought_balloon: | | [`@typescript-eslint/no-unsafe-assignment`](./docs/rules/no-unsafe-assignment.md) | Disallows assigning any to variables and properties | :white_check_mark: | | :thought_balloon: | | [`@typescript-eslint/no-unsafe-call`](./docs/rules/no-unsafe-call.md) | Disallows calling an any type value | :white_check_mark: | | :thought_balloon: | | [`@typescript-eslint/no-unsafe-member-access`](./docs/rules/no-unsafe-member-access.md) | Disallows member access on any typed variables | :white_check_mark: | | :thought_balloon: | @@ -162,7 +161,7 @@ Pro Tip: For larger codebases you may want to consider splitting our linting int | [`@typescript-eslint/prefer-readonly`](./docs/rules/prefer-readonly.md) | Requires that private members are marked as `readonly` if they're never modified outside of the constructor | | :wrench: | :thought_balloon: | | [`@typescript-eslint/prefer-readonly-parameter-types`](./docs/rules/prefer-readonly-parameter-types.md) | Requires that function parameters are typed as readonly to prevent accidental mutation of inputs | | | :thought_balloon: | | [`@typescript-eslint/prefer-reduce-type-parameter`](./docs/rules/prefer-reduce-type-parameter.md) | Prefer using type parameter when calling `Array#reduce` instead of casting | | :wrench: | :thought_balloon: | -| [`@typescript-eslint/prefer-regexp-exec`](./docs/rules/prefer-regexp-exec.md) | Enforce that `RegExp#exec` is used instead of `String#match` if no global flag is provided | :white_check_mark: | :wrench: | :thought_balloon: | +| [`@typescript-eslint/prefer-regexp-exec`](./docs/rules/prefer-regexp-exec.md) | Enforce that `RegExp#exec` is used instead of `String#match` if no global flag is provided | | :wrench: | :thought_balloon: | | [`@typescript-eslint/prefer-return-this-type`](./docs/rules/prefer-return-this-type.md) | Enforce that `this` is used when only `this` type is returned | | :wrench: | :thought_balloon: | | [`@typescript-eslint/prefer-string-starts-ends-with`](./docs/rules/prefer-string-starts-ends-with.md) | Enforce the use of `String#startsWith` and `String#endsWith` instead of other equivalent methods of checking substrings | | :wrench: | :thought_balloon: | | [`@typescript-eslint/prefer-ts-expect-error`](./docs/rules/prefer-ts-expect-error.md) | Recommends using `@ts-expect-error` over `@ts-ignore` | | :wrench: | | @@ -211,7 +210,7 @@ In these cases, we create what we call an extension rule; a rule within our plug | [`@typescript-eslint/no-implied-eval`](./docs/rules/no-implied-eval.md) | Disallow the use of `eval()`-like methods | :white_check_mark: | | :thought_balloon: | | [`@typescript-eslint/no-invalid-this`](./docs/rules/no-invalid-this.md) | Disallow `this` keywords outside of classes or class-like objects | | | | | [`@typescript-eslint/no-loop-func`](./docs/rules/no-loop-func.md) | Disallow function declarations that contain unsafe references inside loop statements | | | | -| [`@typescript-eslint/no-loss-of-precision`](./docs/rules/no-loss-of-precision.md) | Disallow literal numbers that lose precision | | | | +| [`@typescript-eslint/no-loss-of-precision`](./docs/rules/no-loss-of-precision.md) | Disallow literal numbers that lose precision | :white_check_mark: | | | | [`@typescript-eslint/no-magic-numbers`](./docs/rules/no-magic-numbers.md) | Disallow magic numbers | | | | | [`@typescript-eslint/no-redeclare`](./docs/rules/no-redeclare.md) | Disallow variable redeclaration | | | | | [`@typescript-eslint/no-shadow`](./docs/rules/no-shadow.md) | Disallow variable declarations from shadowing variables declared in the outer scope | | | | diff --git a/packages/eslint-plugin/src/configs/all.ts b/packages/eslint-plugin/src/configs/all.ts index 66209b806233..65a1bc50d042 100644 --- a/packages/eslint-plugin/src/configs/all.ts +++ b/packages/eslint-plugin/src/configs/all.ts @@ -65,7 +65,6 @@ export = { '@typescript-eslint/no-extraneous-class': 'error', '@typescript-eslint/no-floating-promises': 'error', '@typescript-eslint/no-for-in-array': 'error', - '@typescript-eslint/no-implicit-any-catch': 'error', 'no-implied-eval': 'off', '@typescript-eslint/no-implied-eval': 'error', '@typescript-eslint/no-inferrable-types': 'error', diff --git a/packages/eslint-plugin/src/configs/recommended-requiring-type-checking.ts b/packages/eslint-plugin/src/configs/recommended-requiring-type-checking.ts index fecfa6002af9..369d33d6687e 100644 --- a/packages/eslint-plugin/src/configs/recommended-requiring-type-checking.ts +++ b/packages/eslint-plugin/src/configs/recommended-requiring-type-checking.ts @@ -12,11 +12,11 @@ export = { '@typescript-eslint/no-implied-eval': 'error', '@typescript-eslint/no-misused-promises': 'error', '@typescript-eslint/no-unnecessary-type-assertion': 'error', + '@typescript-eslint/no-unsafe-argument': 'error', '@typescript-eslint/no-unsafe-assignment': 'error', '@typescript-eslint/no-unsafe-call': 'error', '@typescript-eslint/no-unsafe-member-access': 'error', '@typescript-eslint/no-unsafe-return': 'error', - '@typescript-eslint/prefer-regexp-exec': 'error', 'require-await': 'off', '@typescript-eslint/require-await': 'error', '@typescript-eslint/restrict-plus-operands': 'error', diff --git a/packages/eslint-plugin/src/configs/recommended.ts b/packages/eslint-plugin/src/configs/recommended.ts index d9450197c415..10b1d04581fb 100644 --- a/packages/eslint-plugin/src/configs/recommended.ts +++ b/packages/eslint-plugin/src/configs/recommended.ts @@ -8,7 +8,6 @@ export = { '@typescript-eslint/adjacent-overload-signatures': 'error', '@typescript-eslint/ban-ts-comment': 'error', '@typescript-eslint/ban-types': 'error', - '@typescript-eslint/explicit-module-boundary-types': 'warn', 'no-array-constructor': 'off', '@typescript-eslint/no-array-constructor': 'error', 'no-empty-function': 'off', @@ -19,11 +18,14 @@ export = { 'no-extra-semi': 'off', '@typescript-eslint/no-extra-semi': 'error', '@typescript-eslint/no-inferrable-types': 'error', + 'no-loss-of-precision': 'off', + '@typescript-eslint/no-loss-of-precision': 'error', '@typescript-eslint/no-misused-new': 'error', '@typescript-eslint/no-namespace': 'error', '@typescript-eslint/no-non-null-asserted-optional-chain': 'error', '@typescript-eslint/no-non-null-assertion': 'warn', '@typescript-eslint/no-this-alias': 'error', + '@typescript-eslint/no-unnecessary-type-constraint': 'error', 'no-unused-vars': 'off', '@typescript-eslint/no-unused-vars': 'warn', '@typescript-eslint/no-var-requires': 'error', diff --git a/packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts b/packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts index 6859668bb59c..81af52739fe5 100644 --- a/packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts +++ b/packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts @@ -36,7 +36,7 @@ export default util.createRule({ docs: { description: "Require explicit return and argument types on exported functions' and classes' public class methods", - recommended: 'warn', + recommended: false, }, messages: { missingReturnType: 'Missing return type on function.', diff --git a/packages/eslint-plugin/src/rules/no-implicit-any-catch.ts b/packages/eslint-plugin/src/rules/no-implicit-any-catch.ts index ef9ecaf80698..e42d3f7136c6 100644 --- a/packages/eslint-plugin/src/rules/no-implicit-any-catch.ts +++ b/packages/eslint-plugin/src/rules/no-implicit-any-catch.ts @@ -17,6 +17,7 @@ export type MessageIds = export default util.createRule({ name: 'no-implicit-any-catch', meta: { + deprecated: true, type: 'suggestion', docs: { description: 'Disallow usage of the implicit `any` type in catch clauses', diff --git a/packages/eslint-plugin/src/rules/no-loss-of-precision.ts b/packages/eslint-plugin/src/rules/no-loss-of-precision.ts index 955032474755..d095def03cc9 100644 --- a/packages/eslint-plugin/src/rules/no-loss-of-precision.ts +++ b/packages/eslint-plugin/src/rules/no-loss-of-precision.ts @@ -15,7 +15,7 @@ export default util.createRule({ type: 'problem', docs: { description: 'Disallow literal numbers that lose precision', - recommended: false, + recommended: 'error', extendsBaseRule: true, }, hasSuggestions: baseRule?.meta.hasSuggestions, diff --git a/packages/eslint-plugin/src/rules/no-unnecessary-type-constraint.ts b/packages/eslint-plugin/src/rules/no-unnecessary-type-constraint.ts index 535cae96023b..68895c01e71d 100644 --- a/packages/eslint-plugin/src/rules/no-unnecessary-type-constraint.ts +++ b/packages/eslint-plugin/src/rules/no-unnecessary-type-constraint.ts @@ -33,7 +33,7 @@ export default util.createRule({ meta: { docs: { description: 'Disallows unnecessary constraints on generic types', - recommended: false, + recommended: 'error', suggestion: true, }, fixable: 'code', diff --git a/packages/eslint-plugin/src/rules/no-unsafe-argument.ts b/packages/eslint-plugin/src/rules/no-unsafe-argument.ts index 17aa537fb8c9..431ef52e66d9 100644 --- a/packages/eslint-plugin/src/rules/no-unsafe-argument.ts +++ b/packages/eslint-plugin/src/rules/no-unsafe-argument.ts @@ -138,8 +138,7 @@ export default util.createRule<[], MessageIds>({ type: 'problem', docs: { description: 'Disallows calling an function with an any type value', - // TODO - enable this with next breaking - recommended: false, + recommended: 'error', requiresTypeChecking: true, }, messages: { diff --git a/packages/eslint-plugin/src/rules/prefer-regexp-exec.ts b/packages/eslint-plugin/src/rules/prefer-regexp-exec.ts index c6cd21a152ce..40f106ec5bb9 100644 --- a/packages/eslint-plugin/src/rules/prefer-regexp-exec.ts +++ b/packages/eslint-plugin/src/rules/prefer-regexp-exec.ts @@ -29,7 +29,7 @@ export default createRule({ docs: { description: 'Enforce that `RegExp#exec` is used instead of `String#match` if no global flag is provided', - recommended: 'error', + recommended: false, requiresTypeChecking: true, }, messages: { diff --git a/packages/typescript-estree/src/ast-converter.ts b/packages/typescript-estree/src/ast-converter.ts index 465a0b955c47..49ffd28f479e 100644 --- a/packages/typescript-estree/src/ast-converter.ts +++ b/packages/typescript-estree/src/ast-converter.ts @@ -1,9 +1,9 @@ -import { SourceFile } from 'typescript'; +import type { SourceFile } from 'typescript'; import { convertError, Converter, ASTMaps } from './convert'; import { convertComments } from './convert-comments'; import { convertTokens } from './node-utils'; -import { Extra } from './parser-options'; -import { TSESTree } from './ts-estree'; +import type { Extra } from './parser-options'; +import type { TSESTree } from './ts-estree'; import { simpleTraverse } from './simple-traverse'; export function astConverter( diff --git a/packages/typescript-estree/src/convert.ts b/packages/typescript-estree/src/convert.ts index e800233a7106..37af8556cca4 100644 --- a/packages/typescript-estree/src/convert.ts +++ b/packages/typescript-estree/src/convert.ts @@ -23,7 +23,11 @@ import { TSError, unescapeStringLiteralText, } from './node-utils'; -import { ParserWeakMap, ParserWeakMapESTreeToTSNode } from './parser-options'; +import type { + ParserWeakMap, + ParserWeakMapESTreeToTSNode, +} from './parser-options'; +import type { SemanticOrSyntacticError } from './semantic-or-syntactic-errors'; import { AST_NODE_TYPES, TSESTree, @@ -31,6 +35,7 @@ import { TSNode, } from './ts-estree'; import { typescriptVersionIsAtLeast } from './version-check'; +import { Expression } from '@typescript-eslint/types/src/ast-spec'; const SyntaxKind = ts.SyntaxKind; @@ -44,11 +49,13 @@ interface ConverterOptions { * @param error the error object * @returns converted error object */ -export function convertError(error: any): TSError { +export function convertError( + error: ts.DiagnosticWithLocation | SemanticOrSyntacticError, +): TSError { return createError( - error.file, - error.start, - error.message || error.messageText, + error.file!, + error.start!, + ('message' in error && error.message) || (error.messageText as string), ); } @@ -264,7 +271,7 @@ export class Converter { tsType: ts.TypeNode | undefined, parent?: ts.Node, ): TSESTree.BindingName { - const id = this.convertPattern(name); + const id = this.convertPattern(name) as TSESTree.BindingName; if (tsType) { id.typeAnnotation = this.convertTypeAnnotation(tsType, parent); @@ -509,10 +516,10 @@ export class Converter { ) .forEach(([key, value]) => { if (Array.isArray(value)) { - result[key] = value.map(el => this.convertChild(el)); + result[key] = value.map(el => this.convertChild(el as TSNode)); } else if (value && typeof value === 'object' && value.kind) { // need to check node[key].kind to ensure we don't try to convert a symbol - result[key] = this.convertChild(value); + result[key] = this.convertChild(value as TSNode); } else { result[key] = value; } @@ -694,7 +701,9 @@ export class Converter { result.declare = true; break; default: - remainingModifiers.push(this.convertChild(modifier)); + remainingModifiers.push( + this.convertChild(modifier) as TSESTree.Modifier, + ); break; } } @@ -1505,8 +1514,12 @@ export class Converter { }); node.templateSpans.forEach(templateSpan => { - result.expressions.push(this.convertChild(templateSpan.expression)); - result.quasis.push(this.convertChild(templateSpan.literal)); + result.expressions.push( + this.convertChild(templateSpan.expression) as TSESTree.Expression, + ); + result.quasis.push( + this.convertChild(templateSpan.literal) as TSESTree.TemplateElement, + ); }); return result; } @@ -1731,14 +1744,18 @@ export class Converter { } if (node.importClause.name) { - result.specifiers.push(this.convertChild(node.importClause)); + result.specifiers.push( + this.convertChild(node.importClause) as TSESTree.ImportClause, + ); } if (node.importClause.namedBindings) { switch (node.importClause.namedBindings.kind) { case SyntaxKind.NamespaceImport: result.specifiers.push( - this.convertChild(node.importClause.namedBindings), + this.convertChild( + node.importClause.namedBindings, + ) as TSESTree.ImportClause, ); break; case SyntaxKind.NamedImports: @@ -1893,7 +1910,7 @@ export class Converter { expressions: [], }); - const left = this.convertChild(node.left); + const left = this.convertChild(node.left) as Expression; if ( left.type === AST_NODE_TYPES.SequenceExpression && node.left.kind !== SyntaxKind.ParenthesizedExpression @@ -1903,7 +1920,9 @@ export class Converter { result.expressions.push(left); } - result.expressions.push(this.convertChild(node.right)); + result.expressions.push( + this.convertChild(node.right) as TSESTree.Expression, + ); return result; } else { const type = getBinaryExpressionType(node.operatorToken); @@ -2584,11 +2603,15 @@ export class Converter { for (const heritageClause of interfaceHeritageClauses) { if (heritageClause.token === SyntaxKind.ExtendsKeyword) { for (const n of heritageClause.types) { - interfaceExtends.push(this.convertChild(n, node)); + interfaceExtends.push( + this.convertChild(n, node) as TSESTree.TSInterfaceHeritage, + ); } } else { for (const n of heritageClause.types) { - interfaceImplements.push(this.convertChild(n, node)); + interfaceImplements.push( + this.convertChild(n, node) as TSESTree.TSInterfaceHeritage, + ); } } } @@ -2830,8 +2853,12 @@ export class Converter { }); node.templateSpans.forEach(templateSpan => { - result.types.push(this.convertChild(templateSpan.type)); - result.quasis.push(this.convertChild(templateSpan.literal)); + result.types.push( + this.convertChild(templateSpan.type) as TSESTree.TypeNode, + ); + result.quasis.push( + this.convertChild(templateSpan.literal) as TSESTree.TemplateElement, + ); }); return result; } diff --git a/packages/typescript-estree/src/create-program/createWatchProgram.ts b/packages/typescript-estree/src/create-program/createWatchProgram.ts index 2142065e47b0..ae2dec4de514 100644 --- a/packages/typescript-estree/src/create-program/createWatchProgram.ts +++ b/packages/typescript-estree/src/create-program/createWatchProgram.ts @@ -370,7 +370,7 @@ function createWatchProgram( log('Running without timeout fix'); // But because of https://github.com/microsoft/TypeScript/pull/37308 we cannot just set it to undefined // instead save it and call before getProgram is called - watchCompilerHost.setTimeout = (cb, _ms, ...args): unknown => { + watchCompilerHost.setTimeout = (cb, _ms, ...args: unknown[]): unknown => { callback = cb.bind(/*this*/ undefined, ...args); return callback; }; diff --git a/packages/typescript-estree/src/semantic-or-syntactic-errors.ts b/packages/typescript-estree/src/semantic-or-syntactic-errors.ts index fab5cd642e09..af6f33918df1 100644 --- a/packages/typescript-estree/src/semantic-or-syntactic-errors.ts +++ b/packages/typescript-estree/src/semantic-or-syntactic-errors.ts @@ -1,6 +1,12 @@ -import * as ts from 'typescript'; +import type { + Diagnostic, + DiagnosticWithLocation, + Program, + SourceFile, +} from 'typescript'; +import { flattenDiagnosticMessageText, sys } from 'typescript'; -interface SemanticOrSyntacticError extends ts.Diagnostic { +export interface SemanticOrSyntacticError extends Diagnostic { message: string; } @@ -12,8 +18,8 @@ interface SemanticOrSyntacticError extends ts.Diagnostic { * the user opts in to throwing errors on semantic issues. */ export function getFirstSemanticOrSyntacticError( - program: ts.Program, - ast: ts.SourceFile, + program: Program, + ast: SourceFile, ): SemanticOrSyntacticError | undefined { try { const supportedSyntacticDiagnostics = whitelistSupportedDiagnostics( @@ -52,8 +58,8 @@ export function getFirstSemanticOrSyntacticError( } function whitelistSupportedDiagnostics( - diagnostics: readonly (ts.DiagnosticWithLocation | ts.Diagnostic)[], -): readonly (ts.DiagnosticWithLocation | ts.Diagnostic)[] { + diagnostics: readonly (DiagnosticWithLocation | Diagnostic)[], +): readonly (DiagnosticWithLocation | Diagnostic)[] { return diagnostics.filter(diagnostic => { switch (diagnostic.code) { case 1013: // "A rest parameter or binding pattern may not have a trailing comma." @@ -103,13 +109,10 @@ function whitelistSupportedDiagnostics( } function convertDiagnosticToSemanticOrSyntacticError( - diagnostic: ts.Diagnostic, + diagnostic: Diagnostic, ): SemanticOrSyntacticError { return { ...diagnostic, - message: ts.flattenDiagnosticMessageText( - diagnostic.messageText, - ts.sys.newLine, - ), + message: flattenDiagnosticMessageText(diagnostic.messageText, sys.newLine), }; } diff --git a/packages/typescript-estree/tests/ast-alignment/parse.ts b/packages/typescript-estree/tests/ast-alignment/parse.ts index 7d340278649e..701cf069ff42 100644 --- a/packages/typescript-estree/tests/ast-alignment/parse.ts +++ b/packages/typescript-estree/tests/ast-alignment/parse.ts @@ -1,11 +1,11 @@ /* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access */ - import type babelParser from '@babel/parser'; -import { ParserPlugin } from '@babel/parser'; +import type { ParserPlugin } from '@babel/parser'; import { codeFrameColumns } from '@babel/code-frame'; import type { File } from '@babel/types'; -import * as parser from '../../src/parser'; -import { TSESTree } from '@typescript-eslint/types'; +import type { TSESTree } from '@typescript-eslint/types'; +import { AST, parseAndGenerateServices } from '../../src/parser'; +import type { TSError } from '../../src/node-utils'; function createError( message: string, @@ -48,9 +48,9 @@ function parseWithBabelParser(text: string, jsx = true): File { }); } -function parseWithTypeScriptESTree(text: string, jsx = true): parser.AST { +function parseWithTypeScriptESTree(text: string, jsx = true): AST { try { - const result = parser.parseAndGenerateServices(text, { + const result = parseAndGenerateServices(text, { loc: true, range: true, tokens: false, @@ -66,8 +66,10 @@ function parseWithTypeScriptESTree(text: string, jsx = true): parser.AST { jsx, }); return result.ast; - } catch (e: any) { - throw createError(e.message, e.lineNumber, e.column); + } catch (e: unknown) { + const error = e as TSError; + + throw createError(error.message, error.lineNumber, error.column); } } diff --git a/packages/typescript-estree/tests/ast-alignment/spec.ts b/packages/typescript-estree/tests/ast-alignment/spec.ts index 9e308b73c643..8af9996fc9d6 100644 --- a/packages/typescript-estree/tests/ast-alignment/spec.ts +++ b/packages/typescript-estree/tests/ast-alignment/spec.ts @@ -1,7 +1,12 @@ import fs from 'fs'; +import type { File } from '@babel/types'; import { fixturesToTest } from './fixtures-to-test'; import { parse } from './parse'; -import * as parseUtils from './utils'; +import { + preprocessBabylonAST, + preprocessTypescriptAST, + removeLocationDataAndSourceTypeFromProgramNode, +} from './utils'; fixturesToTest.forEach(fixture => { const filename = fixture.filename; @@ -72,13 +77,13 @@ fixturesToTest.forEach(fixture => { * Perform some extra formatting steps on the babel AST before comparing */ expect( - parseUtils.removeLocationDataAndSourceTypeFromProgramNode( - parseUtils.preprocessBabylonAST(babelParserResult.ast), + removeLocationDataAndSourceTypeFromProgramNode( + preprocessBabylonAST(babelParserResult.ast as File), fixture.ignoreSourceType, ), ).toEqual( - parseUtils.removeLocationDataAndSourceTypeFromProgramNode( - parseUtils.preprocessTypescriptAST(typeScriptESTreeResult.ast), + removeLocationDataAndSourceTypeFromProgramNode( + preprocessTypescriptAST(typeScriptESTreeResult.ast), fixture.ignoreSourceType, ), ); diff --git a/packages/typescript-estree/tests/ast-alignment/utils.ts b/packages/typescript-estree/tests/ast-alignment/utils.ts index 3cc6f4de0161..90ec167744c8 100644 --- a/packages/typescript-estree/tests/ast-alignment/utils.ts +++ b/packages/typescript-estree/tests/ast-alignment/utils.ts @@ -1,8 +1,8 @@ // babel types are something we don't really care about /* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/restrict-plus-operands */ +import type { File, Program } from '@babel/types'; import { AST_NODE_TYPES, TSESTree } from '../../src/ts-estree'; import { deeplyCopy, omitDeep } from '../../tools/test-utils'; -import * as BabelTypes from '@babel/types'; /** * Common predicates for Babylon AST preprocessing @@ -18,8 +18,8 @@ const ifNumber = (val: unknown): boolean => typeof val === 'number'; * @param ast raw babylon AST * @returns processed babylon AST */ -export function preprocessBabylonAST(ast: BabelTypes.File): any { - return omitDeep( +export function preprocessBabylonAST(ast: File): any { + return omitDeep( ast.program, [ { @@ -258,7 +258,7 @@ export function preprocessBabylonAST(ast: BabelTypes.File): any { * Once we use babel 8, this can be removed. * @see https://github.com/babel/babel/pull/12608 */ - TSParenthesizedType(node: any) { + TSParenthesizedType(node) { const { typeAnnotation } = node; Object.keys(node).forEach(key => delete node[key]); Object.assign(node, typeAnnotation); @@ -268,7 +268,7 @@ export function preprocessBabylonAST(ast: BabelTypes.File): any { * `typescript` plugin and the `classStaticBlock` plugin are enabled. * @see https://github.com/babel/babel/issues/13674 */ - StaticBlock(node: any) { + StaticBlock(node) { if (node.static != null) { delete node.static; } diff --git a/packages/typescript-estree/tests/lib/convert.test.ts b/packages/typescript-estree/tests/lib/convert.test.ts index 74ce08aa9673..277063b9d8cb 100644 --- a/packages/typescript-estree/tests/lib/convert.test.ts +++ b/packages/typescript-estree/tests/lib/convert.test.ts @@ -1,7 +1,8 @@ // deeplyCopy is private internal /* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-call */ -import { Converter } from '../../src/convert'; import * as ts from 'typescript'; +import type { TSNode } from '../../src'; +import { Converter } from '../../src/convert'; describe('convert', () => { function convertCode(code: string): ts.SourceFile { @@ -114,7 +115,7 @@ describe('convert', () => { ) { expect(node).toBe( maps.esTreeNodeToTSNodeMap.get( - maps.tsNodeToESTreeNodeMap.get(node as any), + maps.tsNodeToESTreeNodeMap.get(node as TSNode), ), ); } @@ -146,7 +147,7 @@ describe('convert', () => { ) { expect(node).toBe( maps.esTreeNodeToTSNodeMap.get( - maps.tsNodeToESTreeNodeMap.get(node as any), + maps.tsNodeToESTreeNodeMap.get(node as TSNode), ), ); } @@ -187,7 +188,7 @@ describe('convert', () => { expect(maps.esTreeNodeToTSNodeMap.get(program.body[0])).toBeDefined(); expect(program.body[0]).not.toBe( - maps.tsNodeToESTreeNodeMap.get(ast.statements[0] as any), + maps.tsNodeToESTreeNodeMap.get(ast.statements[0] as TSNode), ); checkMaps(ast); }); diff --git a/packages/typescript-estree/tools/test-utils.ts b/packages/typescript-estree/tools/test-utils.ts index c07ab65b831d..65e1cb131e1d 100644 --- a/packages/typescript-estree/tools/test-utils.ts +++ b/packages/typescript-estree/tools/test-utils.ts @@ -1,11 +1,15 @@ -import * as parser from '../src'; -import { TSESTreeOptions } from '../src/parser-options'; +import type { + TSESTree, + TSESTreeOptions, + ParseAndGenerateServicesResult, +} from '../src'; +import { parse as parserParse, parseAndGenerateServices } from '../src'; export function parseCodeAndGenerateServices( code: string, config: TSESTreeOptions, -): parser.ParseAndGenerateServicesResult { - return parser.parseAndGenerateServices(code, config); +): ParseAndGenerateServicesResult { + return parseAndGenerateServices(code, config); } /** @@ -24,10 +28,10 @@ export function createSnapshotTestBlock( /** * @returns the AST object */ - function parse(): parser.TSESTree.Program { + function parse(): TSESTree.Program { const ast = generateServices - ? parser.parseAndGenerateServices(code, config).ast - : parser.parse(code, config); + ? parseAndGenerateServices(code, config).ast + : parserParse(code, config); return deeplyCopy(ast); } @@ -127,7 +131,7 @@ export function omitDeep( continue; } - const child = node[prop]; + const child = node[prop] as UnknownObject | UnknownObject[]; if (Array.isArray(child)) { const value = []; for (const el of child) { diff --git a/tests/jest-resolver.js b/tests/jest-resolver.js index f79f54509b79..a0460ee0ac43 100644 --- a/tests/jest-resolver.js +++ b/tests/jest-resolver.js @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-unsafe-argument */ // https://github.com/facebook/jest/issues/9771#issuecomment-871585234 // temporary workaround while we wait for https://github.com/facebook/jest/issues/9771