Skip to content

Commit 97eefe0

Browse files
committed
Move transform call into printer preprocess step
This is functionally equivalent to running this during the `parse` step since it runs once before printing the whole AST.
1 parent c2bb1a3 commit 97eefe0

File tree

3 files changed

+73
-11
lines changed

3 files changed

+73
-11
lines changed

src/create-plugin.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ export function createPlugin(transforms: TransformOptions<any>[]) {
1616
let printers: Record<string, Init<Printer<any>>> = Object.create(null)
1717

1818
for (let opts of transforms) {
19+
// Even though we run the `transform` step during `printer.preprocess`
20+
// we still have to override the parser as Prettier won't use the printer
21+
// from a plugin that does not *also* define a parser:
22+
//
23+
// https://github.com/prettier/prettier/blob/f2cfef1ec2c20eb04d327cd70d98bd07f7d72f00/src/main/normalize-format-options.js#L66-L68
1924
for (let [name, meta] of Object.entries(opts.parsers)) {
2025
parsers[name] = async () => {
2126
let plugin = await loadPlugins(meta.load ?? opts.load ?? [])
@@ -34,7 +39,7 @@ export function createPlugin(transforms: TransformOptions<any>[]) {
3439

3540
for (let [name, meta] of Object.entries(opts.printers ?? {})) {
3641
printers[name] = async () => {
37-
let plugin = await loadPlugins(opts.load ?? [])
42+
let plugin = await loadPlugins(meta.load ?? opts.load ?? [])
3843
let original = plugin.printers?.[name]
3944
if (!original) return
4045

@@ -79,15 +84,7 @@ function createParser({ name, original, opts }: { name: string; original: Parser
7984
// for Prettier v2 but still work with v3.
8085
//
8186
// Currently only the Twig plugin requires this.
82-
let ast = await original.parse(code, options, options)
83-
84-
await transformAst({
85-
ast,
86-
opts,
87-
options,
88-
})
89-
90-
return ast
87+
return original.parse(code, options, options)
9188
}
9289

9390
return parser
@@ -97,6 +94,25 @@ function createPrinter({ original, opts }: { original: Printer<any>; opts: Trans
9794
let printer: Printer<any> = { ...original }
9895

9996
let reprint = opts.reprint
97+
let transform = opts.transform
98+
99+
if (transform) {
100+
let preprocess = original.preprocess ?? ((ast: any) => ast)
101+
102+
printer.preprocess = new Proxy(preprocess, {
103+
async apply(target, thisArg, args) {
104+
let [ast, options] = args as Parameters<NonNullable<typeof original.preprocess>>
105+
106+
await transformAst({
107+
ast,
108+
opts,
109+
options,
110+
})
111+
112+
return Reflect.apply(target, thisArg, args)
113+
},
114+
})
115+
}
100116

101117
// Hook into the preprocessing phase to load the config
102118
if (reprint) {

src/index.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,10 @@ let html = defineTransform<HtmlNode>({
10231023
vue: { dynamicAttrs: [':class', 'v-bind:class'] },
10241024
},
10251025

1026+
printers: {
1027+
html: {},
1028+
},
1029+
10261030
transform: transformHtml,
10271031
})
10281032

@@ -1041,6 +1045,10 @@ let glimmer = defineTransform<GlimmerNode>({
10411045
glimmer: {},
10421046
},
10431047

1048+
printers: {
1049+
glimmer: {},
1050+
},
1051+
10441052
transform: transformGlimmer,
10451053
})
10461054

@@ -1061,6 +1069,10 @@ let css = defineTransform<CssNode>({
10611069
less: {},
10621070
},
10631071

1072+
printers: {
1073+
postcss: {},
1074+
},
1075+
10641076
transform: transformCss,
10651077
})
10661078

@@ -1099,6 +1111,12 @@ let js = defineTransform<import('@babel/types').Node>({
10991111
},
11001112
},
11011113

1114+
printers: {
1115+
estree: { load: ['prettier/plugins/estree'] },
1116+
'estree-oxc': { load: ['@prettier/plugin-oxc'] },
1117+
'estree-hermes': { load: ['@prettier/plugin-hermes'] },
1118+
},
1119+
11021120
transform: transformJavaScript,
11031121
})
11041122

@@ -1161,6 +1179,10 @@ let astro = defineTransform<AstroNode>({
11611179
astro: {},
11621180
},
11631181

1182+
printers: {
1183+
astro: {},
1184+
},
1185+
11641186
transform: transformAstro,
11651187
})
11661188

@@ -1174,6 +1196,10 @@ let marko = defineTransform<MarkoNode>({
11741196
marko: {},
11751197
},
11761198

1199+
printers: {
1200+
'marko-ast': {},
1201+
},
1202+
11771203
transform: transformMarko,
11781204
})
11791205

@@ -1206,6 +1232,10 @@ let twig = defineTransform<TwigNode>({
12061232
twig: {},
12071233
},
12081234

1235+
printers: {
1236+
twig: {},
1237+
},
1238+
12091239
transform: transformTwig,
12101240
})
12111241

@@ -1222,6 +1252,10 @@ let pug = defineTransform<PugNode>({
12221252
pug: {},
12231253
},
12241254

1255+
printers: {
1256+
'pug-ast': {},
1257+
},
1258+
12251259
transform: transformPug,
12261260
})
12271261

@@ -1239,6 +1273,10 @@ let liquid = defineTransform<LiquidNode>({
12391273

12401274
parsers: { 'liquid-html': {} },
12411275

1276+
printers: {
1277+
'liquid-html-ast': {},
1278+
},
1279+
12421280
transform: transformLiquid,
12431281
})
12441282

src/transform.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,15 @@ export interface TransformOptions<T> {
5656
/**
5757
* A list of supported parser names
5858
*/
59-
printers?: Record<string, {}>
59+
printers?: Record<
60+
string,
61+
{
62+
/**
63+
* Load the given plugins for the parsers and printers
64+
*/
65+
load?: string[]
66+
}
67+
>
6068

6169
/**
6270
* Transform entire ASTs

0 commit comments

Comments
 (0)