Skip to content

Commit c9a6dd9

Browse files
authored
feat(parser, typescript-estree): export withoutProjectParserOptions utility (typescript-eslint#9233)
* feat(typescript-estree): add function to remove parser options that prompt typechecking * feat(parser): re-export removeParserOptionsThatPromptTypechecking * chore: fix lint rule about importing types * refactor: prefer rest/spread over delete * refactor: rename to withoutProjectParserOptions * refactor: rename withoutProjectParserOptions file as well * test: add unit test for withoutProjectParserOptions * docs: add withoutProjectParserOptions * refactor: use single unit test for withoutProjectParserOptions * chore: fix no-unused-vars lint error * chore: avoid wrapping lines in Parser.mdx * fix: update test with toEqual assertion
1 parent 60fb643 commit c9a6dd9

File tree

5 files changed

+62
-0
lines changed

5 files changed

+62
-0
lines changed

docs/packages/Parser.mdx

+28
Original file line numberDiff line numberDiff line change
@@ -402,3 +402,31 @@ module.exports = {
402402

403403
</TabItem>
404404
</Tabs>
405+
406+
### `withoutProjectParserOptions(parserOptions)`
407+
408+
Removes options that prompt the parser to parse the project with type information.
409+
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.
410+
411+
This is useful in cases where you invoke the parser directly, such as in an ESLint plugin context.
412+
413+
```ts
414+
declare function withoutProjectParserOptions(
415+
options: TSESTreeOptions,
416+
): TSESTreeOptions;
417+
```
418+
419+
Example usage:
420+
421+
```js title="somePlugin.js"
422+
const parser = require('@typescript-eslint/parser');
423+
424+
function parse(path, content, context) {
425+
const contextParserOptions = context.languageOptions?.parserOptions ?? {};
426+
const parserOptions =
427+
parser.withoutProjectParserOptions(contextParserOptions);
428+
429+
// Do something with the cleaned-up options eventually, such as invoking the parser
430+
parser.parseForESLint(content, parserOptions);
431+
}
432+
```

packages/parser/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export {
55
ParserServicesWithoutTypeInformation,
66
clearCaches,
77
createProgram,
8+
withoutProjectParserOptions,
89
} from '@typescript-eslint/typescript-estree';
910

1011
// note - cannot migrate this to an import statement because it will make TSC copy the package.json to the dist folder

packages/typescript-estree/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export { typescriptVersionIsAtLeast } from './version-check';
1919
export * from './getModifiers';
2020
export { TSError } from './node-utils';
2121
export * from './clear-caches';
22+
export { withoutProjectParserOptions } from './withoutProjectParserOptions';
2223

2324
// note - cannot migrate this to an import statement because it will make TSC copy the package.json to the dist folder
2425
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { TSESTreeOptions } from './parser-options';
2+
3+
/**
4+
* Removes options that prompt the parser to parse the project with type
5+
* information. In other words, you can use this if you are invoking the parser
6+
* directly, to ensure that one file will be parsed in isolation, which is much,
7+
* much faster.
8+
*
9+
* @see https://github.com/typescript-eslint/typescript-eslint/issues/8428
10+
*/
11+
export function withoutProjectParserOptions(
12+
opts: TSESTreeOptions,
13+
): TSESTreeOptions {
14+
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- The variables are meant to be omitted
15+
const { EXPERIMENTAL_useProjectService, project, ...rest } = opts;
16+
17+
return rest;
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { withoutProjectParserOptions } from '../../src';
2+
3+
describe('withoutProjectParserOptions', () => {
4+
it('removes only project parser options', () => {
5+
const without = withoutProjectParserOptions({
6+
comment: true,
7+
EXPERIMENTAL_useProjectService: true,
8+
project: true,
9+
});
10+
expect(without).toEqual({
11+
comment: true,
12+
});
13+
});
14+
});

0 commit comments

Comments
 (0)