Skip to content

Commit fc800a7

Browse files
Copilotjakebailey
andauthored
Fix JSX indentation in JavaScript output (#1792)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
1 parent 0874642 commit fc800a7

File tree

91 files changed

+479
-519
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+479
-519
lines changed

internal/printer/printer.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -783,10 +783,9 @@ func (p *Printer) shouldEmitBlockFunctionBodyOnSingleLine(body *ast.Block) bool
783783
}
784784

785785
func (p *Printer) shouldEmitOnNewLine(node *ast.Node, format ListFormat) bool {
786-
// !!! TODO: enable multiline emit
787-
// if p.emitContext.EmitFlags(node)&EFStartOnNewLine != 0 {
788-
// return true
789-
// }
786+
if p.emitContext.EmitFlags(node)&EFStartOnNewLine != 0 {
787+
return true
788+
}
790789
return format&LFPreferNewLine != 0
791790
}
792791

internal/transformers/jsxtransforms/jsx.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,9 @@ func (tx *JSXTransformer) convertJsxChildrenToChildrenPropObject(children []*ast
319319
}
320320

321321
func (tx *JSXTransformer) transformJsxChildToExpression(node *ast.Node) *ast.Node {
322+
prev := tx.inJsxChild
322323
tx.setInChild(true)
323-
defer tx.setInChild(false)
324+
defer tx.setInChild(prev)
324325
return tx.Visitor().Visit(node)
325326
}
326327

@@ -688,14 +689,18 @@ func (tx *JSXTransformer) visitJsxOpeningLikeElementCreateElement(element *ast.N
688689
for _, c := range children.Nodes {
689690
res := tx.transformJsxChildToExpression(c)
690691
if res != nil {
691-
if len(children.Nodes) > 1 {
692-
tx.EmitContext().AddEmitFlags(res, printer.EFStartOnNewLine)
693-
}
694692
newChildren = append(newChildren, res)
695693
}
696694
}
697695
}
698696

697+
// Add StartOnNewLine flag only if there are multiple actual children (after filtering)
698+
if len(newChildren) > 1 {
699+
for _, child := range newChildren {
700+
tx.EmitContext().AddEmitFlags(child, printer.EFStartOnNewLine)
701+
}
702+
}
703+
699704
args := make([]*ast.Expression, 0, len(newChildren)+2)
700705
args = append(args, tagName)
701706
args = append(args, objectProperties)
@@ -725,14 +730,18 @@ func (tx *JSXTransformer) visitJsxOpeningFragmentCreateElement(fragment *ast.Jsx
725730
for _, c := range children.Nodes {
726731
res := tx.transformJsxChildToExpression(c)
727732
if res != nil {
728-
if len(children.Nodes) > 1 {
729-
tx.EmitContext().AddEmitFlags(res, printer.EFStartOnNewLine)
730-
}
731733
newChildren = append(newChildren, res)
732734
}
733735
}
734736
}
735737

738+
// Add StartOnNewLine flag only if there are multiple actual children (after filtering)
739+
if len(newChildren) > 1 {
740+
for _, child := range newChildren {
741+
tx.EmitContext().AddEmitFlags(child, printer.EFStartOnNewLine)
742+
}
743+
}
744+
736745
args := make([]*ast.Expression, 0, len(newChildren)+2)
737746
args = append(args, tagName)
738747
args = append(args, tx.Factory().NewKeywordExpression(ast.KindNullKeyword))
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//// [tests/cases/compiler/jsxNestedIndentation.tsx] ////
2+
3+
//// [jsxNestedIndentation.tsx]
4+
declare var React: any;
5+
declare function Child(props: { children?: any }): any;
6+
function Test() {
7+
return <Child>
8+
<Child>
9+
<Child></Child>
10+
</Child>
11+
</Child>
12+
}
13+
14+
15+
//// [jsxNestedIndentation.js]
16+
function Test() {
17+
return React.createElement(Child, null,
18+
React.createElement(Child, null,
19+
React.createElement(Child, null)));
20+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//// [tests/cases/compiler/jsxNestedIndentation.tsx] ////
2+
3+
=== jsxNestedIndentation.tsx ===
4+
declare var React: any;
5+
>React : Symbol(React, Decl(jsxNestedIndentation.tsx, 0, 11))
6+
7+
declare function Child(props: { children?: any }): any;
8+
>Child : Symbol(Child, Decl(jsxNestedIndentation.tsx, 0, 23))
9+
>props : Symbol(props, Decl(jsxNestedIndentation.tsx, 1, 23))
10+
>children : Symbol(children, Decl(jsxNestedIndentation.tsx, 1, 31))
11+
12+
function Test() {
13+
>Test : Symbol(Test, Decl(jsxNestedIndentation.tsx, 1, 55))
14+
15+
return <Child>
16+
>Child : Symbol(Child, Decl(jsxNestedIndentation.tsx, 0, 23))
17+
18+
<Child>
19+
>Child : Symbol(Child, Decl(jsxNestedIndentation.tsx, 0, 23))
20+
21+
<Child></Child>
22+
>Child : Symbol(Child, Decl(jsxNestedIndentation.tsx, 0, 23))
23+
>Child : Symbol(Child, Decl(jsxNestedIndentation.tsx, 0, 23))
24+
25+
</Child>
26+
>Child : Symbol(Child, Decl(jsxNestedIndentation.tsx, 0, 23))
27+
28+
</Child>
29+
>Child : Symbol(Child, Decl(jsxNestedIndentation.tsx, 0, 23))
30+
}
31+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//// [tests/cases/compiler/jsxNestedIndentation.tsx] ////
2+
3+
=== jsxNestedIndentation.tsx ===
4+
declare var React: any;
5+
>React : any
6+
7+
declare function Child(props: { children?: any }): any;
8+
>Child : (props: { children?: any; }) => any
9+
>props : { children?: any; }
10+
>children : any
11+
12+
function Test() {
13+
>Test : () => any
14+
15+
return <Child>
16+
><Child> <Child> <Child></Child> </Child> </Child> : any
17+
>Child : (props: { children?: any; }) => any
18+
19+
<Child>
20+
><Child> <Child></Child> </Child> : any
21+
>Child : (props: { children?: any; }) => any
22+
23+
<Child></Child>
24+
><Child></Child> : any
25+
>Child : (props: { children?: any; }) => any
26+
>Child : (props: { children?: any; }) => any
27+
28+
</Child>
29+
>Child : (props: { children?: any; }) => any
30+
31+
</Child>
32+
>Child : (props: { children?: any; }) => any
33+
}
34+

testdata/baselines/reference/compiler/jsxUnicodeEscapeSequence.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ export const InlineUnicodeChar = () => {
3434
};
3535
export const StandaloneUnicodeChar = () => {
3636
// This should reproduce the issue - unicode character on its own line
37-
return (_jsxs("div", { children: [_jsx("span", { children: "\u26A0" }), "\u26A0"] }));
37+
return (_jsxs("div", { children: [
38+
_jsx("span", { children: "\u26A0" }),
39+
"\u26A0"] }));
3840
};
3941
export const MultipleUnicodeChars = () => {
4042
// Test multiple unicode characters

testdata/baselines/reference/submodule/compiler/errorSpanForUnclosedJsxTag.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ let Foo = {
1818
Bar() { }
1919
};
2020
let Baz = () => { };
21-
let x = React.createElement(Foo.Bar, null, "Hello let y = ", React.createElement(Baz, null, "Hello"));
21+
let x = React.createElement(Foo.Bar, null,
22+
"Hello let y = ",
23+
React.createElement(Baz, null, "Hello"));

testdata/baselines/reference/submodule/compiler/errorSpanForUnclosedJsxTag.js.diff

Lines changed: 0 additions & 10 deletions
This file was deleted.

testdata/baselines/reference/submodule/compiler/excessiveStackDepthFlatArray.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ configureStore({
4848
});
4949
const Component = () => {
5050
const categories = ['Fruit', 'Vegetables'];
51-
return (React.createElement("ul", null, React.createElement("li", null, "All"), categories.map((category) => (React.createElement("li", { key: category }, category) // Error about 'key' only
52-
))));
51+
return (React.createElement("ul", null,
52+
React.createElement("li", null, "All"),
53+
categories.map((category) => (React.createElement("li", { key: category }, category) // Error about 'key' only
54+
))));
5355
};

testdata/baselines/reference/submodule/compiler/excessiveStackDepthFlatArray.js.diff

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)