-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement the new configuration format and pass all existing tests
- Loading branch information
1 parent
815cc88
commit 3755a4c
Showing
11 changed files
with
568 additions
and
246 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import tseslint from 'typescript-eslint' | ||
|
||
export type ExtendableConfigName = keyof typeof tseslint.configs | ||
type ConfigArray = (typeof tseslint.configs)[ExtendableConfigName] | ||
|
||
// TODO: use enum | ||
export type VueTsPreset = | ||
`PLACEHOLDER_THAT_MUST_BE_WRAPPED_INSIDE_defineConfig_${ExtendableConfigName}` | ||
export type VueTsPresets = Record<ExtendableConfigName, VueTsPreset> | ||
|
||
export const configs: VueTsPresets = Object.keys(tseslint.configs).reduce( | ||
(configs, name) => { | ||
configs[name as ExtendableConfigName] = | ||
`PLACEHOLDER_THAT_MUST_BE_WRAPPED_INSIDE_defineConfig_${name as ExtendableConfigName}` | ||
return configs | ||
}, | ||
{} as VueTsPresets, | ||
) | ||
|
||
function toArray<T>(value: T | T[]): T[] { | ||
return Array.isArray(value) ? value : [value] | ||
} | ||
|
||
export function getConfigForPlaceholder( | ||
placeholder: VueTsPreset, | ||
): ConfigArray { | ||
return toArray( | ||
tseslint.configs[ | ||
placeholder.replace( | ||
/^PLACEHOLDER_THAT_MUST_BE_WRAPPED_INSIDE_defineConfig_/, | ||
'', | ||
) as ExtendableConfigName | ||
], | ||
) | ||
.flat() | ||
.map(config => | ||
config.files && config.files.includes('**/*.ts') | ||
? { | ||
...config, | ||
files: [...config.files, '**/*.vue'], | ||
} | ||
: config, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// This is a compatibility layer for the `createConfig` function in <= 14.2.0 | ||
// 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' | ||
|
||
type ConfigOptions = ProjectOptions & { | ||
extends?: Array<ExtendableConfigName> | ||
} | ||
|
||
export default function createConfig({ | ||
extends: configNamesToExtend = ['recommended'], | ||
supportedScriptLangs = { ts: true, tsx: false, js: false, jsx: false }, | ||
rootDir = process.cwd(), | ||
}: ConfigOptions = {}): FlatConfig.ConfigArray { | ||
// More meaningful error message for the user, in case they didn't know the correct config name. | ||
for (const name of configNamesToExtend) { | ||
if (!tseslint.configs[name]) { | ||
const nameInCamelCase = name.replace(/-([a-z])/g, (_, letter) => | ||
letter.toUpperCase(), | ||
) | ||
|
||
// @ts-expect-error | ||
if (tseslint.configs[nameInCamelCase]) { | ||
throw new Error( | ||
`The config name "${name}" is not supported in "extends". ` + | ||
`Please use "${nameInCamelCase}" instead.`, | ||
) | ||
} | ||
|
||
throw new Error(`Unknown config name in "extends": ${name}.`) | ||
} | ||
} | ||
|
||
configureVueProject({ supportedScriptLangs, rootDir }) | ||
return defineConfig( | ||
...configNamesToExtend.map(name => configs[name as ExtendableConfigName]), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import fs from 'node:fs' | ||
import fg from 'fast-glob' | ||
import path from 'node:path' | ||
|
||
type VueFilesByGroup = { | ||
typeCheckable: string[] | ||
nonTypeCheckable: string[] | ||
} | ||
|
||
export default function groupVueFiles(rootDir: string): VueFilesByGroup { | ||
const { vueFilesWithScriptTs, otherVueFiles } = fg | ||
.sync(['**/*.vue'], { | ||
cwd: rootDir, | ||
ignore: ['**/node_modules/**'], | ||
}) | ||
.reduce( | ||
(acc, file) => { | ||
const absolutePath = path.resolve(rootDir, file) | ||
const contents = fs.readFileSync(absolutePath, 'utf8') | ||
// contents matches the <script lang="ts"> (there can be anything but `>` between `script` and `lang`) | ||
if (/<script[^>]*\blang\s*=\s*"ts"[^>]*>/i.test(contents)) { | ||
acc.vueFilesWithScriptTs.push(file) | ||
} else { | ||
acc.otherVueFiles.push(file) | ||
} | ||
return acc | ||
}, | ||
{ vueFilesWithScriptTs: [] as string[], otherVueFiles: [] as string[] }, | ||
) | ||
|
||
return { | ||
// Only `.vue` files with `<script lang="ts">` or `<script setup lang="ts">` can be type-checked. | ||
typeCheckable: vueFilesWithScriptTs, | ||
nonTypeCheckable: otherVueFiles, | ||
} | ||
} |
Oops, something went wrong.