/
index.js
881 lines (757 loc) · 25.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
import prettier from 'prettier'
import prettierParserHTML from 'prettier/parser-html'
import prettierParserAngular from 'prettier/parser-angular'
import prettierParserPostCSS from 'prettier/parser-postcss'
import prettierParserBabel from 'prettier/parser-babel'
import prettierParserEspree from 'prettier/parser-espree'
import prettierParserGlimmer from 'prettier/parser-glimmer'
import prettierParserMeriyah from 'prettier/parser-meriyah'
import prettierParserFlow from 'prettier/parser-flow'
import prettierParserTypescript from 'prettier/parser-typescript'
import { createContext as createContextFallback } from 'tailwindcss/lib/lib/setupContextUtils'
import { generateRules as generateRulesFallback } from 'tailwindcss/lib/lib/generateRules'
import resolveConfigFallback from 'tailwindcss/resolveConfig'
import * as recast from 'recast'
import * as astTypes from 'ast-types'
import * as path from 'path'
import requireFrom from 'import-from'
import requireFresh from 'import-fresh'
import objectHash from 'object-hash'
import lineColumn from 'line-column'
import jsesc from 'jsesc'
import escalade from 'escalade/sync'
let base = getBasePlugins()
let contextMap = new Map()
function bigSign(bigIntValue) {
return (bigIntValue > 0n) - (bigIntValue < 0n)
}
function prefixCandidate(context, selector) {
let prefix = context.tailwindConfig.prefix
return typeof prefix === 'function' ? prefix(selector) : prefix + selector
}
// Polyfill for older Tailwind CSS versions
function getClassOrderPolyfill(classes, { env }) {
// A list of utilities that are used by certain Tailwind CSS utilities but
// that don't exist on their own. This will result in them "not existing" and
// sorting could be weird since you still require them in order to make the
// host utitlies work properly. (Thanks Biology)
let parasiteUtilities = new Set([
prefixCandidate(env.context, 'group'),
prefixCandidate(env.context, 'peer'),
])
let classNamesWithOrder = []
for (let className of classes) {
let order =
env
.generateRules(new Set([className]), env.context)
.sort(([a], [z]) => bigSign(z - a))[0]?.[0] ?? null
if (order === null && parasiteUtilities.has(className)) {
// This will make sure that it is at the very beginning of the
// `components` layer which technically means 'before any
// components'.
order = env.context.layerOrder.components
}
classNamesWithOrder.push([className, order])
}
return classNamesWithOrder
}
function sortClasses(classStr, { env, ignoreFirst = false, ignoreLast = false }) {
if (typeof classStr !== 'string' || classStr === '') {
return classStr
}
// Ignore class attributes containing `{{`, to match Prettier behaviour:
// https://github.com/prettier/prettier/blob/main/src/language-html/embed.js#L83-L88
if (classStr.includes('{{')) {
return classStr
}
let result = ''
let parts = classStr.split(/(\s+)/)
let classes = parts.filter((_, i) => i % 2 === 0)
let whitespace = parts.filter((_, i) => i % 2 !== 0)
if (classes[classes.length - 1] === '') {
classes.pop()
}
let prefix = ''
if (ignoreFirst) {
prefix = `${classes.shift() ?? ''}${whitespace.shift() ?? ''}`
}
let suffix = ''
if (ignoreLast) {
suffix = `${whitespace.pop() ?? ''}${classes.pop() ?? ''}`
}
classes = sortClassList(classes, { env })
for (let i = 0; i < classes.length; i++) {
result += `${classes[i]}${whitespace[i] ?? ''}`
}
return prefix + result + suffix
}
function sortClassList(classList, { env }) {
let classNamesWithOrder = env.context.getClassOrder
? env.context.getClassOrder(classList)
: getClassOrderPolyfill(classList, { env })
return classNamesWithOrder
.sort(([, a], [, z]) => {
if (a === z) return 0
// if (a === null) return options.unknownClassPosition === 'start' ? -1 : 1
// if (z === null) return options.unknownClassPosition === 'start' ? 1 : -1
if (a === null) return -1
if (z === null) return 1
return bigSign(a - z)
})
.map(([className]) => className)
}
function createParser(parserFormat, transform) {
return {
...base.parsers[parserFormat],
preprocess(code, options) {
let original = getCompatibleParser(parserFormat, options)
if (original.preprocess) {
return original.preprocess(code, options)
}
return code
},
parse(text, parsers, options = {}) {
let original = getCompatibleParser(parserFormat, options)
if (original.astFormat === 'svelte-ast') {
options.printer = printers['svelte-ast']
}
let ast = original.parse(text, parsers, options)
let tailwindConfigPath = '__default__'
let tailwindConfig = {}
let resolveConfig = resolveConfigFallback
let createContext = createContextFallback
let generateRules = generateRulesFallback
let baseDir
let prettierConfigPath = prettier.resolveConfigFile.sync(options.filepath)
if (options.tailwindConfig) {
baseDir = prettierConfigPath ? path.dirname(prettierConfigPath) : process.cwd()
tailwindConfigPath = path.resolve(baseDir, options.tailwindConfig)
tailwindConfig = requireFresh(tailwindConfigPath)
} else {
baseDir = prettierConfigPath
? path.dirname(prettierConfigPath)
: options.filepath
? path.dirname(options.filepath)
: process.cwd()
let configPath
try {
configPath = escalade(baseDir, (_dir, names) => {
if (names.includes('tailwind.config.js')) {
return 'tailwind.config.js'
}
if (names.includes('tailwind.config.cjs')) {
return 'tailwind.config.cjs'
}
})
} catch {}
if (configPath) {
tailwindConfigPath = configPath
tailwindConfig = requireFresh(configPath)
}
}
try {
resolveConfig = requireFrom(baseDir, 'tailwindcss/resolveConfig')
createContext = requireFrom(baseDir, 'tailwindcss/lib/lib/setupContextUtils').createContext
generateRules = requireFrom(baseDir, 'tailwindcss/lib/lib/generateRules').generateRules
} catch {}
// suppress "empty content" warning
tailwindConfig.content = ['no-op']
let context
let existing = contextMap.get(tailwindConfigPath)
let hash = objectHash(tailwindConfig)
if (existing && existing.hash === hash) {
context = existing.context
} else {
context = createContext(resolveConfig(tailwindConfig))
contextMap.set(tailwindConfigPath, { context, hash })
}
transform(ast, { env: { context, generateRules, parsers, options } })
return ast
},
}
}
function tryParseAngularAttribute(value, env) {
let parsers = [
// Try parsing as an angular directive
prettierParserAngular.parsers.__ng_directive,
// If this fails we fall back to arbitrary parsing of a JS expression
{ parse: env.parsers.__js_expression },
]
let errors = []
for (const parser of parsers) {
try {
return parser.parse(
value,
env.parsers,
env.options
)
} catch (err) {
errors.push(err)
}
}
console.warn("prettier-plugin-tailwindcss: Unable to parse angular directive")
errors.forEach(err => console.warn(err))
}
function transformHtml(attributes, computedAttributes = [], computedType = 'js') {
let transform = (ast, { env }) => {
for (let attr of ast.attrs ?? []) {
if (attributes.includes(attr.name)) {
attr.value = sortClasses(attr.value, { env })
} else if (computedAttributes.includes(attr.name)) {
if (!/[`'"]/.test(attr.value)) {
continue
}
if (computedType === 'angular') {
let directiveAst = tryParseAngularAttribute(attr.value, env)
// If we've reached this point we couldn't parse the expression we we should bail
// `tryParseAngularAttribute` will display some warnings/errors
// But we shouldn't fail outright — just miss parsing some attributes
if (!directiveAst) {
continue
}
visit(directiveAst, {
StringLiteral(node) {
if (!node.value) return
attr.value =
attr.value.slice(0, node.start + 1) +
sortClasses(node.value, { env }) +
attr.value.slice(node.end - 1)
},
})
continue
}
let ast = recast.parse(`let __prettier_temp__ = ${attr.value}`, {
parser: prettierParserBabel.parsers['babel-ts'],
})
let didChange = false
astTypes.visit(ast, {
visitLiteral(path) {
if (isStringLiteral(path.node)) {
if (sortStringLiteral(path.node, { env })) {
didChange = true
// https://github.com/benjamn/recast/issues/171#issuecomment-224996336
let quote = path.node.extra.raw[0]
let value = jsesc(path.node.value, {
quotes: quote === "'" ? 'single' : 'double',
})
path.node.value = new String(quote + value + quote)
}
}
this.traverse(path)
},
visitTemplateLiteral(path) {
if (sortTemplateLiteral(path.node, { env })) {
didChange = true
}
this.traverse(path)
},
})
if (didChange) {
attr.value = recast.print(ast.program.body[0].declarations[0].init).code
}
}
}
for (let child of ast.children ?? []) {
transform(child, { env })
}
}
return transform
}
function transformGlimmer(ast, { env }) {
visit(ast, {
AttrNode(attr, parent, key, index, meta) {
let attributes = ['class']
if (attributes.includes(attr.name) && attr.value) {
meta.sortTextNodes = true
}
},
TextNode(node, parent, key, index, meta) {
if (!meta.sortTextNodes) {
return
}
let siblings =
parent?.type === 'ConcatStatement'
? {
prev: parent.parts[index - 1],
next: parent.parts[index + 1],
}
: null
node.chars = sortClasses(node.chars, {
env,
ignoreFirst: siblings?.prev && !/^\s/.test(node.chars),
ignoreLast: siblings?.next && !/\s$/.test(node.chars),
})
},
StringLiteral(node, parent, key, index, meta) {
if (!meta.sortTextNodes) {
return
}
node.value = sortClasses(node.value, { env })
},
})
}
function transformLiquid(ast, { env }) {
/** @param {{name: string | {type: string, value: string}[]}} node */
function isClassAttr(node) {
if (Array.isArray(node.name)) {
return node.name.every((n) => n.type === 'TextNode' && n.value === 'class');
}
return node.name === 'class'
}
visit(ast, {
AttrSingleQuoted(node, _parent, _key, _index, meta) {
if (!isClassAttr(node)) {
return;
}
meta.sortTextNodes = true;
meta.sourceNode = node;
},
AttrDoubleQuoted(node, _parent, _key, _index, meta) {
if (!isClassAttr(node)) {
return;
}
meta.sortTextNodes = true;
// With Liquid Script it uses the "source" of certain nodes as the "source of truth"
// We must modify that node's source to get the desired output
// Even if we modify the AST it will be ignored
meta.sourceNode = node;
},
TextNode(node, _parent, _key, _index, meta) {
if (!meta.sortTextNodes) {
return;
}
node.value = sortClasses(node.value, { env });
// This feels hacky but it's necessary
node.source = node.source.slice(0, node.position.start) + node.value + node.source.slice(node.position.end);
meta.sourceNode.source = node.source;
},
String(node, _parent, _key, _index, meta) {
if (!meta.sortTextNodes) {
return;
}
node.value = sortClasses(node.value, { env });
// This feels hacky but it's necessary
// String position includes the quotes even if the value doesn't
// Hence the +1 and -1 when slicing
node.source = node.source.slice(0, node.position.start+1) + node.value + node.source.slice(node.position.end-1);
meta.sourceNode.source = node.source;
},
});
}
function sortStringLiteral(node, { env }) {
let result = sortClasses(node.value, { env })
let didChange = result !== node.value
node.value = result
if (node.extra) {
// JavaScript (StringLiteral)
let raw = node.extra.raw
node.extra = {
...node.extra,
rawValue: result,
raw: raw[0] + result + raw.slice(-1),
}
} else {
// TypeScript (Literal)
let raw = node.raw
node.raw = raw[0] + result + raw.slice(-1)
}
return didChange
}
function isStringLiteral(node) {
return (
node.type === 'StringLiteral' || (node.type === 'Literal' && typeof node.value === 'string')
)
}
function sortTemplateLiteral(node, { env }) {
let didChange = false
for (let i = 0; i < node.quasis.length; i++) {
let quasi = node.quasis[i]
let same = quasi.value.raw === quasi.value.cooked
let originalRaw = quasi.value.raw
let originalCooked = quasi.value.cooked
quasi.value.raw = sortClasses(quasi.value.raw, {
env,
ignoreFirst: i > 0 && !/^\s/.test(quasi.value.raw),
ignoreLast: i < node.expressions.length && !/\s$/.test(quasi.value.raw),
})
quasi.value.cooked = same
? quasi.value.raw
: sortClasses(quasi.value.cooked, {
env,
ignoreFirst: i > 0 && !/^\s/.test(quasi.value.cooked),
ignoreLast: i < node.expressions.length && !/\s$/.test(quasi.value.cooked),
})
if (quasi.value.raw !== originalRaw || quasi.value.cooked !== originalCooked) {
didChange = true
}
}
return didChange
}
function transformJavaScript(ast, { env }) {
visit(ast, {
JSXAttribute(node) {
if (!node.value) {
return
}
if (['class', 'className'].includes(node.name.name)) {
if (isStringLiteral(node.value)) {
sortStringLiteral(node.value, { env })
} else if (node.value.type === 'JSXExpressionContainer') {
visit(node.value, (node, parent, key) => {
if (isStringLiteral(node)) {
sortStringLiteral(node, { env })
} else if (node.type === 'TemplateLiteral') {
sortTemplateLiteral(node, { env })
}
})
}
}
},
})
}
function transformCss(ast, { env }) {
ast.walk((node) => {
if (node.type === 'css-atrule' && node.name === 'apply') {
node.params = sortClasses(node.params, {
env,
ignoreLast: /\s+(?:!important|#{!important})\s*$/.test(node.params),
})
}
})
}
export const options = {
tailwindConfig: {
type: 'string',
category: 'Tailwind CSS',
description: 'TODO',
},
}
export const printers = {
...base.printers['svelte-ast'] ? {'svelte-ast': {
...base.printers['svelte-ast'],
print: (path, options, print) => {
if (!options.__mutatedOriginalText) {
options.__mutatedOriginalText = true
let changes = path.stack[0].changes
if (changes?.length) {
let finder = lineColumn(options.originalText)
for (let change of changes) {
let start = finder.toIndex(change.loc.start.line, change.loc.start.column + 1)
let end = finder.toIndex(change.loc.end.line, change.loc.end.column + 1)
options.originalText =
options.originalText.substring(0, start) +
change.text +
options.originalText.substring(end)
}
}
}
return base.printers['svelte-ast'].print(path, options, print)
},
} } : {},
}
export const parsers = {
html: createParser('html', transformHtml(['class'])),
glimmer: createParser('glimmer', transformGlimmer),
lwc: createParser('lwc', transformHtml(['class'])),
angular: createParser(
'angular',
transformHtml(['class'], ['[ngClass]'], 'angular')
),
vue: createParser('vue', transformHtml(['class'], [':class'])),
css: createParser('css', transformCss),
scss: createParser('scss', transformCss),
less: createParser('less', transformCss),
babel: createParser('babel', transformJavaScript),
'babel-flow': createParser('babel-flow', transformJavaScript),
flow: createParser('flow', transformJavaScript),
typescript: createParser('typescript', transformJavaScript),
'babel-ts': createParser('babel-ts', transformJavaScript),
espree: createParser('espree', transformJavaScript),
meriyah: createParser('meriyah', transformJavaScript),
__js_expression: createParser('__js_expression', transformJavaScript),
...base.parsers.svelte ? { svelte: createParser('svelte', (ast, { env }) => {
let changes = []
transformSvelte(ast.html, { env, changes })
ast.changes = changes
}) } : {},
...base.parsers.astro ? { astro: createParser('astro', transformAstro) } : {},
...base.parsers.php ? { php: createParser('php', transformPHP) } : {},
...base.parsers.melody ? { melody: createParser('melody', transformMelody) } : {},
...base.parsers.pug ? { pug: createParser('pug', transformPug) } : {},
...(base.parsers['liquid-html']
? { 'liquid-html': createParser("liquid-html", transformLiquid) }
: {}),
// ...base.parsers.blade ? { blade: createParser('blade', transformBlade) } : {},
}
function transformAstro(ast, { env, changes }) {
if (ast.type === "element") {
for (let attr of ast.attributes ?? []) {
if (attr.name === "class" && attr.type === "attribute" && attr.kind === "quoted") {
attr.value = sortClasses(attr.value, {
env
});
}
}
}
for (let child of ast.children ?? []) {
transformAstro(child, { env, changes });
}
}
function transformPHP(ast, { env, changes }) {
if (ast.kind === "inline") {
let leading = ast.raw.match(/^\s*/)[0]
let trailing = ast.raw.match(/\s*$/)[0]
// If the inline block is just whitespace then we don't need to format
if (ast.raw === leading) {
return
}
// We have to parse this as HTML with prettier
let parsed = prettier.format(ast.raw, {
...env.options,
parser: "html",
})
let formatted = `${leading}${parsed.trimEnd()}${trailing}`
ast.raw = formatted
ast.value = formatted
}
for (let child of ast.children ?? []) {
transformPHP(child, { env, changes });
}
}
/*
function transformBlade(ast, { env, changes }) {
// Blade gets formatted on parse
// This means we'd have to parse the blade ourselves and figure out what's HTML and what isn't
}
*/
function transformMelody(ast, { env, changes }) {
for (let child of ast.expressions ?? []) {
transformMelody(child, { env })
}
visit(ast, {
Attribute(node, _parent, _key, _index, meta) {
if (node.name.name !== "class") {
return
}
meta.sortTextNodes = true
},
StringLiteral(node, _parent, _key, _index, meta) {
if (!meta.sortTextNodes) {
return
}
node.value = sortClasses(node.value, {
env,
});
}
})
}
function transformPug(ast, { env }) {
// This isn't optimal
// We should merge the classes together across class attributes and class tokens
// And then we sort them
// But this is good enough for now
// First sort the classes in attributes
for (const token of ast.tokens) {
if (token.type === 'attribute' && token.name === 'class') {
token.val = [
token.val.slice(0, 1),
sortClasses(token.val.slice(1, -1), { env }),
token.val.slice(-1)
].join('')
}
}
// Collect lists of consecutive class tokens
let startIdx = -1;
let endIdx = -1;
let ranges = [];
for (let i = 0; i < ast.tokens.length; i++) {
const token = ast.tokens[i];
if (token.type === 'class') {
startIdx = startIdx === -1 ? i : startIdx;
endIdx = i;
} else if (startIdx !== -1) {
ranges.push([startIdx, endIdx]);
startIdx = -1;
endIdx = -1;
}
}
if (startIdx !== -1) {
ranges.push([startIdx, endIdx]);
startIdx = -1;
endIdx = -1;
}
// Sort the lists of class tokens
for (const [startIdx, endIdx] of ranges) {
const classes = ast.tokens.slice(startIdx, endIdx + 1).map(token => token.val);
const classList = sortClassList(classes, { env })
for (let i = startIdx; i <= endIdx; i++) {
ast.tokens[i].val = classList[i - startIdx];
}
}
}
function transformSvelte(ast, { env, changes }) {
for (let attr of ast.attributes ?? []) {
if (attr.name === 'class' && attr.type === 'Attribute') {
for (let i = 0; i < attr.value.length; i++) {
let value = attr.value[i]
if (value.type === 'Text') {
let same = value.raw === value.data
value.raw = sortClasses(value.raw, {
env,
ignoreFirst: i > 0 && !/^\s/.test(value.raw),
ignoreLast: i < attr.value.length - 1 && !/\s$/.test(value.raw),
})
value.data = same
? value.raw
: sortClasses(value.data, {
env,
ignoreFirst: i > 0 && !/^\s/.test(value.data),
ignoreLast: i < attr.value.length - 1 && !/\s$/.test(value.data),
})
} else if (value.type === 'MustacheTag') {
visit(value.expression, {
Literal(node) {
if (isStringLiteral(node)) {
if (sortStringLiteral(node, { env })) {
changes.push({ text: node.raw, loc: node.loc })
}
}
},
TemplateLiteral(node) {
if (sortTemplateLiteral(node, { env })) {
for (let quasi of node.quasis) {
changes.push({ text: quasi.value.raw, loc: quasi.loc })
}
}
},
})
}
}
}
}
for (let child of ast.children ?? []) {
transformSvelte(child, { env, changes })
}
if (ast.type === 'IfBlock') {
for (let child of ast.else?.children ?? []) {
transformSvelte(child, { env, changes })
}
}
}
// https://lihautan.com/manipulating-ast-with-javascript/
function visit(ast, callbackMap) {
function _visit(node, parent, key, index, meta = {}) {
if (typeof callbackMap === 'function') {
if (callbackMap(node, parent, key, index, meta) === false) {
return
}
} else if (node.type in callbackMap) {
if (callbackMap[node.type](node, parent, key, index, meta) === false) {
return
}
}
const keys = Object.keys(node)
for (let i = 0; i < keys.length; i++) {
const child = node[keys[i]]
if (Array.isArray(child)) {
for (let j = 0; j < child.length; j++) {
if (child[j] !== null) {
_visit(child[j], node, keys[i], j, { ...meta })
}
}
} else if (typeof child?.type === 'string') {
_visit(child, node, keys[i], i, { ...meta })
}
}
}
_visit(ast)
}
// For loading prettier plugins only if they exist
function loadIfExists(name) {
try {
if (require.resolve(name)) {
return require(name)
}
} catch (e) {
return null
}
}
function getBasePlugins() {
// We need to load this plugin dynamically because it's not available by default
// And we are not bundling it with the main Prettier plugin
let astro = loadIfExists('prettier-plugin-astro')
let svelte = loadIfExists('prettier-plugin-svelte')
let php = loadIfExists('@prettier/plugin-php')
let melody = loadIfExists('prettier-plugin-twig-melody')
let pug = loadIfExists('@prettier/plugin-pug')
let liquid = loadIfExists('@shopify/prettier-plugin-liquid')
// let blade = loadIfExists('@shufo/prettier-plugin-blade')
return {
parsers: {
html: prettierParserHTML.parsers.html,
glimmer: prettierParserGlimmer.parsers.glimmer,
lwc: prettierParserHTML.parsers.lwc,
angular: prettierParserHTML.parsers.angular,
vue: prettierParserHTML.parsers.vue,
css: prettierParserPostCSS.parsers.css,
scss: prettierParserPostCSS.parsers.scss,
less: prettierParserPostCSS.parsers.less,
babel: prettierParserBabel.parsers.babel,
'babel-flow': prettierParserBabel.parsers['babel-flow'],
flow: prettierParserFlow.parsers.flow,
typescript: prettierParserTypescript.parsers.typescript,
'babel-ts': prettierParserBabel.parsers['babel-ts'],
espree: prettierParserEspree.parsers.espree,
meriyah: prettierParserMeriyah.parsers.meriyah,
__js_expression: prettierParserBabel.parsers.__js_expression,
...(svelte?.parsers ?? {}),
...(astro?.parsers ?? {}),
...(php?.parsers ?? {}),
...(melody?.parsers ?? {}),
...(pug?.parsers ?? {}),
...(liquid?.parsers ?? {}),
// ...(blade?.parsers ?? {}),
},
printers: {
...(svelte ? { 'svelte-ast': svelte.printers['svelte-ast'] } : {}),
},
};
}
function getCompatibleParser(parserFormat, options) {
if (!options.plugins) {
return base.parsers[parserFormat]
}
let parser = {
...base.parsers[parserFormat],
}
// Now load parsers from plugins
let compatiblePlugins = [
'@trivago/prettier-plugin-sort-imports',
'prettier-plugin-organize-imports',
'@prettier/plugin-php',
'@prettier/plugin-pug',
'@shopify/prettier-plugin-liquid',
'@shufo/prettier-plugin-blade',
'prettier-plugin-css-order',
'prettier-plugin-import-sort',
'prettier-plugin-jsdoc',
'prettier-plugin-organize-attributes',
'prettier-plugin-style-order',
'prettier-plugin-twig-melody',
]
for (const name of compatiblePlugins) {
let path = null
try {
path = require.resolve(name)
} catch (err) {
continue
}
let plugin = options.plugins.find(plugin => plugin.name === name || plugin.name === path)
// The plugin is not loaded
if (!plugin) {
continue
}
Object.assign(parser, plugin.parsers[parserFormat])
}
return parser
}