Skip to content

Commit

Permalink
feat: add support for BiomeJS config (#480)
Browse files Browse the repository at this point in the history
Co-authored-by: cipherlogs <[email protected]>
  • Loading branch information
cipherlogs and cipherlogs committed Jul 11, 2024
1 parent 10079b2 commit aa2aa4e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,13 @@ AutoImport({
filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
globalsPropValue: true, // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
},

// Generate corresponding .biomelintrc-auto-import.json file.
// biomejs extends Docs - https://biomejs.dev/guides/how-biome-works/#the-extends-option
biomelintrc: {
enabled: false, // Default `false`
filepath: './.biomelintrc-auto-import.json', // Default `./.biomelintrc-auto-import.json`
},
})
```

Expand Down
16 changes: 16 additions & 0 deletions src/core/biomelintrc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { Import } from 'unimport'

export function generateBiomeLintConfigs(
imports: Import[],
) {
const names
= imports
.map(i => i.as ?? i.name)
.filter(Boolean)
.sort()

const config = { javascript: { globals: names } }
const jsonBody = JSON.stringify(config, null, 2)

return jsonBody
}
25 changes: 24 additions & 1 deletion src/core/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import fg from 'fast-glob'
import { vueTemplateAddon } from 'unimport/addons'
import MagicString from 'magic-string'
import { presets } from '../presets'
import type { ESLintGlobalsPropValue, ESLintrc, ImportExtended, Options } from '../types'
import type { BiomeLintrc, ESLintGlobalsPropValue, ESLintrc, ImportExtended, Options } from '../types'
import { generateESLintConfigs } from './eslintrc'
import { generateBiomeLintConfigs } from './biomelintrc'
import { resolversAddon } from './resolvers'

function resolveGlobsExclude(root: string, glob: string) {
Expand Down Expand Up @@ -48,6 +49,10 @@ export function createContext(options: Options = {}, root = process.cwd()) {
eslintrc.filepath = eslintrc.filepath || './.eslintrc-auto-import.json'
eslintrc.globalsPropValue = eslintrc.globalsPropValue === undefined ? true : eslintrc.globalsPropValue

const biomelintrc: BiomeLintrc = options.biomelintrc || {}
biomelintrc.enabled = biomelintrc.enabled !== undefined
biomelintrc.filepath = biomelintrc.filepath || './.biomelintrc-auto-import.json'

const resolvers = options.resolvers ? [options.resolvers].flat(2) : []

// When "options.injectAtEnd" is undefined or true, it's true.
Expand Down Expand Up @@ -177,6 +182,10 @@ ${dts}`.trim()}\n`
return generateESLintConfigs(await unimport.getImports(), eslintrc, await parseESLint())
}

async function generateBiomeLint() {
return generateBiomeLintConfigs(await unimport.getImports())
}

const writeConfigFilesThrottled = throttle(500, writeConfigFiles, { noLeading: false })

async function writeFile(filePath: string, content = '') {
Expand All @@ -186,6 +195,8 @@ ${dts}`.trim()}\n`

let lastDTS: string | undefined
let lastESLint: string | undefined
let lastBiomeLint: string | undefined

async function writeConfigFiles() {
const promises: any[] = []
if (dts) {
Expand Down Expand Up @@ -215,6 +226,18 @@ ${dts}`.trim()}\n`
}),
)
}

if (biomelintrc.enabled) {
promises.push(
generateBiomeLint().then((content) => {
if (content !== lastBiomeLint) {
lastBiomeLint = content
return writeFile(biomelintrc.filepath!, content)
}
}),
)
}

return Promise.all(promises)
}

Expand Down
13 changes: 13 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ export interface ESLintrc {
globalsPropValue?: ESLintGlobalsPropValue
}

export interface BiomeLintrc {
/**
* @default false
*/
enabled?: boolean
/**
* Filepath to save the generated eslint config
*
* @default './.eslintrc-auto-import.json'
*/
filepath?: string
}

export interface Options {
/**
* Preset names or custom imports map
Expand Down

0 comments on commit aa2aa4e

Please sign in to comment.