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

Add newline before JSX spread when appropriate

Previously, we were losing the newline before a JSX spread attribute which would cause the lines to not match up; this fixes the problem.

Ben Alpert committed Aug 30, 2014 at 01:04 UTC cc9735f8897df40a6c79343cbb6a77123b1df9ad
2 files changed +18 -4
vendor/fbtransform/transforms/__tests__/react-test.js
+14 -1
@@ -379,12 +379,25 @@ describe('react jsx', function() {
379 '<Component { ... x } y\n' +
380 '={2 } z />';
381 var result =
382 - 'React.createElement(Component, Object.assign({}, x , {y: \n' +
382 + 'React.createElement(Component, Object.assign({}, x , {y: \n' +
383 '2, z: true}))';
384
385 expect(transform(code).code).toBe(result);
386 });
387
388 + it('adds appropriate newlines when using spread attribute', function() {
389 + var code =
390 + '<Component\n' +
391 + ' {...this.props}\n' +
392 + ' sound="moo" />';
393 + var result =
394 + 'React.createElement(Component, Object.assign({}, \n' +
395 + ' this.props, \n' +
396 + ' {sound: "moo"}))';
397 +
398 + expect(transform(code).code).toBe(result);
399 + });
400 +
401 it('should transform known hyphenated tags', function() {
402 var code = [
403 '/**',
vendor/fbtransform/transforms/react.js
+4 -3
@@ -142,16 +142,17 @@ function visitReactTag(traverse, object, path, state) {
142 var isLast = index === attributesObject.length - 1;
143
144 if (attr.type === Syntax.XJSSpreadAttribute) {
145 - // Plus 1 to skip `{`.
146 - utils.move(attr.range[0] + 1, state);
147 -
145 // Close the previous object or initial object
146 if (!previousWasSpread) {
147 utils.append('}, ', state);
148 }
149
150 +
151 // Move to the expression start, ignoring everything except parenthesis
152 // and whitespace.
153 + utils.catchup(attr.range[0], state, stripNonWhiteParen);
154 + // Plus 1 to skip `{`.
155 + utils.move(attr.range[0] + 1, state);
156 utils.catchup(attr.argument.range[0], state, stripNonWhiteParen);
157
158 traverse(attr.argument, path, state);