@samitouri / QOS-React-2 / commits / ef796790ec

Put comma after any non-comments, non-whitespace in JSXEspression

Fixes #1673 Closes #3129

Jason Miller committed Feb 12, 2015 at 08:57 UTC ef796790ec80c200bfc99df69e23db2522479272
2 files changed +44 -4
vendor/fbtransform/transforms/__tests__/react-test.js
+13
@@ -324,6 +324,19 @@ describe('react jsx', function() {
324 expect(transform(code).code).toBe(result);
325 });
326
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 +
340 it('should transform known hyphenated tags', function() {
341 var code = '<font-face />;';
342 var result = 'React.createElement("font-face", null);';
vendor/fbtransform/transforms/jsx.js
+31 -4
@@ -11,6 +11,33 @@
11 var Syntax = require('jstransform').Syntax;
12 var utils = require('jstransform/src/utils');
13
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 +
41 function renderJSXLiteral(object, isLast, state, start, end) {
42 var lines = object.value.split(/\r\n|\n|\r/);
43
@@ -84,11 +111,11 @@ function renderJSXExpressionContainer(traverse, object, isLast, path, state) {
111 if (!isLast && object.expression.type !== Syntax.JSXEmptyExpression) {
112 // If we need to append a comma, make sure to do so after the expression.
113 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);
118 }
89 -
90 - // Minus 1 to skip `}`.
91 - utils.catchup(object.range[1] - 1, state, trimLeft);
119 utils.move(object.range[1], state);
120 return false;
121 }