8000 fix(utils): context.parserPath may be undefined by JoshuaKGoldberg · Pull Request #9486 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content

fix(utils): context.parserPath may be undefined #9486

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
8000
Diff view
Diff view
20 changes: 11 additions & 9 deletions packages/utils/src/eslint-utils/getParserServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {
ParserServices,
ParserServicesWithTypeInformation,
} from '../ts-estree';
import { parserPathSeemsToBeTSESLint } from './parserPathSeemsToBeTSESLint';
import { parserSeemsToBeTSESLint } from './parserSeemsToBeTSESLint';

const ERROR_MESSAGE_REQUIRES_PARSER_SERVICES =
'You have used a rule which requires parserServices to be generated. You must therefore provide a value for the "parserOptions.project" property for @typescript-eslint/parser.';
Expand Down Expand Up @@ -60,6 +60,9 @@ function getParserServices(
context: Readonly<TSESLint.RuleContext<string, unknown[]>>,
allowWithoutFullTypeInformation = false,
): ParserServices {
const parser =
context.parserPath || context.languageOptions.parser?.meta?.name;

// This check is unnecessary if the user is using the latest version of our parser.
//
// However the world isn't perfect:
Expand All @@ -74,7 +77,7 @@ function getParserServices(
context.sourceCode.parserServices?.esTreeNodeToTSNodeMap == null ||
context.sourceCode.parserServices.tsNodeToESTreeNodeMap == null
) {
throwError(context.parserPath);
throwError(parser);
}

// if a rule requires full type information, then hard fail if it doesn't exist
Expand All @@ -83,21 +86,20 @@ function getParserServices(
context.sourceCode.parserServices.program == null &&
!allowWithoutFullTypeInformation
) {
throwError(context.parserPath);
throwError(parser);
}

return context.sourceCode.parserServices as ParserServices;
}
/* eslint-enable @typescript-eslint/unified-signatures */

function throwError(parserPath: string): never {
function throwError(parser: string | undefined): never {
const messages = [
ERROR_MESSAGE_REQUIRES_PARSER_SERVICES,
`Parser: ${parserPath}`,
];
if (!parserPathSeemsToBeTSESLint(parserPath)) {
messages.push(ERROR_MESSAGE_UNKNOWN_PARSER);
}
`Parser: ${parser || '(unknown)'}`,
!parserSeemsToBeTSESLint(parser) && ERROR_MESSAGE_UNKNOWN_PARSER,
].filter(Boolean);

throw new Error(messages.join('\n'));
}

Expand Down

This file was deleted.

3 changes: 3 additions & 0 deletions packages/utils/src/eslint-utils/parserSeemsToBeTSESLint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function parserSeemsToBeTSESLint(parser: string | undefined): boolean {
return !!parser && /(?:typescript-eslint|\.\.)[\w/\\]*parser/.test(parser);
}
4 changes: 2 additions & 2 deletions packages/utils/src/ts-eslint/Rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@ export interface RuleContext<
*/
options: Options;
/**
* The name of the parser from configuration.
* The name of the parser from configuration, if in eslintrc (legacy) config.
*/
parserPath: string;
parserPath: string | undefined;
/**
* The language options configured for this run
*/
Expand Down
91 changes: 78 additions & 13 deletions packages/utils/tests/eslint-utils/getParserServices.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type * as ts from 'typescript';

import type { ParserServices, TSESLint, TSESTree } from '../../src';
import { ESLintUtils } from '../../src';
import type { FlatConfig } from '../../src/ts-eslint';

type UnknownRuleContext = Readonly<TSESLint.RuleContext<string, unknown[]>>;

Expand All @@ -25,18 +26,20 @@ const createMockRuleContext = (
...overrides,
}) as unknown as UnknownRuleContext;

const requiresParserServicesMessageTemplate =
const requiresParserServicesMessageTemplate = (parser = '\\S*'): string =>
'You have used a rule which requires parserServices to be generated. You must therefore provide a value for the "parserOptions.project" property for @typescript-eslint/parser.\n' +
'Parser: \\S*';
const baseErrorRegex = new RegExp(requiresParserServicesMessageTemplate);
const unknownParserErrorRegex = new RegExp(
requiresParserServicesMessageTemplate +
'\n' +
'Note: detected a parser other than @typescript-eslint/parser. Make sure the parser is configured to forward "parserOptions.project" to @typescript-eslint/parser.',
);
`Parser: ${parser}`;
const baseErrorRegex = (parser?: string): RegExp =>
new RegExp(requiresParserServicesMessageTemplate(parser));
const unknownParserErrorRegex = (parser?: string): RegExp =>
new RegExp(
requiresParserServicesMessageTemplate(parser) +
'\n' +
'Note: detected a parser other than @typescript-eslint/parser. Make sure the parser is configured to forward "parserOptions.project" to @typescript-eslint/parser.',
);

describe('getParserServices', () => {
it('throws a standard error when parserOptions.esTreeNodeToTSNodeMap is missing and the parser is known', () => {
it('throws a standard error with the parser when parserOptions.esTreeNodeToTSNodeMap is missing and the parser is typescript-eslint', () => {
const context = createMockRuleContext({
sourceCode: {
...defaults.sourceCode,
Expand All @@ -48,7 +51,69 @@ describe('getParserServices', () => {
});

expect(() => ESLintUtils.getParserServices(context)).toThrow(
baseErrorRegex,
baseErrorRegex('@typescript-eslint[\\/]parser[\\/]dist[\\/]index\\.js'),
);
});

it('throws a standard error with the parser when parserOptions.esTreeNodeToTSNodeMap is missing and the parser is custom', () => {
const context = createMockRuleContext({
languageOptions: {
8000 parser: {
meta: {
name: 'custom-parser',
},
} as FlatConfig.Parser,
},
parserPath: undefined,
sourceCode: {
...defaults.sourceCode,
parserServices: {
...defaults.sourceCode.parserServices,
esTreeNodeToTSNodeMap: undefined as any,
},
},
});

expect(() => ESLintUtils.getParserServices(context)).toThrow(
baseErrorRegex('custom-parser'),
);
});

it('throws a standard error with an unknown parser when parserOptions.esTreeNodeToTSNodeMap is missing and the parser is missing', () => {
const context = createMockRuleContext({
languageOptions: {},
parserPath: undefined,
sourceCode: {
...defaults.sourceCode,
parserServices: {
...defaults.sourceCode.parserServices,
esTreeNodeToTSNodeMap: undefined as any,
},
},
});

expect(() => ESLintUtils.getParserServices(context)).toThrow(
baseErrorRegex('\\(unknown\\)'),
);
});

it('throws a standard error with an unknown parser when parserOptions.esTreeNodeToTSNodeMap is missing and the parser is unknown', () => {
const context = createMockRuleContext({
languageOptions: {
parser: {} as FlatConfig.Parser,
},
parserPath: undefined,
sourceCode: {
...defaults.sourceCode,
parserServices: {
...defaults.sourceCode.parserServices,
esTreeNodeToTSNodeMap: undefined as any,
},
},
});

expect(() => ESLintUtils.getParserServices(context)).toThrow(
baseErrorRegex('\\(unknown\\)'),
);
});

Expand All @@ -64,7 +129,7 @@ describe('getParserServices', () => {
},
});
expect(() => ESLintUtils.getParserServices(context)).toThrow(
unknownParserErrorRegex,
unknownParserErrorRegex(),
);
});

Expand All @@ -80,7 +145,7 @@ describe('getParserServices', () => {
});

expect(() => ESLintUtils.getParserServices(context)).toThrow(
baseErrorRegex,
baseErrorRegex(),
);
});

Expand All @@ -96,7 +161,7 @@ describe('getParserServices', () => {
});

expect(() => ESLintUtils.getParserServices(context)).toThrow(
baseErrorRegex,
baseErrorRegex(),
);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { parserPathSeemsToBeTSESLint } from '../../src/eslint-utils/parserPathSeemsToBeTSESLint';
import { parserSeemsToBeTSESLint } from '../../src/eslint-utils/parserSeemsToBeTSESLint';

describe('parserPathSeemsToBeTSESLint', () => {
describe('parserSeemsToBeTSESLint', () => {
test.each([
[undefined, false],
['espree', false],
['local.js', false],
['../other.js', false],
['@babel/eslint-parser/lib/index.cjs', false],
Expand All @@ -19,6 +21,6 @@ describe('parserPathSeemsToBeTSESLint', () => {
['/path/to/@typescript-eslint/packages/parser/dist/index.js', true],
['/path/to/@typescript-eslint/packages/parser/index.js', true],
])('%s', (parserPath, expected) => {
expect(parserPathSeemsToBeTSESLint(parserPath)).toBe(expected);
expect(parserSeemsToBeTSESLint(parserPath)).toBe(expected);
});
});
Loading
0