Wk-notes-01-06-eslint-finalise

Correctly write json to file

fs.writeFile(outputFilename, JSON.stringify(myData, null, 4), function(err) {
    if(err) {
      console.log(err);
    } else {
      console.log("JSON saved to " + outputFilename);
    }
});

** If using CLI-COMMAND > File-Name.json it could went wrong.

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 : null will be treated as true and the fixed file will be listed on the fly. The check in fixer is

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

Remember to use false as the defailt fix flag.

Last updated