inherit string quotes - fixes #991
This commit is contained in:
parent
b8f2a27e90
commit
153e81261c
@ -99,5 +99,12 @@ export function _stringLiteral(val) {
|
||||
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);
|
||||
}
|
||||
|
||||
@ -16,7 +16,7 @@ class CodeGenerator {
|
||||
|
||||
this.comments = ast.comments || [];
|
||||
this.tokens = ast.tokens || [];
|
||||
this.format = CodeGenerator.normalizeOptions(code, opts);
|
||||
this.format = CodeGenerator.normalizeOptions(code, opts, this.tokens);
|
||||
this.opts = opts;
|
||||
this.ast = ast;
|
||||
|
||||
@ -26,7 +26,7 @@ class CodeGenerator {
|
||||
this.buffer = new Buffer(this.position, this.format);
|
||||
}
|
||||
|
||||
static normalizeOptions(code, opts) {
|
||||
static normalizeOptions(code, opts, tokens) {
|
||||
var style = " ";
|
||||
if (code) {
|
||||
var indent = detectIndent(code).indent;
|
||||
@ -36,6 +36,7 @@ class CodeGenerator {
|
||||
var format = {
|
||||
comments: opts.comments == null || opts.comments,
|
||||
compact: opts.compact,
|
||||
quotes: CodeGenerator.findCommonStringDelimeter(code, tokens),
|
||||
indent: {
|
||||
adjustMultilineComment: true,
|
||||
style: style,
|
||||
@ -54,6 +55,36 @@ class CodeGenerator {
|
||||
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 = {
|
||||
templateLiterals: require("./generators/template-literals"),
|
||||
comprehensions: require("./generators/comprehensions"),
|
||||
|
||||
@ -0,0 +1,4 @@
|
||||
foo("foo");
|
||||
foo("foo\nlol");
|
||||
foo("foo\n\"lol");
|
||||
foo("foo\n\"'lol");
|
||||
@ -0,0 +1,4 @@
|
||||
foo("foo");
|
||||
foo("foo\nlol");
|
||||
foo("foo\n\"lol");
|
||||
foo("foo\n\"'lol");
|
||||
@ -0,0 +1,4 @@
|
||||
foo('foo');
|
||||
foo('foo\nlol');
|
||||
foo('foo\n"lol');
|
||||
foo('foo\n"\'lol');
|
||||
@ -0,0 +1,4 @@
|
||||
foo('foo');
|
||||
foo('foo\nlol');
|
||||
foo('foo\n"lol');
|
||||
foo('foo\n"\'lol');
|
||||
@ -3,13 +3,13 @@ function somethingAdvanced({topLeft: {x: x1, y: y1}, bottomRight: {x: x2, y: y2}
|
||||
}
|
||||
|
||||
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]) {
|
||||
return a+b+c;
|
||||
};
|
||||
|
||||
console.log(unpackArray(['hello', ', ', 'world'], [1, 2, 3]));
|
||||
console.log(unpackArray(["hello", ", ", "world"], [1, 2, 3]));
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
const A = 'a';
|
||||
const A = "a";
|
||||
const o = {
|
||||
A // comment
|
||||
};
|
||||
|
||||
@ -1,2 +1,2 @@
|
||||
var string = 'foo💩bar';
|
||||
var string = "foo💩bar";
|
||||
var match = string.match(/foo(.)bar/u);
|
||||
|
||||
@ -1,2 +1,2 @@
|
||||
var string = 'foo💩bar';
|
||||
var string = "foo💩bar";
|
||||
var match = string.match(/foo(.)bar/);
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
var x =
|
||||
<div>
|
||||
foo
|
||||
{'bar'}
|
||||
{"bar"}
|
||||
baz
|
||||
<div>
|
||||
buz
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user