mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Put comma after any non-comments, non-whitespace in JSXEspression
Fixes #1673 Closes #3129
This commit is contained in:
committed by
Paul O’Shannessy
parent
7fe5a3aadd
commit
ef796790ec
@@ -324,6 +324,19 @@ describe('react jsx', function() {
|
||||
expect(transform(code).code).toBe(result);
|
||||
});
|
||||
|
||||
it('handles overparenthesized JS', function() {
|
||||
var code =
|
||||
'<foo a={(b)} c={(d)}>Foo {(e+f //A line comment\n' +
|
||||
'/* A multiline comment */)\n' +
|
||||
'} bar\n' +
|
||||
'</foo>';
|
||||
var result = 'React.createElement("foo", {a: (b), c: (d)}, "Foo ", (e+f //A line comment\n' +
|
||||
'/* A multiline comment */), \n' +
|
||||
'" bar"\n' +
|
||||
')';
|
||||
expect(transform(code).code).toBe(result);
|
||||
});
|
||||
|
||||
it('should transform known hyphenated tags', function() {
|
||||
var code = '<font-face />;';
|
||||
var result = 'React.createElement("font-face", null);';
|
||||
|
||||
Vendored
+31
-4
@@ -11,6 +11,33 @@
|
||||
var Syntax = require('jstransform').Syntax;
|
||||
var utils = require('jstransform/src/utils');
|
||||
|
||||
function commaAfterLastParen(value) {
|
||||
var state = 'normal';
|
||||
var commaPos = 0;
|
||||
for (var i = 0; i < value.length; ++i) {
|
||||
if (state === 'normal') {
|
||||
if (value.substr(i, 2) === '//') {
|
||||
state = 'singleline';
|
||||
i += 1;
|
||||
} else if (value.substr(i, 2) === '/*') {
|
||||
state = 'multiline';
|
||||
i += 1;
|
||||
} else if (value.charAt(i).trim() !== '') {
|
||||
commaPos = i + 1;
|
||||
}
|
||||
} else if (state === 'singleline' && value.charAt(i) === '\n') {
|
||||
state = 'normal';
|
||||
} else if (state === 'multiline' &&
|
||||
value.charAt(i) === '*' &&
|
||||
i + 1 < value.length &&
|
||||
value.charAt(i + 1) === '/') {
|
||||
i += 1;
|
||||
state = 'normal';
|
||||
}
|
||||
}
|
||||
return value.substring(0, commaPos) + ', ' + trimLeft(value.substring(commaPos));
|
||||
}
|
||||
|
||||
function renderJSXLiteral(object, isLast, state, start, end) {
|
||||
var lines = object.value.split(/\r\n|\n|\r/);
|
||||
|
||||
@@ -84,11 +111,11 @@ function renderJSXExpressionContainer(traverse, object, isLast, path, state) {
|
||||
if (!isLast && object.expression.type !== Syntax.JSXEmptyExpression) {
|
||||
// If we need to append a comma, make sure to do so after the expression.
|
||||
utils.catchup(object.expression.range[1], state, trimLeft);
|
||||
utils.append(', ', state);
|
||||
utils.catchup(object.range[1] - 1, state, commaAfterLastParen);
|
||||
} else {
|
||||
// Minus 1 to skip `}`.
|
||||
utils.catchup(object.range[1] - 1, state, trimLeft);
|
||||
}
|
||||
|
||||
// Minus 1 to skip `}`.
|
||||
utils.catchup(object.range[1] - 1, state, trimLeft);
|
||||
utils.move(object.range[1], state);
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user