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

Commit c320ba4

Browse files
Shinigami92antfu
andauthored
Support augmented global properties (#238)
Co-authored-by: antfu <[email protected]>
1 parent 7560275 commit c320ba4

File tree

9 files changed

+224
-30
lines changed

9 files changed

+224
-30
lines changed

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ pnpm add --save-dev eslint eslint-define-config
4747

4848
```ts
4949
// @ts-check
50+
51+
// To activate auto-suggestions for Rules of specific plugins, you need to add a `/// <reference types="eslint-plugin-PLUGIN_NAME/define-config-support" />` comment.
52+
// ⚠️ This feature is very new and requires the support of the respective plugin owners.
53+
54+
/// <reference types="@typescript-eslint/eslint-plugin/define-config-support" />
55+
5056
const { defineConfig } = require('eslint-define-config');
5157

5258
module.exports = defineConfig({
@@ -97,6 +103,40 @@ _Click on the thumbnail to play the video_
97103
<img src="https://user-images.githubusercontent.com/7195563/112726343-30c56b00-8f1d-11eb-9b92-260c530caf1b.png" alt="Video" width="600px"/>
98104
</a>
99105

106+
## Want to support your own plugin?
107+
108+
:warning: **This feature is very new and requires the support of the respective plugin owners**
109+
110+
Add a `declare module` to your plugin package like this:
111+
112+
```ts
113+
declare module 'eslint-define-config' {
114+
export interface CustomRuleOptions {
115+
/**
116+
* Require consistently using either `T[]` or `Array<T>` for arrays.
117+
*
118+
* @see [array-type](https://typescript-eslint.io/rules/array-type)
119+
*/
120+
'@typescript-eslint/array-type': [
121+
{
122+
default?: 'array' | 'generic' | 'array-simple';
123+
readonly?: 'array' | 'generic' | 'array-simple';
124+
},
125+
];
126+
127+
// ... more Rules
128+
}
129+
}
130+
```
131+
132+
There are other interfaces that can be extended.
133+
134+
- `CustomExtends`
135+
- `CustomParserOptions`
136+
- `CustomParsers`
137+
- `CustomPlugins`
138+
- `CustomSettings`
139+
100140
# Credits
101141

102142
- [Proposal Idea](https://github.com/eslint/eslint/issues/14249)

src/config/extends/index.d.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,30 @@ import type { VuePugExtends } from './eslint-plugin-vue-pug';
2121
import type { IntlifyVueI18nExtends } from './intlify-vue-i18n';
2222
import type { TypescriptEslintExtends } from './typescript-eslint';
2323

24+
/**
25+
* This is a special exported interface for other packages to declare
26+
* additional extensions that should bail out for eslint extensions. For example
27+
* `'@typescript-eslint/eslint-plugin'` can declare it like so in its `d.ts`:
28+
*
29+
* ```ts
30+
* declare module 'eslint-define-config' {
31+
* export interface CustomExtends {
32+
* 'plugin:@typescript-eslint/all': void;
33+
* 'plugin:@typescript-eslint/base': void;
34+
* 'plugin:@typescript-eslint/disable-type-checked': void;
35+
* 'plugin:@typescript-eslint/eslint-recommended': void;
36+
* 'plugin:@typescript-eslint/recommended-type-checked': void;
37+
* 'plugin:@typescript-eslint/recommended': void;
38+
* 'plugin:@typescript-eslint/strict-type-checked': void;
39+
* 'plugin:@typescript-eslint/strict': void;
40+
* 'plugin:@typescript-eslint/stylistic-type-checked': void;
41+
* 'plugin:@typescript-eslint/stylistic': void;
42+
* }
43+
* }
44+
* ```
45+
*/
46+
export interface CustomExtends {}
47+
2448
/**
2549
* All known extends.
2650
*/
@@ -46,6 +70,8 @@ export type KnownExtends = LiteralUnion<
4670
| VitestExtends
4771
| VueExtends
4872
| VuePugExtends
73+
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
74+
| keyof CustomExtends
4975
>;
5076

5177
/**

src/config/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export interface ESLintConfig {
8383
*
8484
* @see [Rules](https://eslint.org/docs/latest/user-guide/configuring/rules)
8585
*/
86-
rules?: Rules;
86+
rules?: Partial<Rules>;
8787

8888
/**
8989
* Overrides.

src/config/overrides.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export interface Override {
8080
*
8181
* @see [Rules](https://eslint.org/docs/user-guide/configuring/rules)
8282
*/
83-
rules?: Rules;
83+
rules?: Partial<Rules>;
8484

8585
/**
8686
* Settings.

src/config/plugin.d.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
import type { LiteralUnion } from '../utility-types';
22

3+
/**
4+
* This is a special exported interface for other packages to declare
5+
* additional plugins that should bail out for eslint plugins. For example
6+
* `'@typescript-eslint/eslint-plugin'` can declare it like so in its `d.ts`:
7+
*
8+
* ```ts
9+
* declare module 'eslint-define-config' {
10+
* export interface CustomPlugins {
11+
* '@typescript-eslint': void;
12+
* }
13+
* }
14+
* ```
15+
*/
16+
export interface CustomPlugins {}
17+
318
/** Plugin. */
419
export type Plugin = LiteralUnion<
520
| '@graphql-eslint'
@@ -20,4 +35,6 @@ export type Plugin = LiteralUnion<
2035
| 'unicorn'
2136
| 'vitest'
2237
| 'vue'
38+
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
39+
| keyof CustomPlugins
2340
>;

src/config/settings/index.d.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,23 @@ import type { MdxSettings } from './mdx';
55
import type { NodeSettings } from './node';
66
import type { ReactSettings } from './react';
77

8+
/**
9+
* This is a special exported interface for other packages to declare
10+
* additional settings that should bail out for eslint settings. For example
11+
* `'eslint-plugin-jsx-a11y'` can declare it like so in its `d.ts`:
12+
*
13+
* ```ts
14+
* declare module 'eslint-define-config' {
15+
* export interface CustomSettings {
16+
* 'jsx-a11y': {
17+
* components?: Record<string, string>;
18+
* };
19+
* }
20+
* }
21+
* ```
22+
*/
23+
export interface CustomSettings {}
24+
825
/**
926
* Settings.
1027
*/
@@ -15,4 +32,5 @@ export interface Settings
1532
MdxSettings,
1633
NodeSettings,
1734
ReactSettings,
35+
Partial<CustomSettings>,
1836
Partial<Record<string, unknown>> {}

src/flat-config/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export interface FlatESLintConfigItem {
5454
*
5555
* @see [Configuring rules](https://eslint.org/docs/latest/user-guide/configuring/configuration-files-new#configuring-rules)
5656
*/
57-
rules?: Rules;
57+
rules?: Partial<Rules>;
5858

5959
/**
6060
* An object containing name-value pairs of information that should be available to all rules.

src/parser-options.d.ts

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,20 +132,80 @@ export type DebugLevel =
132132
| boolean
133133
| Array<'eslint' | 'typescript' | 'typescript-eslint'>;
134134

135+
/**
136+
* This is a special exported interface for other packages to declare
137+
* additional parsers that should bail out for eslint parsers. For example
138+
* `'@typescript-eslint/eslint-plugin'` can declare it like so in its `d.ts`:
139+
*
140+
* ```ts
141+
* declare module 'eslint-define-config' {
142+
* export interface CustomParsers {
143+
* '@typescript-eslint/parser': void;
144+
* }
145+
* }
146+
* ```
147+
*/
148+
export interface CustomParsers {}
149+
135150
/** Parser. */
136151
export type Parser = LiteralUnion<
137152
| 'babel-eslint'
138153
| '@typescript-eslint/parser'
139154
| 'jsonc-eslint-parser'
140155
| 'vue-eslint-parser'
156+
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
157+
| keyof CustomParsers
141158
>;
142159

160+
/**
161+
* This is a special exported interface for other packages to declare
162+
* additional parser options that should bail out for eslint parser options. For example
163+
* `@typescript-eslint/eslint-plugin` can declare it like so in its `d.ts`:
164+
*
165+
* ```ts
166+
* declare module 'eslint-define-config' {
167+
* export interface CustomParserOptions {
168+
* /**
169+
* * This option allows you to provide the root directory for relative tsconfig paths specified in the `project` option above.
170+
* *
171+
* * \@see [tsconfigRootDir](https://typescript-eslint.io/architecture/parser/#tsconfigrootdir)
172+
* *\/
173+
* tsconfigRootDir?: string;
174+
*
175+
* useJSXTextNode?: boolean;
176+
*
177+
* /**
178+
* * This option allows you to toggle the warning that the parser will give you if you use a version of TypeScript which is not explicitly supported.
179+
* *
180+
* * \@default true
181+
* *
182+
* * \@see [warnOnUnsupportedTypeScriptVersion](https://typescript-eslint.io/architecture/parser/#warnonunsupportedtypescriptversion)
183+
* *\/
184+
* warnOnUnsupportedTypeScriptVersion?: boolean;
185+
*
186+
* /**
187+
* * This option allow you to tell parser to act as if `emitDecoratorMetadata: true` is set in `tsconfig.json`, but without [type-aware linting](https://typescript-eslint.io/linting/typed-linting).
188+
* * In other words, you don't have to specify `parserOptions.project` in this case, making the linting process faster.
189+
* *
190+
* * \@default undefined
191+
* *
192+
* * \@see [emitDecoratorMetadata](https://typescript-eslint.io/architecture/parser/#emitdecoratormetadata)
193+
* *\/
194+
* emitDecoratorMetadata?: boolean;
195+
* }
196+
* }
197+
* ```
198+
*/
199+
export interface CustomParserOptions {}
200+
143201
/**
144202
* Parser options.
145203
*
146204
* @see [Specifying Parser Options](https://eslint.org/docs/user-guide/configuring/language-options#specifying-parser-options)
147205
*/
148-
export interface ParserOptions extends Partial<Record<string, unknown>> {
206+
export interface ParserOptions
207+
extends Partial<CustomParserOptions>,
208+
Partial<Record<string, unknown>> {
149209
/**
150210
* Accepts any valid ECMAScript version number or `'latest'`:
151211
*

src/rules/index.d.ts

Lines changed: 59 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,67 @@ import type { VueI18nRules } from './vue-i18n';
2323
import type { VuePugRules } from './vue-pug';
2424
import type { YmlRules } from './yml';
2525

26+
/**
27+
* This is a special exported interface for other packages to declare
28+
* additional types that should bail out for eslint rules. For example
29+
* `@typescript-eslint/eslint-plugin` can declare it like so in its `d.ts`:
30+
*
31+
* ```ts
32+
* declare module 'eslint-define-config' {
33+
* export interface CustomRuleOptions {
34+
* /**
35+
* * Require consistently using either `T[]` or `Array<T>` for arrays.
36+
* *
37+
* * \@see [array-type](https://typescript-eslint.io/rules/array-type)
38+
* *\/
39+
* '@typescript-eslint/array-type': [
40+
* {
41+
* default?: 'array' | 'generic' | 'array-simple';
42+
* readonly?: 'array' | 'generic' | 'array-simple';
43+
* },
44+
* ];
45+
*
46+
* // ... more Rules
47+
* }
48+
* }
49+
* ```
50+
*/
51+
export interface CustomRuleOptions {}
52+
53+
type CustomRules = {
54+
[TRuleName in keyof CustomRuleOptions]: RuleConfig<
55+
CustomRuleOptions[TRuleName]
56+
>;
57+
};
58+
2659
/**
2760
* Rules.
2861
*
2962
* @see [Rules](https://eslint.org/docs/user-guide/configuring/rules)
3063
*/
31-
export type Rules = Partial<
32-
DeprecationRules &
33-
EslintRules &
34-
EslintCommentsRules &
35-
GraphQLRules &
36-
ImportRules &
37-
JSDocRules &
38-
JsoncRules &
39-
JsxA11yRules &
40-
NodeRules &
41-
NRules &
42-
PromiseRules &
43-
ReactHooksRules &
44-
ReactRules &
45-
SonarJSRules &
46-
SpellcheckRules &
47-
TestingLibraryRules &
48-
TypeScriptRules &
49-
UnicornRules &
50-
VitestRules &
51-
VueRules &
52-
VueI18nRules &
53-
VuePugRules &
54-
YmlRules &
55-
Record<string, RuleConfig>
56-
>;
64+
export interface Rules
65+
extends CustomRules,
66+
DeprecationRules,
67+
EslintRules,
68+
EslintCommentsRules,
69+
GraphQLRules,
70+
ImportRules,
71+
JSDocRules,
72+
JsoncRules,
73+
JsxA11yRules,
74+
NodeRules,
75+
NRules,
76+
PromiseRules,
77+
ReactHooksRules,
78+
ReactRules,
79+
SonarJSRules,
80+
SpellcheckRules,
81+
TestingLibraryRules,
82+
TypeScriptRules,
83+
UnicornRules,
84+
VitestRules,
85+
VueRules,
86+
VueI18nRules,
87+
VuePugRules,
88+
YmlRules,
89+
Record<string, RuleConfig> {}

0 commit comments

Comments
 (0)