# 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;


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://notes.lirenxn.com/2020/wk-notes-01-06-eslint-finalise.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
