Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -783,10 +783,9 @@ func (p *Printer) shouldEmitBlockFunctionBodyOnSingleLine(body *ast.Block) bool
}

func (p *Printer) shouldEmitOnNewLine(node *ast.Node, format ListFormat) bool {
// !!! TODO: enable multiline emit
// if p.emitContext.EmitFlags(node)&EFStartOnNewLine != 0 {
// return true
// }
if p.emitContext.EmitFlags(node)&EFStartOnNewLine != 0 {
return true
}
return format&LFPreferNewLine != 0
}

Expand Down
23 changes: 16 additions & 7 deletions internal/transformers/jsxtransforms/jsx.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,9 @@ func (tx *JSXTransformer) convertJsxChildrenToChildrenPropObject(children []*ast
}

func (tx *JSXTransformer) transformJsxChildToExpression(node *ast.Node) *ast.Node {
prev := tx.inJsxChild
tx.setInChild(true)
defer tx.setInChild(false)
defer tx.setInChild(prev)
return tx.Visitor().Visit(node)
}

Expand Down Expand Up @@ -688,14 +689,18 @@ func (tx *JSXTransformer) visitJsxOpeningLikeElementCreateElement(element *ast.N
for _, c := range children.Nodes {
res := tx.transformJsxChildToExpression(c)
if res != nil {
if len(children.Nodes) > 1 {
tx.EmitContext().AddEmitFlags(res, printer.EFStartOnNewLine)
}
newChildren = append(newChildren, res)
}
}
}

// Add StartOnNewLine flag only if there are multiple actual children (after filtering)
if len(newChildren) > 1 {
for _, child := range newChildren {
tx.EmitContext().AddEmitFlags(child, printer.EFStartOnNewLine)
}
}

args := make([]*ast.Expression, 0, len(newChildren)+2)
args = append(args, tagName)
args = append(args, objectProperties)
Expand Down Expand Up @@ -725,14 +730,18 @@ func (tx *JSXTransformer) visitJsxOpeningFragmentCreateElement(fragment *ast.Jsx
for _, c := range children.Nodes {
res := tx.transformJsxChildToExpression(c)
if res != nil {
if len(children.Nodes) > 1 {
tx.EmitContext().AddEmitFlags(res, printer.EFStartOnNewLine)
}
newChildren = append(newChildren, res)
}
}
}

// Add StartOnNewLine flag only if there are multiple actual children (after filtering)
if len(newChildren) > 1 {
for _, child := range newChildren {
tx.EmitContext().AddEmitFlags(child, printer.EFStartOnNewLine)
}
}

args := make([]*ast.Expression, 0, len(newChildren)+2)
args = append(args, tagName)
args = append(args, tx.Factory().NewKeywordExpression(ast.KindNullKeyword))
Expand Down
20 changes: 20 additions & 0 deletions testdata/baselines/reference/compiler/jsxNestedIndentation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//// [tests/cases/compiler/jsxNestedIndentation.tsx] ////

//// [jsxNestedIndentation.tsx]
declare var React: any;
declare function Child(props: { children?: any }): any;
function Test() {
return <Child>
<Child>
<Child></Child>
</Child>
</Child>
}


//// [jsxNestedIndentation.js]
function Test() {
return React.createElement(Child, null,
React.createElement(Child, null,
React.createElement(Child, null)));
}
31 changes: 31 additions & 0 deletions testdata/baselines/reference/compiler/jsxNestedIndentation.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//// [tests/cases/compiler/jsxNestedIndentation.tsx] ////

=== jsxNestedIndentation.tsx ===
declare var React: any;
>React : Symbol(React, Decl(jsxNestedIndentation.tsx, 0, 11))

declare function Child(props: { children?: any }): any;
>Child : Symbol(Child, Decl(jsxNestedIndentation.tsx, 0, 23))
>props : Symbol(props, Decl(jsxNestedIndentation.tsx, 1, 23))
>children : Symbol(children, Decl(jsxNestedIndentation.tsx, 1, 31))

function Test() {
>Test : Symbol(Test, Decl(jsxNestedIndentation.tsx, 1, 55))

return <Child>
>Child : Symbol(Child, Decl(jsxNestedIndentation.tsx, 0, 23))

<Child>
>Child : Symbol(Child, Decl(jsxNestedIndentation.tsx, 0, 23))

<Child></Child>
>Child : Symbol(Child, Decl(jsxNestedIndentation.tsx, 0, 23))
>Child : Symbol(Child, Decl(jsxNestedIndentation.tsx, 0, 23))

</Child>
>Child : Symbol(Child, Decl(jsxNestedIndentation.tsx, 0, 23))

</Child>
>Child : Symbol(Child, Decl(jsxNestedIndentation.tsx, 0, 23))
}

34 changes: 34 additions & 0 deletions testdata/baselines/reference/compiler/jsxNestedIndentation.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//// [tests/cases/compiler/jsxNestedIndentation.tsx] ////

=== jsxNestedIndentation.tsx ===
declare var React: any;
>React : any

declare function Child(props: { children?: any }): any;
>Child : (props: { children?: any; }) => any
>props : { children?: any; }
>children : any

function Test() {
>Test : () => any

return <Child>
><Child> <Child> <Child></Child> </Child> </Child> : any
>Child : (props: { children?: any; }) => any

<Child>
><Child> <Child></Child> </Child> : any
>Child : (props: { children?: any; }) => any

<Child></Child>
><Child></Child> : any
>Child : (props: { children?: any; }) => any
>Child : (props: { children?: any; }) => any

</Child>
>Child : (props: { children?: any; }) => any

</Child>
>Child : (props: { children?: any; }) => any
}

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ export const InlineUnicodeChar = () => {
};
export const StandaloneUnicodeChar = () => {
// This should reproduce the issue - unicode character on its own line
return (_jsxs("div", { children: [_jsx("span", { children: "\u26A0" }), "\u26A0"] }));
return (_jsxs("div", { children: [
_jsx("span", { children: "\u26A0" }),
"\u26A0"] }));
};
export const MultipleUnicodeChars = () => {
// Test multiple unicode characters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ let Foo = {
Bar() { }
};
let Baz = () => { };
let x = React.createElement(Foo.Bar, null, "Hello let y = ", React.createElement(Baz, null, "Hello"));
let x = React.createElement(Foo.Bar, null,
"Hello let y = ",
React.createElement(Baz, null, "Hello"));

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ configureStore({
});
const Component = () => {
const categories = ['Fruit', 'Vegetables'];
return (React.createElement("ul", null, React.createElement("li", null, "All"), categories.map((category) => (React.createElement("li", { key: category }, category) // Error about 'key' only
))));
return (React.createElement("ul", null,
React.createElement("li", null, "All"),
categories.map((category) => (React.createElement("li", { key: category }, category) // Error about 'key' only
))));
};

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ declare var React: any;


//// [jsxAttributeWithoutExpressionReact.js]
React.createElement(View, null, React.createElement(ListView, { refreshControl: React.createElement(RefreshControl, { onRefresh: true, refreshing: true }), dataSource: this.state.ds, renderRow: true }));
React.createElement(View, null,
React.createElement(ListView, { refreshControl: React.createElement(RefreshControl, { onRefresh: true, refreshing: true }), dataSource: this.state.ds, renderRow: true }));

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ const a = (
//// [index.js]
/// <reference path="react18/react18.d.ts" />
/// <reference path="react18/global.d.ts" />
const a = (React.createElement("main", null, (React.createElement("div", null)), React.createElement("span", null)));
const a = (React.createElement("main", null,
(React.createElement("div", null)),
React.createElement("span", null)));
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,4 @@
-"use strict";
/// <reference path="react18/react18.d.ts" />
/// <reference path="react18/global.d.ts" />
-const a = (React.createElement("main", null,
- (React.createElement("div", null)),
- React.createElement("span", null)));
+const a = (React.createElement("main", null, (React.createElement("div", null)), React.createElement("span", null)));
const a = (React.createElement("main", null,
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ const b = (
//// [index.js]
/// <reference path="react18/react18.d.ts" />
/// <reference path="react18/global.d.ts" />
const b = (React.createElement(Foo, null, React.createElement("div", null), "aa"));
const b = (React.createElement(Foo, null,
React.createElement("div", null),
"aa"));
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,4 @@
-"use strict";
/// <reference path="react18/react18.d.ts" />
/// <reference path="react18/global.d.ts" />
-const b = (React.createElement(Foo, null,
- React.createElement("div", null),
- "aa"));
+const b = (React.createElement(Foo, null, React.createElement("div", null), "aa"));
const b = (React.createElement(Foo, null,
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ var a = React.createElement(Blah, null, x => x);
// Blah components don't accept text as child elements
var a = React.createElement(Blah, null, "Hello unexpected text!");
// Blah components don't accept multiple children.
var a = React.createElement(Blah, null, x => "" + x, x => "" + x);
var a = React.createElement(Blah, null,
x => "" + x,
x => "" + x);
function Blah2(props) {
return React.createElement(React.Fragment, null);
}
Expand All @@ -103,7 +105,9 @@ var a = React.createElement(Blah2, null, x => x);
// Blah2 components don't accept text as child elements
var a = React.createElement(Blah2, null, "Hello unexpected text!");
// Blah2 components don't accept multiple children of the wrong type.
var a = React.createElement(Blah2, null, x => x, x => x);
var a = React.createElement(Blah2, null,
x => x,
x => x);
function Blah3(props) {
return React.createElement(React.Fragment, null);
}
Expand All @@ -112,4 +116,6 @@ var a = React.createElement(Blah3, null, x => x);
// Blah3 components don't accept text as child elements
var a = React.createElement(Blah3, null, "Hello unexpected text!");
// Blah3 components don't accept multiple children of the wrong type.
var a = React.createElement(Blah3, null, x => x, x => x);
var a = React.createElement(Blah3, null,
x => x,
x => x);
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,4 @@
+const React = require("react");
function Blah(props) {
return React.createElement(React.Fragment, null);
}
@@= skipped -9, +9 lines =@@
// Blah components don't accept text as child elements
var a = React.createElement(Blah, null, "Hello unexpected text!");
// Blah components don't accept multiple children.
-var a = React.createElement(Blah, null,
- x => "" + x,
- x => "" + x);
+var a = React.createElement(Blah, null, x => "" + x, x => "" + x);
function Blah2(props) {
return React.createElement(React.Fragment, null);
}
@@= skipped -11, +9 lines =@@
// Blah2 components don't accept text as child elements
var a = React.createElement(Blah2, null, "Hello unexpected text!");
// Blah2 components don't accept multiple children of the wrong type.
-var a = React.createElement(Blah2, null,
- x => x,
- x => x);
+var a = React.createElement(Blah2, null, x => x, x => x);
function Blah3(props) {
return React.createElement(React.Fragment, null);
}
@@= skipped -11, +9 lines =@@
// Blah3 components don't accept text as child elements
var a = React.createElement(Blah3, null, "Hello unexpected text!");
// Blah3 components don't accept multiple children of the wrong type.
-var a = React.createElement(Blah3, null,
- x => x,
- x => x);
+var a = React.createElement(Blah3, null, x => x, x => x);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ const b = (
//// [other.js]
/// <reference path="react18/react18.d.ts" />
/// <reference path="react18/global.d.ts" />
const b = (React.createElement(Foo, null, React.createElement("div", null), "aa"));
const b = (React.createElement(Foo, null,
React.createElement("div", null),
"aa"));
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,4 @@
-"use strict";
/// <reference path="react18/react18.d.ts" />
/// <reference path="react18/global.d.ts" />
-const b = (React.createElement(Foo, null,
- React.createElement("div", null),
- "aa"));
+const b = (React.createElement(Foo, null, React.createElement("div", null), "aa"));
const b = (React.createElement(Foo, null,
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ const React = require("react");
function Wrapper(props) {
return React.createElement("div", null, props.children);
}
const element = (React.createElement(Wrapper, null, React.createElement("div", null, "Hello")));
const element = (React.createElement(Wrapper, null,
React.createElement("div", null, "Hello")));
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,4 @@
+const React = require("react");
function Wrapper(props) {
return React.createElement("div", null, props.children);
}
-const element = (React.createElement(Wrapper, null,
- React.createElement("div", null, "Hello")));
+const element = (React.createElement(Wrapper, null, React.createElement("div", null, "Hello")));
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ declare var Frag: any;

//// [jsxFactoryAndJsxFragmentFactory.js]
h(Frag, null);
h(Frag, null, h("span", null, "1"), h(Frag, null, h("span", null, "2.1"), h("span", null, "2.2")));
h(Frag, null,
h("span", null, "1"),
h(Frag, null,
h("span", null, "2.1"),
h("span", null, "2.2")));

This file was deleted.

Loading