Skip to content

Commit 3c930e4

Browse files
authored
feat: add a defineConfig utility function and use it in examples (#117)
1 parent 0d2e74a commit 3c930e4

File tree

2 files changed

+46
-27
lines changed

2 files changed

+46
-27
lines changed

README.md

+43-27
Original file line numberDiff line numberDiff line change
@@ -27,40 +27,51 @@ Please also make sure that you have `typescript` and `eslint` installed.
2727

2828
Because of the complexity of this config, it is exported as a factory function that takes an options object and returns an ESLint configuration object.
2929

30+
This package exports 2 utility functions:
31+
32+
- `defineConfig`, as a re-export of the [`config` function from `typescript-eslint`](https://typescript-eslint.io/packages/typescript-eslint#config).
33+
- `createConfig`, used for creating an ESLint configuration array that extends from the [`typescript-eslint` shared configs](https://typescript-eslint.io/users/configs).
34+
3035
### Minimal Setup
3136

3237
```js
3338
// eslint.config.mjs
34-
import pluginVue from "eslint-plugin-vue";
35-
import vueTsEslintConfig from "@vue/eslint-config-typescript";
36-
37-
export default [
38-
...pluginVue.configs["flat/essential"],
39-
...vueTsEslintConfig(),
40-
]
39+
import pluginVue from 'eslint-plugin-vue'
40+
import {
41+
defineConfig,
42+
createConfig as vueTsEslintConfig,
43+
} from '@vue/eslint-config-typescript'
44+
45+
export default defineConfig(
46+
pluginVue.configs['flat/essential'],
47+
vueTsEslintConfig(),
48+
)
4149
```
4250

4351
The above configuration enables [the essential rules for Vue 3](https://eslint.vuejs.org/rules/#priority-a-essential-error-prevention) and [the recommended rules for TypeScript](https://typescript-eslint.io/rules/?=recommended).
4452

45-
All the `<script>` blocks in `.vue` files *MUST* be written in TypeScript (should be either `<script setup lang="ts">` or `<script lang="ts">`).
53+
All the `<script>` blocks in `.vue` files _MUST_ be written in TypeScript (should be either `<script setup lang="ts">` or `<script lang="ts">`).
4654

4755
### Advanced Setup
4856

4957
```js
5058
// eslint.config.mjs
51-
import pluginVue from "eslint-plugin-vue";
52-
import vueTsEslintConfig from "@vue/eslint-config-typescript";
59+
import pluginVue from 'eslint-plugin-vue'
60+
import {
61+
defineConfig,
62+
createConfig as vueTsEslintConfig,
63+
} from '@vue/eslint-config-typescript'
5364

54-
export default [
55-
...pluginVue.configs["flat/essential"],
65+
export default defineConfig(
66+
pluginVue.configs['flat/essential'],
5667

57-
...vueTsEslintConfig({
68+
vueTsEslintConfig({
5869
// Optional: extend additional configurations from `typescript-eslint`.
5970
// Supports all the configurations in
6071
// https://typescript-eslint.io/users/configs#recommended-configurations
6172
extends: [
6273
// By default, only the recommended rules are enabled.
63-
"recommended",
74+
'recommended',
6475
// You can also manually enable the stylistic rules.
6576
// "stylistic",
6677

@@ -98,8 +109,8 @@ export default [
98109
// Our config helper would resolve and parse all the `.vue` files under `rootDir`,
99110
// and only apply the loosened rules to the files that do need them.
100111
rootDir: import.meta.dirname,
101-
})
102-
]
112+
}),
113+
)
103114
```
104115

105116
### Linting with Type Information
@@ -111,32 +122,37 @@ It is not always easy to set up the type-checking environment for ESLint without
111122
So we don't recommend you to configure individual type-aware rules and the corresponding language options all by yourself.
112123
Instead, you can start by extending from the `recommendedTypeChecked` configuration and then turn on/off the rules you need.
113124

114-
As of now, all the rules you need to turn on must appear *before* calling `...vueTsEslintConfig({ extends: ['recommendedTypeChecked'] })`, and all the rules you need to turn off must appear *after* calling it.
125+
As of now, all the rules you need to turn on must appear _before_ calling `vueTsEslintConfig({ extends: ['recommendedTypeChecked'] })`, and all the rules you need to turn off must appear _after_ calling it.
115126

116127
```js
117128
// eslint.config.mjs
118-
import pluginVue from "eslint-plugin-vue";
119-
import vueTsEslintConfig from "@vue/eslint-config-typescript";
129+
import pluginVue from 'eslint-plugin-vue'
130+
import {
131+
defineConfig,
132+
createConfig as vueTsEslintConfig,
133+
} from '@vue/eslint-config-typescript'
120134

121-
export default [
122-
...pluginVue.configs["flat/essential"],
135+
export default defineConfig(
136+
pluginVue.configs['flat/essential'],
123137

124138
{
125139
files: ['**/*.ts', '**/*.tsx', '**/*.mts', '**/*.vue'],
126140
rules: {
127141
// Turn on other rules that you need.
128-
'@typescript-eslint/require-array-sort-compare': 'error'
129-
}
142+
'@typescript-eslint/require-array-sort-compare': 'error',
143+
},
130144
},
131-
...vueTsEslintConfig({ extends: ['recommendedTypeChecked'] }),
145+
146+
vueTsEslintConfig({ extends: ['recommendedTypeChecked'] }),
147+
132148
{
133149
files: ['**/*.ts', '**/*.tsx', '**/*.mts', '**/*.vue'],
134150
rules: {
135151
// Turn off the recommended rules that you don't need.
136152
'@typescript-eslint/no-redundant-type-constituents': 'off',
137-
}
138-
}
139-
]
153+
},
154+
},
155+
)
140156
```
141157

142158
## Further Reading

src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,6 @@ export default function createConfig({
224224
function escapePathForGlob(path: string) {
225225
return path.replace(/([*?{}[\]()])/g, '[$1]')
226226
}
227+
228+
export { createConfig }
229+
export { config as defineConfig } from 'typescript-eslint'

0 commit comments

Comments
 (0)