Skip to content

Commit c4658c1

Browse files
sebmarkbagezpao
authored andcommitted
Update React JSX Transforms
* Extract JSXDOM into separate transform * Rename lower case variables in JSX to upper case * Drop React.DOM docblock, use string tags instead
1 parent 1a1104f commit c4658c1

File tree

2 files changed

+61
-172
lines changed

2 files changed

+61
-172
lines changed

vendor/fbtransform/transforms/__tests__/react-test.js

Lines changed: 33 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*
16-
16+
* @emails react-core
1717
*/
1818

1919
/*jshint evil:true, unused:false*/
@@ -59,48 +59,34 @@ describe('react jsx', function() {
5959
};
6060

6161
it('should convert simple tags', function() {
62-
var code = [
63-
'/**@jsx React.DOM*/',
64-
'var x = <div></div>;'
65-
].join('\n');
66-
var result = [
67-
'/**@jsx React.DOM*/',
68-
'var x = React.createElement(React.DOM.div, null);'
69-
].join('\n');
62+
var code = 'var x = <div></div>;';
63+
var result = 'var x = React.createElement("div", null);';
7064

7165
expect(transform(code).code).toEqual(result);
7266
});
7367

7468
it('should convert simple text', function() {
75-
var code = [
76-
'/**@jsx React.DOM*/\n' +
77-
'var x = <div>text</div>;'
78-
].join('\n');
79-
var result = [
80-
'/**@jsx React.DOM*/',
81-
'var x = React.createElement(React.DOM.div, null, "text");'
82-
].join('\n');
69+
var code = 'var x = <div>text</div>;';
70+
var result = 'var x = React.createElement("div", null, "text");';
8371

8472
expect(transform(code).code).toEqual(result);
8573
});
8674

8775
it('should have correct comma in nested children', function() {
8876
var code = [
89-
'/**@jsx React.DOM*/',
9077
'var x = <div>',
9178
' <div><br /></div>',
9279
' <Component>{foo}<br />{bar}</Component>',
9380
' <br />',
9481
'</div>;'
9582
].join('\n');
9683
var result = [
97-
'/**@jsx React.DOM*/',
98-
'var x = React.createElement(React.DOM.div, null, ',
99-
' React.createElement(React.DOM.div, null, ' +
100-
'React.createElement(React.DOM.br, null)), ',
84+
'var x = React.createElement("div", null, ',
85+
' React.createElement("div", null, ' +
86+
'React.createElement("br", null)), ',
10187
' React.createElement(Component, null, foo, ' +
102-
'React.createElement(React.DOM.br, null), bar), ',
103-
' React.createElement(React.DOM.br, null)',
88+
'React.createElement("br", null), bar), ',
89+
' React.createElement("br", null)',
10490
');'
10591
].join('\n');
10692

@@ -110,14 +96,12 @@ describe('react jsx', function() {
11096
it('should avoid wrapping in extra parens if not needed', function() {
11197
// Try with a single composite child, wrapped in a div.
11298
var code = [
113-
'/**@jsx React.DOM*/',
11499
'var x = <div>',
115100
' <Component />',
116101
'</div>;'
117102
].join('\n');
118103
var result = [
119-
'/**@jsx React.DOM*/',
120-
'var x = React.createElement(React.DOM.div, null, ',
104+
'var x = React.createElement("div", null, ',
121105
' React.createElement(Component, null)',
122106
');'
123107
].join('\n');
@@ -126,28 +110,24 @@ describe('react jsx', function() {
126110

127111
// Try with a single interpolated child, wrapped in a div.
128112
code = [
129-
'/**@jsx React.DOM*/',
130113
'var x = <div>',
131114
' {this.props.children}',
132115
'</div>;'
133116
].join('\n');
134117
result = [
135-
'/**@jsx React.DOM*/',
136-
'var x = React.createElement(React.DOM.div, null, ',
118+
'var x = React.createElement("div", null, ',
137119
' this.props.children',
138120
');'
139121
].join('\n');
140122
expect(transform(code).code).toEqual(result);
141123

142124
// Try with a single interpolated child, wrapped in a composite.
143125
code = [
144-
'/**@jsx React.DOM*/',
145126
'var x = <Composite>',
146127
' {this.props.children}',
147128
'</Composite>;'
148129
].join('\n');
149130
result = [
150-
'/**@jsx React.DOM*/',
151131
'var x = React.createElement(Composite, null, ',
152132
' this.props.children',
153133
');'
@@ -156,13 +136,11 @@ describe('react jsx', function() {
156136

157137
// Try with a single composite child, wrapped in a composite.
158138
code = [
159-
'/**@jsx React.DOM*/',
160139
'var x = <Composite>',
161140
' <Composite2 />',
162141
'</Composite>;'
163142
].join('\n');
164143
result = [
165-
'/**@jsx React.DOM*/',
166144
'var x = React.createElement(Composite, null, ',
167145
' React.createElement(Composite2, null)',
168146
');'
@@ -172,7 +150,6 @@ describe('react jsx', function() {
172150

173151
it('should insert commas after expressions before whitespace', function() {
174152
var code = [
175-
'/**@jsx React.DOM*/',
176153
'var x =',
177154
' <div',
178155
' attr1={',
@@ -192,9 +169,8 @@ describe('react jsx', function() {
192169
' </div>;'
193170
].join('\n');
194171
var result = [
195-
'/**@jsx React.DOM*/',
196172
'var x =',
197-
' React.createElement(React.DOM.div, {',
173+
' React.createElement("div", {',
198174
' attr1: ',
199175
' "foo" + "bar", ',
200176
' ',
@@ -217,9 +193,6 @@ describe('react jsx', function() {
217193

218194
it('should properly handle comments adjacent to children', function() {
219195
var code = [
220-
'/**',
221-
' * @jsx React.DOM',
222-
' */',
223196
'var x = (',
224197
' <div>',
225198
' {/* A comment at the beginning */}',
@@ -235,18 +208,15 @@ describe('react jsx', function() {
235208
');'
236209
].join('\n');
237210
var result = [
238-
'/**',
239-
' * @jsx React.DOM',
240-
' */',
241211
'var x = (',
242-
' React.createElement(React.DOM.div, null, ',
212+
' React.createElement("div", null, ',
243213
' /* A comment at the beginning */',
244214
' /* A second comment at the beginning */',
245-
' React.createElement(React.DOM.span, null',
215+
' React.createElement("span", null',
246216
' /* A nested comment */',
247217
' ), ',
248218
' /* A sandwiched comment */',
249-
' React.createElement(React.DOM.br, null)',
219+
' React.createElement("br", null)',
250220
' /* A comment at the end */',
251221
' /* A second comment at the end */',
252222
' )',
@@ -258,9 +228,6 @@ describe('react jsx', function() {
258228

259229
it('should properly handle comments between props', function() {
260230
var code = [
261-
'/**',
262-
' * @jsx React.DOM',
263-
' */',
264231
'var x = (',
265232
' <div',
266233
' /* a multi-line',
@@ -273,15 +240,12 @@ describe('react jsx', function() {
273240
');'
274241
].join('\n');
275242
var result = [
276-
'/**',
277-
' * @jsx React.DOM',
278-
' */',
279243
'var x = (',
280-
' React.createElement(React.DOM.div, {',
244+
' React.createElement("div", {',
281245
' /* a multi-line',
282246
' comment */',
283247
' attr1: "foo"}, ',
284-
' React.createElement(React.DOM.span, {// a double-slash comment',
248+
' React.createElement("span", {// a double-slash comment',
285249
' attr2: "bar"}',
286250
' )',
287251
' )',
@@ -293,43 +257,35 @@ describe('react jsx', function() {
293257

294258
it('should not strip tags with a single child of &nbsp;', function() {
295259
var code = [
296-
'/**',
297-
' * @jsx React.DOM',
298-
' */',
299260
'<div>&nbsp;</div>;'
300261
].join('\n');
301262
var result = [
302-
'/**',
303-
' * @jsx React.DOM',
304-
' */',
305-
'React.createElement(React.DOM.div, null, "\u00A0");'
263+
'React.createElement("div", null, "\u00A0");'
306264
].join('\n');
307265

308266
expect(transform(code).code).toBe(result);
309267
});
310268

311269
it('should not strip &nbsp; even coupled with other whitespace', function() {
312270
var code = [
313-
'/**',
314-
' * @jsx React.DOM',
315-
' */',
316271
'<div>&nbsp; </div>;'
317272
].join('\n');
318273
var result = [
319-
'/**',
320-
' * @jsx React.DOM',
321-
' */',
322-
'React.createElement(React.DOM.div, null, "\u00A0 ");'
274+
'React.createElement("div", null, "\u00A0 ");'
323275
].join('\n');
324276

325277
expect(transform(code).code).toBe(result);
326278
});
327279

328280
it('should handle hasOwnProperty correctly', function() {
329281
var code = '<hasOwnProperty>testing</hasOwnProperty>;';
330-
var result = 'React.createElement(hasOwnProperty, null, "testing");';
282+
// var result = 'React.createElement("hasOwnProperty", null, "testing");';
331283

332-
expect(transform(code).code).toBe(result);
284+
// expect(transform(code).code).toBe(result);
285+
286+
// This is currently not supported, and will generate a string tag in
287+
// a follow up.
288+
expect(() => transform(code)).toThrow();
333289
});
334290

335291
it('should allow constructor as prop', function() {
@@ -347,29 +303,15 @@ describe('react jsx', function() {
347303
});
348304

349305
it('should allow deeper JS namespacing', function() {
350-
var code = [
351-
'/**',
352-
' * @jsx React.DOM',
353-
' */',
354-
'<Namespace.DeepNamespace.Component />;'
355-
].join('\n');
356-
var result = [
357-
'/**',
358-
' * @jsx React.DOM',
359-
' */',
360-
'React.createElement(Namespace.DeepNamespace.Component, null);'
361-
].join('\n');
306+
var code = '<Namespace.DeepNamespace.Component />;';
307+
var result =
308+
'React.createElement(Namespace.DeepNamespace.Component, null);';
362309

363310
expect(transform(code).code).toBe(result);
364311
});
365312

366313
it('should disallow XML namespacing', function() {
367-
var code = [
368-
'/**',
369-
' * @jsx React.DOM',
370-
' */',
371-
'<Namespace:Component />;'
372-
].join('\n');
314+
var code = '<Namespace:Component />;';
373315

374316
expect(() => transform(code)).toThrow();
375317
});
@@ -399,18 +341,8 @@ describe('react jsx', function() {
399341
});
400342

401343
it('should transform known hyphenated tags', function() {
402-
var code = [
403-
'/**',
404-
' * @jsx React.DOM',
405-
' */',
406-
'<font-face />;'
407-
].join('\n');
408-
var result = [
409-
'/**',
410-
' * @jsx React.DOM',
411-
' */',
412-
'React.createElement(React.DOM[\'font-face\'], null);'
413-
].join('\n');
344+
var code = '<font-face />;';
345+
var result = 'React.createElement("font-face", null);';
414346

415347
expect(transform(code).code).toBe(result);
416348
});
@@ -422,12 +354,7 @@ describe('react jsx', function() {
422354
});
423355

424356
it('should throw for unknown hyphenated tags', function() {
425-
var code = [
426-
'/**',
427-
' * @jsx React.DOM',
428-
' */',
429-
'<x-component />;'
430-
].join('\n');
357+
var code = '<x-component />;';
431358

432359
expect(() => transform(code)).toThrow();
433360
});

0 commit comments

Comments
 (0)