diff --git a/src/babel/generation/generators/types.js b/src/babel/generation/generators/types.js index 9d704ab22e..400f9508b7 100644 --- a/src/babel/generation/generators/types.js +++ b/src/babel/generation/generators/types.js @@ -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); } diff --git a/src/babel/generation/index.js b/src/babel/generation/index.js index 4a52df5a40..5f1d21822d 100644 --- a/src/babel/generation/index.js +++ b/src/babel/generation/index.js @@ -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"), diff --git a/test/core/fixtures/generation/auto-string/double/actual.js b/test/core/fixtures/generation/auto-string/double/actual.js new file mode 100644 index 0000000000..5a8fcf7e29 --- /dev/null +++ b/test/core/fixtures/generation/auto-string/double/actual.js @@ -0,0 +1,4 @@ +foo("foo"); +foo("foo\nlol"); +foo("foo\n\"lol"); +foo("foo\n\"'lol"); diff --git a/test/core/fixtures/generation/auto-string/double/expected.js b/test/core/fixtures/generation/auto-string/double/expected.js new file mode 100644 index 0000000000..5a8fcf7e29 --- /dev/null +++ b/test/core/fixtures/generation/auto-string/double/expected.js @@ -0,0 +1,4 @@ +foo("foo"); +foo("foo\nlol"); +foo("foo\n\"lol"); +foo("foo\n\"'lol"); diff --git a/test/core/fixtures/generation/auto-string/single/actual.js b/test/core/fixtures/generation/auto-string/single/actual.js new file mode 100644 index 0000000000..1d8e7531c9 --- /dev/null +++ b/test/core/fixtures/generation/auto-string/single/actual.js @@ -0,0 +1,4 @@ +foo('foo'); +foo('foo\nlol'); +foo('foo\n"lol'); +foo('foo\n"\'lol'); diff --git a/test/core/fixtures/generation/auto-string/single/expected.js b/test/core/fixtures/generation/auto-string/single/expected.js new file mode 100644 index 0000000000..1d8e7531c9 --- /dev/null +++ b/test/core/fixtures/generation/auto-string/single/expected.js @@ -0,0 +1,4 @@ +foo('foo'); +foo('foo\nlol'); +foo('foo\n"lol'); +foo('foo\n"\'lol'); diff --git a/test/core/fixtures/transformation/es6.destructuring/parameters/actual.js b/test/core/fixtures/transformation/es6.destructuring/parameters/actual.js index 896a7ce416..4446360a01 100644 --- a/test/core/fixtures/transformation/es6.destructuring/parameters/actual.js +++ b/test/core/fixtures/transformation/es6.destructuring/parameters/actual.js @@ -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])); diff --git a/test/core/fixtures/transformation/es6.properties.shorthand/shorthand-comments/actual.js b/test/core/fixtures/transformation/es6.properties.shorthand/shorthand-comments/actual.js index a6a10d4689..a3cca2f02e 100644 --- a/test/core/fixtures/transformation/es6.properties.shorthand/shorthand-comments/actual.js +++ b/test/core/fixtures/transformation/es6.properties.shorthand/shorthand-comments/actual.js @@ -1,4 +1,4 @@ -const A = 'a'; +const A = "a"; const o = { A // comment }; diff --git a/test/core/fixtures/transformation/es6.regex.unicode/basic/actual.js b/test/core/fixtures/transformation/es6.regex.unicode/basic/actual.js index 0b4203cf17..ac9137a21c 100644 --- a/test/core/fixtures/transformation/es6.regex.unicode/basic/actual.js +++ b/test/core/fixtures/transformation/es6.regex.unicode/basic/actual.js @@ -1,2 +1,2 @@ -var string = 'foo💩bar'; +var string = "foo💩bar"; var match = string.match(/foo(.)bar/u); diff --git a/test/core/fixtures/transformation/es6.regex.unicode/ignore-non-unicode/actual.js b/test/core/fixtures/transformation/es6.regex.unicode/ignore-non-unicode/actual.js index df0cdc0df2..3bf193d0ac 100644 --- a/test/core/fixtures/transformation/es6.regex.unicode/ignore-non-unicode/actual.js +++ b/test/core/fixtures/transformation/es6.regex.unicode/ignore-non-unicode/actual.js @@ -1,2 +1,2 @@ -var string = 'foo💩bar'; +var string = "foo💩bar"; var match = string.match(/foo(.)bar/); diff --git a/test/core/fixtures/transformation/react/concatenates-adjacent-string-literals/actual.js b/test/core/fixtures/transformation/react/concatenates-adjacent-string-literals/actual.js index e998a87c31..f3f30af044 100644 --- a/test/core/fixtures/transformation/react/concatenates-adjacent-string-literals/actual.js +++ b/test/core/fixtures/transformation/react/concatenates-adjacent-string-literals/actual.js @@ -1,7 +1,7 @@ var x =