Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

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 6 commits into from
Mar 16, 2024
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
16 changes: 8 additions & 8 deletions packages/utils/src/eslint-utils/getParserServices.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ const createMockRuleContext = (
...overrides,
}) as unknown as UnknownRuleContext;

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.',
);

describe('getParserServices', () => {
it('throws a standard error when parserOptions.esTreeNodeToTSNodeMap is missing and the parser is known', () => {
const context = createMockRuleContext({
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