Skip to content

Commit 306cf60

Browse files
committed
feat(oxfmt): override all js formatting in prettier POC [ci skip]
`pnpm --filter oxfmt-app build-dev && node ./apps/oxfmt/dist/cli.js -c oxfmtrc.jsonc test.vue`
1 parent 0a3bb7b commit 306cf60

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

apps/oxfmt/src-js/prettier-worker.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,63 @@ export async function formatFile({ parserName, fileName, code }: FormatFileArgs)
5757
...prettierConfig,
5858
parser: parserName,
5959
filepath: fileName,
60+
// TODO remove this, enabling for test
61+
embeddedLanguageFormatting: "auto",
62+
// TODO: cache this
63+
plugins: await getPlugins(prettierCache),
6064
});
6165
}
66+
67+
async function getPlugins(prettier) {
68+
const { languages } = await prettier.getSupportInfo();
69+
70+
const programParser = {
71+
astFormat: "oxfmt",
72+
parse: (text) => ({
73+
type: "Program",
74+
body: [],
75+
sourceType: "module",
76+
__oxfmtFormatted: "formatted by oxfmt",
77+
}),
78+
};
79+
80+
const expressionParser = {
81+
astFormat: "oxfmt",
82+
parse: (text) => ({
83+
type: "Program",
84+
body: [],
85+
sourceType: "module",
86+
__oxfmtFormatted: "formatted by oxfmt expr",
87+
}),
88+
};
89+
90+
return [
91+
{
92+
languages: languages
93+
.filter((l) => ["JavaScript", "JSX", "TypeScript", "TSX"].includes(l.name))
94+
.map((l) => ({ ...l, parsers: l.name.includes("Type") ? ["oxc-ts"] : ["oxc"] })),
95+
96+
parsers: {
97+
oxc: programParser,
98+
"oxc-ts": programParser,
99+
babel: programParser,
100+
"babel-flow": programParser,
101+
"babel-ts": programParser,
102+
typescript: programParser,
103+
flow: programParser,
104+
__vue_expression: expressionParser,
105+
__vue_ts_expression: expressionParser,
106+
__vue_event_binding: expressionParser,
107+
__vue_ts_event_binding: expressionParser,
108+
__js_expression: expressionParser,
109+
__ts_expression: expressionParser,
110+
},
111+
112+
printers: {
113+
oxfmt: {
114+
print: (path) => path.node.__oxfmtFormatted || "/* ERROR */",
115+
},
116+
},
117+
},
118+
];
119+
}

test.vue

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<template>
2+
<div class="wrapper">
3+
<header class="header">
4+
<h1>{{ formatted by oxfmt expr }}</h1>
5+
<p v-if="formatted by oxfmt expr">{{ formatted by oxfmt expr }}</p>
6+
7+
<!-- Slots -->
8+
<slot name="actions" />
9+
10+
<!-- v-for with key + destructuring -->
11+
<ul>
12+
<li v-for="formatted by oxfmt in formatted by oxfmt expr" :key="formatted by oxfmt expr">
13+
{{ formatted by oxfmt expr }}. {{ formatted by oxfmt expr }}
14+
</li>
15+
</ul>
16+
</header>
17+
</div>
18+
</template>
19+
20+
<script lang="ts" setup>
21+
formatted by oxfmt
22+
</script>

0 commit comments

Comments
 (0)