Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add biome prettier #105

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"cSpell.words": [
"arraify",
"atou",
"biomejs",
"browserfs",
"credentialless",
"debundle",
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export default defineConfig({
'process.env.NODE_DEBUG': 'undefined',
},
minify: true,
external: [
'@biomejs/js-api',
],
noExternal: [
'jscodeshift',
'ast-types',
Expand Down
2 changes: 2 additions & 0 deletions packages/unminify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
"zod": "^3.22.4"
},
"devDependencies": {
"@biomejs/js-api": "^0.5.0",
"@biomejs/wasm-nodejs": "^1.6.3",
"@types/jscodeshift": "^0.11.11",
"@types/prettier": "^2.7.3",
"@types/yargs": "^17.0.32",
Expand Down
45 changes: 45 additions & 0 deletions packages/unminify/src/transformations/biome.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Biome, Distribution } from '@biomejs/js-api'
import { createStringTransformationRule } from '@wakaru/shared/rule'

type FormatConfiguration = Parameters<Biome['applyConfiguration']>[0]

let biome: Biome | null = null

const formatConfig: FormatConfiguration = {
formatter: {
enabled: true,
indentStyle: 'space',
indentWidth: 2,
lineWidth: 80,
},
javascript: {
formatter: {
quoteStyle: 'double',
jsxQuoteStyle: 'double',
// trailing_comma: 'all',
trailingComma: 'es5',
semicolons: 'always',
arrowParentheses: 'always',
bracketSameLine: false,
bracketSpacing: true,
enabled: true,
},
},
}

/**
* @url https://github.com/biomejs/biome
*/
export default createStringTransformationRule({
name: 'prettier',
transform: async (code) => {
if (!biome) {
biome = await Biome.create({
distribution: Distribution.NODE,
})
biome.applyConfiguration(formatConfig)
}

return biome.formatContent(code, { filePath: 'example.js' }).content
},
})

Check warning on line 45 in packages/unminify/src/transformations/biome.ts

View check run for this annotation

Codecov / codecov/patch

packages/unminify/src/transformations/biome.ts#L1-L45

Added lines #L1 - L45 were not covered by tests
6 changes: 3 additions & 3 deletions packages/unminify/src/transformations/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import biome from './biome'

Check warning on line 1 in packages/unminify/src/transformations/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/unminify/src/transformations/index.ts#L1

Added line #L1 was not covered by tests
import lebab from './lebab'
import moduleMapping from './module-mapping'
import prettier from './prettier'
import smartInline from './smart-inline'
import smartRename from './smart-rename'
import unArgumentSpread from './un-argument-spread'
Expand Down Expand Up @@ -40,7 +40,7 @@

export const transformationRules: TransformationRule[] = [
// first stage - basically prettify the code
prettier.withId('prettier'),
biome,

Check warning on line 43 in packages/unminify/src/transformations/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/unminify/src/transformations/index.ts#L43

Added line #L43 was not covered by tests
moduleMapping,
unCurlyBraces, // add curly braces so that other transformations can works easier, but generally this is not required
unSequenceExpression, // curly braces can bring out return sequence expression, so it runs before this
Expand Down Expand Up @@ -88,5 +88,5 @@
unAsyncAwait,

// last stage - prettify the code again after we finish all the transformations
prettier.withId('prettier-1'),
biome,

Check warning on line 91 in packages/unminify/src/transformations/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/unminify/src/transformations/index.ts#L91

Added line #L91 was not covered by tests
]
27 changes: 27 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.