Skip to content
This repository was archived by the owner on Mar 7, 2025. It is now read-only.

Commit c83e66c

Browse files
sxzzShinigami92
andauthored
feat: improve type suggestions with specific defineFlatConfig (#150)
Co-authored-by: Shinigami92 <[email protected]>
1 parent a0ee545 commit c83e66c

File tree

7 files changed

+69
-25
lines changed

7 files changed

+69
-25
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
"eslint-plugin-unicorn": "~44.0.2",
7474
"eslint-plugin-vue": "~9.6.0",
7575
"eslint-plugin-vue-pug": "~0.5.4",
76+
"expect-type": "~0.15.0",
7677
"json-schema": "~0.4.0",
7778
"json-schema-to-typescript": "~11.0.2",
7879
"prettier": "2.7.1",

pnpm-lock.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/flat-config/index.d.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { ESLint, Linter } from 'eslint';
22
import type { Rules } from '../rules';
3-
import type { LiteralUnion } from '../utility-types';
43
import type { LanguageOptions } from './language-options';
54
import type { LinterOptions } from './linter-options';
65

@@ -63,12 +62,9 @@ export interface FlatESLintConfigItem {
6362
*
6463
* @see [Using predefined configurations](https://eslint.org/docs/latest/user-guide/configuring/configuration-files-new#using-predefined-configurations)
6564
*/
66-
export type PredefinedConfig = LiteralUnion<
67-
'eslint:recommended' | 'eslint:all'
68-
>;
65+
export type PredefinedConfig = 'eslint:recommended' | 'eslint:all';
6966

7067
export type FlatESLintConfig = FlatESLintConfigItem | PredefinedConfig;
71-
export type FlatESLintConfigs = Array<FlatESLintConfig>;
7268

7369
export * from './language-options';
7470
export * from './linter-options';

src/index.d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ESLintConfig } from './config';
2-
import type { FlatESLintConfig, FlatESLintConfigs } from './flat-config';
2+
import type { FlatESLintConfig } from './flat-config';
33

44
/**
55
* Define an ESLint config.
@@ -17,7 +17,7 @@ export function defineConfig(config: ESLintConfig): ESLintConfig;
1717
* @param config an item of Flat ESLint config.
1818
* @returns an item of Flat ESLint config.
1919
*/
20-
export function defineConfig(config: FlatESLintConfig): FlatESLintConfig;
20+
export function defineFlatConfig(config: FlatESLintConfig): FlatESLintConfig;
2121

2222
/**
2323
* Define a flat ESLint config.
@@ -27,7 +27,9 @@ export function defineConfig(config: FlatESLintConfig): FlatESLintConfig;
2727
* @param config Flat ESLint config.
2828
* @returns Flat ESLint config.
2929
*/
30-
export function defineConfig(config: FlatESLintConfigs): FlatESLintConfigs;
30+
export function defineFlatConfig(
31+
config: readonly FlatESLintConfig[],
32+
): FlatESLintConfig[];
3133

3234
export * from './config';
3335
export * from './flat-config';

src/index.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,7 @@
22

33
exports.__esModule = true;
44
exports.defineConfig = void 0;
5+
exports.defineFlatConfig = void 0;
56

6-
/**
7-
* Define an ESLint config.
8-
*
9-
* @param config ESLint config.
10-
* @returns ESLint config.
11-
*/
12-
function defineConfig(config) {
13-
return config;
14-
}
15-
16-
exports.defineConfig = defineConfig;
7+
exports.defineConfig = (config) => config;
8+
exports.defineFlatConfig = (config) => config;

src/index.mjs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
/**
2-
* Define an eslint config.
3-
*
4-
* @param config Eslint config.
5-
* @returns Eslint config.
6-
*/
71
export function defineConfig(config) {
82
return config;
93
}
4+
5+
export function defineFlatConfig(config) {
6+
return config;
7+
}

tests/define.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { expectTypeOf } from 'expect-type';
2+
import { describe, test } from 'vitest';
3+
import type { ESLintConfig, FlatESLintConfig } from '../src';
4+
import { defineConfig, defineFlatConfig } from '../src';
5+
6+
describe('define', () => {
7+
test('define empty config', () => {
8+
expectTypeOf(defineConfig({})).toEqualTypeOf<ESLintConfig>();
9+
});
10+
11+
test('define ESLint config', () => {
12+
expectTypeOf(
13+
defineConfig({
14+
env: {},
15+
extends: [],
16+
rules: {},
17+
}),
18+
).toEqualTypeOf<ESLintConfig>();
19+
});
20+
21+
test('define an item of flat ESLint config', () => {
22+
expectTypeOf(
23+
defineFlatConfig({
24+
ignores: [],
25+
plugins: {},
26+
rules: {},
27+
}),
28+
).toEqualTypeOf<FlatESLintConfig>();
29+
});
30+
31+
test('define predefined flat ESLint config', () => {
32+
expectTypeOf(
33+
defineFlatConfig('eslint:recommended'),
34+
).toEqualTypeOf<FlatESLintConfig>();
35+
});
36+
37+
test('define flat ESLint config', () => {
38+
expectTypeOf(
39+
defineFlatConfig([
40+
'eslint:recommended',
41+
{
42+
ignores: [],
43+
plugins: {},
44+
rules: {},
45+
},
46+
]),
47+
).toEqualTypeOf<FlatESLintConfig[]>();
48+
});
49+
});

0 commit comments

Comments
 (0)