Skip to content

Commit 90aff0c

Browse files
committed
Use synthetic identifier during emit instead
1 parent 6b476b2 commit 90aff0c

File tree

5 files changed

+146
-2
lines changed

5 files changed

+146
-2
lines changed

src/compiler/emitter.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,9 +1177,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
11771177
}
11781178

11791179
function emitJsxElement(openingNode: JsxOpeningLikeElement, children?: JsxChild[]) {
1180+
let syntheticReactRef = <Identifier>createSynthesizedNode(SyntaxKind.Identifier);
1181+
syntheticReactRef.text = 'React';
1182+
syntheticReactRef.parent = openingNode;
1183+
11801184
// Call React.createElement(tag, ...
11811185
emitLeadingComments(openingNode);
1182-
write("React.createElement(");
1186+
emitExpressionIdentifier(syntheticReactRef);
1187+
write(".createElement(");
11831188
emitTagName(openingNode.tagName);
11841189
write(", ");
11851190

@@ -1193,7 +1198,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
11931198
// a call to React.__spread
11941199
let attrs = openingNode.attributes;
11951200
if (forEach(attrs, attr => attr.kind === SyntaxKind.JsxSpreadAttribute)) {
1196-
write("React.__spread(");
1201+
emitExpressionIdentifier(syntheticReactRef);
1202+
write(".__spread(");
11971203

11981204
let haveOpenedObjectLiteral = false;
11991205
for (let i = 0; i < attrs.length; i++) {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//// [tests/cases/conformance/jsx/tsxReactEmit6.tsx] ////
2+
3+
//// [file.tsx]
4+
5+
declare module JSX {
6+
interface Element { }
7+
interface IntrinsicElements {
8+
[s: string]: any;
9+
}
10+
}
11+
12+
//// [react-consumer.tsx]
13+
namespace M {
14+
export var React: any;
15+
}
16+
17+
namespace M {
18+
// Should emit M.React.createElement
19+
// and M.React.__spread
20+
var foo;
21+
var spread1 = <div x='' {...foo} y='' />;
22+
}
23+
24+
25+
//// [file.js]
26+
//// [react-consumer.js]
27+
var M;
28+
(function (M) {
29+
})(M || (M = {}));
30+
var M;
31+
(function (M) {
32+
// Should emit M.React.createElement
33+
// and M.React.__spread
34+
var foo;
35+
var spread1 = M.React.createElement("div", M.React.__spread({x: ''}, foo, {y: ''}));
36+
})(M || (M = {}));
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
=== tests/cases/conformance/jsx/file.tsx ===
2+
3+
declare module JSX {
4+
>JSX : Symbol(JSX, Decl(file.tsx, 0, 0))
5+
6+
interface Element { }
7+
>Element : Symbol(Element, Decl(file.tsx, 1, 20))
8+
9+
interface IntrinsicElements {
10+
>IntrinsicElements : Symbol(IntrinsicElements, Decl(file.tsx, 2, 22))
11+
12+
[s: string]: any;
13+
>s : Symbol(s, Decl(file.tsx, 4, 3))
14+
}
15+
}
16+
17+
=== tests/cases/conformance/jsx/react-consumer.tsx ===
18+
namespace M {
19+
>M : Symbol(M, Decl(react-consumer.tsx, 0, 0), Decl(react-consumer.tsx, 2, 1))
20+
21+
export var React: any;
22+
>React : Symbol(React, Decl(react-consumer.tsx, 1, 11))
23+
}
24+
25+
namespace M {
26+
>M : Symbol(M, Decl(react-consumer.tsx, 0, 0), Decl(react-consumer.tsx, 2, 1))
27+
28+
// Should emit M.React.createElement
29+
// and M.React.__spread
30+
var foo;
31+
>foo : Symbol(foo, Decl(react-consumer.tsx, 7, 4))
32+
33+
var spread1 = <div x='' {...foo} y='' />;
34+
>spread1 : Symbol(spread1, Decl(react-consumer.tsx, 8, 4))
35+
>div : Symbol(JSX.IntrinsicElements, Decl(file.tsx, 2, 22))
36+
>x : Symbol(unknown)
37+
>y : Symbol(unknown)
38+
}
39+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
=== tests/cases/conformance/jsx/file.tsx ===
2+
3+
declare module JSX {
4+
>JSX : any
5+
6+
interface Element { }
7+
>Element : Element
8+
9+
interface IntrinsicElements {
10+
>IntrinsicElements : IntrinsicElements
11+
12+
[s: string]: any;
13+
>s : string
14+
}
15+
}
16+
17+
=== tests/cases/conformance/jsx/react-consumer.tsx ===
18+
namespace M {
19+
>M : typeof M
20+
21+
export var React: any;
22+
>React : any
23+
}
24+
25+
namespace M {
26+
>M : typeof M
27+
28+
// Should emit M.React.createElement
29+
// and M.React.__spread
30+
var foo;
31+
>foo : any
32+
33+
var spread1 = <div x='' {...foo} y='' />;
34+
>spread1 : JSX.Element
35+
><div x='' {...foo} y='' /> : JSX.Element
36+
>div : any
37+
>x : any
38+
>foo : any
39+
>y : any
40+
}
41+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//@jsx: react
2+
//@module: commonjs
3+
4+
//@filename: file.tsx
5+
declare module JSX {
6+
interface Element { }
7+
interface IntrinsicElements {
8+
[s: string]: any;
9+
}
10+
}
11+
12+
//@filename: react-consumer.tsx
13+
namespace M {
14+
export var React: any;
15+
}
16+
17+
namespace M {
18+
// Should emit M.React.createElement
19+
// and M.React.__spread
20+
var foo;
21+
var spread1 = <div x='' {...foo} y='' />;
22+
}

0 commit comments

Comments
 (0)