Skip to content

Commit

Permalink
feat(parser, typescript-estree): export withoutProjectParserOptions u…
Browse files Browse the repository at this point in the history
…tility (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
  • Loading branch information
fpapado authored Jun 6, 2024
1 parent 60fb643 commit c9a6dd9
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
28 changes: 28 additions & 0 deletions docs/packages/Parser.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -402,3 +402,31 @@ module.exports = {

</TabItem>
</Tabs>

### `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 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="somePlugin.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);
}
```
1 change: 1 addition & 0 deletions packages/parser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export {
ParserServicesWithoutTypeInformation,
clearCaches,
createProgram,
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
Expand Down
1 change: 1 addition & 0 deletions packages/typescript-estree/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export { typescriptVersionIsAtLeast } from './version-check';
export * from './getModifiers';
export { TSError } from './node-utils';
export * from './clear-caches';
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
Expand Down
18 changes: 18 additions & 0 deletions packages/typescript-estree/src/withoutProjectParserOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { 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 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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { withoutProjectParserOptions } from '../../src';

describe('withoutProjectParserOptions', () => {
it('removes only project parser options', () => {
const without = withoutProjectParserOptions({
comment: true,
EXPERIMENTAL_useProjectService: true,
project: true,
});
expect(without).toEqual({
comment: true,
});
});
});

0 comments on commit c9a6dd9

Please sign in to comment.