@@ -379,75 +379,55 @@ function transformLiquid(ast, { env }) {
379379 : node . name === 'class'
380380 }
381381
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 ) {
389383 visit ( attr . value , {
390384 TextNode ( node ) {
391385 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 ) )
396389 } ,
397390
398391 String ( node ) {
399392 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 ) )
409398 } ,
410399 } )
411400 }
412401
413402 visit ( ast , {
414- LiquidTag ( node ) {
415- sources . push ( node )
403+ LiquidTag ( node , _parent , _key , _index , meta ) {
404+ meta . path = [ ... meta . path ?? [ ] , node ] ;
416405 } ,
417406
418- HtmlElement ( node ) {
419- sources . push ( node )
407+ HtmlElement ( node , _parent , _key , _index , meta ) {
408+ meta . path = [ ... meta . path ?? [ ] , node ] ;
420409 } ,
421410
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 ;
426414 }
415+
416+ meta . path = [ ...meta . path ?? [ ] , node ] ;
417+
418+ sortAttribute ( node , meta . path )
427419 } ,
428420
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 ;
433424 }
434- } ,
435- } ) ;
436425
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 ] ;
442427
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+ } ) ;
451431}
452432
453433function sortStringLiteral ( node , { env } ) {
0 commit comments