Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall 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
20 changes: 11 additions & 9 deletionspackages/utils/src/eslint-utils/getParserServices.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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
View file
Open in desktop

This file was deleted.

View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionspackages/utils/src/ts-eslint/Rule.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionspackages/utils/tests/eslint-utils/getParserServices.test.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 isknown', () => {
it('throws a standard errorwith the parserwhen parserOptions.esTreeNodeToTSNodeMap is missing and the parser istypescript-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: {
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 numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp