@@ -3,6 +3,7 @@ import prettierParserHTML from 'prettier/parser-html'
3
3
import prettierParserPostCSS from 'prettier/parser-postcss'
4
4
import prettierParserBabel from 'prettier/parser-babel'
5
5
import prettierParserEspree from 'prettier/parser-espree'
6
+ import prettierParserGlimmer from 'prettier/parser-glimmer'
6
7
import prettierParserMeriyah from 'prettier/parser-meriyah'
7
8
import prettierParserFlow from 'prettier/parser-flow'
8
9
import prettierParserTypescript from 'prettier/parser-typescript'
@@ -234,6 +235,43 @@ function transformHtml(attributes, computedAttributes = []) {
234
235
return transform
235
236
}
236
237
238
+ function transformGlimmer ( ast , { env } ) {
239
+ visit ( ast , {
240
+ AttrNode ( attr , parent , key , index , meta ) {
241
+ let attributes = [ 'class' ]
242
+
243
+ if ( attributes . includes ( attr . name ) && attr . value ) {
244
+ meta . sortTextNodes = true
245
+ }
246
+ } ,
247
+
248
+ TextNode ( node , parent , key , index , meta ) {
249
+ if ( ! meta . sortTextNodes ) {
250
+ return
251
+ }
252
+
253
+ let siblings = parent ?. type === 'ConcatStatement' ? {
254
+ prev : parent . parts [ index - 1 ] ,
255
+ next : parent . parts [ index + 1 ] ,
256
+ } : null
257
+
258
+ node . chars = sortClasses ( node . chars , {
259
+ env,
260
+ ignoreFirst : siblings ?. prev && ! / ^ \s / . test ( node . chars ) ,
261
+ ignoreLast : siblings ?. next && ! / \s $ / . test ( node . chars ) ,
262
+ } )
263
+ } ,
264
+
265
+ StringLiteral ( node , parent , key , index , meta ) {
266
+ if ( ! meta . sortTextNodes ) {
267
+ return
268
+ }
269
+
270
+ node . value = sortClasses ( node . value , { env } )
271
+ } ,
272
+ } )
273
+ }
274
+
237
275
function sortStringLiteral ( node , { env } ) {
238
276
let result = sortClasses ( node . value , { env } )
239
277
let didChange = result !== node . value
@@ -364,6 +402,7 @@ export const printers = {
364
402
365
403
export const parsers = {
366
404
html : createParser ( prettierParserHTML . parsers . html , transformHtml ( [ 'class' ] ) ) ,
405
+ glimmer : createParser ( prettierParserGlimmer . parsers . glimmer , transformGlimmer ) ,
367
406
lwc : createParser ( prettierParserHTML . parsers . lwc , transformHtml ( [ 'class' ] ) ) ,
368
407
angular : createParser (
369
408
prettierParserHTML . parsers . angular ,
@@ -442,13 +481,13 @@ function transformSvelte(ast, { env, changes }) {
442
481
443
482
// https://lihautan.com/manipulating-ast-with-javascript/
444
483
function visit ( ast , callbackMap ) {
445
- function _visit ( node , parent , key , index ) {
484
+ function _visit ( node , parent , key , index , meta = { } ) {
446
485
if ( typeof callbackMap === 'function' ) {
447
- if ( callbackMap ( node , parent , key , index ) === false ) {
486
+ if ( callbackMap ( node , parent , key , index , meta ) === false ) {
448
487
return
449
488
}
450
489
} else if ( node . type in callbackMap ) {
451
- if ( callbackMap [ node . type ] ( node , parent , key , index ) === false ) {
490
+ if ( callbackMap [ node . type ] ( node , parent , key , index , meta ) === false ) {
452
491
return
453
492
}
454
493
}
@@ -459,11 +498,11 @@ function visit(ast, callbackMap) {
459
498
if ( Array . isArray ( child ) ) {
460
499
for ( let j = 0 ; j < child . length ; j ++ ) {
461
500
if ( child [ j ] !== null ) {
462
- _visit ( child [ j ] , node , keys [ i ] , j )
501
+ _visit ( child [ j ] , node , keys [ i ] , j , { ... meta } )
463
502
}
464
503
}
465
504
} else if ( typeof child ?. type === 'string' ) {
466
- _visit ( child , node , keys [ i ] , i )
505
+ _visit ( child , node , keys [ i ] , i , { ... meta } )
467
506
}
468
507
}
469
508
}
0 commit comments