Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(parser, typescript-estree): export withoutProjectParserOptions utility #9233

Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions docs/packages/Parser.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,31 @@ 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 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);
}
```
1 change: 1 addition & 0 deletions packages/parser/src/index.ts
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
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
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
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;
}
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
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,
});
});
});