Skip to content

Commit

Permalink
doc: Added flat config example to the Readme
Browse files Browse the repository at this point in the history
  • Loading branch information
bfanger committed Apr 7, 2024
1 parent a05c31c commit 35d001f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 23 deletions.
30 changes: 24 additions & 6 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,23 @@ $ npm install eslint-plugin-only-warn --save-dev

**Note:** If you installed ESLint globally (using the `-g` flag) then you must also install `eslint-plugin-only-warn` globally.

## Usage
## Usage (Flat Config)

```js
// eslint.config.js
import "eslint-plugin-only-warn";
```

Getting `SyntaxError: Cannot use import statement outside a module`? Change your _package.json_ to `"type": "module",` or use require:

```js
// eslint.config.js
require("eslint-plugin-only-warn");`
```

## Usage (Old style ESLint config)

Not using the new flat config from ESLint 9+?

Add `only-warn` to the plugins section of your `.eslintrc` configuration file:

Expand All @@ -28,18 +44,20 @@ Add `only-warn` to the plugins section of your `.eslintrc` configuration file:
}
```

## Recommendations

Add `--max-warnings=0` to the lint script in your package.json.

```json
"lint": "eslint --max-warnings=0 ...",
```

This will make eslint cli report an errorcode which can be detected in git hook or CI pipeline.
This will make eslint cli report an errorcode which can be detected in a git hook or CI pipeline.

Tip: Use [husky](https://typicode.github.io/husky/) and [lint-staged](https://github.com/okonet/lint-staged) te prevent committing eslint warnings.
Tip: Use [husky](https://typicode.github.io/husky/) and [lint-staged](https://github.com/okonet/lint-staged) to prevent committing eslint warnings.

# Why only warnings?

- Don't waste time thinking or discussing about if it should be an error or a warning, focus on enabling of disabling a rule
- Warnings look different in editors, this allows you to quickly see that some tweaking is required, but your code still runs (eslint rules generally don't block the code from executing and fatal errors are still reported as error)
- Prevents noise, disallowing warnings to be committed in a codebase prevents clutter in the output of eslint (use [special eslint comments](https://eslint.org/docs/2.13.1/user-guide/configuring#disabling-rules-with-inline-comments) for the instances when you need an exception to the rules)
- Don't waste time thinking or discussing about when a rule should be an error or a warning, focus on enabling of disabling a rule
- Warnings look different in editors, this allows you to quickly see that some tweaking is required, but your code still runs (ESLint rules generally don't block the code from executing and fatal errors are still reported as error)
- Prevents noise, disallowing warnings to be committed in a codebase prevents clutter in the output of ESLint (use [special eslint comments](https://eslint.org/docs/latest/use/configure/rules#disabling-rules) for the instances when you need an exception to the rules)
13 changes: 13 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require("./src/only-warn");
const js = require("@eslint/js");
const prettierConfig = require("eslint-config-prettier");

module.exports = [
js.configs.recommended,
prettierConfig,
{
languageOptions: {
globals: { require: true, module: true },
},
},
];
21 changes: 4 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,17 @@
"engines": {
"node": ">=18"
},
"type": "commonjs",
"main": "src/only-warn.js",
"scripts": {
"lint": "prettier --check src tests && eslint src tests",
"format": "prettier --write src tests && eslint --fix src tests",
"format": "eslint --fix src tests && prettier --write src tests",
"test": "vitest run",
"test:watch": "vitest"
},
"eslintConfig": {
"extends": "eslint:recommended",
"plugins": [
"prettier"
],
"env": {
"jest": true,
"node": true,
"es6": true
},
"rules": {
"prettier/prettier": "error"
}
},
"devDependencies": {
"eslint": "^8.56.0",
"eslint-plugin-prettier": "^5.1.3",
"eslint": "^9.0.0",
"eslint-config-prettier": "^9.1.0",
"prettier": "^3.2.4",
"vitest": "^1.2.1"
}
Expand Down
File renamed without changes.

0 comments on commit 35d001f

Please sign in to comment.