From a4e179680f858a0d3392ebd28aead5cc70160e86 Mon Sep 17 00:00:00 2001 From: Derrick Isaacson Date: Thu, 15 Feb 2024 17:45:01 -0500 Subject: [PATCH 1/4] chore: include root cause in parser path error message --- .../src/eslint-utils/getParserServices.ts | 17 ++++++------ .../eslint-utils/getParserServices.test.ts | 27 ++++++++++--------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/packages/utils/src/eslint-utils/getParserServices.ts b/packages/utils/src/eslint-utils/getParserServices.ts index c834c230091d..14059365fd47 100644 --- a/packages/utils/src/eslint-utils/getParserServices.ts +++ b/packages/utils/src/eslint-utils/getParserServices.ts @@ -1,3 +1,4 @@ +import { parse } from 'path'; import type * as TSESLint from '../ts-eslint'; import type { ParserServices, @@ -91,14 +92,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 }; diff --git a/packages/utils/tests/eslint-utils/getParserServices.test.ts b/packages/utils/tests/eslint-utils/getParserServices.test.ts index 103acb91044b..115b52491531 100644 --- a/packages/utils/tests/eslint-utils/getParserServices.test.ts +++ b/packages/utils/tests/eslint-utils/getParserServices.test.ts @@ -26,6 +26,11 @@ 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); + it('throws a standard error when parserOptions.esTreeNodeToTSNodeMap is missing and the parser is known', () => { const context = createMockRuleContext({ sourceCode: { @@ -38,9 +43,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, ); }); @@ -56,11 +59,13 @@ describe('getParserServices', () => { }, }); + 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.', + ); 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, ); }); @@ -76,9 +81,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, ); }); @@ -94,9 +97,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, ); }); From fb2570b849c6f10d0c3759357dff8b4278d177f1 Mon Sep 17 00:00:00 2001 From: Derrick Isaacson Date: Thu, 15 Feb 2024 17:54:23 -0500 Subject: [PATCH 2/4] parser error message enhancement - code formatting --- packages/utils/src/eslint-utils/getParserServices.ts | 1 + .../tests/eslint-utils/getParserServices.test.ts | 11 +++++------ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/utils/src/eslint-utils/getParserServices.ts b/packages/utils/src/eslint-utils/getParserServices.ts index 14059365fd47..184ef56e9319 100644 --- a/packages/utils/src/eslint-utils/getParserServices.ts +++ b/packages/utils/src/eslint-utils/getParserServices.ts @@ -1,4 +1,5 @@ import { parse } from 'path'; + import type * as TSESLint from '../ts-eslint'; import type { ParserServices, diff --git a/packages/utils/tests/eslint-utils/getParserServices.test.ts b/packages/utils/tests/eslint-utils/getParserServices.test.ts index 115b52491531..efa83bdd0e58 100644 --- a/packages/utils/tests/eslint-utils/getParserServices.test.ts +++ b/packages/utils/tests/eslint-utils/getParserServices.test.ts @@ -30,6 +30,11 @@ describe('getParserServices', () => { '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.', + ); it('throws a standard error when parserOptions.esTreeNodeToTSNodeMap is missing and the parser is known', () => { const context = createMockRuleContext({ @@ -58,12 +63,6 @@ describe('getParserServices', () => { }, }, }); - - 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.', - ); expect(() => ESLintUtils.getParserServices(context)).toThrow( unknownParserErrorRegex, ); From d98a3810b80f03b83e9924e605c4e73604df90fc Mon Sep 17 00:00:00 2001 From: Derrick Isaacson Date: Thu, 15 Feb 2024 17:57:42 -0500 Subject: [PATCH 3/4] parser error message enhancement - rm extra import --- packages/utils/src/eslint-utils/getParserServices.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/utils/src/eslint-utils/getParserServices.ts b/packages/utils/src/eslint-utils/getParserServices.ts index 184ef56e9319..4af4851ad547 100644 --- a/packages/utils/src/eslint-utils/getParserServices.ts +++ b/packages/utils/src/eslint-utils/getParserServices.ts @@ -1,5 +1,3 @@ -import { parse } from 'path'; - import type * as TSESLint from '../ts-eslint'; import type { ParserServices, From 8759052be3a9d07de1904731bd6446a7a5252c3f Mon Sep 17 00:00:00 2001 From: Derrick Isaacson Date: Wed, 28 Feb 2024 13:37:56 -0500 Subject: [PATCH 4/4] #8484: parser detection error message - move constants to root of the file --- .../eslint-utils/getParserServices.test.ts | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/utils/tests/eslint-utils/getParserServices.test.ts b/packages/utils/tests/eslint-utils/getParserServices.test.ts index efa83bdd0e58..b247727786ae 100644 --- a/packages/utils/tests/eslint-utils/getParserServices.test.ts +++ b/packages/utils/tests/eslint-utils/getParserServices.test.ts @@ -25,17 +25,17 @@ const createMockRuleContext = ( ...overrides, }) 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.', - ); +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({ sourceCode: {