-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Biome config is now available Closes: #2
- Loading branch information
Showing
9 changed files
with
249 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { existsSync } from "node:fs"; | ||
import { join } from "node:path"; | ||
import { parseBiomeConfig } from "./parser/biome"; | ||
import { parsePrettierConfig } from "./parser/prettier"; | ||
import { ConfigType, type StyleConfig } from "./types/config"; | ||
|
||
|
||
export async function parseConfig(): Promise<StyleConfig> { | ||
const configType = getConfigType(); | ||
return getConfig(configType) | ||
} | ||
|
||
function getConfigType(): ConfigType { | ||
const prettierrc = existsSync(join(process.cwd(), ConfigType.PRETTIER)); | ||
if (prettierrc) { | ||
return ConfigType.PRETTIER; | ||
} | ||
|
||
const prettierrcjson = existsSync(join(process.cwd(), ConfigType.PRETTIER_JSON)); | ||
if (prettierrcjson) { | ||
return ConfigType.PRETTIER_JSON; | ||
} | ||
|
||
const biomeJson = existsSync(join(process.cwd(), ConfigType.BIOME_JSON)); | ||
if (biomeJson) { | ||
return ConfigType.BIOME_JSON; | ||
} | ||
|
||
const biomeJsonc = existsSync(join(process.cwd(), ConfigType.BIOME_JSONC)); | ||
if (biomeJsonc) { | ||
return ConfigType.BIOME_JSONC; | ||
} | ||
|
||
return ConfigType.UNKNOWN; | ||
} | ||
|
||
async function getConfig(type: ConfigType): Promise<StyleConfig> { | ||
switch (type) { | ||
case ConfigType.PRETTIER: | ||
case ConfigType.PRETTIER_JSON: | ||
return parsePrettierConfig(type); | ||
case ConfigType.BIOME_JSON: | ||
case ConfigType.BIOME_JSONC: | ||
return parseBiomeConfig(type); | ||
default: | ||
return { | ||
isSingleQuote: true, | ||
tabIndent: false, | ||
tabSize: 2, | ||
useSemi: true, | ||
}; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { readFile } from "node:fs/promises"; | ||
import { join } from "node:path"; | ||
import type { Biome } from "../types/biome"; | ||
import { ConfigType, type StyleConfig } from "../types/config"; | ||
|
||
const FILE_PATTERN = /\*(?:\.d)?\.ts$/; | ||
|
||
export async function parseBiomeConfig(type: ConfigType): Promise<StyleConfig> { | ||
const configs = await retriveExtendsConfig(type); | ||
|
||
return { | ||
isSingleQuote: isSingleQuote(configs), | ||
useSemi: useSemi(configs), | ||
tabIndent: useTabs(configs), | ||
tabSize: indentSize(configs), | ||
}; | ||
} | ||
|
||
async function retriveExtendsConfig(...paths: string[]): Promise<Biome[]> { | ||
const files = await Promise.all( | ||
paths.map(path => ( | ||
readFile(join(process.cwd(), path), { encoding: 'utf-8' }) | ||
)) | ||
); | ||
const configs: Biome[] = files.map(file => JSON.parse(file)); | ||
for (const config of configs) { | ||
if ('extends' in config && config.extends.length > 0) { | ||
config.extends = await retriveExtendsConfig(...config.extends as string[]) | ||
} | ||
} | ||
|
||
return configs; | ||
} | ||
|
||
function isSingleQuote(configs: Biome[]): boolean { | ||
return configs.some(config => ( | ||
config.override | ||
.filter(override => override.include.some(file => FILE_PATTERN.test(file))) | ||
.some(override => override.javascript?.formatter.quoteStyle === 'single') | ||
|
||
|| config.javascript?.formatter.quoteStyle === 'single' | ||
|| isSingleQuote(config.extends as Biome[]) | ||
)); | ||
} | ||
|
||
function useSemi(configs: Biome[]): boolean { | ||
return configs.some(config => ( | ||
config.override | ||
.filter(override => override.include.some(file => FILE_PATTERN.test(file))) | ||
.some(override => override.javascript?.formatter.semicolons === 'always') | ||
|
||
|| config.javascript?.formatter.semicolons === 'always' | ||
|| useSemi(config.extends as Biome[]) | ||
)); | ||
} | ||
|
||
function useTabs(configs: Biome[]): boolean { | ||
return configs.some(config => ( | ||
config.override | ||
.filter(override => override.include.some(file => FILE_PATTERN.test(file))) | ||
.some(override => ( | ||
override.javascript?.formatter.indentStyle === 'tab' | ||
|| override.formatter?.indentStyle === 'tab' | ||
)) | ||
|
||
|| config.javascript?.formatter.indentStyle === 'tab' | ||
|| config.formatter?.indentStyle === 'tab' | ||
|| useTabs(config.extends as Biome[]) | ||
)); | ||
} | ||
|
||
function indentSize(configs: Biome[]): number { | ||
return configs.map(config => { | ||
const overrideSize = config.override | ||
.filter(override => override.include.some(file => FILE_PATTERN.test(file))) | ||
.filter(override => ('javascript' in override && 'formatter' in override.javascript!) || 'formatter' in override) | ||
.map(override => override.javascript?.formatter.indentWidth || override.formatter?.indentWidth).at(-1); // We assum the last one is the right to use | ||
|
||
const jsFormatterSize = config.javascript?.formatter.indentWidth; | ||
const formatterSize = config.formatter?.indentWidth; | ||
|
||
return overrideSize || jsFormatterSize || formatterSize || indentSize(config.extends as Biome[]) | ||
}).at(-1) || 2 // Same we assum the last found is the one to use | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { readFile } from "node:fs/promises"; | ||
import { join } from "node:path"; | ||
import { ConfigType, type StyleConfig } from "../types/config"; | ||
import type { Prettier } from "../types/prettier"; | ||
|
||
export async function parsePrettierConfig(type: ConfigType): Promise<StyleConfig> { | ||
const file = await readFile(join(process.cwd(), type), { encoding: 'utf-8' }) | ||
const config: Prettier = JSON.parse(file); | ||
|
||
const isSingleQuote: boolean = | ||
config.overrides?.find(override => override.files === '*.d.ts' || override.files === '*.ts')?.options.singleQuote ?? | ||
config.singleQuote ?? | ||
false; | ||
|
||
const useSemi: boolean = | ||
config.overrides?.find(override => override.files === '*.d.ts' || override.files === '*.ts')?.options.semi ?? | ||
config.semi ?? | ||
false; | ||
|
||
const useTabs: boolean = | ||
config.overrides?.find(override => override.files === '*.d.ts' || override.files === '*.ts')?.options.useTabs ?? | ||
config.useTabs ?? | ||
false; | ||
|
||
const indentSize: number = | ||
config.overrides?.find(override => override.files === '*.d.ts' || override.files === '*.ts')?.options.tabWidth ?? | ||
config.tabWidth ?? | ||
2; | ||
|
||
return { | ||
isSingleQuote, | ||
useSemi, | ||
tabIndent: useTabs, | ||
tabSize: indentSize, | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export interface Biome { | ||
extends: string[] | Biome[]; | ||
formatter?: BiomeFormatter; | ||
javascript?: BiomeJavascript; | ||
override: BiomeOverride[]; | ||
} | ||
|
||
|
||
interface BiomeFormatter { | ||
indentWidth?: number; | ||
indentStyle?: 'tab' | 'space'; | ||
} | ||
|
||
|
||
interface BiomeJavascript { | ||
formatter: BiomeJsFormatter; | ||
} | ||
type BiomeJsFormatter = BiomeFormatter & { | ||
semicolons?: 'always' | 'asNeeded'; | ||
quoteStyle: 'single' | 'double' | ||
} | ||
|
||
type BiomeOverride = Omit<Biome, 'override' | 'extends'> & { | ||
include: string[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export enum ConfigType { | ||
PRETTIER = '.prettierrc', | ||
PRETTIER_JSON = '.prettierrc.json', | ||
BIOME_JSON = 'biome.json', | ||
BIOME_JSONC = 'biome.jsonc', | ||
UNKNOWN = 'unknown', | ||
} | ||
|
||
export interface StyleConfig { | ||
isSingleQuote: boolean; | ||
useSemi: boolean; | ||
tabIndent: boolean; | ||
|
||
/** | ||
* used when `tabIndent` is `false` | ||
*/ | ||
tabSize: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export interface Prettier { | ||
singleQuote?: boolean; | ||
semi?: boolean; | ||
tabWidth?: number; | ||
useTabs?: boolean; | ||
|
||
overrides?: PrettierOverride[]; | ||
} | ||
|
||
interface PrettierOverride { | ||
files: string; | ||
options: Omit<Prettier, 'overrides'>; | ||
} |