Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/oxfmt-config/src/configs/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,13 @@ export const base: OxfmtConfig = {
jsxSingleQuote: true,
trailingComma: 'none'
}

/**
* Default configuration that is always applied regardless of presets.
*
* Disable sorting of package.json due that is enabled by default in Oxfmt.
* @see https://oxc.rs/docs/guide/usage/formatter/sorting.html#sort-package-json-fields
*/
export const defaults: OxfmtConfig = {
sortPackageJson: false
}
1 change: 1 addition & 0 deletions packages/oxfmt-config/src/configs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { base } from './base.ts'
export { jsdoc } from './jsdoc.ts'
export { mdx } from './mdx.ts'
export { yaml } from './yaml.ts'
export { packageJson } from './packageJson.ts'
13 changes: 13 additions & 0 deletions packages/oxfmt-config/src/configs/packageJson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { OxfmtConfig } from 'oxfmt'

/**
* Package json order configuration for Oxfmt.
*
* This configuration is designed to enforce a specific order of properties in package.json files.
* Displaying properties in a predefined order can help maintain consistency and readability.
*/
export const packageJson: OxfmtConfig = {
sortPackageJson: {
sortScripts: true
}
}
8 changes: 5 additions & 3 deletions packages/oxfmt-config/src/presets.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import type { PresetName } from './types/index.ts'

import { base, jsdoc, mdx, yaml } from './configs/index.ts'
import { base, jsdoc, mdx, yaml, packageJson } from './configs/index.ts'

export const presetEntries = [
['base', base],
['jsdoc', jsdoc],
['mdx', mdx],
['yaml', yaml]
['yaml', yaml],
['packageJson', packageJson]
] as const

export const defaultOptions: Readonly<Record<PresetName, boolean>> = {
base: true,
jsdoc: true,
mdx: true,
yaml: true
yaml: true,
packageJson: false
}
5 changes: 4 additions & 1 deletion packages/oxfmt-config/src/utils/selectPresetConfigs.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import type { OxfmtConfig } from 'oxfmt'
import type { Options, PresetMap, PresetName } from '../types/index.ts'

import { defaults } from '../configs/base.ts'
import { presetEntries } from '../presets.ts'

export function selectPresetConfigs(options: Required<Options>): OxfmtConfig[] {
const presets = Object.fromEntries(presetEntries) as PresetMap

return (Object.keys(options) as PresetName[])
const configs = (Object.keys(options) as PresetName[])
.filter(name => options[name])
.map(name => presets[name])

return [defaults, ...configs]
}
13 changes: 5 additions & 8 deletions packages/oxfmt-config/tests/fixture.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ runWithConfig('without-presets', {
base: false,
yaml: false,
jsdoc: false,
mdx: false
mdx: false,
packageJson: false
})
runWithConfig(
'with-user-config',
{},
{
packageJson: true
},
{
semi: true,
trailingComma: 'all',
Expand Down Expand Up @@ -94,14 +97,8 @@ function runWithConfig(name: string, options: Options, ...userConfigs: OxfmtConf
await Promise.all(
files.map(async(file: string) => {
const content = await readFile(join(target, file), 'utf-8')
const source = await readFile(join(from, file), 'utf-8')
const outputPath = join(output, file)

if (content === source) {
await rm(outputPath, { force: true })
return
}

await expect.soft(content).toMatchFileSnapshot(outputPath)
})
)
Expand Down
33 changes: 33 additions & 0 deletions packages/oxfmt-config/tests/fixtures/input/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"simple-git-hooks": {
"pre-commit": "pnpm nano-staged"
},
"author": "Felix Icaza <fx.joliett17@gmail.com>",
"version": "0.0.0",
"license": "ISC",
"type": "module",
"description": "Package.json example for testing the packageJson option of oxfmt",
"scripts": {
"format": "oxfmt . --check",
"dev": "servor --reload src",
"lint": "oxlint .",
"format:fix": "oxfmt . --write",
"lint:fix": "oxlint . --fix",
"prepare": "simple-git-hooks"
},
"private": true,
"devDependencies": {
"@felixicaza/oxfmt-config": "0.1.1",
"@felixicaza/oxlint-config": "0.1.1",
"eslint": "10.4.0",
"nano-staged": "1.0.2",
"oxfmt": "0.50.0",
"oxlint": "1.65.0",
"servor": "4.0.2",
"simple-git-hooks": "2.13.1"
},
"name": "example",
"nano-staged": {
"*": "pnpm lint:fix"
}
}
33 changes: 33 additions & 0 deletions packages/oxfmt-config/tests/fixtures/output/default/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"simple-git-hooks": {
"pre-commit": "pnpm nano-staged"
},
"author": "Felix Icaza <fx.joliett17@gmail.com>",
"version": "0.0.0",
"license": "ISC",
"type": "module",
"description": "Package.json example for testing the packageJson option of oxfmt",
"scripts": {
"format": "oxfmt . --check",
"dev": "servor --reload src",
"lint": "oxlint .",
"format:fix": "oxfmt . --write",
"lint:fix": "oxlint . --fix",
"prepare": "simple-git-hooks"
},
"private": true,
"devDependencies": {
"@felixicaza/oxfmt-config": "0.1.1",
"@felixicaza/oxlint-config": "0.1.1",
"eslint": "10.4.0",
"nano-staged": "1.0.2",
"oxfmt": "0.50.0",
"oxlint": "1.65.0",
"servor": "4.0.2",
"simple-git-hooks": "2.13.1"
},
"name": "example",
"nano-staged": {
"*": "pnpm lint:fix"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "example",
"version": "0.0.0",
"private": true,
"description": "Package.json example for testing the packageJson option of oxfmt",
"license": "ISC",
"author": "Felix Icaza <fx.joliett17@gmail.com>",
"type": "module",
"scripts": {
"dev": "servor --reload src",
"format": "oxfmt . --check",
"format:fix": "oxfmt . --write",
"lint": "oxlint .",
"lint:fix": "oxlint . --fix",
"prepare": "simple-git-hooks"
},
"devDependencies": {
"@felixicaza/oxfmt-config": "0.1.1",
"@felixicaza/oxlint-config": "0.1.1",
"eslint": "10.4.0",
"nano-staged": "1.0.2",
"oxfmt": "0.50.0",
"oxlint": "1.65.0",
"servor": "4.0.2",
"simple-git-hooks": "2.13.1"
},
"simple-git-hooks": {
"pre-commit": "pnpm nano-staged"
},
"nano-staged": {
"*": "pnpm lint:fix"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"simple-git-hooks": {
"pre-commit": "pnpm nano-staged"
},
"author": "Felix Icaza <fx.joliett17@gmail.com>",
"version": "0.0.0",
"license": "ISC",
"type": "module",
"description": "Package.json example for testing the packageJson option of oxfmt",
"scripts": {
"format": "oxfmt . --check",
"dev": "servor --reload src",
"lint": "oxlint .",
"format:fix": "oxfmt . --write",
"lint:fix": "oxlint . --fix",
"prepare": "simple-git-hooks"
},
"private": true,
"devDependencies": {
"@felixicaza/oxfmt-config": "0.1.1",
"@felixicaza/oxlint-config": "0.1.1",
"eslint": "10.4.0",
"nano-staged": "1.0.2",
"oxfmt": "0.50.0",
"oxlint": "1.65.0",
"servor": "4.0.2",
"simple-git-hooks": "2.13.1"
},
"name": "example",
"nano-staged": {
"*": "pnpm lint:fix"
}
}
3 changes: 2 additions & 1 deletion packages/oxfmt-config/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const allDisabled: Required<Options> = {
base: false,
jsdoc: false,
mdx: false,
yaml: false
yaml: false,
packageJson: false
}

describe('index public API', () => {
Expand Down
27 changes: 16 additions & 11 deletions packages/oxfmt-config/tests/selectPresetConfigs.test.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,55 @@
import { describe, expect, it } from 'vitest'

import { base, jsdoc, mdx, yaml } from '../src/configs/index.ts'
import { base, jsdoc, mdx, yaml, packageJson } from '../src/configs/index.ts'
import { defaults } from '../src/configs/base.ts'
import { selectPresetConfigs } from '../src/utils/selectPresetConfigs.ts'

describe('utils/selectPresetConfigs', () => {
it('returns empty list when all presets are disabled', () => {
it('returns only defaults when all presets are disabled', () => {
const result = selectPresetConfigs({
base: false,
jsdoc: false,
mdx: false,
yaml: false
yaml: false,
packageJson: false
})

expect(result).toEqual([])
expect(result).toEqual([defaults])
})

it('selects only base preset when base is enabled', () => {
const result = selectPresetConfigs({
base: true,
jsdoc: false,
mdx: false,
yaml: false
yaml: false,
packageJson: false
})

expect(result).toEqual([base])
expect(result).toEqual([defaults, base])
})

it('selects only mdx preset when mdx is enabled', () => {
it('selects only mdx and yaml presets when they are enabled', () => {
const result = selectPresetConfigs({
base: false,
jsdoc: false,
mdx: true,
yaml: true
yaml: true,
packageJson: false
})

expect(result).toEqual([mdx, yaml])
expect(result).toEqual([defaults, mdx, yaml])
})

it('selects all presets in declaration order when all are enabled', () => {
const result = selectPresetConfigs({
base: true,
jsdoc: true,
mdx: true,
yaml: true
yaml: true,
packageJson: true
})

expect(result).toEqual([base, jsdoc, mdx, yaml])
expect(result).toEqual([defaults, base, jsdoc, mdx, yaml, packageJson])
})
})
Loading