2017-01-20 00:45:36 +03:00
|
|
|
"use strict";
|
2017-01-24 00:33:58 +03:00
|
|
|
|
2017-11-03 10:06:25 +03:00
|
|
|
const path = require("path");
|
2017-12-26 16:04:09 +03:00
|
|
|
const getSupportInfo = require("../common/support").getSupportInfo;
|
2018-01-18 10:26:27 +03:00
|
|
|
const supportInfo = getSupportInfo(null, { showUnreleased: true });
|
|
|
|
const normalizer = require("./options-normalizer");
|
2017-12-26 16:04:09 +03:00
|
|
|
const loadPlugins = require("../common/load-plugins");
|
|
|
|
const resolveParser = require("./parser").resolveParser;
|
|
|
|
const getPrinter = require("./get-printer");
|
2017-01-24 00:33:58 +03:00
|
|
|
|
2018-01-18 10:26:27 +03:00
|
|
|
const hiddenDefaults = {
|
2017-12-26 16:04:09 +03:00
|
|
|
astFormat: "estree",
|
2018-01-18 10:26:27 +03:00
|
|
|
printer: {}
|
2017-01-05 04:45:28 +03:00
|
|
|
};
|
2016-12-23 21:38:10 +03:00
|
|
|
|
2018-01-18 10:26:27 +03:00
|
|
|
const defaults = supportInfo.options.reduce(
|
|
|
|
(reduced, optionInfo) =>
|
|
|
|
Object.assign(reduced, { [optionInfo.name]: optionInfo.default }),
|
|
|
|
Object.assign({}, hiddenDefaults)
|
|
|
|
);
|
2017-01-24 00:33:58 +03:00
|
|
|
|
2016-12-23 21:38:10 +03:00
|
|
|
// Copy options and fill in default values.
|
2018-01-18 10:26:27 +03:00
|
|
|
function normalize(options, opts) {
|
|
|
|
opts = opts || {};
|
|
|
|
|
|
|
|
const rawOptions = Object.assign({}, options);
|
|
|
|
rawOptions.plugins = loadPlugins(rawOptions.plugins);
|
|
|
|
|
|
|
|
if (opts.inferParser !== false) {
|
|
|
|
if (
|
|
|
|
rawOptions.filepath &&
|
|
|
|
(!rawOptions.parser || rawOptions.parser === defaults.parser)
|
|
|
|
) {
|
|
|
|
const inferredParser = inferParser(
|
|
|
|
rawOptions.filepath,
|
|
|
|
rawOptions.plugins
|
|
|
|
);
|
|
|
|
if (inferredParser) {
|
|
|
|
rawOptions.parser = inferredParser;
|
|
|
|
}
|
2017-11-03 10:06:25 +03:00
|
|
|
}
|
2017-06-28 19:29:47 +03:00
|
|
|
}
|
|
|
|
|
2018-01-18 10:26:27 +03:00
|
|
|
rawOptions.astFormat = resolveParser(rawOptions).astFormat;
|
|
|
|
rawOptions.printer = getPrinter(rawOptions);
|
2017-12-26 16:04:09 +03:00
|
|
|
|
2017-01-05 04:45:28 +03:00
|
|
|
Object.keys(defaults).forEach(k => {
|
2018-01-18 10:26:27 +03:00
|
|
|
if (rawOptions[k] == null) {
|
|
|
|
rawOptions[k] = defaults[k];
|
2017-01-05 04:45:28 +03:00
|
|
|
}
|
|
|
|
});
|
2017-01-20 01:35:12 +03:00
|
|
|
|
2018-01-18 10:26:27 +03:00
|
|
|
if (rawOptions.parser === "json") {
|
|
|
|
rawOptions.trailingComma = "none";
|
|
|
|
}
|
|
|
|
|
|
|
|
return normalizer.normalizeApiOptions(
|
|
|
|
rawOptions,
|
|
|
|
supportInfo.options,
|
|
|
|
Object.assign({ passThrough: Object.keys(hiddenDefaults) }, opts)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function inferParser(filepath, plugins) {
|
|
|
|
const extension = path.extname(filepath);
|
|
|
|
const filename = path.basename(filepath).toLowerCase();
|
|
|
|
|
|
|
|
const language = getSupportInfo(null, {
|
|
|
|
plugins,
|
|
|
|
pluginsLoaded: true
|
|
|
|
}).languages.find(
|
|
|
|
language =>
|
|
|
|
typeof language.since === "string" &&
|
|
|
|
(language.extensions.indexOf(extension) > -1 ||
|
|
|
|
(language.filenames &&
|
|
|
|
language.filenames.find(name => name.toLowerCase() === filename)))
|
|
|
|
);
|
|
|
|
|
|
|
|
return language && language.parsers[0];
|
2017-01-20 21:12:37 +03:00
|
|
|
}
|
2017-01-20 00:45:36 +03:00
|
|
|
|
2018-01-18 10:26:27 +03:00
|
|
|
module.exports = { normalize, defaults, hiddenDefaults };
|