Skip to content

Commit b5a11a4

Browse files
Jeff MorrisonJeff Morrison
authored andcommitted
Merge pull request facebook#336 from spicyj/jsx-spacing
JSX: Respect original spacing and newlines better
2 parents 832d9de + f69112c commit b5a11a4

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

vendor/fbtransform/transforms/react.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
var Syntax = require('esprima-fb').Syntax;
2020

2121
var catchup = require('jstransform/src/utils').catchup;
22+
var catchupWhiteSpace = require('jstransform/src/utils').catchupWhiteSpace;
2223
var append = require('jstransform/src/utils').append;
2324
var move = require('jstransform/src/utils').move;
2425
var getDocblock = require('jstransform/src/utils').getDocblock;
@@ -105,19 +106,21 @@ function visitReactTag(traverse, object, path, state) {
105106
if (!isLast) {
106107
append(',', state);
107108
}
108-
} else if (JSX_ATTRIBUTE_TRANSFORMS[attr.name.name]) {
109-
move(attr.value.range[0], state);
110-
append(JSX_ATTRIBUTE_TRANSFORMS[attr.name.name](attr), state);
111-
move(attr.value.range[1], state);
112-
if (!isLast) {
113-
append(',', state);
114-
}
115-
} else if (attr.value.type === Syntax.Literal) {
116-
move(attr.value.range[0], state);
117-
renderXJSLiteral(attr.value, isLast, state);
118109
} else {
119-
move(attr.value.range[0], state);
120-
renderXJSExpressionContainer(traverse, attr.value, isLast, path, state);
110+
move(attr.name.range[1], state);
111+
// Use catchupWhiteSpace to skip over the '=' in the attribute
112+
catchupWhiteSpace(attr.value.range[0], state);
113+
if (JSX_ATTRIBUTE_TRANSFORMS[attr.name.name]) {
114+
append(JSX_ATTRIBUTE_TRANSFORMS[attr.name.name](attr), state);
115+
move(attr.value.range[1], state);
116+
if (!isLast) {
117+
append(',', state);
118+
}
119+
} else if (attr.value.type === Syntax.Literal) {
120+
renderXJSLiteral(attr.value, isLast, state);
121+
} else {
122+
renderXJSExpressionContainer(traverse, attr.value, isLast, path, state);
123+
}
121124
}
122125

123126
if (isLast) {

vendor/fbtransform/transforms/xjs.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ function trimWithSingleSpace(string) {
172172
function renderXJSLiteral(object, isLast, state, start, end) {
173173
/** Added blank check filtering and triming*/
174174
var trimmedChildValue = safeTrim(object.value);
175+
var hasFinalNewLine = false;
175176

176177
if (trimmedChildValue) {
177178
// head whitespace
@@ -191,7 +192,7 @@ function renderXJSLiteral(object, isLast, state, start, end) {
191192
});
192193

193194
var hasInitialNewLine = initialLines[0] !== lines[0];
194-
var hasFinalNewLine =
195+
hasFinalNewLine =
195196
initialLines[initialLines.length - 1] !== lines[lines.length - 1];
196197

197198
var numLines = lines.length;
@@ -203,20 +204,18 @@ function renderXJSLiteral(object, isLast, state, start, end) {
203204
} else {
204205
var preString = '';
205206
var postString = '';
206-
var leading = '';
207+
var leading = line.match(/^[ \t]*/)[0];
207208

208209
if (ii === 0) {
209210
if (hasInitialNewLine) {
210211
preString = ' ';
211-
leading = '\n';
212+
leading = '\n' + leading;
212213
}
213214
if (trimmedChildValueWithSpace.substring(0, 1) === ' ') {
214215
// If this is the first line, and the original content starts with
215216
// whitespace, place a single space at the beginning.
216217
preString = ' ';
217218
}
218-
} else {
219-
leading = line.match(/^[ \t]*/)[0];
220219
}
221220
if (!lastLine || trimmedChildValueWithSpace.substr(
222221
trimmedChildValueWithSpace.length - 1, 1) === ' ' ||
@@ -256,6 +255,9 @@ function renderXJSLiteral(object, isLast, state, start, end) {
256255
}
257256

258257
// tail whitespace
258+
if (hasFinalNewLine) {
259+
append('\n', state);
260+
}
259261
append(object.value.match(/[ \t]*$/)[0], state);
260262
move(object.range[1], state);
261263
}

0 commit comments

Comments
 (0)