Skip to content

Commit 60d7a02

Browse files
committed
Normalize whitespace for transformed JSX code
1 parent 6fd5381 commit 60d7a02

2 files changed

Lines changed: 40 additions & 32 deletions

File tree

vendor/fbtransform/transforms/react.js

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ var renderXJSExpressionContainer =
2525
var renderXJSLiteral = require('./xjs').renderXJSLiteral;
2626
var quoteAttrName = require('./xjs').quoteAttrName;
2727

28+
var trimLeft = require('./xjs').trimLeft;
29+
2830
/**
2931
* Customized desugar processor.
3032
*
@@ -53,7 +55,7 @@ function visitReactTag(traverse, object, path, state) {
5355
var nameObject = openingElement.name;
5456
var attributesObject = openingElement.attributes;
5557

56-
utils.catchup(openingElement.range[0], state);
58+
utils.catchup(openingElement.range[0], state, trimLeft);
5759

5860
if (nameObject.namespace) {
5961
throw new Error(
@@ -68,44 +70,43 @@ function visitReactTag(traverse, object, path, state) {
6870

6971
utils.move(nameObject.range[1], state);
7072

73+
var hasAttributes = attributesObject.length;
74+
7175
// if we don't have any attributes, pass in null
72-
if (attributesObject.length === 0) {
76+
if (hasAttributes) {
77+
utils.append('{', state);
78+
} else {
7379
utils.append('null', state);
7480
}
7581

7682
// write attributes
7783
attributesObject.forEach(function(attr, index) {
78-
utils.catchup(attr.range[0], state);
7984
if (attr.name.namespace) {
8085
throw new Error(
8186
'Namespace attributes are not supported. ReactJSX is not XML.');
8287
}
8388
var name = attr.name.name;
84-
var isFirst = index === 0;
8589
var isLast = index === attributesObject.length - 1;
8690

87-
if (isFirst) {
88-
utils.append('{', state);
89-
}
90-
91+
utils.catchup(attr.range[0], state, trimLeft);
9192
utils.append(quoteAttrName(name), state);
92-
utils.append(':', state);
93+
utils.append(': ', state);
9394

9495
if (!attr.value) {
9596
state.g.buffer += 'true';
9697
state.g.position = attr.name.range[1];
9798
if (!isLast) {
98-
utils.append(',', state);
99+
utils.append(', ', state);
99100
}
100101
} else {
101102
utils.move(attr.name.range[1], state);
102-
// Use catchupWhiteSpace to skip over the '=' in the attribute
103-
utils.catchupWhiteSpace(attr.value.range[0], state);
103+
// Use catchupNewlines to skip over the '=' in the attribute
104+
utils.catchupNewlines(attr.value.range[0], state);
104105
if (JSX_ATTRIBUTE_TRANSFORMS.hasOwnProperty(attr.name.name)) {
105106
utils.append(JSX_ATTRIBUTE_TRANSFORMS[attr.name.name](attr), state);
106107
utils.move(attr.value.range[1], state);
107108
if (!isLast) {
108-
utils.append(',', state);
109+
utils.append(', ', state);
109110
}
110111
} else if (attr.value.type === Syntax.Literal) {
111112
renderXJSLiteral(attr.value, isLast, state);
@@ -114,18 +115,18 @@ function visitReactTag(traverse, object, path, state) {
114115
}
115116
}
116117

117-
if (isLast) {
118-
utils.append('}', state);
119-
}
120-
121-
utils.catchup(attr.range[1], state);
118+
utils.catchup(attr.range[1], state, trimLeft);
122119
});
123120

124121
if (!openingElement.selfClosing) {
125-
utils.catchup(openingElement.range[1] - 1, state);
122+
utils.catchup(openingElement.range[1] - 1, state, trimLeft);
126123
utils.move(openingElement.range[1], state);
127124
}
128125

126+
if (hasAttributes) {
127+
utils.append('}', state);
128+
}
129+
129130
// filter out whitespace
130131
var childrenToRender = object.children.filter(function(child) {
131132
return !(child.type === Syntax.Literal
@@ -147,7 +148,7 @@ function visitReactTag(traverse, object, path, state) {
147148
}
148149

149150
childrenToRender.forEach(function(child, index) {
150-
utils.catchup(child.range[0], state);
151+
utils.catchup(child.range[0], state, trimLeft);
151152

152153
var isLast = index >= lastRenderableIndex;
153154

@@ -158,22 +159,21 @@ function visitReactTag(traverse, object, path, state) {
158159
} else {
159160
traverse(child, path, state);
160161
if (!isLast) {
161-
utils.append(',', state);
162-
state.g.buffer = state.g.buffer.replace(/(\s*),$/, ',$1');
162+
utils.append(', ', state);
163163
}
164164
}
165165

166-
utils.catchup(child.range[1], state);
166+
utils.catchup(child.range[1], state, trimLeft);
167167
});
168168
}
169169

170170
if (openingElement.selfClosing) {
171171
// everything up to />
172-
utils.catchup(openingElement.range[1] - 2, state);
172+
utils.catchup(openingElement.range[1] - 2, state, trimLeft);
173173
utils.move(openingElement.range[1], state);
174174
} else {
175175
// everything up to </ sdflksjfd>
176-
utils.catchup(object.closingElement.range[0], state);
176+
utils.catchup(object.closingElement.range[0], state, trimLeft);
177177
utils.move(object.closingElement.range[1], state);
178178
}
179179

vendor/fbtransform/transforms/xjs.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,25 +180,27 @@ function renderXJSLiteral(object, isLast, state, start, end) {
180180
trimmedLine = trimmedLine.replace(/[ ]+$/, '');
181181
}
182182

183-
utils.append(line.match(/^[ \t]*/)[0], state);
183+
if (!isFirstLine) {
184+
utils.append(line.match(/^[ \t]*/)[0], state);
185+
}
184186

185187
if (trimmedLine || isLastNonEmptyLine) {
186188
utils.append(
187189
JSON.stringify(trimmedLine) +
188-
(!isLastNonEmptyLine ? "+' '+" : ''),
190+
(!isLastNonEmptyLine ? " + ' ' +" : ''),
189191
state);
190192

191193
if (isLastNonEmptyLine) {
192194
if (end) {
193195
utils.append(end, state);
194196
}
195197
if (!isLast) {
196-
utils.append(',', state);
198+
utils.append(', ', state);
197199
}
198200
}
199201

200202
// only restore tail whitespace if line had literals
201-
if (trimmedLine) {
203+
if (trimmedLine && !isLastLine) {
202204
utils.append(line.match(/[ \t]*$/)[0], state);
203205
}
204206
}
@@ -215,14 +217,15 @@ function renderXJSExpressionContainer(traverse, object, isLast, path, state) {
215217
// Plus 1 to skip `{`.
216218
utils.move(object.range[0] + 1, state);
217219
traverse(object.expression, path, state);
220+
218221
if (!isLast && object.expression.type !== Syntax.XJSEmptyExpression) {
219222
// If we need to append a comma, make sure to do so after the expression.
220-
utils.catchup(object.expression.range[1], state);
221-
utils.append(',', state);
223+
utils.catchup(object.expression.range[1], state, trimLeft);
224+
utils.append(', ', state);
222225
}
223226

224227
// Minus 1 to skip `}`.
225-
utils.catchup(object.range[1] - 1, state);
228+
utils.catchup(object.range[1] - 1, state, trimLeft);
226229
utils.move(object.range[1], state);
227230
return false;
228231
}
@@ -235,7 +238,12 @@ function quoteAttrName(attr) {
235238
return attr;
236239
}
237240

241+
function trimLeft(value) {
242+
return value.replace(/^[ ]+/, '');
243+
}
244+
238245
exports.knownTags = knownTags;
239246
exports.renderXJSExpressionContainer = renderXJSExpressionContainer;
240247
exports.renderXJSLiteral = renderXJSLiteral;
241248
exports.quoteAttrName = quoteAttrName;
249+
exports.trimLeft = trimLeft;

0 commit comments

Comments
 (0)