Skip to content

Commit 5d61ea0

Browse files
committed
refactor: use scriptLangs instead of supportedScriptLangs for more succint config
1 parent 02189a8 commit 5d61ea0

File tree

7 files changed

+39
-50
lines changed

7 files changed

+39
-50
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ This package exports:
3131

3232
- `defineConfig`, a utility function whose type signature is the same as the [`config` function from `typescript-eslint`](https://typescript-eslint.io/packages/typescript-eslint#config).
3333
- `configs`, contains all the [shared configruations from `typescript-eslint`](https://typescript-eslint.io/users/configs) (in camelCase, e.g. `configs.recommendedTypeChecked`).
34-
- a Vue-specific config factory: `configureVueProject({ supportedScriptLangs, rootDir })`. More info below.
34+
- a Vue-specific config factory: `configureVueProject({ scriptLangs, rootDir })`. More info below.
3535

3636
### Minimal Setup
3737

@@ -66,27 +66,27 @@ import {
6666

6767
configureVueProject({
6868
// Optional: specify the script langs in `.vue` files
69-
// Defaults to `{ ts: true, js: false, tsx: false, jsx: false }`
70-
supportedScriptLangs: {
71-
ts: true,
69+
// Defaults to `['ts']`.
70+
scriptLangs: [
71+
'ts',
7272

7373
// [!DISCOURAGED]
74-
// Set to `true` to allow plain `<script>` or `<script setup>` blocks.
74+
// Include 'js' to allow plain `<script>` or `<script setup>` blocks.
7575
// This might result-in false positive or negatives in some rules for `.vue` files.
7676
// Note you also need to configure `allowJs: true` and `checkJs: true`
7777
// in corresponding `tsconfig.json` files.
78-
js: false,
78+
'js',
7979

8080
// [!STRONGLY DISCOURAGED]
81-
// Set to `true` to allow `<script lang="tsx">` blocks.
81+
// Include 'tsx' to allow `<script lang="tsx">` blocks.
8282
// This would be in conflict with all type-aware rules.
83-
tsx: false,
83+
'tsx',
8484

8585
// [!STRONGLY DISCOURAGED]
86-
// Set to `true` to allow `<script lang="jsx">` blocks.
86+
// Include 'jsx' to allow `<script lang="jsx">` blocks.
8787
// This would be in conflict with all type-aware rules and may result in false positives.
88-
jsx: false,
89-
},
88+
'jsx',
89+
],
9090

9191
// <https://github.com/vuejs/eslint-plugin-vue/issues/1910#issuecomment-1819993961>
9292
// Optional: the root directory to resolve the `.vue` files, defaults to `process.cwd()`.

examples/allow-js/eslint.config.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@ import {
55
configs,
66
} from '@vue/eslint-config-typescript'
77

8-
configureVueProject({
9-
supportedScriptLangs: {
10-
js: true,
11-
ts: true,
12-
},
13-
})
8+
configureVueProject({ scriptLangs: ['js', 'ts'] })
149

1510
export default defineConfig(
1611
{

examples/with-jsx-in-vue/eslint.config.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,7 @@ import {
55
configs,
66
} from '@vue/eslint-config-typescript'
77

8-
configureVueProject({
9-
supportedScriptLangs: {
10-
js: true,
11-
jsx: true,
12-
ts: true,
13-
tsx: true,
14-
},
15-
})
8+
configureVueProject({ scriptLangs: ['js', 'jsx', 'ts', 'tsx'] })
169

1710
export default defineConfig(
1811
{

examples/with-tsx-in-vue/eslint.config.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@ import {
55
configs,
66
} from '@vue/eslint-config-typescript'
77

8-
configureVueProject({
9-
supportedScriptLangs: {
10-
ts: true,
11-
tsx: true,
12-
}
13-
})
8+
configureVueProject({ scriptLangs: ['ts', 'tsx'] })
149

1510
export default defineConfig(
1611
{

src/createConfig.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@
22
// Will be removed in 15.0.0
33

44
import * as tseslint from 'typescript-eslint'
5-
import { configureVueProject, defineConfig, type ProjectOptions } from './utilities'
6-
import { configs, type ExtendableConfigName } from './configs'
75
import type { FlatConfig } from '@typescript-eslint/utils/ts-eslint'
86

7+
import {
8+
configureVueProject,
9+
defineConfig,
10+
type ProjectOptions,
11+
} from './utilities'
12+
import { configs, type ExtendableConfigName } from './configs'
13+
import type { ScriptLang } from './internals'
14+
915
type ConfigOptions = ProjectOptions & {
1016
extends?: Array<ExtendableConfigName>
17+
supportedScriptLangs?: Record<ScriptLang, boolean>
1118
}
1219

1320
export default function createConfig({
@@ -34,7 +41,12 @@ export default function createConfig({
3441
}
3542
}
3643

37-
configureVueProject({ supportedScriptLangs, rootDir })
44+
configureVueProject({
45+
scriptLangs: Object.keys(supportedScriptLangs).filter(
46+
lang => supportedScriptLangs[lang as ScriptLang],
47+
) as ScriptLang[],
48+
rootDir,
49+
})
3850
return defineConfig(
3951
...configNamesToExtend.map(name => configs[name as ExtendableConfigName]),
4052
)

src/internals.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ export const additionalRulesRequiringParserServices = [
3131
]
3232

3333
export function createBasicSetupConfigs(
34-
supportedScriptLangs: Record<ScriptLang, boolean>,
34+
scriptLangs: ScriptLang[],
3535
): ConfigArray {
36-
const mayHaveJsxInSfc = supportedScriptLangs.jsx || supportedScriptLangs.tsx
36+
const mayHaveJsxInSfc =
37+
scriptLangs.includes('jsx') || scriptLangs.includes('tsx')
3738

3839
return [
3940
// Must set eslint-plugin-vue's base config again no matter whether the user
@@ -77,10 +78,8 @@ export function createBasicSetupConfigs(
7778
'error',
7879
{
7980
script: {
80-
lang: Object.keys(supportedScriptLangs!).filter(
81-
lang => supportedScriptLangs![lang as ScriptLang],
82-
),
83-
allowNoLang: supportedScriptLangs!.js,
81+
lang: scriptLangs,
82+
allowNoLang: scriptLangs.includes('js'),
8483
},
8584
},
8685
],

src/utilities.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,20 @@ import process from 'node:process'
2222
import type { ScriptLang } from './internals'
2323

2424
export type ProjectOptions = {
25-
supportedScriptLangs?: Record<ScriptLang, boolean>
25+
scriptLangs?: ScriptLang[]
2626
rootDir?: string
2727
}
2828

2929
let projectOptions: ProjectOptions = {
30-
supportedScriptLangs: {
31-
ts: true,
32-
tsx: false,
33-
js: false,
34-
jsx: false,
35-
},
30+
scriptLangs: ['ts'],
3631
rootDir: process.cwd(),
3732
}
3833

3934
// This function, if called, is guaranteed to be executed before `defineConfig`,
4035
// so mutating the `projectOptions` object is safe and will be reflected in the final ESLint config.
4136
export function configureVueProject(userOptions: ProjectOptions) {
42-
if (userOptions.supportedScriptLangs) {
43-
projectOptions.supportedScriptLangs = userOptions.supportedScriptLangs
37+
if (userOptions.scriptLangs) {
38+
projectOptions.scriptLangs = userOptions.scriptLangs
4439
}
4540
if (userOptions.rootDir) {
4641
projectOptions.rootDir = userOptions.rootDir
@@ -114,7 +109,7 @@ function insertAndReorderConfigs(
114109

115110
return [
116111
...configsWithoutTypeAwareRules.slice(0, lastExtendedConfigIndex + 1),
117-
...createBasicSetupConfigs(projectOptions.supportedScriptLangs!),
112+
...createBasicSetupConfigs(projectOptions.scriptLangs!),
118113

119114
// user-turned-off type-aware rules must come after the last extended config
120115
// in case some rules re-enabled by the extended config

0 commit comments

Comments
 (0)