Skip to content

Commit 7860554

Browse files
committed
Merge pull request #3129
Put comma after any non-whitespace non-comment characters in JSXExpression
2 parents 70f16cc + ef79679 commit 7860554

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,19 @@ describe('react jsx', function() {
324324
expect(transform(code).code).toBe(result);
325325
});
326326

327+
it('handles overparenthesized JS', function() {
328+
var code =
329+
'<foo a={(b)} c={(d)}>Foo {(e+f //A line comment\n' +
330+
'/* A multiline comment */)\n' +
331+
'} bar\n' +
332+
'</foo>';
333+
var result = 'React.createElement("foo", {a: (b), c: (d)}, "Foo ", (e+f //A line comment\n' +
334+
'/* A multiline comment */), \n' +
335+
'" bar"\n' +
336+
')';
337+
expect(transform(code).code).toBe(result);
338+
});
339+
327340
it('should transform known hyphenated tags', function() {
328341
var code = '<font-face />;';
329342
var result = 'React.createElement("font-face", null);';

vendor/fbtransform/transforms/jsx.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,33 @@
1111
var Syntax = require('jstransform').Syntax;
1212
var utils = require('jstransform/src/utils');
1313

14+
function commaAfterLastParen(value) {
15+
var state = 'normal';
16+
var commaPos = 0;
17+
for (var i = 0; i < value.length; ++i) {
18+
if (state === 'normal') {
19+
if (value.substr(i, 2) === '//') {
20+
state = 'singleline';
21+
i += 1;
22+
} else if (value.substr(i, 2) === '/*') {
23+
state = 'multiline';
24+
i += 1;
25+
} else if (value.charAt(i).trim() !== '') {
26+
commaPos = i + 1;
27+
}
28+
} else if (state === 'singleline' && value.charAt(i) === '\n') {
29+
state = 'normal';
30+
} else if (state === 'multiline' &&
31+
value.charAt(i) === '*' &&
32+
i + 1 < value.length &&
33+
value.charAt(i + 1) === '/') {
34+
i += 1;
35+
state = 'normal';
36+
}
37+
}
38+
return value.substring(0, commaPos) + ', ' + trimLeft(value.substring(commaPos));
39+
}
40+
1441
function renderJSXLiteral(object, isLast, state, start, end) {
1542
var lines = object.value.split(/\r\n|\n|\r/);
1643

@@ -84,11 +111,11 @@ function renderJSXExpressionContainer(traverse, object, isLast, path, state) {
84111
if (!isLast && object.expression.type !== Syntax.JSXEmptyExpression) {
85112
// If we need to append a comma, make sure to do so after the expression.
86113
utils.catchup(object.expression.range[1], state, trimLeft);
87-
utils.append(', ', state);
114+
utils.catchup(object.range[1] - 1, state, commaAfterLastParen);
115+
} else {
116+
// Minus 1 to skip `}`.
117+
utils.catchup(object.range[1] - 1, state, trimLeft);
88118
}
89-
90-
// Minus 1 to skip `}`.
91-
utils.catchup(object.range[1] - 1, state, trimLeft);
92119
utils.move(object.range[1], state);
93120
return false;
94121
}

0 commit comments

Comments
 (0)