Spaces:
Sleeping
Sleeping
File size: 7,536 Bytes
fed6451 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | /**
* @fileoverview Translates CLI options into ESLint constructor options.
* @author Nicholas C. Zakas
* @author Francesco Trotta
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const { normalizeSeverityToString } = require("./severity");
const { getShorthandName, normalizePackageName } = require("./naming");
const { ModuleImporter } = require("@humanwhocodes/module-importer");
//------------------------------------------------------------------------------
// Types
//------------------------------------------------------------------------------
/** @typedef {import("../types").ESLint.Options} ESLintOptions */
/** @typedef {import("../types").ESLint.LegacyOptions} LegacyESLintOptions */
/** @typedef {import("../types").Linter.LintMessage} LintMessage */
/** @typedef {import("../options").ParsedCLIOptions} ParsedCLIOptions */
/** @typedef {import("../types").ESLint.Plugin} Plugin */
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
/**
* Loads plugins with the specified names.
* @param {{ "import": (name: string) => Promise<any> }} importer An object with an `import` method called once for each plugin.
* @param {string[]} pluginNames The names of the plugins to be loaded, with or without the "eslint-plugin-" prefix.
* @returns {Promise<Record<string, Plugin>>} A mapping of plugin short names to implementations.
*/
async function loadPlugins(importer, pluginNames) {
const plugins = {};
await Promise.all(
pluginNames.map(async pluginName => {
const longName = normalizePackageName(pluginName, "eslint-plugin");
const module = await importer.import(longName);
if (!("default" in module)) {
throw new Error(
`"${longName}" cannot be used with the \`--plugin\` option because its default module does not provide a \`default\` export`,
);
}
const shortName = getShorthandName(pluginName, "eslint-plugin");
plugins[shortName] = module.default;
}),
);
return plugins;
}
/**
* Predicate function for whether or not to apply fixes in quiet mode.
* If a message is a warning, do not apply a fix.
* @param {LintMessage} message The lint result.
* @returns {boolean} True if the lint message is an error (and thus should be
* autofixed), false otherwise.
*/
function quietFixPredicate(message) {
return message.severity === 2;
}
/**
* Predicate function for whether or not to run a rule in quiet mode.
* If a rule is set to warning, do not run it.
* @param {{ ruleId: string; severity: number; }} rule The rule id and severity.
* @returns {boolean} True if the lint rule should run, false otherwise.
*/
function quietRuleFilter(rule) {
return rule.severity === 2;
}
//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------
/**
* Translates the CLI options into the options expected by the ESLint constructor.
* @param {ParsedCLIOptions} cliOptions The CLI options to translate.
* @param {"flat"|"eslintrc"} [configType="eslintrc"] The format of the config to generate.
* @returns {Promise<ESLintOptions | LegacyESLintOptions>} The options object for the ESLint constructor.
*/
async function translateOptions(
{
cache,
cacheFile,
cacheLocation,
cacheStrategy,
concurrency,
config,
configLookup,
env,
errorOnUnmatchedPattern,
eslintrc,
ext,
fix,
fixDryRun,
fixType,
flag,
global,
ignore,
ignorePath,
ignorePattern,
inlineConfig,
parser,
parserOptions,
plugin,
quiet,
reportUnusedDisableDirectives,
reportUnusedDisableDirectivesSeverity,
reportUnusedInlineConfigs,
resolvePluginsRelativeTo,
rule,
rulesdir,
stats,
warnIgnored,
passOnNoPatterns,
maxWarnings,
},
configType,
) {
let overrideConfig, overrideConfigFile;
const importer = new ModuleImporter();
if (configType === "flat") {
overrideConfigFile =
typeof config === "string" ? config : !configLookup;
if (overrideConfigFile === false) {
overrideConfigFile = void 0;
}
const languageOptions = {};
if (global) {
languageOptions.globals = global.reduce((obj, name) => {
if (name.endsWith(":true")) {
obj[name.slice(0, -5)] = "writable";
} else {
obj[name] = "readonly";
}
return obj;
}, {});
}
if (parserOptions) {
languageOptions.parserOptions = parserOptions;
}
if (parser) {
languageOptions.parser = await importer.import(parser);
}
overrideConfig = [
{
...(Object.keys(languageOptions).length > 0
? { languageOptions }
: {}),
rules: rule ? rule : {},
},
];
if (
reportUnusedDisableDirectives ||
reportUnusedDisableDirectivesSeverity !== void 0
) {
overrideConfig[0].linterOptions = {
reportUnusedDisableDirectives: reportUnusedDisableDirectives
? "error"
: normalizeSeverityToString(
reportUnusedDisableDirectivesSeverity,
),
};
}
if (reportUnusedInlineConfigs !== void 0) {
overrideConfig[0].linterOptions = {
...overrideConfig[0].linterOptions,
reportUnusedInlineConfigs: normalizeSeverityToString(
reportUnusedInlineConfigs,
),
};
}
if (plugin) {
overrideConfig[0].plugins = await loadPlugins(importer, plugin);
}
if (ext) {
overrideConfig.push({
files: ext.map(
extension =>
`**/*${extension.startsWith(".") ? "" : "."}${extension}`,
),
});
}
} else {
overrideConfigFile = config;
overrideConfig = {
env:
env &&
env.reduce((obj, name) => {
obj[name] = true;
return obj;
}, {}),
globals:
global &&
global.reduce((obj, name) => {
if (name.endsWith(":true")) {
obj[name.slice(0, -5)] = "writable";
} else {
obj[name] = "readonly";
}
return obj;
}, {}),
ignorePatterns: ignorePattern,
parser,
parserOptions,
plugins: plugin,
rules: rule,
};
}
const options = {
allowInlineConfig: inlineConfig,
cache,
cacheLocation: cacheLocation || cacheFile,
cacheStrategy,
errorOnUnmatchedPattern,
fix: (fix || fixDryRun) && (quiet ? quietFixPredicate : true),
fixTypes: fixType,
ignore,
overrideConfig,
overrideConfigFile,
passOnNoPatterns,
};
if (configType === "flat") {
options.concurrency = concurrency;
options.flags = flag;
options.ignorePatterns = ignorePattern;
options.stats = stats;
options.warnIgnored = warnIgnored;
/*
* For performance reasons rules not marked as 'error' are filtered out in quiet mode. As maxWarnings
* requires rules set to 'warn' to be run, we only filter out 'warn' rules if maxWarnings is not specified.
*/
options.ruleFilter =
quiet && maxWarnings === -1 ? quietRuleFilter : () => true;
} else {
options.resolvePluginsRelativeTo = resolvePluginsRelativeTo;
options.rulePaths = rulesdir;
options.useEslintrc = eslintrc;
options.extensions = ext;
options.ignorePath = ignorePath;
if (
reportUnusedDisableDirectives ||
reportUnusedDisableDirectivesSeverity !== void 0
) {
options.reportUnusedDisableDirectives =
reportUnusedDisableDirectives
? "error"
: normalizeSeverityToString(
reportUnusedDisableDirectivesSeverity,
);
}
}
return options;
}
module.exports = translateOptions;
|