@paulhfischer/eslint-config-javascript 1.24.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +70 -15
  2. package/index.js +59 -8
  3. package/package.json +15 -14
  4. package/rules.js +308 -0
package/README.md CHANGED
@@ -1,38 +1,93 @@
1
1
  # eslint-config-javascript
2
2
 
3
- ESLint-config for JavaScript projects. It is based on
3
+ ESLint-config for JavaScript projects. It uses ESLint's flat config and consists of
4
4
 
5
- - [`eslint-config-airbnb-base`](https://github.com/airbnb/javascript/tree/master/packages/eslint-config-airbnb-base),
6
- - [`eslint-config-prettier`](https://github.com/prettier/eslint-config-prettier) and
7
- - [`prettier-config`](https://github.com/paulhfischer/prettier-config)
5
+ - the recommended rules of [`@eslint/js`](https://github.com/eslint/eslint/tree/main/packages/js)
6
+ and [`eslint-plugin-import-x`](https://github.com/un-ts/eslint-plugin-import-x),
7
+ - the rules of the [Airbnb JavaScript Style Guide](https://github.com/airbnb/javascript), ported
8
+ to current ESLint versions (see [`rules.js`](rules.js)) and
9
+ - [`prettier-config`](https://github.com/paulhfischer/prettier-config) for formatting (via
10
+ [`eslint-plugin-prettier`](https://github.com/prettier/eslint-plugin-prettier))
8
11
 
9
12
  and uses
10
13
 
11
- - [`eslint-plugin-html`](https://github.com/BenoitZugmeyer/eslint-plugin-html) and
12
- - [`eslint-plugin-jinja2-processor`](https://github.com/paulhfischer/eslint-plugin-jinja2-processor)
14
+ - [`eslint-plugin-html`](https://github.com/BenoitZugmeyer/eslint-plugin-html) and
15
+ - [`eslint-plugin-jinja2-processor`](https://github.com/paulhfischer/eslint-plugin-jinja2-processor)
13
16
 
14
- to enable linting for more file types.
17
+ to lint scripts in HTML-, PHP- and Jinja-files.
18
+
19
+ For TypeScript and React projects, use
20
+ [`eslint-config-typescript`](https://github.com/paulhfischer/eslint-config-typescript) or
21
+ [`eslint-config-typescript-react`](https://github.com/paulhfischer/eslint-config-typescript-react).
15
22
 
16
23
  ### Usage
17
24
 
18
- 1. Install via `npm`:
25
+ #### pre-commit
26
+
27
+ 1. Add the hook to your `.pre-commit-config.yaml`:
28
+
29
+ ```yaml
30
+ - repo: https://github.com/paulhfischer/eslint-config-javascript
31
+ rev: v2.0.0
32
+ hooks:
33
+ - id: lint
34
+ ```
35
+
36
+ 2. Add the config to your `eslint.config.cjs`:
19
37
 
38
+ ```js
39
+ module.exports = require('@paulhfischer/eslint-config-javascript');
20
40
  ```
21
- npm install --save-dev @paulhfischer/eslint-config-javascript --engine-strict
41
+
42
+ or, to adjust it:
43
+
44
+ ```js
45
+ const config = require('@paulhfischer/eslint-config-javascript');
46
+
47
+ module.exports = [...config, { rules: { 'no-console': 'off' } }];
22
48
  ```
23
49
 
24
- _Note: The installation requires `npm` version 7 or higher (otherwise peer dependencies aren't installed automatically). Have a look at the `.pre-commit-hooks.yaml`-file for a list of all peer-dependencies._
50
+ _Note: The config has to be a CommonJS-file using `require()`, as ES modules can't import
51
+ packages from the environment pre-commit installs the hook into._
25
52
 
26
- 2. Add the plugin to your eslint-config:
53
+ 3. Add the prettier configuration to your `.prettierrc`:
27
54
 
28
55
  ```json
29
- {
30
- "extends": ["@paulhfischer/eslint-config-javascript"]
31
- }
56
+ "@paulhfischer/prettier-config"
32
57
  ```
33
58
 
34
- 3. Add the prettier configuration to your prettier-config:
59
+ #### npm
60
+
61
+ 1. Install via `npm`:
62
+
63
+ ```
64
+ npm install --save-dev @paulhfischer/eslint-config-javascript
65
+ ```
66
+
67
+ _Note: Have a look at the `.pre-commit-hooks.yaml`-file for a list of all peer-dependencies._
68
+
69
+ 2. Add the config to your `eslint.config.js`:
70
+
71
+ ```js
72
+ import config from '@paulhfischer/eslint-config-javascript';
73
+
74
+ export default config;
75
+ ```
76
+
77
+ 3. Add the prettier configuration to your `.prettierrc` (or the `prettier`-key of your
78
+ `package.json`):
35
79
 
36
80
  ```json
37
81
  "@paulhfischer/prettier-config"
38
82
  ```
83
+
84
+ ### Migrating from version 1
85
+
86
+ - Version 2 requires ESLint 10 and flat configs. Replace your `.eslintrc`-file or the
87
+ `eslintConfig`-key of your `package.json` with an `eslint.config.cjs`-file (see above).
88
+ - Rules of `eslint-plugin-import` are now provided by `eslint-plugin-import-x`, i.e. `import/…`
89
+ becomes `import-x/…`.
90
+ - Compared to `eslint-config-airbnb-base`, formatting rules are left to prettier, rules of the
91
+ recommended configs are enabled and some rules were modernized: `for…of`-loops and `void` as a
92
+ statement are allowed, `++` is allowed in for-loops, named exports are no longer discouraged and
93
+ file extensions in imports are no longer forbidden (as ES modules require them).
package/index.js CHANGED
@@ -1,8 +1,59 @@
1
- module.exports = {
2
- root: true,
3
- plugins: ['html', 'jinja2-processor'],
4
- extends: ['airbnb-base', 'plugin:prettier/recommended'],
5
- settings: {
6
- 'html/report-bad-indent': 'error',
7
- },
8
- };
1
+ import js from '@eslint/js';
2
+ import html from 'eslint-plugin-html';
3
+ import { flatConfigs as importXConfigs } from 'eslint-plugin-import-x';
4
+ import jinja2 from 'eslint-plugin-jinja2-processor';
5
+ import prettierRecommended from 'eslint-plugin-prettier/recommended';
6
+ import globals from 'globals';
7
+
8
+ import rules from './rules.js';
9
+
10
+ export { rules };
11
+
12
+ export const markupFiles = ['**/*.html', '**/*.php'];
13
+
14
+ export const base = [
15
+ {
16
+ name: '@paulhfischer/javascript/ignores',
17
+ ignores: ['**/*.min.js'],
18
+ },
19
+ {
20
+ name: '@paulhfischer/javascript/setup',
21
+ languageOptions: {
22
+ globals: {
23
+ ...globals.browser,
24
+ ...globals.node,
25
+ },
26
+ },
27
+ },
28
+ {
29
+ name: '@paulhfischer/javascript/markup',
30
+ files: markupFiles,
31
+ plugins: { html },
32
+ settings: {
33
+ 'html/report-bad-indent': 'error',
34
+ },
35
+ },
36
+ jinja2.configs.recommended,
37
+ js.configs.recommended,
38
+ importXConfigs.recommended,
39
+ {
40
+ name: '@paulhfischer/javascript/rules',
41
+ rules,
42
+ },
43
+ {
44
+ // the config may be installed by pre-commit instead of being listed in the package.json
45
+ name: '@paulhfischer/javascript/eslint-config',
46
+ files: ['**/eslint.config.{js,mjs,cjs,ts,mts,cts}'],
47
+ rules: {
48
+ 'import-x/no-extraneous-dependencies': 'off',
49
+ },
50
+ },
51
+ ];
52
+
53
+ export const prettier = [prettierRecommended];
54
+
55
+ const config = [...base, ...prettier];
56
+
57
+ export default config;
58
+ // returned by `require()`, so that CommonJS configs can use it directly
59
+ export { config as 'module.exports' };
package/package.json CHANGED
@@ -1,35 +1,36 @@
1
1
  {
2
2
  "name": "@paulhfischer/eslint-config-javascript",
3
- "version": "1.24.0",
3
+ "version": "2.0.0",
4
4
  "description": "ESLint-config for JavaScript projects.",
5
+ "type": "module",
5
6
  "main": "index.js",
6
7
  "author": "Paul Fischer <paul.fischer@mailbox.org>",
7
8
  "repository": {
8
9
  "type": "git",
9
- "url": "git+https://github.com/paulhfischer/eslint-config-javscript"
10
+ "url": "git+https://github.com/paulhfischer/eslint-config-javascript.git"
10
11
  },
11
12
  "license": "GPL-3.0",
12
13
  "publishConfig": {
13
14
  "access": "public"
14
15
  },
15
- "eslintConfig": {
16
- "extends": [
17
- "."
18
- ]
19
- },
20
16
  "prettier": "@paulhfischer/prettier-config",
21
17
  "peerDependencies": {
22
- "@paulhfischer/prettier-config": "1.13.0",
23
- "eslint-config-airbnb-base": "15.0.0",
24
- "eslint-config-prettier": "9.1.0",
25
- "eslint-plugin-html": "8.1.1",
26
- "eslint-plugin-jinja2-processor": "1.2.0",
27
- "eslint-plugin-prettier": "5.1.3"
18
+ "@eslint/js": "10.0.1",
19
+ "@paulhfischer/prettier-config": "2.0.0",
20
+ "eslint": "10.12.0",
21
+ "eslint-config-prettier": "10.1.8",
22
+ "eslint-plugin-html": "8.2.1",
23
+ "eslint-plugin-import-x": "4.17.1",
24
+ "eslint-plugin-jinja2-processor": "2.0.0",
25
+ "eslint-plugin-prettier": "5.5.7",
26
+ "globals": "17.13.0"
28
27
  },
29
28
  "files": [
30
- "index.js"
29
+ "index.js",
30
+ "rules.js"
31
31
  ],
32
32
  "engines": {
33
+ "node": "^20.19.0 || ^22.13.0 || >=24",
33
34
  "npm": ">=7"
34
35
  }
35
36
  }
package/rules.js ADDED
@@ -0,0 +1,308 @@
1
+ // Rules ported from the Airbnb JavaScript Style Guide (https://github.com/airbnb/javascript), based
2
+ // on the flat config port in eslint-config-airbnb-extended. Formatting rules are left to prettier,
3
+ // rules already enabled by `@eslint/js` and `eslint-plugin-import-x` are not repeated.
4
+
5
+ const confusingBrowserGlobals = [
6
+ 'addEventListener',
7
+ 'blur',
8
+ 'close',
9
+ 'closed',
10
+ 'confirm',
11
+ 'defaultStatus',
12
+ 'defaultstatus',
13
+ 'event',
14
+ 'external',
15
+ 'find',
16
+ 'focus',
17
+ 'frameElement',
18
+ 'frames',
19
+ 'history',
20
+ 'innerHeight',
21
+ 'innerWidth',
22
+ 'length',
23
+ 'location',
24
+ 'locationbar',
25
+ 'menubar',
26
+ 'moveBy',
27
+ 'moveTo',
28
+ 'name',
29
+ 'onblur',
30
+ 'onerror',
31
+ 'onfocus',
32
+ 'onload',
33
+ 'onresize',
34
+ 'onunload',
35
+ 'open',
36
+ 'opener',
37
+ 'opera',
38
+ 'outerHeight',
39
+ 'outerWidth',
40
+ 'pageXOffset',
41
+ 'pageYOffset',
42
+ 'parent',
43
+ 'print',
44
+ 'removeEventListener',
45
+ 'resizeBy',
46
+ 'resizeTo',
47
+ 'screen',
48
+ 'screenLeft',
49
+ 'screenTop',
50
+ 'screenX',
51
+ 'screenY',
52
+ 'scroll',
53
+ 'scrollbars',
54
+ 'scrollBy',
55
+ 'scrollTo',
56
+ 'scrollX',
57
+ 'scrollY',
58
+ 'self',
59
+ 'status',
60
+ 'statusbar',
61
+ 'stop',
62
+ 'toolbar',
63
+ 'top',
64
+ ];
65
+
66
+ export const bestPractices = {
67
+ 'array-callback-return': ['error', { allowImplicit: true }],
68
+ 'block-scoped-var': 'error',
69
+ 'class-methods-use-this': 'error',
70
+ 'consistent-return': 'error',
71
+ 'default-case': ['error', { commentPattern: '^no default$' }],
72
+ 'default-case-last': 'error',
73
+ 'default-param-last': 'error',
74
+ 'dot-notation': ['error', { allowKeywords: true }],
75
+ eqeqeq: ['error', 'always', { null: 'ignore' }],
76
+ 'grouped-accessor-pairs': 'error',
77
+ 'guard-for-in': 'error',
78
+ 'max-classes-per-file': ['error', 1],
79
+ 'no-alert': 'warn',
80
+ 'no-caller': 'error',
81
+ 'no-constructor-return': 'error',
82
+ 'no-else-return': ['error', { allowElseIf: false }],
83
+ 'no-empty-function': ['error', { allow: ['arrowFunctions', 'functions', 'methods'] }],
84
+ 'no-eval': 'error',
85
+ 'no-extend-native': 'error',
86
+ 'no-extra-bind': 'error',
87
+ 'no-extra-label': 'error',
88
+ 'no-implied-eval': 'error',
89
+ 'no-iterator': 'error',
90
+ 'no-labels': ['error', { allowLoop: false, allowSwitch: false }],
91
+ 'no-lone-blocks': 'error',
92
+ 'no-loop-func': 'error',
93
+ 'no-multi-str': 'error',
94
+ 'no-new': 'error',
95
+ 'no-new-func': 'error',
96
+ 'no-new-wrappers': 'error',
97
+ 'no-octal-escape': 'error',
98
+ 'no-param-reassign': [
99
+ 'error',
100
+ {
101
+ props: true,
102
+ ignorePropertyModificationsFor: [
103
+ 'acc',
104
+ 'accumulator',
105
+ 'e',
106
+ 'ctx',
107
+ 'context',
108
+ 'req',
109
+ 'request',
110
+ 'res',
111
+ 'response',
112
+ ],
113
+ },
114
+ ],
115
+ 'no-proto': 'error',
116
+ 'no-restricted-properties': [
117
+ 'error',
118
+ { object: 'arguments', property: 'callee', message: 'arguments.callee is deprecated' },
119
+ ...['global', 'globalThis', 'self', 'window'].flatMap((object) => [
120
+ { object, property: 'isFinite', message: 'Please use Number.isFinite instead' },
121
+ { object, property: 'isNaN', message: 'Please use Number.isNaN instead' },
122
+ ]),
123
+ { property: '__defineGetter__', message: 'Please use Object.defineProperty instead.' },
124
+ { property: '__defineSetter__', message: 'Please use Object.defineProperty instead.' },
125
+ {
126
+ object: 'Math',
127
+ property: 'pow',
128
+ message: 'Use the exponentiation operator (**) instead.',
129
+ },
130
+ ],
131
+ 'no-return-assign': ['error', 'always'],
132
+ 'no-script-url': 'error',
133
+ 'no-self-assign': ['error', { props: true }],
134
+ 'no-self-compare': 'error',
135
+ 'no-sequences': 'error',
136
+ 'no-throw-literal': 'error',
137
+ 'no-unused-expressions': [
138
+ 'error',
139
+ { allowShortCircuit: false, allowTernary: false, allowTaggedTemplates: false },
140
+ ],
141
+ 'no-useless-concat': 'error',
142
+ 'no-useless-return': 'error',
143
+ // allow `void` to explicitly mark floating promises
144
+ 'no-void': ['error', { allowAsStatement: true }],
145
+ 'prefer-object-has-own': 'error',
146
+ 'prefer-promise-reject-errors': ['error', { allowEmptyReject: true }],
147
+ 'prefer-regex-literals': ['error', { disallowRedundantWrapping: true }],
148
+ radix: 'error',
149
+ 'vars-on-top': 'error',
150
+ yoda: 'error',
151
+ };
152
+
153
+ export const errors = {
154
+ 'getter-return': ['error', { allowImplicit: true }],
155
+ 'no-await-in-loop': 'error',
156
+ 'no-cond-assign': ['error', 'always'],
157
+ 'no-console': 'warn',
158
+ 'no-constant-condition': 'warn',
159
+ 'no-promise-executor-return': 'error',
160
+ 'no-template-curly-in-string': 'error',
161
+ 'no-unreachable-loop': 'error',
162
+ 'no-unsafe-optional-chaining': ['error', { disallowArithmeticOperators: true }],
163
+ 'valid-typeof': ['error', { requireStringLiterals: true }],
164
+ };
165
+
166
+ export const es6 = {
167
+ 'no-restricted-exports': ['error', { restrictedNamedExports: ['default', 'then'] }],
168
+ 'no-useless-computed-key': 'error',
169
+ 'no-useless-constructor': 'error',
170
+ 'no-useless-rename': 'error',
171
+ 'no-var': 'error',
172
+ 'object-shorthand': ['error', 'always', { ignoreConstructors: false, avoidQuotes: true }],
173
+ 'prefer-const': ['error', { destructuring: 'any', ignoreReadBeforeAssign: true }],
174
+ 'prefer-destructuring': [
175
+ 'error',
176
+ {
177
+ VariableDeclarator: { array: false, object: true },
178
+ AssignmentExpression: { array: true, object: false },
179
+ },
180
+ { enforceForRenamedProperties: false },
181
+ ],
182
+ 'prefer-numeric-literals': 'error',
183
+ 'prefer-rest-params': 'error',
184
+ 'prefer-spread': 'error',
185
+ 'prefer-template': 'error',
186
+ 'symbol-description': 'error',
187
+ };
188
+
189
+ export const imports = {
190
+ 'import-x/export': 'error',
191
+ 'import-x/first': 'error',
192
+ 'import-x/newline-after-import': 'error',
193
+ 'import-x/no-absolute-path': 'error',
194
+ 'import-x/no-amd': 'error',
195
+ 'import-x/no-cycle': ['error', { maxDepth: '∞' }],
196
+ 'import-x/no-duplicates': 'error',
197
+ 'import-x/no-dynamic-require': 'error',
198
+ 'import-x/no-empty-named-blocks': 'error',
199
+ 'import-x/no-extraneous-dependencies': [
200
+ 'error',
201
+ {
202
+ devDependencies: [
203
+ '**/test/**',
204
+ '**/tests/**',
205
+ '**/spec/**',
206
+ '**/__tests__/**',
207
+ '**/__mocks__/**',
208
+ '**/*{.,_}{test,spec}.*',
209
+ '**/*.config.*',
210
+ ],
211
+ optionalDependencies: false,
212
+ peerDependencies: true,
213
+ },
214
+ ],
215
+ 'import-x/no-import-module-exports': 'error',
216
+ 'import-x/no-mutable-exports': 'error',
217
+ 'import-x/no-named-as-default': 'error',
218
+ 'import-x/no-named-as-default-member': 'error',
219
+ 'import-x/no-named-default': 'error',
220
+ 'import-x/no-relative-packages': 'error',
221
+ 'import-x/no-self-import': 'error',
222
+ 'import-x/no-unresolved': ['error', { commonjs: true, caseSensitive: true }],
223
+ 'import-x/no-useless-path-segments': ['error', { commonjs: true }],
224
+ 'import-x/no-webpack-loader-syntax': 'error',
225
+ 'import-x/order': ['error', { groups: [['builtin', 'external', 'internal']] }],
226
+ };
227
+
228
+ export const strict = {
229
+ strict: ['error', 'never'],
230
+ };
231
+
232
+ export const style = {
233
+ camelcase: ['error', { properties: 'never', ignoreDestructuring: false }],
234
+ 'func-names': 'warn',
235
+ 'logical-assignment-operators': ['error', 'always'],
236
+ 'new-cap': ['error', { newIsCap: true, capIsNew: false }],
237
+ 'no-array-constructor': 'error',
238
+ 'no-bitwise': 'error',
239
+ 'no-continue': 'error',
240
+ 'no-lonely-if': 'error',
241
+ 'no-multi-assign': 'error',
242
+ 'no-nested-ternary': 'error',
243
+ 'no-object-constructor': 'error',
244
+ 'no-plusplus': ['error', { allowForLoopAfterthoughts: true }],
245
+ 'no-restricted-syntax': [
246
+ 'error',
247
+ {
248
+ selector: 'ForInStatement',
249
+ message:
250
+ 'for..in loops iterate over the entire prototype chain, which is virtually never what you want. Use Object.{keys,values,entries}, and iterate over the resulting array.',
251
+ },
252
+ {
253
+ selector: 'LabeledStatement',
254
+ message:
255
+ 'Labels are a form of GOTO; using them makes code confusing and hard to maintain and understand.',
256
+ },
257
+ {
258
+ selector: 'WithStatement',
259
+ message:
260
+ '`with` is disallowed in strict mode because it makes code impossible to predict and optimize.',
261
+ },
262
+ ],
263
+ 'no-underscore-dangle': [
264
+ 'error',
265
+ { allowAfterThis: false, allowAfterSuper: false, enforceInMethodNames: true },
266
+ ],
267
+ 'no-unneeded-ternary': ['error', { defaultAssignment: false }],
268
+ 'one-var': ['error', 'never'],
269
+ 'operator-assignment': ['error', 'always'],
270
+ 'prefer-exponentiation-operator': 'error',
271
+ 'prefer-object-spread': 'error',
272
+ 'unicode-bom': ['error', 'never'],
273
+ };
274
+
275
+ export const variables = {
276
+ 'no-label-var': 'error',
277
+ 'no-restricted-globals': [
278
+ 'error',
279
+ { name: 'isFinite', message: 'Use Number.isFinite instead.' },
280
+ { name: 'isNaN', message: 'Use Number.isNaN instead.' },
281
+ ...confusingBrowserGlobals.map((name) => ({
282
+ name,
283
+ message: `Use window.${name} instead.`,
284
+ })),
285
+ ],
286
+ 'no-shadow': 'error',
287
+ 'no-undef-init': 'error',
288
+ 'no-unused-vars': [
289
+ 'error',
290
+ {
291
+ ignoreRestSiblings: true,
292
+ argsIgnorePattern: '^_',
293
+ caughtErrorsIgnorePattern: '^_',
294
+ destructuredArrayIgnorePattern: '^_',
295
+ },
296
+ ],
297
+ 'no-use-before-define': ['error', { functions: true, classes: true, variables: true }],
298
+ };
299
+
300
+ export default {
301
+ ...bestPractices,
302
+ ...errors,
303
+ ...es6,
304
+ ...imports,
305
+ ...strict,
306
+ ...style,
307
+ ...variables,
308
+ };