Skip to content

feat(typescript-eslint): improve undefined extension handling #10177

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
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
25 changes: 24 additions & 1 deletion packages/typescript-eslint/src/config-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,34 @@ export interface ConfigWithExtends extends TSESLint.FlatConfig.Config {
export function config(
...configs: ConfigWithExtends[]
): TSESLint.FlatConfig.ConfigArray {
return configs.flatMap(configWithExtends => {
return configs.flatMap((configWithExtends, configIndex) => {
const { extends: extendsArr, ...config } = configWithExtends;
if (extendsArr == null || extendsArr.length === 0) {
return config;
}
const undefinedExtensions = extendsArr.reduce<number[]>(
(acc, extension, extensionIndex) => {
const maybeExtension = extension as
| TSESLint.FlatConfig.Config
| undefined;
if (maybeExtension == null) {
acc.push(extensionIndex);
}
return acc;
},
[],
);
if (undefinedExtensions.length) {
const configName =
configWithExtends.name != null
? `, named "${configWithExtends.name}",`
: ' (anonymous)';
const extensionIndices = undefinedExtensions.join(', ');
throw new Error(
`Your config at index ${configIndex}${configName} contains undefined` +
` extensions at the following indices: ${extensionIndices}.`,
);
}

return [
...extendsArr.map(extension => {
Expand Down
54 changes: 54 additions & 0 deletions packages/typescript-eslint/tests/configs.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { TSESLint } from '@typescript-eslint/utils';
import type {
FlatConfig,
RuleRecommendation,
Expand Down Expand Up @@ -368,6 +369,59 @@ describe('config helper', () => {
]);
});

it('throws error containing config name when some extensions are undefined', () => {
const extension: TSESLint.FlatConfig.Config = { rules: { rule1: 'error' } };

expect(() =>
plugin.config(
{
extends: [extension],
files: ['common-file'],
ignores: ['common-ignored'],
name: 'my-config-1',
rules: { rule: 'error' },
},
{
// eslint-disable-next-line @typescript-eslint/no-explicit-any
extends: [undefined as any, extension, undefined as any],
files: ['common-file'],
ignores: ['common-ignored'],
name: 'my-config-2',
rules: { rule: 'error' },
},
),
).toThrow(
'Your config at index 1, named "my-config-2", contains undefined ' +
'extensions at the following indices: 0, 2',
);
});

it('throws error without config name when some extensions are undefined', () => {
const extension: TSESLint.FlatConfig.Config = { rules: { rule1: 'error' } };

expect(() =>
plugin.config(
{
extends: [extension],
files: ['common-file'],
ignores: ['common-ignored'],
name: 'my-config-1',
rules: { rule: 'error' },
},
{
// eslint-disable-next-line @typescript-eslint/no-explicit-any
extends: [undefined as any, extension, undefined as any],
files: ['common-file'],
ignores: ['common-ignored'],
rules: { rule: 'error' },
},
),
).toThrow(
'Your config at index 1 (anonymous) contains undefined extensions at ' +
'the following indices: 0, 2',
);
});

it('flattens extended configs with config name', () => {
expect(
plugin.config({
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy