@@ -379,75 +379,55 @@ function transformLiquid(ast, { env }) {
379
379
: node . name === 'class'
380
380
}
381
381
382
- /** @type {{type: string, source: string}[] } */
383
- let sources = [ ]
384
-
385
- /** @type {{pos: {start: number, end: number}, value: string}[] } */
386
- let changes = [ ]
387
-
388
- function sortAttribute ( attr ) {
382
+ function sortAttribute ( attr , path ) {
389
383
visit ( attr . value , {
390
384
TextNode ( node ) {
391
385
node . value = sortClasses ( node . value , { env } ) ;
392
- changes . push ( {
393
- pos : node . position ,
394
- value : node . value ,
395
- } )
386
+
387
+ let source = node . source . slice ( 0 , node . position . start ) + node . value + node . source . slice ( node . position . end )
388
+ path . forEach ( node => ( node . source = source ) )
396
389
} ,
397
390
398
391
String ( node ) {
399
392
node . value = sortClasses ( node . value , { env } ) ;
400
- changes . push ( {
401
- pos : {
402
- // String position includes the quotes even if the value doesn't
403
- // Hence the +1 and -1 when slicing
404
- start : node . position . start + 1 ,
405
- end : node . position . end - 1 ,
406
- } ,
407
- value : node . value ,
408
- } )
393
+
394
+ // String position includes the quotes even if the value doesn't
395
+ // Hence the +1 and -1 when slicing
396
+ let source = node . source . slice ( 0 , node . position . start + 1 ) + node . value + node . source . slice ( node . position . end - 1 )
397
+ path . forEach ( node => ( node . source = source ) )
409
398
} ,
410
399
} )
411
400
}
412
401
413
402
visit ( ast , {
414
- LiquidTag ( node ) {
415
- sources . push ( node )
403
+ LiquidTag ( node , _parent , _key , _index , meta ) {
404
+ meta . path = [ ... meta . path ?? [ ] , node ] ;
416
405
} ,
417
406
418
- HtmlElement ( node ) {
419
- sources . push ( node )
407
+ HtmlElement ( node , _parent , _key , _index , meta ) {
408
+ meta . path = [ ... meta . path ?? [ ] , node ] ;
420
409
} ,
421
410
422
- AttrSingleQuoted ( node ) {
423
- if ( isClassAttr ( node ) ) {
424
- sources . push ( node )
425
- sortAttribute ( node )
411
+ AttrSingleQuoted ( node , _parent , _key , _index , meta ) {
412
+ if ( ! isClassAttr ( node ) ) {
413
+ return ;
426
414
}
415
+
416
+ meta . path = [ ...meta . path ?? [ ] , node ] ;
417
+
418
+ sortAttribute ( node , meta . path )
427
419
} ,
428
420
429
- AttrDoubleQuoted ( node ) {
430
- if ( isClassAttr ( node ) ) {
431
- sources . push ( node )
432
- sortAttribute ( node )
421
+ AttrDoubleQuoted ( node , _parent , _key , _index , meta ) {
422
+ if ( ! isClassAttr ( node ) ) {
423
+ return ;
433
424
}
434
- } ,
435
- } ) ;
436
425
437
- // Sort so all changes occur in order
438
- changes = changes . sort ( ( a , b ) => {
439
- return a . start - b . start
440
- || a . end - b . end
441
- } )
426
+ meta . path = [ ...meta . path ?? [ ] , node ] ;
442
427
443
- for ( let change of changes ) {
444
- for ( let node of sources ) {
445
- node . source =
446
- node . source . slice ( 0 , change . pos . start ) +
447
- change . value +
448
- node . source . slice ( change . pos . end )
449
- }
450
- }
428
+ sortAttribute ( node , meta . path )
429
+ } ,
430
+ } ) ;
451
431
}
452
432
453
433
function sortStringLiteral ( node , { env } ) {
0 commit comments