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

Skip to content
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