@@ -358,55 +358,75 @@ function transformLiquid(ast, { env }) {
358
358
: node . name === 'class'
359
359
}
360
360
361
- function sortAttribute ( attr , path ) {
361
+ /** @type {{type: string, source: string}[] } */
362
+ let sources = [ ]
363
+
364
+ /** @type {{pos: {start: number, end: number}, value: string}[] } */
365
+ let changes = [ ]
366
+
367
+ function sortAttribute ( attr ) {
362
368
visit ( attr . value , {
363
369
TextNode ( node ) {
364
370
node . value = sortClasses ( node . value , { env } ) ;
365
-
366
- let source = node . source . slice ( 0 , node . position . start ) + node . value + node . source . slice ( node . position . end )
367
- path . forEach ( node => ( node . source = source ) )
371
+ changes . push ( {
372
+ pos : node . position ,
373
+ value : node . value ,
374
+ } )
368
375
} ,
369
376
370
377
String ( node ) {
371
378
node . value = sortClasses ( node . value , { env } ) ;
372
-
373
- // String position includes the quotes even if the value doesn't
374
- // Hence the +1 and -1 when slicing
375
- let source = node . source . slice ( 0 , node . position . start + 1 ) + node . value + node . source . slice ( node . position . end - 1 )
376
- path . forEach ( node => ( node . source = source ) )
379
+ changes . push ( {
380
+ pos : {
381
+ // String position includes the quotes even if the value doesn't
382
+ // Hence the +1 and -1 when slicing
383
+ start : node . position . start + 1 ,
384
+ end : node . position . end - 1 ,
385
+ } ,
386
+ value : node . value ,
387
+ } )
377
388
} ,
378
389
} )
379
390
}
380
391
381
392
visit ( ast , {
382
- LiquidTag ( node , _parent , _key , _index , meta ) {
383
- meta . path = [ ... meta . path ?? [ ] , node ] ;
393
+ LiquidTag ( node ) {
394
+ sources . push ( node )
384
395
} ,
385
396
386
- HtmlElement ( node , _parent , _key , _index , meta ) {
387
- meta . path = [ ... meta . path ?? [ ] , node ] ;
397
+ HtmlElement ( node ) {
398
+ sources . push ( node )
388
399
} ,
389
400
390
- AttrSingleQuoted ( node , _parent , _key , _index , meta ) {
391
- if ( ! isClassAttr ( node ) ) {
392
- return ;
401
+ AttrSingleQuoted ( node ) {
402
+ if ( isClassAttr ( node ) ) {
403
+ sources . push ( node )
404
+ sortAttribute ( node )
393
405
}
394
-
395
- meta . path = [ ...meta . path ?? [ ] , node ] ;
396
-
397
- sortAttribute ( node , meta . path )
398
406
} ,
399
407
400
- AttrDoubleQuoted ( node , _parent , _key , _index , meta ) {
401
- if ( ! isClassAttr ( node ) ) {
402
- return ;
408
+ AttrDoubleQuoted ( node ) {
409
+ if ( isClassAttr ( node ) ) {
410
+ sources . push ( node )
411
+ sortAttribute ( node )
403
412
}
404
-
405
- meta . path = [ ...meta . path ?? [ ] , node ] ;
406
-
407
- sortAttribute ( node , meta . path )
408
413
} ,
409
414
} ) ;
415
+
416
+ // Sort so all changes occur in order
417
+ changes = changes . sort ( ( a , b ) => {
418
+ return a . start - b . start
419
+ || a . end - b . end
420
+ } )
421
+
422
+ for ( let change of changes ) {
423
+ for ( let node of sources ) {
424
+ node . source =
425
+ node . source . slice ( 0 , change . pos . start ) +
426
+ change . value +
427
+ node . source . slice ( change . pos . end )
428
+ }
429
+ }
410
430
}
411
431
412
432
function sortStringLiteral ( node , { env } ) {
0 commit comments