Skip to content

Commit

Permalink
fix: tweak biome configs (#367)
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework authored Jul 14, 2024
1 parent 42228c5 commit 82972e0
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-ants-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vite-plugin-checker': patch
---

fix: tweak biome configs
2 changes: 2 additions & 0 deletions .github/renovate.json5
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
$schema: 'https://docs.renovatebot.com/renovate-schema.json',
extends: ['config:recommended', 'schedule:weekly'],
ignorePaths: ['**/tests/**', '**/node_modules/**'],
labels: ['dependencies'],
rangeStrategy: 'bump',
ignoreDeps: [
// manually update some packages
'pnpm',
Expand Down
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"vetur.experimental.templateInterpolationService": true,
"typescript.tsdk": "node_modules/typescript/lib",
"vitest.disableWorkspaceWarning": true
"vitest.disableWorkspaceWarning": true,
"cSpell.words": ["Stylelint"]
}
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://biomejs.dev/schemas/1.8.0/schema.json",
"$schema": "https://biomejs.dev/schemas/1.8.3/schema.json",
"organizeImports": {
"enabled": true,
"include": ["./**/*.js", "./**/*.jsx", "./**/*.ts", "./**/*.tsx", "./**/*.mjs", "./**/*.cjs"]
Expand Down
14 changes: 9 additions & 5 deletions docs/checkers/biome.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@

Advanced object configuration table of `options.biome`

| field | Type | Default value | Description |
| :----------- | --------------------------------------- | ----------------------------------- | ------------------------------------------------------------------------------------------------------------- |
| command | `'check' \| 'lint' \| 'format' \| 'ci'` | `'lint'` in dev, `'check'` in build | The command to execute biome with |
| flags | `string` | `''` | CLI flags to pass to the command |
| dev.logLevel | `('error' \| 'warning')[]` | `['error', 'warning']` | **(Only in dev mode)** Which level of Biome diagnostics should be emitted to terminal and overlay in dev mode |
| field | Type | Default value | Description |
| :------------ | --------------------------------------- | ------------------------------------ | -------------------------------------------------------------------------------------------------------------- |
| command | `'check' \| 'lint' \| 'format' \| 'ci'` | `'lint'` in dev, `'check'` in build. | The command to execute biome with. |
| flags | `string` | `''` | CLI flags to pass to the command. |
| dev.logLevel | `('error' \| 'warning')[]` | `['error', 'warning']` | **(Only in dev mode)** Which level of Biome diagnostics should be emitted to terminal and overlay in dev mode. |
| dev.command | `'check' \| 'lint' \| 'format' \| 'ci'` | `''` | Command to run in dev mode, it will override `command` config in dev mode. |
| dev.flags | `string` | `''` | Flags to run in dev mode, it will override `flags` config in dev mode. |
| build.command | `'check' \| 'lint' \| 'format' \| 'ci'` | `''` | Command to run in build mode, it will override `command` config in build mode. |
| build.flags | `string` | `''` | Flags to run in build mode, it will override `flags` config in build mode. |
8 changes: 7 additions & 1 deletion packages/vite-plugin-checker/src/checkers/biome/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ export const severityMap = {

export function getBiomeCommand(command: string, flags: string, files: string) {
const defaultFlags = '--reporter json'
return ['biome', command || 'lint', flags, defaultFlags, files].filter(Boolean).join(' ')
if (flags.includes('--flags')) {
throw Error(
`vite-plugin-checker will force append "--reporter json" to the flags in dev mode, please don't use "--flags" in "config.biome.flags".
If you need to customize "--flags" in build mode, please use "config.biome.build.flags" instead.`
)
}
return ['biome', command, flags, defaultFlags, files].filter(Boolean).join(' ')
}

export function runBiome(command: string, cwd: string) {
Expand Down
20 changes: 11 additions & 9 deletions packages/vite-plugin-checker/src/checkers/biome/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ const manager = new FileDiagnosticManager()
let createServeAndBuild: any

const createDiagnostic: CreateDiagnostic<'biome'> = (pluginConfig) => {
const biomeConfig = pluginConfig.biome
let overlay = true
let terminal = true

let command = ''
let command = 'lint'
let flags = ''

if (typeof pluginConfig.biome === 'object') {
command = pluginConfig.biome.command || ''
flags = pluginConfig.biome.flags || ''
if (typeof biomeConfig === 'object') {
command = biomeConfig?.dev?.command || biomeConfig?.command || 'lint'
flags = biomeConfig?.dev?.flags || biomeConfig?.flags || ''
}

return {
Expand All @@ -38,11 +39,11 @@ const createDiagnostic: CreateDiagnostic<'biome'> = (pluginConfig) => {
terminal = enableTerminal
},
async configureServer({ root }) {
if (!pluginConfig.biome) return
if (!biomeConfig) return

const logLevel = (() => {
if (typeof pluginConfig.biome !== 'object') return undefined
const userLogLevel = pluginConfig.biome.dev?.logLevel
if (typeof biomeConfig !== 'object') return undefined
const userLogLevel = biomeConfig.dev?.logLevel
if (!userLogLevel) return undefined

return userLogLevel.map((l) => severityMap[l])
Expand Down Expand Up @@ -74,6 +75,7 @@ const createDiagnostic: CreateDiagnostic<'biome'> = (pluginConfig) => {

const handleFileChange = async (filePath: string, type: 'change' | 'unlink') => {
const absPath = path.resolve(root, filePath)

if (type === 'unlink') {
manager.updateByFileId(absPath, [])
} else if (type === 'change') {
Expand Down Expand Up @@ -125,9 +127,9 @@ export class BiomeChecker extends Checker<'biome'> {
buildBin: (pluginConfig) => {
if (typeof pluginConfig.biome === 'object') {
const { command, flags } = pluginConfig.biome
return ['biome', [command || 'lint', flags || ''] as const]
return ['biome', [command || 'check', flags || ''] as const]
}
return ['biome', ['lint']]
return ['biome', ['check']]
},
},
createDiagnostic,
Expand Down
23 changes: 21 additions & 2 deletions packages/vite-plugin-checker/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,35 @@ export type StylelintConfig =
}>
}

type BiomeCommand = 'lint' | 'check' | 'format' | 'ci'
/** Biome checker configuration */
export type BiomeConfig =
| boolean
| {
command?: 'lint' | 'check' | 'format' | 'ci'
/**
* Command will be used in dev and build mode, will be override
* if `dev.command` or `build.command` is set their mode.
*/
command?: BiomeCommand
/**
* Flags of the command, will be override if `dev.flags`
* or `build.command` is set their mode.
* */
flags?: string
dev?: Partial<{
/** which level of the diagnostic will be emitted from plugin */
/** Command will be used in dev mode */
command: BiomeCommand
/** Flags of the command */
flags?: string
/** Which level of the diagnostic will be emitted from plugin */
logLevel: ('error' | 'warning' | 'info')[]
}>
build?: Partial<{
/** Command will be used in build mode */
command: BiomeCommand
/** Flags of the command */
flags?: string
}>
}

export enum DiagnosticLevel {
Expand Down
7 changes: 4 additions & 3 deletions playground/biome-default/__tests__/test.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import stringify from 'fast-json-stable-stringify'
import fs from 'node:fs'
import { describe, expect, it } from 'vitest'
import {
diagnostics,
editFile,
expectStderrContains,
isBuild,
isServe,
log,
stripedLog,
resetDiagnostics,
resetReceivedLog,
sleepForEdit,
sleepForServerReady,
stripedLog,
} from '../../testUtils'
import path from 'node:path'

describe('biome', () => {
describe.runIf(isServe)('serve', () => {
Expand All @@ -34,7 +35,7 @@ describe('biome', () => {
describe.runIf(isBuild)('build', () => {
it('should fail', async () => {
const expectedMsg = ['Use let or const instead of var', 'Found 2 errors']
expectStderrContains(log, expectedMsg)
expectStderrContains(stripedLog, expectedMsg)
})
})
})
9 changes: 7 additions & 2 deletions playground/biome-default/biome.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
{
"$schema": "https://biomejs.dev/schemas/1.8.0/schema.json",
"$schema": "https://biomejs.dev/schemas/1.8.3/schema.json",
"files": {
"include": ["./src/*.js"]
},
"linter": {
"include": ["./src/**/*.js"],
"rules": {
"recommended": true
}
},
"formatter": {
"enabled": false
}
}

0 comments on commit 82972e0

Please sign in to comment.