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

feat: improve type suggestions with specific defineFlatConfig #150

Merged
merged 4 commits into from
Oct 30, 2022
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"eslint-plugin-unicorn": "~44.0.2",
"eslint-plugin-vue": "~9.6.0",
"eslint-plugin-vue-pug": "~0.5.4",
"expect-type": "~0.15.0",
"json-schema": "~0.4.0",
"json-schema-to-typescript": "~11.0.2",
"prettier": "2.7.1",
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions src/flat-config/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { ESLint, Linter } from 'eslint';
import type { Rules } from '../rules';
import type { LiteralUnion } from '../utility-types';
import type { LanguageOptions } from './language-options';
import type { LinterOptions } from './linter-options';

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

export type FlatESLintConfig = FlatESLintConfigItem | PredefinedConfig;
export type FlatESLintConfigs = Array<FlatESLintConfig>;

export * from './language-options';
export * from './linter-options';
8 changes: 5 additions & 3 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ESLintConfig } from './config';
import type { FlatESLintConfig, FlatESLintConfigs } from './flat-config';
import type { FlatESLintConfig } from './flat-config';

/**
* Define an ESLint config.
Expand All @@ -17,7 +17,7 @@ export function defineConfig(config: ESLintConfig): ESLintConfig;
* @param config an item of Flat ESLint config.
* @returns an item of Flat ESLint config.
*/
export function defineConfig(config: FlatESLintConfig): FlatESLintConfig;
export function defineFlatConfig(config: FlatESLintConfig): FlatESLintConfig;

/**
* Define a flat ESLint config.
Expand All @@ -27,7 +27,9 @@ export function defineConfig(config: FlatESLintConfig): FlatESLintConfig;
* @param config Flat ESLint config.
* @returns Flat ESLint config.
*/
export function defineConfig(config: FlatESLintConfigs): FlatESLintConfigs;
export function defineFlatConfig(
config: readonly FlatESLintConfig[],
): FlatESLintConfig[];

export * from './config';
export * from './flat-config';
Expand Down
14 changes: 3 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@

exports.__esModule = true;
exports.defineConfig = void 0;
exports.defineFlatConfig = void 0;

/**
* Define an ESLint config.
*
* @param config ESLint config.
* @returns ESLint config.
*/
function defineConfig(config) {
return config;
}

exports.defineConfig = defineConfig;
exports.defineConfig = (config) => config;
exports.defineFlatConfig = (config) => config;
10 changes: 4 additions & 6 deletions src/index.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/**
* Define an eslint config.
*
* @param config Eslint config.
* @returns Eslint config.
*/
export function defineConfig(config) {
return config;
}

export function defineFlatConfig(config) {
return config;
}
49 changes: 49 additions & 0 deletions tests/define.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { expectTypeOf } from 'expect-type';
import { describe, test } from 'vitest';
import type { ESLintConfig, FlatESLintConfig } from '../src';
import { defineConfig, defineFlatConfig } from '../src';

describe('define', () => {
test('define empty config', () => {
expectTypeOf(defineConfig({})).toEqualTypeOf<ESLintConfig>();
});

test('define ESLint config', () => {
expectTypeOf(
defineConfig({
env: {},
extends: [],
rules: {},
}),
).toEqualTypeOf<ESLintConfig>();
});

test('define an item of flat ESLint config', () => {
expectTypeOf(
defineFlatConfig({
ignores: [],
plugins: {},
rules: {},
}),
).toEqualTypeOf<FlatESLintConfig>();
});

test('define predefined flat ESLint config', () => {
expectTypeOf(
defineFlatConfig('eslint:recommended'),
).toEqualTypeOf<FlatESLintConfig>();
});

test('define flat ESLint config', () => {
expectTypeOf(
defineFlatConfig([
'eslint:recommended',
{
ignores: [],
plugins: {},
rules: {},
},
]),
).toEqualTypeOf<FlatESLintConfig[]>();
});
});