eslint with fix

pitfall with CLIEngine()

eslint pitfall in custom CLI tool:

In cli.js, passing default fix as null to eslint.

yargs.command(
    "eslint",
    "run the linter",
    yargs => {
      yargs
        .option("fix", {
          describe: "enable auto fixing option in eslint",
// ------***** BELOW *****---------
          default: null 
// ------***** ABOVE *****---------
        });
    },
    argv => {
      const eslint = require("./eslint").default;

      eslint(argv);
    }
  )

Consuming fix directly in CLIEngine

const eslintCLI = new CLIEngine({
      useEslintrc,
      ...eslintArgs,
      ...( !useEslintrc ? { configFile: DEFAULT_ESLINTRC } : {}),
      fix
    });

The following code, will be transpiled by babel to

// ...
{ fix: fix }
// ...

Key bug : in eslint CLIEngine, null will be treated as true and the fixed file could be listed on the fly. The check in eslint fixer is

if(shouldFix === false){
	// DO NOT FIX
}
// APPLY FIX

Programmatically linting (in custom cli)

eslint have a CliEngine which loads the cli options. --print-config will trigger engine.getConfigForFile(PATH_TO_SINGLE_FILE) to load the config for single file. Cos the config can vary for different type of files, you need to specify single file here.

vscode eslint extension

The extension uses the ESLint library installed in the opened workspace folder. If the folder doesn't provide one the extension looks for a global install version.

Last updated