Skip to content

Commit

Permalink
feat(utils): add parser name to thrown parser error message (#8484)
Browse files Browse the repository at this point in the history
Co-authored-by: Brad Zacher <brad.zacher@gmail.com>
  • Loading branch information
disaacson and bradzacher committed Mar 16, 2024
1 parent 883f220 commit df00134
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
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

0 comments on commit df00134

Please sign in to comment.