8000 feat(parser, typescript-estree): export withoutProjectParserOptions utility by fpapado · Pull Request #9233 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions 8000 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