Skip to content

Commit

Permalink
feat: piped antfu
Browse files Browse the repository at this point in the history
* chore: pipeline append `lvPlugin`

* chore: let go oxlintRules type hints

* chore: refactoring

* chore: almost

* feat: works.

* chore: rename
  • Loading branch information
lvjiaxuan committed Apr 9, 2024
1 parent 7877f0e commit 66e0dcc
Show file tree
Hide file tree
Showing 12 changed files with 295 additions and 538 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "@lvjiaxuan/eslint-config-monorepo",
"type": "module",
"version": "1.9.1",
"private": true,
"scripts": {
"lint": "oxlint . && eslint .",
"lint:fix": "oxlint . --fix && eslint . --fix"
},
"devDependencies": {
"@antfu/eslint-config": "^2.9.0",
"@antfu/eslint-config": "^2.12.2",
"@lvjiaxuan/eslint-config": "workspace:*",
"@lvjiaxuan/eslint-plugin-oxlint": "workspace:^",
"@types/eslint": "^8.56.7",
Expand All @@ -26,7 +27,7 @@
},
"pnpm": {
"patchedDependencies": {
"@antfu/eslint-config@2.9.0": "patches/@antfu__eslint-config@2.9.0.patch"
"@antfu/eslint-config@2.12.2": "patches/@antfu__eslint-config@2.12.2.patch"
}
},
"simple-git-hooks": {
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
],
"scripts": {
"dev": "pnpm run build --watch",
"view": "pnpm run dev & pnpm dlx eslint-flat-config-viewer",
"view": "pnpm run dev & pnpm dlx @eslint/config-inspector",
"build": "tsup src/index.ts --format=esm,cjs --dts",
"test": "vitest --globals",
"prepare": "pnpm run build",
Expand All @@ -46,7 +46,7 @@
}
},
"dependencies": {
"@antfu/eslint-config": "^2.8.3",
"@antfu/eslint-config": "^2.12.2",
"@lvjiaxuan/eslint-plugin": "workspace:*",
"@lvjiaxuan/eslint-plugin-oxlint": "workspace:*",
"fs-extra": "^11.2.0"
Expand Down
131 changes: 69 additions & 62 deletions packages/eslint-config/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,88 +1,95 @@
import antfu, { ensurePackages, typescript } from '@antfu/eslint-config'
import type { FlatConfigItem, OptionsTypeScriptParserOptions, OptionsTypeScriptWithTypes } from '@antfu/eslint-config'
import type { OptionsTypeScriptParserOptions, OptionsTypeScriptWithTypes } from '@antfu/eslint-config'
import { lvPlugin } from '@lvjiaxuan/eslint-plugin'
import type { OptionsOXLint } from '@lvjiaxuan/eslint-plugin-oxlint'
import type { OXLintOptions } from '@lvjiaxuan/eslint-plugin-oxlint'
import { detectTsconfigPaths } from './tsconfigs'

type Antfu = typeof antfu
type _Params<Params extends Parameters<Antfu> = Parameters<Antfu>> = [ options?: Params[0] & { oxlint: OptionsOXLint }, ...userConfigs: Params[1][] ]
type AntfuParams<Params extends Parameters<Antfu> = Parameters<Antfu>> = [ options?: Params[0] & { oxlint: OXLintOptions }, ...userConfigs: Params[1][] ]
type AntfuReturnType = ReturnType<Antfu>

const lv: (...args: _Params) => ReturnType<Antfu> = async (...args) => {
const [options] = args
const lv: (...args: AntfuParams) => AntfuReturnType = (...args) => {
let pipeline = antfu(...args)

const pluginsInstalled = [lvPlugin()]
pipeline = pipeline.append(
lvPlugin(),
)

if (options?.oxlint) {
await ensurePackages(['@lvjiaxuan/eslint-plugin-oxlint'])
const { oxlint } = await import('@lvjiaxuan/eslint-plugin-oxlint')
pluginsInstalled.push(...(await oxlint(options.oxlint)))
const [antfuOptions] = args
if (antfuOptions?.oxlint) {
pipeline = pipeline.append((async () => {
await ensurePackages(['@lvjiaxuan/eslint-plugin-oxlint'])
const { oxlint } = await import('@lvjiaxuan/eslint-plugin-oxlint')
return oxlint(antfuOptions.oxlint)
})())
}

const merged = await antfu(
...args,
...pluginsInstalled,
) as FlatConfigItem[]
void pipeline.onResolved(async (configs) => {
// The name comes from https://github.com/antfu/eslint-config/blob/main/src/configs/typescript.ts .
if (configs.findIndex(item => item.name === 'antfu/typescript/setup') > -1) {
let tsOptions = antfuOptions!.typescript as OptionsTypeScriptWithTypes & OptionsTypeScriptParserOptions

if (merged.find(item => item.name === 'antfu:typescript:setup')) {
// Means ts is setup.
let tsOptions = args[0]?.typescript as OptionsTypeScriptWithTypes & OptionsTypeScriptParserOptions
let isUseDetected = false
if (typeof tsOptions === 'object') {
if (tsOptions.notDetectTsconfig === true) {
// Do nothing.
}
else if (!Object.hasOwn(tsOptions, 'tsconfigPath')) {
// Add detected `tsconfigPath` if no-set.
const paths = await detectTsconfigPaths()
if (paths.length) {
tsOptions.tsconfigPath = paths
isUseDetected = true
}
}

let isUseDetected = false
if (typeof tsOptions === 'object') {
if ('notDetectTsconfig' in tsOptions && tsOptions.notDetectTsconfig === true) {
// Do nothing.
// Use settings.

tsOptions.parserOptions ??= {}
tsOptions.parserOptions = {
warnOnUnsupportedTypeScriptVersion: true,
EXPERIMENTAL_useProjectService: true,
...tsOptions.parserOptions,
}
}
else if (!Object.hasOwn(tsOptions, 'tsconfigPath')) {
// Overwrite with detected `tsconfigPath` if no-set.
else {
const paths = await detectTsconfigPaths()
if (paths.length) {
tsOptions.tsconfigPath = paths
// typescript = true means {}
tsOptions = {
tsconfigPath: paths,
parserOptions: {
warnOnUnsupportedTypeScriptVersion: true,
EXPERIMENTAL_useProjectService: true,
},
}
isUseDetected = true
}
}

// Use settings.

tsOptions.parserOptions ??= {}
tsOptions.parserOptions = {
warnOnUnsupportedTypeScriptVersion: true,
EXPERIMENTAL_useProjectService: true,
...tsOptions.parserOptions,
}
}
else {
const paths = await detectTsconfigPaths()
if (paths.length) {
// typescript = true means {}
tsOptions = {
tsconfigPath: paths,
parserOptions: {
warnOnUnsupportedTypeScriptVersion: true,
EXPERIMENTAL_useProjectService: true,
},
}
isUseDetected = true
}
}

if (isUseDetected) {
// New ts flat config with detected `tsconfigPath`.
const tsItemsWithTsConfig = await typescript(tsOptions as OptionsTypeScriptWithTypes)
if (isUseDetected) {
// New ts flat config items with detected `tsconfigPath`.
const flatConfigItemsWithTsConfig = await typescript(tsOptions as OptionsTypeScriptWithTypes)

const parserItemName = 'antfu:typescript:parser'
const originParserItemIdx = merged.findIndex(item => item.name === parserItemName)
merged.splice(originParserItemIdx, 1, tsItemsWithTsConfig.find(item => item.name === parserItemName)!)
const parserItemIdx = configs.findIndex(item => item.name === 'antfu/typescript/parser')
configs.splice(
parserItemIdx,
1,
flatConfigItemsWithTsConfig.find(item => item.name === 'antfu/typescript/type-aware-parser')!,
flatConfigItemsWithTsConfig.find(item => item.name === 'antfu/typescript/parser')!,
)

const typeAwareParserItemName = 'antfu:typescript:type-aware-parser'
merged.splice(originParserItemIdx + 1, 0, tsItemsWithTsConfig.find(item => item.name === typeAwareParserItemName)!)

const typeAwareItemName = 'antfu:typescript:rules-type-aware'
const tsTypeAwareItemIdx = merged.findIndex(item => item.name === typeAwareItemName)
merged.splice(tsTypeAwareItemIdx, 1, tsItemsWithTsConfig.find(item => item.name === typeAwareItemName)!)
const rulesItemIdx = configs.findIndex(item => item.name = 'antfu/typescript/rules')
configs.splice(
rulesItemIdx + 1,
0,
flatConfigItemsWithTsConfig.find(item => item.name === 'antfu/typescript/rules-type-aware')!,
)
}
}
}
})

return merged
return pipeline
}

export default lv
2 changes: 1 addition & 1 deletion packages/eslint-config/tests/tsconfigs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { detectTsconfigPaths } from './../src/tsconfigs'

it('detectTsconfigPaths basic', async () => {
const paths = await detectTsconfigPaths({
cwd: join(__dirname, 'fixtures'),
cwd: join(import.meta.dirname, 'fixtures'),
})

expect(paths).toMatchInlineSnapshot(`
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin-oxlint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"registry": "https://registry.npmjs.org/"
},
"devDependencies": {
"@antfu/eslint-config": "^2.12.2",
"@antfu/eslint-define-config": "1.23.0-2",
"@type-challenges/utils": "^0.1.1"
}
Expand Down
25 changes: 6 additions & 19 deletions packages/eslint-plugin-oxlint/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { FlatESLintConfigItem, RuleLevel } from '@antfu/eslint-define-config'
import type { RuleLevel } from '@antfu/eslint-define-config'
import type { TypedFlatConfigItem } from '@antfu/eslint-config'
import type { MergeInsertions, UnionToIntersection } from '@type-challenges/utils'
import categoryRules from './../category-rules.json'

Expand All @@ -14,27 +15,13 @@ const rules = categoryRules as CategoryRules

export type OXLintRules = MergeInsertions<UnionToIntersection<CategoryRules[Categories]>>

export type OptionsOXLint = {
export type OXLintOptions = {
deny?: Categories | 'all'
allow?: (keyof OXLintRules)[]
// plugins: TODO
// TODO plugins
} | boolean

// https://github.com/antfu/eslint-config/blob/3707078921b8d246b1d2980c5c4cfe7f39c67699/src/types.ts#L59
type FlatConfigItem = Omit<FlatESLintConfigItem<OXLintRules, false>, 'plugins'> & {
/**
* Custom name of each config item
*/
name?: string
/**
* An object containing a name-value mapping of plugin names to plugin objects. When `files` is specified, these plugins are only available to the matching files.
*
* @see [Using plugins in your configuration](https://eslint.org/docs/latest/user-guide/configuring/configuration-files-new#using-plugins-in-your-configuration)
*/
plugins?: Record<string, any>
}

export async function oxlint(options: OptionsOXLint = true): Promise<FlatConfigItem[]> {
export function oxlint(options: OXLintOptions = true): TypedFlatConfigItem[] {
if (options === true)
options = { deny: 'correctness' }
else if (options === false)
Expand All @@ -51,7 +38,7 @@ export async function oxlint(options: OptionsOXLint = true): Promise<FlatConfigI
}

return [{
name: 'lvjixuan:eslint-oxlint',
name: 'lvjixuan/plugin/oxlint',
rules: denyRules,
}]
}
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"eslint": ">=8.21.0"
},
"dependencies": {
"@antfu/eslint-config": "^2.8.3",
"@antfu/eslint-config": "^2.12.2",
"@typescript-eslint/utils": "^7.5.0"
},
"devDependencies": {
Expand Down
15 changes: 9 additions & 6 deletions packages/eslint-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { TSESLint } from '@typescript-eslint/utils'
import { type FlatConfigItem, GLOB_SRC } from '@antfu/eslint-config'
import type { TypedFlatConfigItem } from '@antfu/eslint-config'
import { GLOB_SRC } from '@antfu/eslint-config'
import preferGenericRestExtends from './prefer-generic-rest-extends'

const rulesSetup = {
Expand All @@ -8,15 +9,17 @@ const rulesSetup = {

const rulesSettings = {
'@lvjiaxuan/prefer-generic-rest-extends': 'warn',
} as const // Support types later, reference to my oxlint plugin.
} as const // TODO type hints

const pluginName = '@lvjiaxuan'

// For flat config.
export function lvPlugin(): FlatConfigItem {
export function lvPlugin(): TypedFlatConfigItem {
return {
files: [GLOB_SRC],
name: 'lvjiaxuan:rules',
name: 'lvjiaxuan/plugin',
plugins: {
'@lvjiaxuan': { rules: rulesSetup },
[pluginName]: { rules: rulesSetup },
},
rules: rulesSettings,
}
Expand All @@ -27,7 +30,7 @@ export default {
rules: rulesSetup,
configs: {
recommended: {
plugins: ['@lvjiaxuan'],
plugins: [pluginName],
rules: rulesSettings,
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
diff --git a/dist/index.d.cts b/dist/index.d.cts
index 054ef1e86a49d76248087894f57b6d47e05af143..9bb95f6b80aca4d4e66516fbbeb1704da6a13d07 100644
index 64907250c0d27e43bc980da3e50f9b564919df42..3f54df246b333472c46b60e68d28c06a1a8ec496 100644
--- a/dist/index.d.cts
+++ b/dist/index.d.cts
@@ -211,7 +211,13 @@ interface OptionsTypeScriptParserOptions {
@@ -15180,7 +15180,13 @@ interface OptionsTypeScriptParserOptions {
/**
* Additional parser options for TypeScript.
*/
Expand All @@ -17,7 +17,7 @@ index 054ef1e86a49d76248087894f57b6d47e05af143..9bb95f6b80aca4d4e66516fbbeb1704d
/**
* Glob patterns for files that should be type aware.
* @default ['**\/*.{ts,tsx}']
@@ -224,6 +230,10 @@ interface OptionsTypeScriptWithTypes {
@@ -15193,6 +15199,10 @@ interface OptionsTypeScriptWithTypes {
* @see https://typescript-eslint.io/linting/typed-linting/
*/
tsconfigPath?: string | string[];
Expand All @@ -29,10 +29,10 @@ index 054ef1e86a49d76248087894f57b6d47e05af143..9bb95f6b80aca4d4e66516fbbeb1704d
interface OptionsHasTypeScript {
typescript?: boolean;
diff --git a/dist/index.d.ts b/dist/index.d.ts
index 054ef1e86a49d76248087894f57b6d47e05af143..9bb95f6b80aca4d4e66516fbbeb1704da6a13d07 100644
index 64907250c0d27e43bc980da3e50f9b564919df42..3f54df246b333472c46b60e68d28c06a1a8ec496 100644
--- a/dist/index.d.ts
+++ b/dist/index.d.ts
@@ -211,7 +211,13 @@ interface OptionsTypeScriptParserOptions {
@@ -15180,7 +15180,13 @@ interface OptionsTypeScriptParserOptions {
/**
* Additional parser options for TypeScript.
*/
Expand All @@ -47,7 +47,7 @@ index 054ef1e86a49d76248087894f57b6d47e05af143..9bb95f6b80aca4d4e66516fbbeb1704d
/**
* Glob patterns for files that should be type aware.
* @default ['**\/*.{ts,tsx}']
@@ -224,6 +230,10 @@ interface OptionsTypeScriptWithTypes {
@@ -15193,6 +15199,10 @@ interface OptionsTypeScriptWithTypes {
* @see https://typescript-eslint.io/linting/typed-linting/
*/
tsconfigPath?: string | string[];
Expand Down

0 comments on commit 66e0dcc

Please sign in to comment.