UNPKG

23 kBJavaScriptView Raw
1/**
2 * @fileoverview Config initialization wizard.
3 * @author Ilya Volodin
4 */
5
6
7"use strict";
8
9//------------------------------------------------------------------------------
10// Requirements
11//------------------------------------------------------------------------------
12
13const util = require("util"),
14 inquirer = require("inquirer"),
15 ProgressBar = require("progress"),
16 semver = require("semver"),
17 autoconfig = require("./autoconfig.js"),
18 ConfigFile = require("./config-file"),
19 ConfigOps = require("./config-ops"),
20 getSourceCodeOfFiles = require("../util/source-code-utils").getSourceCodeOfFiles,
21 ModuleResolver = require("../util/module-resolver"),
22 npmUtils = require("../util/npm-utils"),
23 recConfig = require("../../conf/eslint-recommended"),
24 log = require("../util/logging");
25
26const debug = require("debug")("eslint:config-initializer");
27
28//------------------------------------------------------------------------------
29// Private
30//------------------------------------------------------------------------------
31
32const DEFAULT_ECMA_VERSION = 2018;
33
34/* istanbul ignore next: hard to test fs function */
35/**
36 * Create .eslintrc file in the current working directory
37 * @param {Object} config object that contains user's answers
38 * @param {string} format The file format to write to.
39 * @returns {void}
40 */
41function writeFile(config, format) {
42
43 // default is .js
44 let extname = ".js";
45
46 if (format === "YAML") {
47 extname = ".yml";
48 } else if (format === "JSON") {
49 extname = ".json";
50 }
51
52 const installedESLint = config.installedESLint;
53
54 delete config.installedESLint;
55
56 ConfigFile.write(config, `./.eslintrc${extname}`);
57 log.info(`Successfully created .eslintrc${extname} file in ${process.cwd()}`);
58
59 if (installedESLint) {
60 log.info("ESLint was installed locally. We recommend using this local copy instead of your globally-installed copy.");
61 }
62}
63
64/**
65 * Get the peer dependencies of the given module.
66 * This adds the gotten value to cache at the first time, then reuses it.
67 * In a process, this function is called twice, but `npmUtils.fetchPeerDependencies` needs to access network which is relatively slow.
68 * @param {string} moduleName The module name to get.
69 * @returns {Object} The peer dependencies of the given module.
70 * This object is the object of `peerDependencies` field of `package.json`.
71 * Returns null if npm was not found.
72 */
73function getPeerDependencies(moduleName) {
74 let result = getPeerDependencies.cache.get(moduleName);
75
76 if (!result) {
77 log.info(`Checking peerDependencies of ${moduleName}`);
78
79 result = npmUtils.fetchPeerDependencies(moduleName);
80 getPeerDependencies.cache.set(moduleName, result);
81 }
82
83 return result;
84}
85getPeerDependencies.cache = new Map();
86
87/**
88 * Return necessary plugins, configs, parsers, etc. based on the config
89 * @param {Object} config config object
90 * @param {boolean} [installESLint=true] If `false` is given, it does not install eslint.
91 * @returns {string[]} An array of modules to be installed.
92 */
93function getModulesList(config, installESLint) {
94 const modules = {};
95
96 // Create a list of modules which should be installed based on config
97 if (config.plugins) {
98 for (const plugin of config.plugins) {
99 modules[`eslint-plugin-${plugin}`] = "latest";
100 }
101 }
102 if (config.extends && config.extends.indexOf("eslint:") === -1) {
103 const moduleName = `eslint-config-${config.extends}`;
104
105 modules[moduleName] = "latest";
106 Object.assign(
107 modules,
108 getPeerDependencies(`${moduleName}@latest`)
109 );
110 }
111
112 if (installESLint === false) {
113 delete modules.eslint;
114 } else {
115 const installStatus = npmUtils.checkDevDeps(["eslint"]);
116
117 // Mark to show messages if it's new installation of eslint.
118 if (installStatus.eslint === false) {
119 log.info("Local ESLint installation not found.");
120 modules.eslint = modules.eslint || "latest";
121 config.installedESLint = true;
122 }
123 }
124
125 return Object.keys(modules).map(name => `${name}@${modules[name]}`);
126}
127
128/**
129 * Set the `rules` of a config by examining a user's source code
130 *
131 * Note: This clones the config object and returns a new config to avoid mutating
132 * the original config parameter.
133 *
134 * @param {Object} answers answers received from inquirer
135 * @param {Object} config config object
136 * @returns {Object} config object with configured rules
137 */
138function configureRules(answers, config) {
139 const BAR_TOTAL = 20,
140 BAR_SOURCE_CODE_TOTAL = 4,
141 newConfig = Object.assign({}, config),
142 disabledConfigs = {};
143 let sourceCodes,
144 registry;
145
146 // Set up a progress bar, as this process can take a long time
147 const bar = new ProgressBar("Determining Config: :percent [:bar] :elapseds elapsed, eta :etas ", {
148 width: 30,
149 total: BAR_TOTAL
150 });
151
152 bar.tick(0); // Shows the progress bar
153
154 // Get the SourceCode of all chosen files
155 const patterns = answers.patterns.split(/[\s]+/u);
156
157 try {
158 sourceCodes = getSourceCodeOfFiles(patterns, { baseConfig: newConfig, useEslintrc: false }, total => {
159 bar.tick((BAR_SOURCE_CODE_TOTAL / total));
160 });
161 } catch (e) {
162 log.info("\n");
163 throw e;
164 }
165 const fileQty = Object.keys(sourceCodes).length;
166
167 if (fileQty === 0) {
168 log.info("\n");
169 throw new Error("Automatic Configuration failed. No files were able to be parsed.");
170 }
171
172 // Create a registry of rule configs
173 registry = new autoconfig.Registry();
174 registry.populateFromCoreRules();
175
176 // Lint all files with each rule config in the registry
177 registry = registry.lintSourceCode(sourceCodes, newConfig, total => {
178 bar.tick((BAR_TOTAL - BAR_SOURCE_CODE_TOTAL) / total); // Subtract out ticks used at beginning
179 });
180 debug(`\nRegistry: ${util.inspect(registry.rules, { depth: null })}`);
181
182 // Create a list of recommended rules, because we don't want to disable them
183 const recRules = Object.keys(recConfig.rules).filter(ruleId => ConfigOps.isErrorSeverity(recConfig.rules[ruleId]));
184
185 // Find and disable rules which had no error-free configuration
186 const failingRegistry = registry.getFailingRulesRegistry();
187
188 Object.keys(failingRegistry.rules).forEach(ruleId => {
189
190 // If the rule is recommended, set it to error, otherwise disable it
191 disabledConfigs[ruleId] = (recRules.indexOf(ruleId) !== -1) ? 2 : 0;
192 });
193
194 // Now that we know which rules to disable, strip out configs with errors
195 registry = registry.stripFailingConfigs();
196
197 /*
198 * If there is only one config that results in no errors for a rule, we should use it.
199 * createConfig will only add rules that have one configuration in the registry.
200 */
201 const singleConfigs = registry.createConfig().rules;
202
203 /*
204 * The "sweet spot" for number of options in a config seems to be two (severity plus one option).
205 * Very often, a third option (usually an object) is available to address
206 * edge cases, exceptions, or unique situations. We will prefer to use a config with
207 * specificity of two.
208 */
209 const specTwoConfigs = registry.filterBySpecificity(2).createConfig().rules;
210
211 // Maybe a specific combination using all three options works
212 const specThreeConfigs = registry.filterBySpecificity(3).createConfig().rules;
213
214 // If all else fails, try to use the default (severity only)
215 const defaultConfigs = registry.filterBySpecificity(1).createConfig().rules;
216
217 // Combine configs in reverse priority order (later take precedence)
218 newConfig.rules = Object.assign({}, disabledConfigs, defaultConfigs, specThreeConfigs, specTwoConfigs, singleConfigs);
219
220 // Make sure progress bar has finished (floating point rounding)
221 bar.update(BAR_TOTAL);
222
223 // Log out some stats to let the user know what happened
224 const finalRuleIds = Object.keys(newConfig.rules);
225 const totalRules = finalRuleIds.length;
226 const enabledRules = finalRuleIds.filter(ruleId => (newConfig.rules[ruleId] !== 0)).length;
227 const resultMessage = [
228 `\nEnabled ${enabledRules} out of ${totalRules}`,
229 `rules based on ${fileQty}`,
230 `file${(fileQty === 1) ? "." : "s."}`
231 ].join(" ");
232
233 log.info(resultMessage);
234
235 ConfigOps.normalizeToStrings(newConfig);
236 return newConfig;
237}
238
239/**
240 * process user's answers and create config object
241 * @param {Object} answers answers received from inquirer
242 * @returns {Object} config object
243 */
244function processAnswers(answers) {
245 let config = {
246 rules: {},
247 env: {},
248 parserOptions: {},
249 extends: []
250 };
251
252 // set the latest ECMAScript version
253 config.parserOptions.ecmaVersion = DEFAULT_ECMA_VERSION;
254 config.env.es6 = true;
255 config.globals = {
256 Atomics: "readonly",
257 SharedArrayBuffer: "readonly"
258 };
259
260 // set the module type
261 if (answers.moduleType === "esm") {
262 config.parserOptions.sourceType = "module";
263 } else if (answers.moduleType === "commonjs") {
264 config.env.commonjs = true;
265 }
266
267 // add in browser and node environments if necessary
268 answers.env.forEach(env => {
269 config.env[env] = true;
270 });
271
272 // add in library information
273 if (answers.framework === "react") {
274 config.parserOptions.ecmaFeatures = {
275 jsx: true
276 };
277 config.plugins = ["react"];
278 } else if (answers.framework === "vue") {
279 config.plugins = ["vue"];
280 config.extends.push("plugin:vue/essential");
281 }
282
283 // setup rules based on problems/style enforcement preferences
284 if (answers.purpose === "problems") {
285 config.extends.unshift("eslint:recommended");
286 } else if (answers.purpose === "style") {
287 if (answers.source === "prompt") {
288 config.extends.unshift("eslint:recommended");
289 config.rules.indent = ["error", answers.indent];
290 config.rules.quotes = ["error", answers.quotes];
291 config.rules["linebreak-style"] = ["error", answers.linebreak];
292 config.rules.semi = ["error", answers.semi ? "always" : "never"];
293 } else if (answers.source === "auto") {
294 config = configureRules(answers, config);
295 config = autoconfig.extendFromRecommended(config);
296 }
297 }
298
299 // normalize extends
300 if (config.extends.length === 0) {
301 delete config.extends;
302 } else if (config.extends.length === 1) {
303 config.extends = config.extends[0];
304 }
305
306 ConfigOps.normalizeToStrings(config);
307 return config;
308}
309
310/**
311 * process user's style guide of choice and return an appropriate config object.
312 * @param {string} guide name of the chosen style guide
313 * @returns {Object} config object
314 */
315function getConfigForStyleGuide(guide) {
316 const guides = {
317 google: { extends: "google" },
318 airbnb: { extends: "airbnb" },
319 "airbnb-base": { extends: "airbnb-base" },
320 standard: { extends: "standard" }
321 };
322
323 if (!guides[guide]) {
324 throw new Error("You referenced an unsupported guide.");
325 }
326
327 return guides[guide];
328}
329
330/**
331 * Get the version of the local ESLint.
332 * @returns {string|null} The version. If the local ESLint was not found, returns null.
333 */
334function getLocalESLintVersion() {
335 try {
336 const resolver = new ModuleResolver();
337 const eslintPath = resolver.resolve("eslint", process.cwd());
338 const eslint = require(eslintPath);
339
340 return eslint.linter.version || null;
341 } catch (_err) {
342 return null;
343 }
344}
345
346/**
347 * Get the shareable config name of the chosen style guide.
348 * @param {Object} answers The answers object.
349 * @returns {string} The shareable config name.
350 */
351function getStyleGuideName(answers) {
352 if (answers.styleguide === "airbnb" && answers.framework !== "react") {
353 return "airbnb-base";
354 }
355 return answers.styleguide;
356}
357
358/**
359 * Check whether the local ESLint version conflicts with the required version of the chosen shareable config.
360 * @param {Object} answers The answers object.
361 * @returns {boolean} `true` if the local ESLint is found then it conflicts with the required version of the chosen shareable config.
362 */
363function hasESLintVersionConflict(answers) {
364
365 // Get the local ESLint version.
366 const localESLintVersion = getLocalESLintVersion();
367
368 if (!localESLintVersion) {
369 return false;
370 }
371
372 // Get the required range of ESLint version.
373 const configName = getStyleGuideName(answers);
374 const moduleName = `eslint-config-${configName}@latest`;
375 const peerDependencies = getPeerDependencies(moduleName) || {};
376 const requiredESLintVersionRange = peerDependencies.eslint;
377
378 if (!requiredESLintVersionRange) {
379 return false;
380 }
381
382 answers.localESLintVersion = localESLintVersion;
383 answers.requiredESLintVersionRange = requiredESLintVersionRange;
384
385 // Check the version.
386 if (semver.satisfies(localESLintVersion, requiredESLintVersionRange)) {
387 answers.installESLint = false;
388 return false;
389 }
390
391 return true;
392}
393
394/**
395 * Install modules.
396 * @param {string[]} modules Modules to be installed.
397 * @returns {void}
398 */
399function installModules(modules) {
400 log.info(`Installing ${modules.join(", ")}`);
401 npmUtils.installSyncSaveDev(modules);
402}
403
404/* istanbul ignore next: no need to test inquirer */
405/**
406 * Ask user to install modules.
407 * @param {string[]} modules Array of modules to be installed.
408 * @param {boolean} packageJsonExists Indicates if package.json is existed.
409 * @returns {Promise} Answer that indicates if user wants to install.
410 */
411function askInstallModules(modules, packageJsonExists) {
412
413 // If no modules, do nothing.
414 if (modules.length === 0) {
415 return Promise.resolve();
416 }
417
418 log.info("The config that you've selected requires the following dependencies:\n");
419 log.info(modules.join(" "));
420 return inquirer.prompt([
421 {
422 type: "confirm",
423 name: "executeInstallation",
424 message: "Would you like to install them now with npm?",
425 default: true,
426 when() {
427 return modules.length && packageJsonExists;
428 }
429 }
430 ]).then(({ executeInstallation }) => {
431 if (executeInstallation) {
432 installModules(modules);
433 }
434 });
435}
436
437/* istanbul ignore next: no need to test inquirer */
438/**
439 * Ask use a few questions on command prompt
440 * @returns {Promise} The promise with the result of the prompt
441 */
442function promptUser() {
443
444 return inquirer.prompt([
445 {
446 type: "list",
447 name: "purpose",
448 message: "How would you like to use ESLint?",
449 default: "problems",
450 choices: [
451 { name: "To check syntax only", value: "syntax" },
452 { name: "To check syntax and find problems", value: "problems" },
453 { name: "To check syntax, find problems, and enforce code style", value: "style" }
454 ]
455 },
456 {
457 type: "list",
458 name: "moduleType",
459 message: "What type of modules does your project use?",
460 default: "esm",
461 choices: [
462 { name: "JavaScript modules (import/export)", value: "esm" },
463 { name: "CommonJS (require/exports)", value: "commonjs" },
464 { name: "None of these", value: "none" }
465 ]
466 },
467 {
468 type: "list",
469 name: "framework",
470 message: "Which framework does your project use?",
471 default: "react",
472 choices: [
473 { name: "React", value: "react" },
474 { name: "Vue.js", value: "vue" },
475 { name: "None of these", value: "none" }
476 ]
477 },
478 {
479 type: "checkbox",
480 name: "env",
481 message: "Where does your code run?",
482 default: ["browser"],
483 choices: [
484 { name: "Browser", value: "browser" },
485 { name: "Node", value: "node" }
486 ]
487 },
488 {
489 type: "list",
490 name: "source",
491 message: "How would you like to define a style for your project?",
492 default: "guide",
493 choices: [
494 { name: "Use a popular style guide", value: "guide" },
495 { name: "Answer questions about your style", value: "prompt" },
496 { name: "Inspect your JavaScript file(s)", value: "auto" }
497 ],
498 when(answers) {
499 return answers.purpose === "style";
500 }
501 },
502 {
503 type: "list",
504 name: "styleguide",
505 message: "Which style guide do you want to follow?",
506 choices: [
507 { name: "Airbnb (https://github.com/airbnb/javascript)", value: "airbnb" },
508 { name: "Standard (https://github.com/standard/standard)", value: "standard" },
509 { name: "Google (https://github.com/google/eslint-config-google)", value: "google" }
510 ],
511 when(answers) {
512 answers.packageJsonExists = npmUtils.checkPackageJson();
513 return answers.source === "guide" && answers.packageJsonExists;
514 }
515 },
516 {
517 type: "input",
518 name: "patterns",
519 message: "Which file(s), path(s), or glob(s) should be examined?",
520 when(answers) {
521 return (answers.source === "auto");
522 },
523 validate(input) {
524 if (input.trim().length === 0 && input.trim() !== ",") {
525 return "You must tell us what code to examine. Try again.";
526 }
527 return true;
528 }
529 },
530 {
531 type: "list",
532 name: "format",
533 message: "What format do you want your config file to be in?",
534 default: "JavaScript",
535 choices: ["JavaScript", "YAML", "JSON"]
536 },
537 {
538 type: "confirm",
539 name: "installESLint",
540 message(answers) {
541 const verb = semver.ltr(answers.localESLintVersion, answers.requiredESLintVersionRange)
542 ? "upgrade"
543 : "downgrade";
544
545 return `The style guide "${answers.styleguide}" requires eslint@${answers.requiredESLintVersionRange}. You are currently using eslint@${answers.localESLintVersion}.\n Do you want to ${verb}?`;
546 },
547 default: true,
548 when(answers) {
549 return answers.source === "guide" && answers.packageJsonExists && hasESLintVersionConflict(answers);
550 }
551 }
552 ]).then(earlyAnswers => {
553
554 // early exit if no style guide is necessary
555 if (earlyAnswers.purpose !== "style") {
556 const config = processAnswers(earlyAnswers);
557 const modules = getModulesList(config);
558
559 return askInstallModules(modules, earlyAnswers.packageJsonExists)
560 .then(() => writeFile(config, earlyAnswers.format));
561 }
562
563 // early exit if you are using a style guide
564 if (earlyAnswers.source === "guide") {
565 if (!earlyAnswers.packageJsonExists) {
566 log.info("A package.json is necessary to install plugins such as style guides. Run `npm init` to create a package.json file and try again.");
567 return void 0;
568 }
569 if (earlyAnswers.installESLint === false && !semver.satisfies(earlyAnswers.localESLintVersion, earlyAnswers.requiredESLintVersionRange)) {
570 log.info(`Note: it might not work since ESLint's version is mismatched with the ${earlyAnswers.styleguide} config.`);
571 }
572 if (earlyAnswers.styleguide === "airbnb" && earlyAnswers.framework !== "react") {
573 earlyAnswers.styleguide = "airbnb-base";
574 }
575
576 const config = ConfigOps.merge(processAnswers(earlyAnswers), getConfigForStyleGuide(earlyAnswers.styleguide));
577 const modules = getModulesList(config);
578
579 return askInstallModules(modules, earlyAnswers.packageJsonExists)
580 .then(() => writeFile(config, earlyAnswers.format));
581
582 }
583
584 if (earlyAnswers.source === "auto") {
585 const combinedAnswers = Object.assign({}, earlyAnswers);
586 const config = processAnswers(combinedAnswers);
587 const modules = getModulesList(config);
588
589 return askInstallModules(modules).then(() => writeFile(config, earlyAnswers.format));
590 }
591
592 // continue with the style questions otherwise...
593 return inquirer.prompt([
594 {
595 type: "list",
596 name: "indent",
597 message: "What style of indentation do you use?",
598 default: "tab",
599 choices: [{ name: "Tabs", value: "tab" }, { name: "Spaces", value: 4 }]
600 },
601 {
602 type: "list",
603 name: "quotes",
604 message: "What quotes do you use for strings?",
605 default: "double",
606 choices: [{ name: "Double", value: "double" }, { name: "Single", value: "single" }]
607 },
608 {
609 type: "list",
610 name: "linebreak",
611 message: "What line endings do you use?",
612 default: "unix",
613 choices: [{ name: "Unix", value: "unix" }, { name: "Windows", value: "windows" }]
614 },
615 {
616 type: "confirm",
617 name: "semi",
618 message: "Do you require semicolons?",
619 default: true
620 },
621 {
622 type: "list",
623 name: "format",
624 message: "What format do you want your config file to be in?",
625 default: "JavaScript",
626 choices: ["JavaScript", "YAML", "JSON"]
627 }
628 ]).then(answers => {
629 const totalAnswers = Object.assign({}, earlyAnswers, answers);
630
631 const config = processAnswers(totalAnswers);
632 const modules = getModulesList(config);
633
634 return askInstallModules(modules).then(() => writeFile(config, answers.format));
635 });
636 });
637}
638
639//------------------------------------------------------------------------------
640// Public Interface
641//------------------------------------------------------------------------------
642
643const init = {
644 getConfigForStyleGuide,
645 getModulesList,
646 hasESLintVersionConflict,
647 installModules,
648 processAnswers,
649 /* istanbul ignore next */initializeConfig() {
650 return promptUser();
651 }
652};
653
654module.exports = init;