@@ -675,25 +675,68 @@ function canCollapseWhitespaceIn(path: Path<import('@babel/types').Node, any>) {
675675function transformJavaScript ( ast : import ( '@babel/types' ) . Node , { env } : TransformerContext ) {
676676 let { matcher } = env
677677
678- function sortInside ( ast : import ( '@babel/types' ) . Node , column ?: number ) {
678+ function getNestingDepth ( path : any ) : number {
679+ let depth = 0
680+ let current = path
681+ while ( current ) {
682+ const node = current . node
683+ if ( node ) {
684+ if (
685+ node . type === 'JSXElement' ||
686+ node . type === 'JSXFragment' ||
687+ node . type === 'BlockStatement' ||
688+ node . type === 'ObjectExpression' ||
689+ node . type === 'ArrayExpression' ||
690+ node . type === 'ClassBody' ||
691+ node . type === 'SwitchCase'
692+ ) {
693+ depth ++
694+ } else if (
695+ node . type === 'ArrowFunctionExpression' &&
696+ node . body . type !== 'BlockStatement'
697+ ) {
698+ // Implicit return arrow function body
699+ depth ++
700+ } else if (
701+ node . type === 'ConditionalExpression' ||
702+ node . type === 'LogicalExpression'
703+ ) {
704+ // Nested expressions
705+ depth ++
706+ } else if ( node . type === 'ReturnStatement' ) {
707+ depth ++
708+ }
709+ }
710+ current = current . parentPath
711+ }
712+ return depth
713+ }
714+
715+ function sortInside ( ast : import ( '@babel/types' ) . Node , depth ?: number ) {
679716 visit ( ast , ( node , path ) => {
680717 let collapseWhitespace = canCollapseWhitespaceIn ( path )
681- let nodeColumn = column ?? ( node as any ) . loc ?. start ?. column
718+ // If depth is not provided, calculate it.
719+ // If we are recursing inside (depth is provided), we might need to adjust it based on current node type?
720+ // Actually, sortInside is called for children. The 'depth' arg represents the base depth of the container.
721+ // But we are visiting nodes inside. It's safer to re-calculate depth for each node to be precise,
722+ // OR pass the depth of the ExpressionContainer.
723+ // Let's rely on getNestingDepth(path) for absolute correctness relative to root.
724+ let nodeDepth = depth ?? getNestingDepth ( path )
682725
683726 if ( isStringLiteral ( node ) ) {
684- sortStringLiteral ( node , { env, collapseWhitespace, column : nodeColumn } )
727+ sortStringLiteral ( node , { env, collapseWhitespace, column : nodeDepth } )
685728 } else if ( node . type === 'TemplateLiteral' ) {
686- sortTemplateLiteral ( node , { env, collapseWhitespace, column : nodeColumn } )
729+ sortTemplateLiteral ( node , { env, collapseWhitespace, column : nodeDepth } )
687730 } else if ( node . type === 'TaggedTemplateExpression' ) {
688731 if ( isSortableTemplateExpression ( node , matcher ) ) {
689- sortTemplateLiteral ( node . quasi , { env, collapseWhitespace, column : nodeColumn } )
732+ sortTemplateLiteral ( node . quasi , { env, collapseWhitespace, column : nodeDepth } )
690733 }
691734 }
692735 } )
693736 }
694737
695738 visit ( ast , {
696- JSXAttribute ( node ) {
739+ JSXAttribute ( node , path ) {
697740 node = node as import ( '@babel/types' ) . JSXAttribute
698741
699742 if ( ! node . value ) {
@@ -710,21 +753,30 @@ function transformJavaScript(ast: import('@babel/types').Node, { env }: Transfor
710753 return
711754 }
712755
756+ // Logic:
757+ // JSXElement (level N) -> Attributes are at N+1.
758+ // Multiline content inside attribute usually starts at N+2 (relative to element start).
759+ // Or simply: indent = (depth + 1) * tabWidth.
760+ // getNestingDepth includes the JSXElement itself. So depth is N+1 (if we count 1 for Element).
761+ // If we use base depth of JSXElement, then attribute is +1 level deep.
762+ const depth = getNestingDepth ( path ) + 1
763+
713764 if ( isStringLiteral ( node . value ) ) {
714- sortStringLiteral ( node . value , { env, column : node . loc ?. start ?. column } )
765+ sortStringLiteral ( node . value , { env, column : depth } )
715766 } else if ( node . value . type === 'JSXExpressionContainer' ) {
716- sortInside ( node . value , node . loc ?. start ?. column )
767+ sortInside ( node . value , depth )
717768 }
718769 } ,
719770
720- CallExpression ( node ) {
771+ CallExpression ( node , path ) {
721772 node = node as import ( '@babel/types' ) . CallExpression
722773
723774 if ( ! isSortableCallExpression ( node , matcher ) ) {
724775 return
725776 }
726777
727- node . arguments . forEach ( ( arg ) => sortInside ( arg , node . loc ?. start ?. column ) )
778+ const depth = getNestingDepth ( path ) + 1
779+ node . arguments . forEach ( ( arg ) => sortInside ( arg , depth ) )
728780 } ,
729781
730782 TaggedTemplateExpression ( node , path ) {
@@ -735,11 +787,12 @@ function transformJavaScript(ast: import('@babel/types').Node, { env }: Transfor
735787 }
736788
737789 let collapseWhitespace = canCollapseWhitespaceIn ( path )
790+ const depth = getNestingDepth ( path ) + 1
738791
739792 sortTemplateLiteral ( node . quasi , {
740793 env,
741794 collapseWhitespace,
742- column : node . loc ?. start ?. column ,
795+ column : depth ,
743796 } )
744797 } ,
745798 } )
0 commit comments