> For the complete documentation index, see [llms.txt](https://notes.lirenxn.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://notes.lirenxn.com/2020/wk-notes-01-06-eslint-finalise.md). Agents can also search and read this documentation through its [MCP server](https://notes.lirenxn.com/~gitbook/mcp).

# Wk-notes-01-06-eslint-finalise

### Correctly write json to file

* <https://stackoverflow.com/questions/5670752/how-can-i-pretty-print-json-using-node-js>

```javascript
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.

```javascript
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`

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

&#x20;The following code, will be transpiled by babel to

```javascript
// ...
{ 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

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

**Remember to use false as the defailt fix flag.**&#x20;
