/
index.js
532 lines (470 loc) · 16.7 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
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 * as svelte from 'prettier-plugin-svelte'
import lineColumn from 'line-column'
import jsesc from 'jsesc'
import escalade from 'escalade/sync'
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() ?? ''}`
}
let classNamesWithOrder = env.context.getClassOrder
? env.context.getClassOrder(classes)
: getClassOrderPolyfill(classes, { env })
classes = 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)
for (let i = 0; i < classes.length; i++) {
result += `${classes[i]}${whitespace[i] ?? ''}`
}
return prefix + result + suffix
}
function createParser(original, transform) {
return {
...original,
parse(text, parsers, options = {}) {
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 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 = prettierParserAngular.parsers.__ng_directive.parse(
attr.value,
env.parsers,
env.options
)
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 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 = {
...svelte.options,
tailwindConfig: {
type: 'string',
category: 'Tailwind CSS',
description: 'TODO',
},
}
export const languages = svelte.languages
export const printers = {
'svelte-ast': {
...svelte.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 svelte.printers['svelte-ast'].print(path, options, print)
},
},
}
export const parsers = {
html: createParser(prettierParserHTML.parsers.html, transformHtml(['class'])),
glimmer: createParser(prettierParserGlimmer.parsers.glimmer, transformGlimmer),
lwc: createParser(prettierParserHTML.parsers.lwc, transformHtml(['class'])),
angular: createParser(
prettierParserHTML.parsers.angular,
transformHtml(['class'], ['[ngClass]'], 'angular')
),
vue: createParser(prettierParserHTML.parsers.vue, transformHtml(['class'], [':class'])),
css: createParser(prettierParserPostCSS.parsers.css, transformCss),
scss: createParser(prettierParserPostCSS.parsers.scss, transformCss),
less: createParser(prettierParserPostCSS.parsers.less, transformCss),
babel: createParser(prettierParserBabel.parsers.babel, transformJavaScript),
'babel-flow': createParser(prettierParserBabel.parsers['babel-flow'], transformJavaScript),
flow: createParser(prettierParserFlow.parsers.flow, transformJavaScript),
typescript: createParser(prettierParserTypescript.parsers.typescript, transformJavaScript),
'babel-ts': createParser(prettierParserBabel.parsers['babel-ts'], transformJavaScript),
espree: createParser(prettierParserEspree.parsers.espree, transformJavaScript),
meriyah: createParser(prettierParserMeriyah.parsers.meriyah, transformJavaScript),
__js_expression: createParser(prettierParserBabel.parsers.__js_expression, transformJavaScript),
svelte: createParser(svelte.parsers.svelte, (ast, { env }) => {
let changes = []
transformSvelte(ast.html, { env, changes })
ast.changes = changes
}),
}
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)
}