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

Already on GitHub? Sign in to your account

feat(utils): add parser name to thrown parser error message #8484

Merged
merged 6 commits into from Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
16 changes: 8 additions & 8 deletions packages/utils/src/eslint-utils/getParserServices.ts
Expand Up @@ -91,14 +91,14 @@ function getParserServices(
/* eslint-enable @typescript-eslint/unified-signatures */

function throwError(parserPath: string): never {
throw new Error(
parserPathSeemsToBeTSESLint(parserPath)
? ERROR_MESSAGE_REQUIRES_PARSER_SERVICES
: [
ERROR_MESSAGE_REQUIRES_PARSER_SERVICES,
ERROR_MESSAGE_UNKNOWN_PARSER,
].join('\n'),
);
const messages = [
ERROR_MESSAGE_REQUIRES_PARSER_SERVICES,
`Parser: ${parserPath}`,
];
if (!parserPathSeemsToBeTSESLint(parserPath)) {
messages.push(ERROR_MESSAGE_UNKNOWN_PARSER);
}
throw new Error(messages.join('\n'));
}

export { getParserServices };
28 changes: 14 additions & 14 deletions packages/utils/tests/eslint-utils/getParserServices.test.ts
Expand Up @@ -26,6 +26,16 @@ const createMockRuleContext = (
}) as unknown as UnknownRuleContext;

describe('getParserServices', () => {
const requiresParserServicesMessageTemplate =
'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.',
);
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved

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

expect(() => ESLintUtils.getParserServices(context)).toThrow(
new Error(
'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.',
),
baseErrorRegex,
);
});

Expand All @@ -55,12 +63,8 @@ describe('getParserServices', () => {
},
},
});

expect(() => ESLintUtils.getParserServices(context)).toThrow(
new Error(
'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' +
'Note: detected a parser other than @typescript-eslint/parser. Make sure the parser is configured to forward "parserOptions.project" to @typescript-eslint/parser.',
),
unknownParserErrorRegex,
);
});

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

expect(() => ESLintUtils.getParserServices(context)).toThrow(
new Error(
'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.',
),
baseErrorRegex,
);
});

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

expect(() => ESLintUtils.getParserServices(context)).toThrow(
new Error(
'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.',
),
baseErrorRegex,
);
});

Expand Down