@@ -15,7 +15,7 @@ import { getCustomizations } from './options.js'
15
15
import { loadPlugins } from './plugins.js'
16
16
import { sortClasses , sortClassList } from './sorting.js'
17
17
import type { Customizations , StringChange , TransformerContext , TransformerEnv , TransformerMetadata } from './types'
18
- import { spliceChangesIntoString , visit } from './utils.js'
18
+ import { spliceChangesIntoString , visit , type Path } from './utils.js'
19
19
20
20
let base = await loadPlugins ( )
21
21
@@ -109,30 +109,23 @@ function transformDynamicAngularAttribute(attr: any, env: TransformerEnv) {
109
109
StringLiteral ( node , path ) {
110
110
if ( ! node . value ) return
111
111
112
- let concat = path . find ( ( entry ) => {
113
- return entry . parent && entry . parent . type === 'BinaryExpression' && entry . parent . operator === '+'
114
- } )
112
+ let collapseWhitespace = canCollapseWhitespaceIn ( path )
115
113
116
114
changes . push ( {
117
115
start : node . start + 1 ,
118
116
end : node . end - 1 ,
119
117
before : node . value ,
120
118
after : sortClasses ( node . value , {
121
119
env,
122
- collapseWhitespace : {
123
- start : concat ?. key !== 'right' ,
124
- end : concat ?. key !== 'left' ,
125
- } ,
120
+ collapseWhitespace,
126
121
} ) ,
127
122
} )
128
123
} ,
129
124
130
125
TemplateLiteral ( node , path ) {
131
126
if ( ! node . quasis . length ) return
132
127
133
- let concat = path . find ( ( entry ) => {
134
- return entry . parent && entry . parent . type === 'BinaryExpression' && entry . parent . operator === '+'
135
- } )
128
+ let collapseWhitespace = canCollapseWhitespaceIn ( path )
136
129
137
130
for ( let i = 0 ; i < node . quasis . length ; i ++ ) {
138
131
let quasi = node . quasis [ i ]
@@ -152,8 +145,8 @@ function transformDynamicAngularAttribute(attr: any, env: TransformerEnv) {
152
145
ignoreLast : i < node . expressions . length && ! / \s $ / . test ( quasi . value . raw ) ,
153
146
154
147
collapseWhitespace : {
155
- start : concat ?. key !== 'right' && i === 0 ,
156
- end : concat ?. key !== 'left' && i >= node . expressions . length ,
148
+ start : collapseWhitespace . start && i === 0 ,
149
+ end : collapseWhitespace . end && i >= node . expressions . length ,
157
150
} ,
158
151
} ) ,
159
152
} )
@@ -612,6 +605,49 @@ function isSortableExpression(
612
605
return false
613
606
}
614
607
608
+ function canCollapseWhitespaceIn ( path : Path < import ( '@babel/types' ) . Node , any > ) {
609
+ let start = true
610
+ let end = true
611
+
612
+ for ( let entry of path ) {
613
+ if ( ! entry . parent ) continue
614
+
615
+ // Nodes inside concat expressions shouldn't collapse whitespace
616
+ // depending on which side they're part of.
617
+ if ( entry . parent . type === 'BinaryExpression' && entry . parent . operator === '+' ) {
618
+ start &&= entry . key !== 'right'
619
+ end &&= entry . key !== 'left'
620
+ }
621
+
622
+ // This is probably expression *inside* of a template literal. To collapse whitespace
623
+ // `Expression`s adjacent-before a quasi must start with whitespace
624
+ // `Expression`s adjacent-after a quasi must end with whitespace
625
+ //
626
+ // Note this check will bail out on more than it really should as it
627
+ // could be reset somewhere along the way by having whitespace around a
628
+ // string further up but not at the "root" but that complicates things
629
+ if ( entry . parent . type === 'TemplateLiteral' ) {
630
+ let nodeStart = entry . node . start ?? null
631
+ let nodeEnd = entry . node . end ?? null
632
+
633
+ for ( let quasi of entry . parent . quasis ) {
634
+ let quasiStart = quasi . end ?? null
635
+ let quasiEnd = quasi . end ?? null
636
+
637
+ if ( nodeStart !== null && quasiEnd !== null && nodeStart - quasiEnd <= 2 ) {
638
+ start &&= / ^ \s / . test ( quasi . value . raw )
639
+ }
640
+
641
+ if ( nodeEnd !== null && quasiStart !== null && nodeEnd - quasiStart <= 2 ) {
642
+ end &&= / \s $ / . test ( quasi . value . raw )
643
+ }
644
+ }
645
+ }
646
+ }
647
+
648
+ return { start, end }
649
+ }
650
+
615
651
// TODO: The `ast` types here aren't strictly correct.
616
652
//
617
653
// We cross several parsers that share roughly the same shape so things are
@@ -621,35 +657,15 @@ function transformJavaScript(ast: import('@babel/types').Node, { env }: Transfor
621
657
622
658
function sortInside ( ast : import ( '@babel/types' ) . Node ) {
623
659
visit ( ast , ( node , path ) => {
624
- let concat = path . find ( ( entry ) => {
625
- return entry . parent && entry . parent . type === 'BinaryExpression' && entry . parent . operator === '+'
626
- } )
660
+ let collapseWhitespace = canCollapseWhitespaceIn ( path )
627
661
628
662
if ( isStringLiteral ( node ) ) {
629
- sortStringLiteral ( node , {
630
- env,
631
- collapseWhitespace : {
632
- start : concat ?. key !== 'right' ,
633
- end : concat ?. key !== 'left' ,
634
- } ,
635
- } )
663
+ sortStringLiteral ( node , { env, collapseWhitespace } )
636
664
} else if ( node . type === 'TemplateLiteral' ) {
637
- sortTemplateLiteral ( node , {
638
- env,
639
- collapseWhitespace : {
640
- start : concat ?. key !== 'right' ,
641
- end : concat ?. key !== 'left' ,
642
- } ,
643
- } )
665
+ sortTemplateLiteral ( node , { env, collapseWhitespace } )
644
666
} else if ( node . type === 'TaggedTemplateExpression' ) {
645
667
if ( isSortableTemplateExpression ( node , functions ) ) {
646
- sortTemplateLiteral ( node . quasi , {
647
- env,
648
- collapseWhitespace : {
649
- start : concat ?. key !== 'right' ,
650
- end : concat ?. key !== 'left' ,
651
- } ,
652
- } )
668
+ sortTemplateLiteral ( node . quasi , { env, collapseWhitespace } )
653
669
}
654
670
}
655
671
} )
@@ -697,16 +713,11 @@ function transformJavaScript(ast: import('@babel/types').Node, { env }: Transfor
697
713
return
698
714
}
699
715
700
- let concat = path . find ( ( entry ) => {
701
- return entry . parent && entry . parent . type === 'BinaryExpression' && entry . parent . operator === '+'
702
- } )
716
+ let collapseWhitespace = canCollapseWhitespaceIn ( path )
703
717
704
718
sortTemplateLiteral ( node . quasi , {
705
719
env,
706
- collapseWhitespace : {
707
- start : concat ?. key !== 'right' ,
708
- end : concat ?. key !== 'left' ,
709
- } ,
720
+ collapseWhitespace,
710
721
} )
711
722
} ,
712
723
} )
0 commit comments