inherit string quotes - fixes #991

This commit is contained in:
Sebastian McKenzie 2015-03-31 02:09:56 +11:00
parent b8f2a27e90
commit 153e81261c
11 changed files with 63 additions and 9 deletions

View File

@ -99,5 +99,12 @@ export function _stringLiteral(val) {
return "\\u" + ("0000" + c.charCodeAt(0).toString(16)).slice(-4); return "\\u" + ("0000" + c.charCodeAt(0).toString(16)).slice(-4);
}); });
if (this.format.quotes === "single") {
val = val.slice(1, -1);
val = val.replace(/\\"/g, '"');
val = val.replace(/'/g, "\\'");
val = `'${val}'`;
}
this.push(val); this.push(val);
} }

View File

@ -16,7 +16,7 @@ class CodeGenerator {
this.comments = ast.comments || []; this.comments = ast.comments || [];
this.tokens = ast.tokens || []; this.tokens = ast.tokens || [];
this.format = CodeGenerator.normalizeOptions(code, opts); this.format = CodeGenerator.normalizeOptions(code, opts, this.tokens);
this.opts = opts; this.opts = opts;
this.ast = ast; this.ast = ast;
@ -26,7 +26,7 @@ class CodeGenerator {
this.buffer = new Buffer(this.position, this.format); this.buffer = new Buffer(this.position, this.format);
} }
static normalizeOptions(code, opts) { static normalizeOptions(code, opts, tokens) {
var style = " "; var style = " ";
if (code) { if (code) {
var indent = detectIndent(code).indent; var indent = detectIndent(code).indent;
@ -36,6 +36,7 @@ class CodeGenerator {
var format = { var format = {
comments: opts.comments == null || opts.comments, comments: opts.comments == null || opts.comments,
compact: opts.compact, compact: opts.compact,
quotes: CodeGenerator.findCommonStringDelimeter(code, tokens),
indent: { indent: {
adjustMultilineComment: true, adjustMultilineComment: true,
style: style, style: style,
@ -54,6 +55,36 @@ class CodeGenerator {
return format; return format;
} }
static findCommonStringDelimeter(code, tokens) {
var occurences = {
single: 0,
double: 0
};
var checked = 0;
for (var i = 0; i < tokens.length; i++) {
var token = tokens[i];
if (token.type.label !== "string") continue;
if (checked >= 3) continue;
var raw = code.slice(token.start, token.end);
if (raw[0] === "'") {
occurences.single++;
} else {
occurences.double++;
}
checked++;
}
if (occurences.single > occurences.double) {
return "single";
} else {
return "double";
}
}
static generators = { static generators = {
templateLiterals: require("./generators/template-literals"), templateLiterals: require("./generators/template-literals"),
comprehensions: require("./generators/comprehensions"), comprehensions: require("./generators/comprehensions"),

View File

@ -0,0 +1,4 @@
foo("foo");
foo("foo\nlol");
foo("foo\n\"lol");
foo("foo\n\"'lol");

View File

@ -0,0 +1,4 @@
foo("foo");
foo("foo\nlol");
foo("foo\n\"lol");
foo("foo\n\"'lol");

View File

@ -0,0 +1,4 @@
foo('foo');
foo('foo\nlol');
foo('foo\n"lol');
foo('foo\n"\'lol');

View File

@ -0,0 +1,4 @@
foo('foo');
foo('foo\nlol');
foo('foo\n"lol');
foo('foo\n"\'lol');

View File

@ -3,13 +3,13 @@ function somethingAdvanced({topLeft: {x: x1, y: y1}, bottomRight: {x: x2, y: y2}
} }
function unpackObject({title: title, author: author}) { function unpackObject({title: title, author: author}) {
return title + ' ' + author; return title + " " + author;
} }
console.log(unpackObject({title: 'title', author: 'author'})); console.log(unpackObject({title: "title", author: "author"}));
var unpackArray = function ([a, b, c], [x, y, z]) { var unpackArray = function ([a, b, c], [x, y, z]) {
return a+b+c; return a+b+c;
}; };
console.log(unpackArray(['hello', ', ', 'world'], [1, 2, 3])); console.log(unpackArray(["hello", ", ", "world"], [1, 2, 3]));

View File

@ -1,4 +1,4 @@
const A = 'a'; const A = "a";
const o = { const o = {
A // comment A // comment
}; };

View File

@ -1,2 +1,2 @@
var string = 'foo💩bar'; var string = "foo💩bar";
var match = string.match(/foo(.)bar/u); var match = string.match(/foo(.)bar/u);

View File

@ -1,2 +1,2 @@
var string = 'foo💩bar'; var string = "foo💩bar";
var match = string.match(/foo(.)bar/); var match = string.match(/foo(.)bar/);

View File

@ -1,7 +1,7 @@
var x = var x =
<div> <div>
foo foo
{'bar'} {"bar"}
baz baz
<div> <div>
buz buz