Skip to content

Commit aa2aa4e

Browse files
authoredJul 11, 2024··
feat: add support for BiomeJS config (#480)
Co-authored-by: cipherlogs <73669345+0x78logs@users.noreply.github.com>
1 parent 10079b2 commit aa2aa4e

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed
 

Diff for: ‎README.md

+7
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,13 @@ AutoImport({
320320
filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
321321
globalsPropValue: true, // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
322322
},
323+
324+
// Generate corresponding .biomelintrc-auto-import.json file.
325+
// biomejs extends Docs - https://biomejs.dev/guides/how-biome-works/#the-extends-option
326+
biomelintrc: {
327+
enabled: false, // Default `false`
328+
filepath: './.biomelintrc-auto-import.json', // Default `./.biomelintrc-auto-import.json`
329+
},
323330
})
324331
```
325332

Diff for: ‎src/core/biomelintrc.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { Import } from 'unimport'
2+
3+
export function generateBiomeLintConfigs(
4+
imports: Import[],
5+
) {
6+
const names
7+
= imports
8+
.map(i => i.as ?? i.name)
9+
.filter(Boolean)
10+
.sort()
11+
12+
const config = { javascript: { globals: names } }
13+
const jsonBody = JSON.stringify(config, null, 2)
14+
15+
return jsonBody
16+
}

Diff for: ‎src/core/ctx.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import fg from 'fast-glob'
1212
import { vueTemplateAddon } from 'unimport/addons'
1313
import MagicString from 'magic-string'
1414
import { presets } from '../presets'
15-
import type { ESLintGlobalsPropValue, ESLintrc, ImportExtended, Options } from '../types'
15+
import type { BiomeLintrc, ESLintGlobalsPropValue, ESLintrc, ImportExtended, Options } from '../types'
1616
import { generateESLintConfigs } from './eslintrc'
17+
import { generateBiomeLintConfigs } from './biomelintrc'
1718
import { resolversAddon } from './resolvers'
1819

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

52+
const biomelintrc: BiomeLintrc = options.biomelintrc || {}
53+
biomelintrc.enabled = biomelintrc.enabled !== undefined
54+
biomelintrc.filepath = biomelintrc.filepath || './.biomelintrc-auto-import.json'
55+
5156
const resolvers = options.resolvers ? [options.resolvers].flat(2) : []
5257

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

185+
async function generateBiomeLint() {
186+
return generateBiomeLintConfigs(await unimport.getImports())
187+
}
188+
180189
const writeConfigFilesThrottled = throttle(500, writeConfigFiles, { noLeading: false })
181190

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

187196
let lastDTS: string | undefined
188197
let lastESLint: string | undefined
198+
let lastBiomeLint: string | undefined
199+
189200
async function writeConfigFiles() {
190201
const promises: any[] = []
191202
if (dts) {
@@ -215,6 +226,18 @@ ${dts}`.trim()}\n`
215226
}),
216227
)
217228
}
229+
230+
if (biomelintrc.enabled) {
231+
promises.push(
232+
generateBiomeLint().then((content) => {
233+
if (content !== lastBiomeLint) {
234+
lastBiomeLint = content
235+
return writeFile(biomelintrc.filepath!, content)
236+
}
237+
}),
238+
)
239+
}
240+
218241
return Promise.all(promises)
219242
}
220243

Diff for: ‎src/types.ts

+13
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,19 @@ export interface ESLintrc {
7070
globalsPropValue?: ESLintGlobalsPropValue
7171
}
7272

73+
export interface BiomeLintrc {
74+
/**
75+
* @default false
76+
*/
77+
enabled?: boolean
78+
/**
79+
* Filepath to save the generated eslint config
80+
*
81+
* @default './.eslintrc-auto-import.json'
82+
*/
83+
filepath?: string
84+
}
85+
7386
export interface Options {
7487
/**
7588
* Preset names or custom imports map

0 commit comments

Comments
 (0)
Please sign in to comment.