Skip to content
Merged
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
11 changes: 9 additions & 2 deletions lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ var defaults = {

// If you want esprima not to throw exceptions when it encounters
// non-fatal errors, keep this option true.
tolerant: true
tolerant: true,

// If you want to override the quotes used in string literals, specify
// either "single", "double", or "auto" here ("auto" will select the one
// which results in the shorter literal)
// Otherwise, the input marks will be preserved
quote: null,
}, hasOwn = defaults.hasOwnProperty;

// Copy options and fill in default values.
Expand All @@ -73,6 +79,7 @@ exports.normalize = function(options) {
inputSourceMap: get("inputSourceMap"),
esprima: get("esprima"),
range: get("range"),
tolerant: get("tolerant")
tolerant: get("tolerant"),
quote: get("quote"),
};
};
22 changes: 19 additions & 3 deletions lib/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ function genericPrintNoParens(path, options, print) {

case "ModuleSpecifier":
// A ModuleSpecifier is a string-valued Literal.
return fromString(nodeStr(n), options);
return fromString(nodeStr(n, options), options);

case "UnaryExpression":
var parts = [n.operator];
Expand Down Expand Up @@ -1283,10 +1283,26 @@ function endsWithBrace(lines) {
return lastNonSpaceCharacter(lines) === "}";
}

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

function nodeStr(n, options) {
namedTypes.Literal.assert(n);
isString.assert(n.value);
return JSON.stringify(n.value);
switch (options.quote) {
case "auto":
var double = JSON.stringify(n.value);
var single = swapQuotes(JSON.stringify(swapQuotes(n.value)));
return double.length > single.length ? single : double;
case "single":
return swapQuotes(JSON.stringify(swapQuotes(n.value)));
case "double":
default:
return JSON.stringify(n.value);
}
}

function maybeAddSemicolon(lines) {
Expand Down
36 changes: 36 additions & 0 deletions test/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -622,4 +622,40 @@ describe("printer", function() {
"}"
].join("\n"));
});

it("should print string literals with the specified delimiter", function() {
var ast = parse([
"var obj = {",
" \"foo's\": 'bar',",
" '\"bar\\'s\"': /regex/m",
"};"
].join("\n"));

var variableDeclaration = ast.program.body[0];
n.VariableDeclaration.assert(variableDeclaration);

var printer = new Printer({ quote: "single" });
assert.strictEqual(printer.printGenerically(ast).code, [
"var obj = {",
" 'foo\\'s': 'bar',",
" '\"bar\\'s\"': /regex/m",
"};"
].join("\n"));

var printer2 = new Printer({ quote: "double" });
assert.strictEqual(printer2.printGenerically(ast).code, [
"var obj = {",
" \"foo's\": \"bar\",",
' "\\"bar\'s\\"": /regex/m',
"};"
].join("\n"));

var printer3 = new Printer({ quote: "auto" });
assert.strictEqual(printer3.printGenerically(ast).code, [
"var obj = {",
' "foo\'s": "bar",',
' \'"bar\\\'s"\': /regex/m',
"};"
].join("\n"));
});
});