67 lines
1.6 KiB
JavaScript
67 lines
1.6 KiB
JavaScript
"use strict";
|
|
|
|
const validate = require("jest-validate").validate;
|
|
const deprecatedConfig = require("./deprecated");
|
|
|
|
const defaults = {
|
|
cursorOffset: -1,
|
|
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, {
|
|
filepath: "path/to/Filename",
|
|
printWidth: 80,
|
|
originalText: "text"
|
|
});
|
|
|
|
// Copy options and fill in default values.
|
|
function normalize(options) {
|
|
const normalized = Object.assign({}, options || {});
|
|
const filepath = normalized.filepath;
|
|
|
|
if (/\.(css|less|scss)$/.test(filepath)) {
|
|
normalized.parser = "postcss";
|
|
} else if (/\.(ts|tsx)$/.test(filepath)) {
|
|
normalized.parser = "typescript";
|
|
}
|
|
|
|
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 };
|