diff --git a/eslint.config.mjs b/eslint.config.mjs index 9966f2d475fa..51d591a29d2b 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -573,6 +573,7 @@ export default tseslint.config( files: [ 'packages/ast-spec/{src,tests,typings}/**/*.ts', 'packages/integration-tests/{tests,tools,typing}/**/*.ts', + 'packages/parser/{src,tests}/**/*.ts', 'packages/utils/src/**/*.ts', 'packages/visitor-keys/src/**/*.ts', 'packages/website*/src/**/*.ts', diff --git a/packages/parser/src/index.ts b/packages/parser/src/index.ts index 9799bc982d07..b166e7bd85fb 100644 --- a/packages/parser/src/index.ts +++ b/packages/parser/src/index.ts @@ -1,10 +1,10 @@ export { parse, parseForESLint, ParserOptions } from './parser'; export { - ParserServices, - ParserServicesWithTypeInformation, - ParserServicesWithoutTypeInformation, clearCaches, createProgram, + ParserServices, + ParserServicesWithoutTypeInformation, + ParserServicesWithTypeInformation, withoutProjectParserOptions, } from '@typescript-eslint/typescript-estree'; diff --git a/packages/parser/src/parser.ts b/packages/parser/src/parser.ts index 59ea3f2befe4..584d5727e12c 100644 --- a/packages/parser/src/parser.ts +++ b/packages/parser/src/parser.ts @@ -2,19 +2,20 @@ import type { AnalyzeOptions, ScopeManager, } from '@typescript-eslint/scope-manager'; -import { analyze } from '@typescript-eslint/scope-manager'; import type { Lib, TSESTree } from '@typescript-eslint/types'; -import { ParserOptions } from '@typescript-eslint/types'; import type { AST, ParserServices, TSESTreeOptions, } from '@typescript-eslint/typescript-estree'; -import { parseAndGenerateServices } from '@typescript-eslint/typescript-estree'; import type { VisitorKeys } from '@typescript-eslint/visitor-keys'; +import type * as ts from 'typescript'; + +import { analyze } from '@typescript-eslint/scope-manager'; +import { ParserOptions } from '@typescript-eslint/types'; +import { parseAndGenerateServices } from '@typescript-eslint/typescript-estree'; import { visitorKeys } from '@typescript-eslint/visitor-keys'; import debug from 'debug'; -import type * as ts from 'typescript'; import { ScriptTarget } from 'typescript'; const log = debug('typescript-eslint:parser:parser'); @@ -27,9 +28,9 @@ interface ESLintProgram extends AST<{ comment: true; tokens: true }> { interface ParseForESLintResult { ast: ESLintProgram; + scopeManager: ScopeManager; services: ParserServices; visitorKeys: VisitorKeys; - scopeManager: ScopeManager; } function validateBoolean( @@ -58,24 +59,24 @@ function getLib(compilerOptions: ts.CompilerOptions): Lib[] { const target = compilerOptions.target ?? ScriptTarget.ES5; // https://github.com/microsoft/TypeScript/blob/ae582a22ee1bb052e19b7c1bc4cac60509b574e0/src/compiler/utilitiesPublic.ts#L13-L36 switch (target) { - case ScriptTarget.ESNext: - return ['esnext.full']; - case ScriptTarget.ES2022: - return ['es2022.full']; - case ScriptTarget.ES2021: - return ['es2021.full']; - case ScriptTarget.ES2020: - return ['es2020.full']; - case ScriptTarget.ES2019: - return ['es2019.full']; - case ScriptTarget.ES2018: - return ['es2018.full']; - case ScriptTarget.ES2017: - return ['es2017.full']; - case ScriptTarget.ES2016: - return ['es2016.full']; case ScriptTarget.ES2015: return ['es6']; + case ScriptTarget.ES2016: + return ['es2016.full']; + case ScriptTarget.ES2017: + return ['es2017.full']; + case ScriptTarget.ES2018: + return ['es2018.full']; + case ScriptTarget.ES2019: + return ['es2019.full']; + case ScriptTarget.ES2020: + return ['es2020.full']; + case ScriptTarget.ES2021: + return ['es2021.full']; + case ScriptTarget.ES2022: + return ['es2022.full']; + case ScriptTarget.ESNext: + return ['esnext.full']; default: return ['lib']; } @@ -135,8 +136,8 @@ function parseForESLint( const analyzeOptions: AnalyzeOptions = { globalReturn: parserOptions.ecmaFeatures.globalReturn, - jsxPragma: parserOptions.jsxPragma, jsxFragmentName: parserOptions.jsxFragmentName, + jsxPragma: parserOptions.jsxPragma, lib: parserOptions.lib, sourceType: parserOptions.sourceType, }; @@ -184,7 +185,7 @@ function parseForESLint( services.experimentalDecorators ??= parserOptions.experimentalDecorators === true; - return { ast, services, scopeManager, visitorKeys }; + return { ast, scopeManager, services, visitorKeys }; } export { parse, parseForESLint, ParserOptions }; diff --git a/packages/parser/tests/lib/parser.test.ts b/packages/parser/tests/lib/parser.test.ts index 8a3c4ab45578..4da882979b29 100644 --- a/packages/parser/tests/lib/parser.test.ts +++ b/packages/parser/tests/lib/parser.test.ts @@ -1,8 +1,8 @@ -import path from 'node:path'; +import type { ParserOptions } from '@typescript-eslint/types'; import * as scopeManager from '@typescript-eslint/scope-manager'; -import type { ParserOptions } from '@typescript-eslint/types'; import * as typescriptESTree from '@typescript-eslint/typescript-estree'; +import path from 'node:path'; import { ScriptTarget } from 'typescript'; import { parse, parseForESLint } from '../../src/parser'; @@ -26,17 +26,17 @@ describe('parser', () => { const code = 'const valid = true;'; const spy = jest.spyOn(typescriptESTree, 'parseAndGenerateServices'); const config: ParserOptions = { - sourceType: 'module' as const, ecmaFeatures: { globalReturn: false, jsx: false, }, + sourceType: 'module' as const, // ts-estree specific + errorOnTypeScriptSyntacticAndSemanticIssues: false, + extraFileExtensions: ['.foo'], filePath: './isolated-file.src.ts', project: 'tsconfig.json', - errorOnTypeScriptSyntacticAndSemanticIssues: false, tsconfigRootDir: path.resolve(__dirname, '../fixtures/services'), - extraFileExtensions: ['.foo'], }; parseForESLint(code, config); expect(spy).toHaveBeenCalledTimes(1); @@ -173,21 +173,21 @@ describe('parser', () => { const code = 'const valid = true;'; const spy = jest.spyOn(scopeManager, 'analyze'); const config: ParserOptions = { - sourceType: 'module' as const, ecmaFeatures: { globalReturn: false, jsx: false, }, + sourceType: 'module' as const, // scope-manager specific - lib: ['dom.iterable'], - jsxPragma: 'Foo', jsxFragmentName: 'Bar', + jsxPragma: 'Foo', + lib: ['dom.iterable'], // ts-estree specific + errorOnTypeScriptSyntacticAndSemanticIssues: false, + extraFileExtensions: ['.foo'], filePath: 'isolated-file.src.ts', project: 'tsconfig.json', - errorOnTypeScriptSyntacticAndSemanticIssues: false, tsconfigRootDir: path.join(__dirname, '../fixtures/services'), - extraFileExtensions: ['.foo'], }; parseForESLint(code, config); @@ -195,9 +195,9 @@ describe('parser', () => { expect(spy).toHaveBeenCalledTimes(1); expect(spy).toHaveBeenLastCalledWith(expect.anything(), { globalReturn: false, - lib: ['dom.iterable'], - jsxPragma: 'Foo', jsxFragmentName: 'Bar', + jsxPragma: 'Foo', + lib: ['dom.iterable'], sourceType: 'module', }); }); diff --git a/packages/parser/tests/lib/services.test.ts b/packages/parser/tests/lib/services.test.ts index f1784541e768..3b3121017610 100644 --- a/packages/parser/tests/lib/services.test.ts +++ b/packages/parser/tests/lib/services.test.ts @@ -1,10 +1,10 @@ -import fs from 'node:fs'; -import path from 'node:path'; - import { createProgram } from '@typescript-eslint/typescript-estree'; import * as glob from 'glob'; +import fs from 'node:fs'; +import path from 'node:path'; import type { ParserOptions } from '../../src/parser'; + import { createSnapshotTestBlock, formatSnapshotName, diff --git a/packages/parser/tests/lib/tsx.test.ts b/packages/parser/tests/lib/tsx.test.ts index 5258b6ec2c93..5b6922e2c8f3 100644 --- a/packages/parser/tests/lib/tsx.test.ts +++ b/packages/parser/tests/lib/tsx.test.ts @@ -64,10 +64,10 @@ describe('TSX', () => { expect( parseWithError(code, { - filePath: 'test.ts', ecmaFeatures: { jsx: true, }, + filePath: 'test.ts', }), ).toMatchInlineSnapshot(` TSError { diff --git a/packages/parser/tests/test-utils/test-utils.ts b/packages/parser/tests/test-utils/test-utils.ts index 55b352ce242a..a286f9df4a86 100644 --- a/packages/parser/tests/test-utils/test-utils.ts +++ b/packages/parser/tests/test-utils/test-utils.ts @@ -1,16 +1,17 @@ import type { TSESTree } from '@typescript-eslint/typescript-estree'; import type { ParserOptions } from '../../src/parser'; + import * as parser from '../../src/parser'; const defaultConfig = { + comment: true, + errorOnUnknownASTType: true, loc: true, range: true, raw: true, - tokens: true, - comment: true, - errorOnUnknownASTType: true, sourceType: 'module' as const, + tokens: true, }; /** diff --git a/packages/parser/tests/test-utils/ts-error-serializer.ts b/packages/parser/tests/test-utils/ts-error-serializer.ts index 5267898b5260..9c9acca97eee 100644 --- a/packages/parser/tests/test-utils/ts-error-serializer.ts +++ b/packages/parser/tests/test-utils/ts-error-serializer.ts @@ -1,8 +1,8 @@ -import { TSError } from '@typescript-eslint/typescript-estree'; import type { Plugin } from 'pretty-format'; +import { TSError } from '@typescript-eslint/typescript-estree'; + export const serializer: Plugin = { - test: (val: unknown): val is TSError => val instanceof TSError, serialize(val: TSError, config, indentation, depth, refs, printer) { const format = (value: unknown): string => printer(value, config, indentation, depth + 1, refs); @@ -15,4 +15,5 @@ export const serializer: Plugin = { `}` ); }, + test: (val: unknown): val is TSError => val instanceof TSError, };