@@ -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
@@ -152,8 +152,8 @@ function transformDynamicAngularAttribute(attr: any, env: TransformerEnv) {
152
152
ignoreLast : i < node . expressions . length && ! / \s $ / . test ( quasi . value . raw ) ,
153
153
154
154
collapseWhitespace : {
155
- start : concat ?. key !== 'right' && i === 0 ,
156
- end : concat ?. key !== 'left' && i >= node . expressions . length ,
155
+ start : collapseWhitespace . start && i === 0 ,
156
+ end : collapseWhitespace . end && i >= node . expressions . length ,
157
157
} ,
158
158
} ) ,
159
159
} )
@@ -612,6 +612,49 @@ function isSortableExpression(
612
612
return false
613
613
}
614
614
615
+ function canCollapseWhitespaceIn ( path : Path < import ( '@babel/types' ) . Node , any > ) {
616
+ let start = true
617
+ let end = true
618
+
619
+ for ( let entry of path ) {
620
+ if ( ! entry . parent ) continue
621
+
622
+ // Nodes inside concat expressions shouldn't collapse whitespace
623
+ // depending on which side they're part of.
624
+ if ( entry . parent . type === 'BinaryExpression' && entry . parent . operator === '+' ) {
625
+ start &&= entry . key !== 'right'
626
+ end &&= entry . key !== 'left'
627
+ }
628
+
629
+ // This is probably expression *inside* of a template literal. To collapse whitespace
630
+ // `Expression`s adjacent-before a quasi must start with whitespace
631
+ // `Expression`s adjacent-after a quasi must end with whitespace
632
+ //
633
+ // Note this check will bail out on more than it really should as it
634
+ // could be reset somewhere along the way by having whitespace around a
635
+ // string further up but not at the "root" but that complicates things
636
+ if ( entry . parent . type === 'TemplateLiteral' ) {
637
+ let nodeStart = entry . node . start ?? null
638
+ let nodeEnd = entry . node . end ?? null
639
+
640
+ for ( let quasi of entry . parent . quasis ) {
641
+ let quasiStart = quasi . end ?? null
642
+ let quasiEnd = quasi . end ?? null
643
+
644
+ if ( nodeStart !== null && quasiEnd !== null && nodeStart - quasiEnd <= 2 ) {
645
+ start &&= / ^ \s / . test ( quasi . value . raw )
646
+ }
647
+
648
+ if ( nodeEnd !== null && quasiStart !== null && nodeEnd - quasiStart <= 2 ) {
649
+ end &&= / \s $ / . test ( quasi . value . raw )
650
+ }
651
+ }
652
+ }
653
+ }
654
+
655
+ return { start, end }
656
+ }
657
+
615
658
// TODO: The `ast` types here aren't strictly correct.
616
659
//
617
660
// We cross several parsers that share roughly the same shape so things are
@@ -621,61 +664,15 @@ function transformJavaScript(ast: import('@babel/types').Node, { env }: Transfor
621
664
622
665
function sortInside ( ast : import ( '@babel/types' ) . Node ) {
623
666
visit ( ast , ( node , path ) => {
624
- let start = true
625
- let end = true
626
-
627
- for ( let entry of path ) {
628
- if ( ! entry . parent ) continue
629
-
630
- // Nodes inside concat expressions shouldn't collapse whitespace
631
- // depending on which side they're part of.
632
- if ( entry . parent . type === 'BinaryExpression' && entry . parent . operator === '+' ) {
633
- start &&= entry . key !== 'right'
634
- end &&= entry . key !== 'left'
635
- }
636
-
637
- // This is probably expression *inside* of a template literal. To collapse whitespace
638
- // `Expression`s adjacent-before a quasi must start with whitespace
639
- // `Expression`s adjacent-after a quasi must end with whitespace
640
- //
641
- // Note this check will bail out on more than it really should as it
642
- // could be reset somewhere along the way by having whitespace around a
643
- // string further up but not at the "root" but that complicates things
644
- if ( entry . parent . type === 'TemplateLiteral' ) {
645
- let nodeStart = entry . node . start ?? null
646
- let nodeEnd = entry . node . end ?? null
647
-
648
- for ( let quasi of entry . parent . quasis ) {
649
- let quasiStart = quasi . end ?? null
650
- let quasiEnd = quasi . end ?? null
651
-
652
- if ( nodeStart !== null && quasiEnd !== null && nodeStart - quasiEnd <= 2 ) {
653
- start &&= / ^ \s / . test ( quasi . value . raw )
654
- }
655
-
656
- if ( nodeEnd !== null && quasiStart !== null && nodeEnd - quasiStart <= 2 ) {
657
- end &&= / \s $ / . test ( quasi . value . raw )
658
- }
659
- }
660
- }
661
- }
667
+ let collapseWhitespace = canCollapseWhitespaceIn ( path )
662
668
663
669
if ( isStringLiteral ( node ) ) {
664
- sortStringLiteral ( node , {
665
- env,
666
- collapseWhitespace : { start, end } ,
667
- } )
670
+ sortStringLiteral ( node , { env, collapseWhitespace } )
668
671
} else if ( node . type === 'TemplateLiteral' ) {
669
- sortTemplateLiteral ( node , {
670
- env,
671
- collapseWhitespace : { start, end } ,
672
- } )
672
+ sortTemplateLiteral ( node , { env, collapseWhitespace } )
673
673
} else if ( node . type === 'TaggedTemplateExpression' ) {
674
674
if ( isSortableTemplateExpression ( node , functions ) ) {
675
- sortTemplateLiteral ( node . quasi , {
676
- env,
677
- collapseWhitespace : { start, end } ,
678
- } )
675
+ sortTemplateLiteral ( node . quasi , { env, collapseWhitespace } )
679
676
}
680
677
}
681
678
} )
0 commit comments