From 3f657cd492734fad6661d4838f72fbfd3c1ae3c5 Mon Sep 17 00:00:00 2001 From: Fotis Papadogeorgopoulos Date: Mon, 3 Jun 2024 20:51:25 +0300 Subject: [PATCH 01/12] feat(typescript-estree): add function to remove parser options that prompt typechecking --- packages/typescript-estree/src/index.ts | 1 + ...emoveParserOptionsThatPromptTypechecking.ts | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 packages/typescript-estree/src/removeParserOptionsThatPromptTypechecking.ts diff --git a/packages/typescript-estree/src/index.ts b/packages/typescript-estree/src/index.ts index c86262b7cd9d..868fcce8f504 100644 --- a/packages/typescript-estree/src/index.ts +++ b/packages/typescript-estree/src/index.ts @@ -19,6 +19,7 @@ export { typescriptVersionIsAtLeast } from './version-check'; export * from './getModifiers'; export { TSError } from './node-utils'; export * from './clear-caches'; +export { removeParserOptionsThatPromptTypechecking } from './removeParserOptionsThatPromptTypechecking'; // note - cannot migrate this to an import statement because it will make TSC copy the package.json to the dist folder // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access diff --git a/packages/typescript-estree/src/removeParserOptionsThatPromptTypechecking.ts b/packages/typescript-estree/src/removeParserOptionsThatPromptTypechecking.ts new file mode 100644 index 000000000000..fc33f94167c7 --- /dev/null +++ b/packages/typescript-estree/src/removeParserOptionsThatPromptTypechecking.ts @@ -0,0 +1,18 @@ +import { TSESTreeOptions } from './parser-options'; + +/** + * Removes options that prompt the parser to parse the project with type + * information. In other words, you can use this if you are invoking the parser + * directly, to ensure that one file will be parsed in isolation, which is much, + * much faster. + * + * @see https://github.com/typescript-eslint/typescript-eslint/issues/8428 + */ +export function removeParserOptionsThatPromptTypechecking( + opts: TSESTreeOptions, +): TSESTreeOptions { + delete opts.EXPERIMENTAL_useProjectService; + delete opts.project; + + return opts; +} From 62b677e69e4cac046701b27bc55f2f092622e60c Mon Sep 17 00:00:00 2001 From: Fotis Papadogeorgopoulos Date: Mon, 3 Jun 2024 20:52:54 +0300 Subject: [PATCH 02/12] feat(parser): re-export removeParserOptionsThatPromptTypechecking --- packages/parser/src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/parser/src/index.ts b/packages/parser/src/index.ts index 9ccf6b17ed8f..9d4beaf36c68 100644 --- a/packages/parser/src/index.ts +++ b/packages/parser/src/index.ts @@ -5,6 +5,7 @@ export { ParserServicesWithoutTypeInformation, clearCaches, createProgram, + removeParserOptionsThatPromptTypechecking, } from '@typescript-eslint/typescript-estree'; // note - cannot migrate this to an import statement because it will make TSC copy the package.json to the dist folder From 5d9d6e7e00a78545bdc17d4a73c2c000d15894a9 Mon Sep 17 00:00:00 2001 From: Fotis Papadogeorgopoulos Date: Mon, 3 Jun 2024 22:17:39 +0300 Subject: [PATCH 03/12] chore: fix lint rule about importing types --- .../src/removeParserOptionsThatPromptTypechecking.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/typescript-estree/src/removeParserOptionsThatPromptTypechecking.ts b/packages/typescript-estree/src/removeParserOptionsThatPromptTypechecking.ts index fc33f94167c7..88e499a81765 100644 --- a/packages/typescript-estree/src/removeParserOptionsThatPromptTypechecking.ts +++ b/packages/typescript-estree/src/removeParserOptionsThatPromptTypechecking.ts @@ -1,4 +1,4 @@ -import { TSESTreeOptions } from './parser-options'; +import type { TSESTreeOptions } from './parser-options'; /** * Removes options that prompt the parser to parse the project with type From b0319e438fad66ac922a3ad9ef427ff4a0f02393 Mon Sep 17 00:00:00 2001 From: Fotis Papadogeorgopoulos Date: Tue, 4 Jun 2024 10:06:07 +0300 Subject: [PATCH 04/12] refactor: prefer rest/spread over delete --- .../src/removeParserOptionsThatPromptTypechecking.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/typescript-estree/src/removeParserOptionsThatPromptTypechecking.ts b/packages/typescript-estree/src/removeParserOptionsThatPromptTypechecking.ts index 88e499a81765..c89664f0ccc9 100644 --- a/packages/typescript-estree/src/removeParserOptionsThatPromptTypechecking.ts +++ b/packages/typescript-estree/src/removeParserOptionsThatPromptTypechecking.ts @@ -11,8 +11,7 @@ import type { TSESTreeOptions } from './parser-options'; export function removeParserOptionsThatPromptTypechecking( opts: TSESTreeOptions, ): TSESTreeOptions { - delete opts.EXPERIMENTAL_useProjectService; - delete opts.project; + const { EXPERIMENTAL_useProjectService, project, ...rest } = opts; - return opts; + return rest; } From f583a3e64d6f511068752fb04eefff75466c97eb Mon Sep 17 00:00:00 2001 From: Fotis Papadogeorgopoulos Date: Tue, 4 Jun 2024 10:07:27 +0300 Subject: [PATCH 05/12] refactor: rename to withoutProjectParserOptions --- packages/typescript-estree/src/index.ts | 2 +- .../src/removeParserOptionsThatPromptTypechecking.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/typescript-estree/src/index.ts b/packages/typescript-estree/src/index.ts index 868fcce8f504..facecc32b836 100644 --- a/packages/typescript-estree/src/index.ts +++ b/packages/typescript-estree/src/index.ts @@ -19,7 +19,7 @@ export { typescriptVersionIsAtLeast } from './version-check'; export * from './getModifiers'; export { TSError } from './node-utils'; export * from './clear-caches'; -export { removeParserOptionsThatPromptTypechecking } from './removeParserOptionsThatPromptTypechecking'; +export { withoutProjectParserOptions as removeParserOptionsThatPromptTypechecking } from './removeParserOptionsThatPromptTypechecking'; // note - cannot migrate this to an import statement because it will make TSC copy the package.json to the dist folder // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access diff --git a/packages/typescript-estree/src/removeParserOptionsThatPromptTypechecking.ts b/packages/typescript-estree/src/removeParserOptionsThatPromptTypechecking.ts index c89664f0ccc9..e5d27b87dd17 100644 --- a/packages/typescript-estree/src/removeParserOptionsThatPromptTypechecking.ts +++ b/packages/typescript-estree/src/removeParserOptionsThatPromptTypechecking.ts @@ -8,7 +8,7 @@ import type { TSESTreeOptions } from './parser-options'; * * @see https://github.com/typescript-eslint/typescript-eslint/issues/8428 */ -export function removeParserOptionsThatPromptTypechecking( +export function withoutProjectParserOptions( opts: TSESTreeOptions, ): TSESTreeOptions { const { EXPERIMENTAL_useProjectService, project, ...rest } = opts; From 4dbeacf3c96f26a360dec5a715263cbde3c82d92 Mon Sep 17 00:00:00 2001 From: Fotis Papadogeorgopoulos Date: Tue, 4 Jun 2024 10:11:24 +0300 Subject: [PATCH 06/12] refactor: rename withoutProjectParserOptions file as well --- packages/parser/src/index.ts | 2 +- packages/typescript-estree/src/index.ts | 2 +- ...ThatPromptTypechecking.ts => withoutProjectParserOptions.ts} | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename packages/typescript-estree/src/{removeParserOptionsThatPromptTypechecking.ts => withoutProjectParserOptions.ts} (100%) diff --git a/packages/parser/src/index.ts b/packages/parser/src/index.ts index 9d4beaf36c68..9799bc982d07 100644 --- a/packages/parser/src/index.ts +++ b/packages/parser/src/index.ts @@ -5,7 +5,7 @@ export { ParserServicesWithoutTypeInformation, clearCaches, createProgram, - removeParserOptionsThatPromptTypechecking, + withoutProjectParserOptions, } from '@typescript-eslint/typescript-estree'; // note - cannot migrate this to an import statement because it will make TSC copy the package.json to the dist folder diff --git a/packages/typescript-estree/src/index.ts b/packages/typescript-estree/src/index.ts index facecc32b836..5f6e8768d0c4 100644 --- a/packages/typescript-estree/src/index.ts +++ b/packages/typescript-estree/src/index.ts @@ -19,7 +19,7 @@ export { typescriptVersionIsAtLeast } from './version-check'; export * from './getModifiers'; export { TSError } from './node-utils'; export * from './clear-caches'; -export { withoutProjectParserOptions as removeParserOptionsThatPromptTypechecking } from './removeParserOptionsThatPromptTypechecking'; +export { withoutProjectParserOptions } from './withoutProjectParserOptions'; // note - cannot migrate this to an import statement because it will make TSC copy the package.json to the dist folder // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access diff --git a/packages/typescript-estree/src/removeParserOptionsThatPromptTypechecking.ts b/packages/typescript-estree/src/withoutProjectParserOptions.ts similarity index 100% rename from packages/typescript-estree/src/removeParserOptionsThatPromptTypechecking.ts rename to packages/typescript-estree/src/withoutProjectParserOptions.ts From 4ca0ac032fa9849a84a2d59d08539128e646cc3f Mon Sep 17 00:00:00 2001 From: Fotis Papadogeorgopoulos Date: Tue, 4 Jun 2024 10:19:33 +0300 Subject: [PATCH 07/12] test: add unit test for withoutProjectParserOptions --- .../lib/withoutProjectParserOptions.test.ts | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 packages/typescript-estree/tests/lib/withoutProjectParserOptions.test.ts diff --git a/packages/typescript-estree/tests/lib/withoutProjectParserOptions.test.ts b/packages/typescript-estree/tests/lib/withoutProjectParserOptions.test.ts new file mode 100644 index 000000000000..2166ac6173f3 --- /dev/null +++ b/packages/typescript-estree/tests/lib/withoutProjectParserOptions.test.ts @@ -0,0 +1,20 @@ +import { withoutProjectParserOptions } from '../../src'; + +describe('withoutProjectParserOptions', () => { + it('removes all project parser options', () => { + const without = withoutProjectParserOptions({ + EXPERIMENTAL_useProjectService: true, + project: true, + }); + expect(without.EXPERIMENTAL_useProjectService).toBeUndefined(); + expect(without.project).toBeUndefined(); + }); + it('leaves other parser options intact', () => { + const without = withoutProjectParserOptions({ + EXPERIMENTAL_useProjectService: true, + project: true, + comment: true, + }); + expect(without.comment).toEqual(true); + }); +}); From 3f83bf36c9a561d02670abe991f7e114cfed85ef Mon Sep 17 00:00:00 2001 From: Fotis Papadogeorgopoulos Date: Tue, 4 Jun 2024 12:19:12 +0300 Subject: [PATCH 08/12] docs: add withoutProjectParserOptions --- docs/packages/Parser.mdx | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/docs/packages/Parser.mdx b/docs/packages/Parser.mdx index 26b5fb80d200..734fea565c91 100644 --- a/docs/packages/Parser.mdx +++ b/docs/packages/Parser.mdx @@ -355,3 +355,34 @@ module.exports = { }, }; ``` + +### `withoutProjectParserOptions(parserOptions)` + +Removes options that prompt the parser to parse the project with type +information. In other words, you can use this if you are invoking the parser +directly, to ensure that one file will be parsed in isolation, which is much, +much faster. + +This is useful in cases where you invoke the parser directly, such as in an +ESLint plugin context. + +```ts +declare function withoutProjectParserOptions( + options: TSESTreeOptions, +): TSESTreeOptions; +``` + +Example usage: + +```js title=".eslintrc.js" +const parser = require('@typescript-eslint/parser'); + +function parse(path, content, context) { + const contextParserOptions = context.languageOptions?.parserOptions ?? {}; + const parserOptions = + parser.withoutProjectParserOptions(contextParserOptions); + + // Do something with the cleaned-up options eventually, such as invoking the parser + parser.parseForESLint(content, parserOptions); +} +``` From 30c0e63de9909f99e36e7aacb6a7d7a8a26fc9a1 Mon Sep 17 00:00:00 2001 From: Fotis Papadogeorgopoulos Date: Tue, 4 Jun 2024 14:28:03 +0300 Subject: [PATCH 09/12] refactor: use single unit test for withoutProjectParserOptions --- .../tests/lib/withoutProjectParserOptions.test.ts | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/packages/typescript-estree/tests/lib/withoutProjectParserOptions.test.ts b/packages/typescript-estree/tests/lib/withoutProjectParserOptions.test.ts index 2166ac6173f3..fb06dc1473cd 100644 --- a/packages/typescript-estree/tests/lib/withoutProjectParserOptions.test.ts +++ b/packages/typescript-estree/tests/lib/withoutProjectParserOptions.test.ts @@ -1,20 +1,14 @@ import { withoutProjectParserOptions } from '../../src'; describe('withoutProjectParserOptions', () => { - it('removes all project parser options', () => { + it('removes only project parser options', () => { const without = withoutProjectParserOptions({ + comment: true, EXPERIMENTAL_useProjectService: true, project: true, }); - expect(without.EXPERIMENTAL_useProjectService).toBeUndefined(); - expect(without.project).toBeUndefined(); - }); - it('leaves other parser options intact', () => { - const without = withoutProjectParserOptions({ - EXPERIMENTAL_useProjectService: true, - project: true, + expect(without).toBe({ comment: true, }); - expect(without.comment).toEqual(true); }); }); From a1a62820f95701f946fd59a5c4112d7f42b3539a Mon Sep 17 00:00:00 2001 From: Fotis Papadogeorgopoulos Date: Tue, 4 Jun 2024 14:30:01 +0300 Subject: [PATCH 10/12] chore: fix no-unused-vars lint error --- packages/typescript-estree/src/withoutProjectParserOptions.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/typescript-estree/src/withoutProjectParserOptions.ts b/packages/typescript-estree/src/withoutProjectParserOptions.ts index e5d27b87dd17..f911bee1dcf4 100644 --- a/packages/typescript-estree/src/withoutProjectParserOptions.ts +++ b/packages/typescript-estree/src/withoutProjectParserOptions.ts @@ -11,6 +11,7 @@ import type { TSESTreeOptions } from './parser-options'; export function withoutProjectParserOptions( opts: TSESTreeOptions, ): TSESTreeOptions { + // eslint-disable-next-line @typescript-eslint/no-unused-vars -- The variables are meant to be omitted const { EXPERIMENTAL_useProjectService, project, ...rest } = opts; return rest; From 15d47de3c351ef4e1d1fb836dbc69002330733bd Mon Sep 17 00:00:00 2001 From: Fotis Papadogeorgopoulos Date: Tue, 4 Jun 2024 16:52:53 +0300 Subject: [PATCH 11/12] chore: avoid wrapping lines in Parser.mdx --- docs/packages/Parser.mdx | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/docs/packages/Parser.mdx b/docs/packages/Parser.mdx index 734fea565c91..2233461d39cf 100644 --- a/docs/packages/Parser.mdx +++ b/docs/packages/Parser.mdx @@ -358,13 +358,10 @@ module.exports = { ### `withoutProjectParserOptions(parserOptions)` -Removes options that prompt the parser to parse the project with type -information. In other words, you can use this if you are invoking the parser -directly, to ensure that one file will be parsed in isolation, which is much, -much faster. +Removes options that prompt the parser to parse the project with type information. +In other words, you can use this if you are invoking the parser directly, to ensure that one file will be parsed in isolation, which is much faster. -This is useful in cases where you invoke the parser directly, such as in an -ESLint plugin context. +This is useful in cases where you invoke the parser directly, such as in an ESLint plugin context. ```ts declare function withoutProjectParserOptions( From 8e62e0b22e3d916e1c27d08eb86fe6f5dc2e83ac Mon Sep 17 00:00:00 2001 From: Fotis Papadogeorgopoulos Date: Tue, 4 Jun 2024 20:48:34 +0300 Subject: [PATCH 12/12] fix: update test with toEqual assertion --- .../tests/lib/withoutProjectParserOptions.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/typescript-estree/tests/lib/withoutProjectParserOptions.test.ts b/packages/typescript-estree/tests/lib/withoutProjectParserOptions.test.ts index fb06dc1473cd..56a96ca2bd91 100644 --- a/packages/typescript-estree/tests/lib/withoutProjectParserOptions.test.ts +++ b/packages/typescript-estree/tests/lib/withoutProjectParserOptions.test.ts @@ -7,7 +7,7 @@ describe('withoutProjectParserOptions', () => { EXPERIMENTAL_useProjectService: true, project: true, }); - expect(without).toBe({ + expect(without).toEqual({ comment: true, }); });