Skip to content

Commit

Permalink
refactor: use scriptLangs instead of supportedScriptLangs for mor…
Browse files Browse the repository at this point in the history
…e succint config
  • Loading branch information
haoqunjiang committed Dec 26, 2024
1 parent 02189a8 commit 5d61ea0
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 50 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ This package exports:

- `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).
- `configs`, contains all the [shared configruations from `typescript-eslint`](https://typescript-eslint.io/users/configs) (in camelCase, e.g. `configs.recommendedTypeChecked`).
- a Vue-specific config factory: `configureVueProject({ supportedScriptLangs, rootDir })`. More info below.
- a Vue-specific config factory: `configureVueProject({ scriptLangs, rootDir })`. More info below.

### Minimal Setup

Expand Down Expand Up @@ -66,27 +66,27 @@ import {

configureVueProject({
// Optional: specify the script langs in `.vue` files
// Defaults to `{ ts: true, js: false, tsx: false, jsx: false }`
supportedScriptLangs: {
ts: true,
// Defaults to `['ts']`.
scriptLangs: [
'ts',

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

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

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

// <https://github.com/vuejs/eslint-plugin-vue/issues/1910#issuecomment-1819993961>
// Optional: the root directory to resolve the `.vue` files, defaults to `process.cwd()`.
Expand Down
7 changes: 1 addition & 6 deletions examples/allow-js/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@ import {
configs,
} from '@vue/eslint-config-typescript'

configureVueProject({
supportedScriptLangs: {
js: true,
ts: true,
},
})
configureVueProject({ scriptLangs: ['js', 'ts'] })

export default defineConfig(
{
Expand Down
9 changes: 1 addition & 8 deletions examples/with-jsx-in-vue/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@ import {
configs,
} from '@vue/eslint-config-typescript'

configureVueProject({
supportedScriptLangs: {
js: true,
jsx: true,
ts: true,
tsx: true,
},
})
configureVueProject({ scriptLangs: ['js', 'jsx', 'ts', 'tsx'] })

export default defineConfig(
{
Expand Down
7 changes: 1 addition & 6 deletions examples/with-tsx-in-vue/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@ import {
configs,
} from '@vue/eslint-config-typescript'

configureVueProject({
supportedScriptLangs: {
ts: true,
tsx: true,
}
})
configureVueProject({ scriptLangs: ['ts', 'tsx'] })

export default defineConfig(
{
Expand Down
18 changes: 15 additions & 3 deletions src/createConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@
// Will be removed in 15.0.0

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

import {
configureVueProject,
defineConfig,
type ProjectOptions,
} from './utilities'
import { configs, type ExtendableConfigName } from './configs'
import type { ScriptLang } from './internals'

type ConfigOptions = ProjectOptions & {
extends?: Array<ExtendableConfigName>
supportedScriptLangs?: Record<ScriptLang, boolean>
}

export default function createConfig({
Expand All @@ -34,7 +41,12 @@ export default function createConfig({
}
}

configureVueProject({ supportedScriptLangs, rootDir })
configureVueProject({
scriptLangs: Object.keys(supportedScriptLangs).filter(
lang => supportedScriptLangs[lang as ScriptLang],
) as ScriptLang[],
rootDir,
})
return defineConfig(
...configNamesToExtend.map(name => configs[name as ExtendableConfigName]),
)
Expand Down
11 changes: 5 additions & 6 deletions src/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ export const additionalRulesRequiringParserServices = [
]

export function createBasicSetupConfigs(
supportedScriptLangs: Record<ScriptLang, boolean>,
scriptLangs: ScriptLang[],
): ConfigArray {
const mayHaveJsxInSfc = supportedScriptLangs.jsx || supportedScriptLangs.tsx
const mayHaveJsxInSfc =
scriptLangs.includes('jsx') || scriptLangs.includes('tsx')

return [
// Must set eslint-plugin-vue's base config again no matter whether the user
Expand Down Expand Up @@ -77,10 +78,8 @@ export function createBasicSetupConfigs(
'error',
{
script: {
lang: Object.keys(supportedScriptLangs!).filter(
lang => supportedScriptLangs![lang as ScriptLang],
),
allowNoLang: supportedScriptLangs!.js,
lang: scriptLangs,
allowNoLang: scriptLangs.includes('js'),
},
},
],
Expand Down
15 changes: 5 additions & 10 deletions src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,20 @@ import process from 'node:process'
import type { ScriptLang } from './internals'

export type ProjectOptions = {
supportedScriptLangs?: Record<ScriptLang, boolean>
scriptLangs?: ScriptLang[]
rootDir?: string
}

let projectOptions: ProjectOptions = {
supportedScriptLangs: {
ts: true,
tsx: false,
js: false,
jsx: false,
},
scriptLangs: ['ts'],
rootDir: process.cwd(),
}

// This function, if called, is guaranteed to be executed before `defineConfig`,
// so mutating the `projectOptions` object is safe and will be reflected in the final ESLint config.
export function configureVueProject(userOptions: ProjectOptions) {
if (userOptions.supportedScriptLangs) {
projectOptions.supportedScriptLangs = userOptions.supportedScriptLangs
if (userOptions.scriptLangs) {
projectOptions.scriptLangs = userOptions.scriptLangs
}
if (userOptions.rootDir) {
projectOptions.rootDir = userOptions.rootDir
Expand Down Expand Up @@ -114,7 +109,7 @@ function insertAndReorderConfigs(

return [
...configsWithoutTypeAwareRules.slice(0, lastExtendedConfigIndex + 1),
...createBasicSetupConfigs(projectOptions.supportedScriptLangs!),
...createBasicSetupConfigs(projectOptions.scriptLangs!),

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

0 comments on commit 5d61ea0

Please sign in to comment.