prettier/src/options.js

59 lines
1.4 KiB
JavaScript

"use strict";
const validate = require("jest-validate").validate;
const deprecatedConfig = require("./deprecated");
const defaults = {
rangeStart: 0,
rangeEnd: Infinity,
useTabs: false,
tabWidth: 2,
printWidth: 80,
singleQuote: false,
trailingComma: "none",
bracketSpacing: true,
jsxBracketSameLine: false,
parser: "babylon",
semi: true
};
const exampleConfig = Object.assign({}, defaults, {
filename: "testFilename",
printWidth: 80,
originalText: "text"
});
// Copy options and fill in default values.
function normalize(options) {
const normalized = Object.assign({}, options || {});
if (typeof normalized.trailingComma === "boolean") {
// Support a deprecated boolean type for the trailing comma config
// for a few versions. This code can be removed later.
normalized.trailingComma = "es5";
console.warn(
"Warning: `trailingComma` without any argument is deprecated. " +
'Specify "none", "es5", or "all".'
);
}
validate(normalized, { exampleConfig, deprecatedConfig });
// For backward compatibility. Deprecated in 0.0.10
if ("useFlowParser" in normalized) {
normalized.parser = normalized.useFlowParser ? "flow" : "babylon";
delete normalized.useFlowParser;
}
Object.keys(defaults).forEach(k => {
if (normalized[k] == null) {
normalized[k] = defaults[k];
}
});
return normalized;
}
module.exports = { normalize };