Skip to content

Commit

Permalink
feat: detect tsconfig if ts enables.
Browse files Browse the repository at this point in the history
  • Loading branch information
lvjiaxuan committed Jan 14, 2024
1 parent 3f6d2d6 commit faac190
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { join } from 'node:path'
import process from 'node:process'
import antfu, { GLOB_SRC } from '@antfu/eslint-config'
import antfu, { typescript } from '@antfu/eslint-config'
import type { FlatConfigItem, OptionsTypeScriptWithTypes } from '@antfu/eslint-config'
import pluginLv from '@lvjiaxuan/eslint-plugin'
import type { FlatConfigItem } from '@antfu/eslint-config'
import { pathExists } from 'fs-extra'

const pluginItem: FlatConfigItem = {
files: [GLOB_SRC],
files: ['*'],
name: '@lvjiaxuan:plugin',
plugins: {
'@lvjiaxuan': pluginLv,
Expand All @@ -17,16 +17,43 @@ const pluginItem: FlatConfigItem = {
}

async function detectTsconfigPath() {
const defaultFile = 'tsconfig.json'
const defaultFile = 'tsconfig.json' as const
if (await pathExists(join(process.cwd(), defaultFile)))
return defaultFile
}

const lv: typeof antfu = (...args) => {
return antfu(
const lv: typeof antfu = async (...args) => {
const merged = await antfu(
...args,
pluginItem,
)
) as FlatConfigItem[]

if (merged.find(item => item.name === 'antfu:typescript:setup')) {
// Means ts is setup
let tsOptions = args[0]?.typescript

// Overwrite with tsconfigPath
if (typeof tsOptions === 'object') {
(tsOptions as OptionsTypeScriptWithTypes).tsconfigPath ??= await detectTsconfigPath()
}
else {
// @ts-expect-errord typescript = true means {}
;(tsOptions as OptionsTypeScriptWithTypes) = { tsconfigPath: await detectTsconfigPath() }
}

// New ts options with `tsconfigPath`.
const tsItems = await typescript(tsOptions as OptionsTypeScriptWithTypes)

const tsRulesItemName = 'antfu:typescript:rules'
const tsRulesItemIdx = merged.findIndex(item => item.name === tsRulesItemName)
merged.splice(tsRulesItemIdx, 1, tsItems.find(item => item.name === tsRulesItemName)!)

const tsTypeAwareItemName = 'antfu:typescript:rules-type-aware'
const tsTypeAwareItemIdx = merged.findIndex(item => item.name === tsTypeAwareItemName)
merged.splice(tsTypeAwareItemIdx, 1, tsItems.find(item => item.name === tsTypeAwareItemName)!)
}

return merged
}

export default lv

0 comments on commit faac190

Please sign in to comment.