Hardcode to double quotes, indent to 2 spaces (#5824)

This commit is contained in:
Henry Zhu
2017-06-05 23:14:37 -04:00
committed by Brian Ng
parent b0524427c1
commit 123dce5fcd
46 changed files with 276 additions and 322 deletions

View File

@@ -1,4 +1,3 @@
import detectIndent from "detect-indent";
import SourceMap from "./source-map";
import * as messages from "babel-messages";
import Printer from "./printer";
@@ -12,7 +11,7 @@ import type { Format } from "./printer";
class Generator extends Printer {
constructor(ast, opts = {}, code) {
const tokens = ast.tokens || [];
const format = normalizeOptions(code, opts, tokens);
const format = normalizeOptions(code, opts);
const map = opts.sourceMaps ? new SourceMap(opts, code) : null;
super(format, map, tokens);
@@ -39,13 +38,7 @@ class Generator extends Printer {
* - If `opts.compact = "auto"` and the code is over 500KB, `compact` will be set to `true`.
*/
function normalizeOptions(code, opts, tokens): Format {
let style = " ";
if (code && typeof code === "string") {
const indent = detectIndent(code).indent;
if (indent && indent !== " ") style = indent;
}
function normalizeOptions(code, opts): Format {
const format = {
auxiliaryCommentBefore: opts.auxiliaryCommentBefore,
auxiliaryCommentAfter: opts.auxiliaryCommentAfter,
@@ -56,11 +49,11 @@ function normalizeOptions(code, opts, tokens): Format {
compact: opts.compact,
minified: opts.minified,
concise: opts.concise,
quotes: findCommonStringDelimiter(code, tokens),
quotes: "double",
jsonCompatibleStrings: opts.jsonCompatibleStrings,
indent: {
adjustMultilineComment: true,
style: style,
style: " ",
base: 0,
},
};
@@ -89,43 +82,6 @@ function normalizeOptions(code, opts, tokens): Format {
return format;
}
/**
* Determine if input code uses more single or double quotes.
*/
function findCommonStringDelimiter(code, tokens) {
const DEFAULT_STRING_DELIMITER = "double";
if (!code) {
return DEFAULT_STRING_DELIMITER;
}
const occurences = {
single: 0,
double: 0,
};
let checked = 0;
for (let i = 0; i < tokens.length; i++) {
const token = tokens[i];
if (token.type.label !== "string") continue;
const raw = code.slice(token.start, token.end);
if (raw[0] === "'") {
occurences.single++;
} else {
occurences.double++;
}
checked++;
if (checked >= 3) break;
}
if (occurences.single > occurences.double) {
return "single";
} else {
return "double";
}
}
/**
* We originally exported the Generator class above, but to make it extra clear that it is a private API,
* we have moved that to an internal class instance and simplified the interface to the two public methods