Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion bin/jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@ require('commoner').version(
}).option(
'--harmony',
'Turns on JS transformations such as ES6 Classes etc.'
).option(
'--use-single-quotes',
'Uses single quotes for transform output.'
).process(function(id, source) {
// This is where JSX, ES6, etc. desugaring happens.
var visitorList;

if (this.options.harmony) {
visitorList = visitors.getAllVisitors();
} else {
visitorList = visitors.transformVisitors.react;
}
return transform(visitorList, source).code;
return transform(visitorList, source, {
useSingleQuotes: this.options.useSingleQuotes
}).code;
});
2 changes: 1 addition & 1 deletion vendor/fbtransform/transforms/react.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function visitReactTag(traverse, object, path, state) {
var isLast = index === attributesObject.length - 1;

utils.catchup(attr.range[0], state, trimLeft);
utils.append(quoteAttrName(name), state);
utils.append(quoteAttrName(name, state.g.opts.useSingleQuotes), state);
utils.append(': ', state);

if (!attr.value) {
Expand Down
6 changes: 5 additions & 1 deletion vendor/fbtransform/transforms/reactDisplayName.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

var Syntax = require('esprima-fb').Syntax;
var utils = require('jstransform/src/utils');
var doubleOrSingleQuotes = require('./xjs').doubleOrSingleQuotes;

function addDisplayName(displayName, object, state) {
if (object &&
Expand All @@ -40,7 +41,10 @@ function addDisplayName(displayName, object, state) {

if (safe) {
utils.catchup(object['arguments'][0].range[0] + 1, state);
utils.append("displayName: '" + displayName + "',", state);
utils.append(
'displayName: ' + doubleOrSingleQuotes(displayName, state) + ',',
state
);
}
}
}
Expand Down
25 changes: 21 additions & 4 deletions vendor/fbtransform/transforms/xjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,12 @@ function renderXJSLiteral(object, isLast, state, start, end) {

if (trimmedLine || isLastNonEmptyLine) {
utils.append(
JSON.stringify(trimmedLine) +
state.g.opts.useSingleQuotes ? swapQuotes(
JSON.stringify(swapQuotes(trimmedLine))
) : JSON.stringify(trimmedLine) +
(!isLastNonEmptyLine ? " + ' ' +" : ''),
state);
state
);

if (isLastNonEmptyLine) {
if (end) {
Expand Down Expand Up @@ -234,10 +237,23 @@ function renderXJSExpressionContainer(traverse, object, isLast, path, state) {
return false;
}

function quoteAttrName(attr) {
function doubleOrSingleQuotes(value, state) {
if (state && state.g.opts.useSingleQuotes) {
return '\'' + value + '\'';
}
return '"' + value + '"';
}

function swapQuotes(str) {
return str.replace(/['"]/g, function(m) {
return m === '"' ? '\'' : '"';
});
}

function quoteAttrName(attr, state) {
// Quote invalid JS identifiers.
if (!/^[a-z_$][a-z\d_$]*$/i.test(attr)) {
return "'" + attr + "'";
return doubleOrSingleQuotes(attr, state);
}
return attr;
}
Expand All @@ -250,4 +266,5 @@ exports.knownTags = knownTags;
exports.renderXJSExpressionContainer = renderXJSExpressionContainer;
exports.renderXJSLiteral = renderXJSLiteral;
exports.quoteAttrName = quoteAttrName;
exports.doubleOrSingleQuotes = doubleOrSingleQuotes;
exports.trimLeft = trimLeft;