eslint-plugin-constructors 1.0.2 → 1.1.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.
package/README.md CHANGED
@@ -1,14 +1,19 @@
1
- # eslint-plugin-constructors
1
+ <a id="top"></a>
2
2
 
3
- [![](https://img.shields.io/npm/v/eslint-plugin-constructors?style=for-the-badge&label=Latest%20Build&color=32fcee)](https://www.npmjs.com/package/eslint-plugin-constructors)
4
- <a href="./#license">
5
- <img src="https://img.shields.io/badge/License-MIT-f99b27.svg?logo=internetarchive&logoColor=white&labelColor=464646&style=for-the-badge"></a>
3
+ # <img height=24 src="https://cdn.staticdelivr.com/gl/adamlui/eslint-plugin-constructors/9b14b5d0f2/assets/images/icons/eslint/purple/icon32.png"> eslint-plugin-constructors
4
+
5
+ [![](https://img.shields.io/npm/v/eslint-plugin-constructors?logo=icinga&style=for-the-badge&label=Latest%20Build&color=32fcee)][epc-npm]
6
+ [![](https://img.shields.io/badge/License-MIT-f1f20a.svg?logo=internetarchive&logoColor=white&labelColor=464646&style=for-the-badge)](#license)
6
7
  [![](https://img.shields.io/badge/Requires-ESLint_%3E%3D9.0.0-4B32C3?style=for-the-badge&logo=eslint)](https://eslint.org/)
7
8
 
8
9
  ESLint rules to enforce best practices for constructor usage.
9
10
 
11
+ <img src="https://cdn.staticdelivr.com/gl/adamlui/eslint-plugin-constructors/6eeba7b2cb/assets/images/screenshots/vs-code/squiggles.png">
12
+
10
13
  ## Installation
11
14
 
15
+ From your project root:
16
+
12
17
  ```bash
13
18
  npm i -D eslint-plugin-constructors
14
19
  ```
@@ -27,13 +32,15 @@ export default [
27
32
  rules: {
28
33
  'constructors/no-explicit-return': 'error',
29
34
  'constructors/require-error-subclass-name': 'error',
30
- 'constructors/require-new': 'error'
35
+ 'constructors/require-new': ['error', 'error-only']
31
36
  }
32
37
  }
33
38
  ]
34
39
  ```
35
40
 
36
- Or use the bundled recommended config:
41
+ **Note:** Rule config is either a severity string (`'off'`, `'warn'`, `'error'`) or an array `[severity, options]` when the rule accepts options.
42
+
43
+ To use the bundled recommended config (enables all 3 rules at `'error'`):
37
44
 
38
45
  ```js
39
46
  import constructors from 'eslint-plugin-constructors'
@@ -43,62 +50,75 @@ export default [
43
50
  files: ['**/*.js'],
44
51
  ...constructors.configs.recommended
45
52
  }
46
- ];
53
+ ]
47
54
  ```
48
55
 
49
- ## Rules
50
-
51
- The plugin has 3 rules:
56
+ ## Supported rules
52
57
 
53
- ### `require-new`
58
+ <!-- eslint-disable markdown/no-missing-link-fragments -->
59
+ | ID | Description | Auto-fixable | Recommended |
60
+ | --- | --- | :-: | :-: |
61
+ | [`no-explicit-return`](#no-explicit-return) | Disallows explicit `return` statements in class constructors | ❌ | ✅ |
62
+ | [`require-error-subclass-name`](#require-error-subclass-name) | Requires classes extending `Error` to have names ending in `Error` | ❌ | ✅ |
63
+ | [`require-new`](#require-new-auto-fixable) | Requires `new` when calling error constructors | ✅ | ✅ |
64
+ <!-- eslint-enable markdown/no-missing-link-fragments -->
54
65
 
55
- Requires `new` when calling error constructors (and optionally other built‑in constructors).
66
+ ---
56
67
 
57
- #### Options
68
+ ### 𝄃𝄃𝄂𝄂𝄀𝄁𝄃𝄂𝄂𝄃 `no-explicit-return`
58
69
 
59
- - `"error-only"` (default) – only error constructors (`Error`, `TypeError`, `RangeError`, `SyntaxError`, `ReferenceError`, `URIError`, `EvalError`, `AggregateError`)
60
- - `"builtins"` – also includes `Array`, `Boolean`, `Date`, `Function`, `Number`, `Object`, `RegExp`, `String`, `Symbol`, `BigInt`, `Map`, `Set`, `WeakMap`, `WeakSet`
70
+ > Disallows explicit `return` statements inside class constructors.
61
71
 
62
- #### Example incorrect code
72
+ #### Incorrect code
63
73
 
64
74
  ```js
65
- throw Error('boom')
66
- throw TypeError('bad')
75
+ class A {
76
+ constructor() {
77
+ return
78
+ }
79
+ }
67
80
 
68
- // w/ builtins:
69
- const arr = Array(),
70
- m = Map()
81
+ class B {
82
+ constructor() {
83
+ return this
84
+ }
85
+ }
71
86
  ```
72
87
 
73
- #### Example correct code
88
+ #### Correct code
74
89
 
75
90
  ```js
76
- throw new Error('boom')
77
- throw new TypeError('bad')
78
-
79
- // w/ builtins:
80
- const arr = new Array(),
81
- m = new Map()
91
+ class A {
92
+ constructor() {
93
+ this.x = 1
94
+ }
95
+ }
82
96
  ```
83
97
 
84
98
  #### When not to use it
85
99
 
86
- If you prefer the convenience of calling built‑ins without `new` (which is allowed by the spec).
100
+ If you need to return a different object from a constructor.
101
+
102
+ <div align="center">
103
+
104
+ [Back to index ↑][epc-rules]
105
+
106
+ </div>
87
107
 
88
108
  ---
89
109
 
90
- ### `require-error-subclass-name`
110
+ ### 𝄃𝄃𝄂𝄂𝄀𝄁𝄃𝄂𝄂𝄃 `require-error-subclass-name`
91
111
 
92
- Requires classes extending `Error` have a name ending in `Error`.
112
+ > Requires classes extending `Error` have a name ending in `Error`.
93
113
 
94
- #### Example incorrect code
114
+ #### Incorrect code
95
115
 
96
116
  ```js
97
117
  class My extends Error {}
98
118
  const Custom = class extends Error {}
99
119
  ```
100
120
 
101
- #### Example correct code
121
+ #### Correct code
102
122
 
103
123
  ```js
104
124
  class MyError extends Error {}
@@ -109,42 +129,132 @@ const CustomError = class extends Error {}
109
129
 
110
130
  If you don't follow the convention of suffixing custom error classes w/ `Error`.
111
131
 
132
+ <div align="center">
133
+
134
+ [Back to index ↑][epc-rules]
135
+
136
+ </div>
137
+
112
138
  ---
113
139
 
114
- ### `no-explicit-return`
140
+ ### 𝄃𝄃𝄂𝄂𝄀𝄁𝄃𝄂𝄂𝄃 `require-new` (auto-fixable)
115
141
 
116
- Disallows explicit `return` statements inside class constructors.
142
+ > Requires `new` when calling error constructors (and optionally other built-in constructors).
117
143
 
118
- #### Incorrect Code
144
+ #### Options
145
+
146
+ - `'error-only'` (default) – only error constructors (`AggregateError`, `Error`, `EvalError`, `RangeError`, `ReferenceError`, `SyntaxError`, `TypeError`, `URIError`)
147
+ - `'builtins'` – also includes `Array`, `BigInt`, `Boolean`, `Date`, `Function`, `Map`, `Number`, `Object`, `RegExp`, `Set`, `String`, `Symbol`, `WeakMap`, `WeakSet`
148
+
149
+ #### Incorrect code
119
150
 
120
151
  ```js
121
- class A {
122
- constructor() {
123
- return
124
- }
125
- }
152
+ throw Error('boom')
153
+ throw TypeError('bad')
126
154
 
127
- class B {
128
- constructor() {
129
- return this
130
- }
131
- }
155
+ // w/ builtins:
156
+ const arr = Array(),
157
+ m = Map()
132
158
  ```
133
159
 
134
- #### Correct Code
160
+ #### Correct code
135
161
 
136
162
  ```js
137
- class A {
138
- constructor() {
139
- this.x = 1
140
- }
141
- }
163
+ throw new Error('boom')
164
+ throw new TypeError('bad')
165
+
166
+ // w/ builtins:
167
+ const arr = new Array(),
168
+ m = new Map()
142
169
  ```
143
170
 
144
171
  #### When not to use it
145
172
 
146
- If you need to return a different object from a constructor.
173
+ If you prefer the convenience of calling built-ins without `new` (which is allowed for some of them).
174
+
175
+ <div align="center">
176
+
177
+ [Back to index ↑][epc-rules]
178
+
179
+ </div>
180
+
181
+ ---
182
+
183
+ ## Shield
184
+
185
+ <a href="https://codeberg.org/adamlui/eslint-plugin-constructors/#readme">
186
+ <img height=32 alt="[Linted by eslint-plugin-constructors]" src="https://img.shields.io/badge/Linted_by-eslint--plugin--constructors-4b32c3?logo=eslint&logoColor=white&labelColor=464646&style=for-the-badge"></a>
187
+
188
+ #### HTML
189
+
190
+ ```html
191
+ <a href="https://codeberg.org/adamlui/eslint-plugin-constructors/#readme">
192
+ <img height=32 alt="[Linted by eslint-plugin-constructors]" src="https://img.shields.io/badge/Linted_by-eslint--plugin--constructors-4b32c3?logo=eslint&logoColor=white&labelColor=464646&style=for-the-badge"></a>
193
+ ```
194
+
195
+ #### Markdown
196
+
197
+ ```md
198
+ [![Linted by eslint-plugin-constructors](https://img.shields.io/badge/Linted_by-eslint--plugin--constructors-4b32c3?logo=eslint&logoColor=white&labelColor=464646&style=for-the-badge)](https://codeberg.org/adamlui/eslint-plugin-constructors/#readme)
199
+ ```
147
200
 
148
201
  ## License
149
202
 
150
- Copyright © 2026 [Adam Lui](https://codeberg.org/adamlui).
203
+ [<img height=62 src="https://cdn.staticdelivr.com/gl/adamlui/eslint-plugin-constructors/af5fe76586/assets/images/logos/mit-license/red-pixelated.png">][epc-license]
204
+
205
+ <details>
206
+
207
+ <summary>
208
+
209
+ Copyright © 2026 [Adam Lui][adamlui-cb] under the [MIT license][epc-license].
210
+
211
+ </summary><br>
212
+
213
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
214
+
215
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
216
+
217
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
218
+
219
+ </details>
220
+
221
+ ## Related
222
+
223
+ <!-- eslint-plugin-greasemonkey -->
224
+
225
+ ### [<img height=15 width=auto src="https://cdn.staticdelivr.com/gl/adamlui/eslint-plugin-constructors/9b14b5d0f2/assets/images/icons/eslint/purple/icon16.png">][epg-readme] [eslint-plugin-greasemonkey][epg-readme]
226
+
227
+ <details>
228
+
229
+ <summary>ESLint rules for Greasemonkey userscripts.</summary><br>
230
+
231
+ > [<img width=555 src="https://cdn.staticdelivr.com/gl/adamlui/eslint-plugin-greasemonkey/c9ac2985bb/assets/images/screenshots/vs-code/squiggles.png">][epg-readme]
232
+
233
+ > [**Install**][epg-install] / [Readme][epg-readme] / [Supported rules][epg-rules]
234
+
235
+ </details>
236
+
237
+ [epg-install]: https://codeberg.org/adamlui/eslint-plugin-greasemonkey/#installation
238
+ [epg-readme]: https://codeberg.org/adamlui/eslint-plugin-greasemonkey/#readme
239
+ [epg-rules]: https://codeberg.org/adamlui/eslint-plugin-greasemonkey/#supported-rules
240
+
241
+ <!-- Footer -->
242
+
243
+ <img height=10 width="100%" src="https://cdn.staticdelivr.com/gl/adamlui/ai-web-extensions/d11d2ee/assets/images/separators/gradient-aqua.png">
244
+
245
+ <div align="center">
246
+
247
+ <picture><source media="(prefers-color-scheme: dark)" srcset="https://cdn.staticdelivr.com/gl/adamlui/js-utils/6b0d399/assets/images/icons/tag/white/icon16.svg"><img height=14 src="https://cdn.staticdelivr.com/gl/adamlui/js-utils/6b0d399/assets/images/icons/tag/dark-gray/icon16.svg"></picture>
248
+ [**Latest releases**][epc-releases] / [Report a bug][epc-bugs] / [More packages by author][adamlui-npmx] / [Back to top ↑](#top)
249
+
250
+ </div>
251
+
252
+ <!-- Links -->
253
+
254
+ [adamlui-cb]: https://codeberg.org/adamlui
255
+ [adamlui-npmx]: https://npmx.dev/org/adamlui
256
+ [epc-bugs]: https://codeberg.org/adamlui/eslint-plugin-constructors/issues/new
257
+ [epc-license]: https://codeberg.org/adamlui/eslint-plugin-constructors/src/branch/main/docs/LICENSE.md
258
+ [epc-npm]: https://www.npmjs.com/package/eslint-plugin-constructors
259
+ [epc-rules]: #supported-rules
260
+ [epc-releases]: https://codeberg.org/adamlui/eslint-plugin-constructors/releases
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-constructors",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "description": "ESLint rules for constructor usage",
5
5
  "type": "module",
6
6
  "engines": {
@@ -12,12 +12,27 @@
12
12
  "url": "https://codeberg.org/adamlui"
13
13
  },
14
14
  "homepage": "https://codeberg.org/adamlui/eslint-plugin-constructors/#readme",
15
+ "bugs": {
16
+ "url": "https://codeberg.org/adamlui/eslint-plugin-constructors/issues",
17
+ "email": "support@adamlui.com"
18
+ },
15
19
  "license": "MIT",
20
+ "funding": [
21
+ {
22
+ "type": "ko-fi",
23
+ "url": "https://ko-fi.com/adamlui"
24
+ },
25
+ {
26
+ "type": "paypal",
27
+ "url": "https://paypal.me/adamlui"
28
+ }
29
+ ],
16
30
  "main": "src/index.js",
17
31
  "files": [
18
32
  "docs/",
19
33
  "src/"
20
34
  ],
35
+ "types": "src/index.d.ts",
21
36
  "scripts": {
22
37
  "prepare": "husky",
23
38
  "lint": "eslint . --cache && yamllint .",
@@ -31,6 +46,10 @@
31
46
  "bump:feat": "npm run bump:minor",
32
47
  "bump:major": "bash utils/bump.sh major"
33
48
  },
49
+ "repository": {
50
+ "type": "git",
51
+ "url": "git+https://codeberg.org/adamlui/eslint-plugin-constructors.git"
52
+ },
34
53
  "keywords": [
35
54
  "code-quality",
36
55
  "constructor",
@@ -40,6 +59,9 @@
40
59
  "eslintplugin",
41
60
  "security"
42
61
  ],
62
+ "peerDependencies": {
63
+ "eslint": ">=9.0.0"
64
+ },
43
65
  "devDependencies": {
44
66
  "@e18e/eslint-plugin": "^0.8.0",
45
67
  "@eslint/js": "^10.0.1",
@@ -49,15 +71,14 @@
49
71
  "eslint": "^10.10.0",
50
72
  "eslint-plugin-eslint-plugin": "^7.6.2",
51
73
  "eslint-plugin-import-x": "^4.17.1",
74
+ "eslint-plugin-n": "^18.3.0",
52
75
  "eslint-plugin-no-secrets": "^2.3.3",
53
76
  "eslint-plugin-no-unsanitized": "^4.1.5",
54
77
  "eslint-plugin-promise": "^7.3.0",
55
78
  "eslint-plugin-regexp": "^3.3.0",
79
+ "eslint-plugin-security": "^4.0.1",
56
80
  "globals": "^17.12.0",
57
81
  "husky": "^9.1.7",
58
82
  "yamllint-js": "^0.2.4"
59
- },
60
- "peerDependencies": {
61
- "eslint": ">=9.0.0"
62
83
  }
63
84
  }
package/src/index.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ declare const plugin: {
2
+ rules: Record<string, unknown>
3
+ configs: { recommended: Record<string, unknown> }
4
+ }
5
+
6
+ export default plugin
package/src/index.js CHANGED
@@ -21,4 +21,4 @@ plugin.configs = {
21
21
  }
22
22
  }
23
23
 
24
- export default plugin
24
+ export { plugin as default, plugin as constructors }
@@ -1,12 +1,12 @@
1
- const ERR_CONSTRUCTORS = new Set([
1
+ const errors = new Set([
2
2
  'AggregateError', 'Error', 'EvalError', 'RangeError', 'ReferenceError', 'SyntaxError', 'TypeError', 'URIError'])
3
3
 
4
- const BUILTIN_CONSTRUCTORS = new Set([
5
- 'Array', 'BigInt', 'Boolean', 'Date', 'Function', 'Number', 'Object', 'RegExp', 'String', 'Symbol', 'Map', 'Set',
4
+ const builtins = new Set([
5
+ 'Array', 'BigInt', 'Boolean', 'Date', 'Function', 'Map', 'Number', 'Object', 'RegExp', 'Set', 'String', 'Symbol',
6
6
  'WeakMap', 'WeakSet'
7
7
  ])
8
8
 
9
- function isErrConstructor(name) { return ERR_CONSTRUCTORS.has(name) }
9
+ function isErrConstructor(name) { return errors.has(name) }
10
10
 
11
11
  export default {
12
12
  meta: {
@@ -33,7 +33,7 @@ export default {
33
33
  if (node.new) return
34
34
  const calleeName = node.callee.name ; if (!calleeName) return
35
35
  const shouldReport = mode == 'error-only' ? isErrConstructor(calleeName)
36
- : isErrConstructor(calleeName) || BUILTIN_CONSTRUCTORS.has(calleeName)
36
+ : isErrConstructor(calleeName) || builtins.has(calleeName)
37
37
  if (shouldReport)
38
38
  context.report({
39
39
  node, messageId: 'requireNew', data: { name: calleeName },