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 +164 -54
- package/package.json +25 -4
- package/src/index.d.ts +6 -0
- package/src/index.js +1 -1
- package/src/rules/require-new.js +5 -5
package/README.md
CHANGED
|
@@ -1,14 +1,19 @@
|
|
|
1
|
-
|
|
1
|
+
<a id="top"></a>
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
+
[][epc-npm]
|
|
6
|
+
[](#license)
|
|
6
7
|
[](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
|
-
|
|
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
|
-
##
|
|
50
|
-
|
|
51
|
-
The plugin has 3 rules:
|
|
56
|
+
## Supported rules
|
|
52
57
|
|
|
53
|
-
|
|
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
|
-
|
|
66
|
+
---
|
|
56
67
|
|
|
57
|
-
|
|
68
|
+
### 𝄃𝄃𝄂𝄂𝄀𝄁𝄃𝄂𝄂𝄃 `no-explicit-return`
|
|
58
69
|
|
|
59
|
-
|
|
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
|
-
####
|
|
72
|
+
#### Incorrect code
|
|
63
73
|
|
|
64
74
|
```js
|
|
65
|
-
|
|
66
|
-
|
|
75
|
+
class A {
|
|
76
|
+
constructor() {
|
|
77
|
+
return
|
|
78
|
+
}
|
|
79
|
+
}
|
|
67
80
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
81
|
+
class B {
|
|
82
|
+
constructor() {
|
|
83
|
+
return this
|
|
84
|
+
}
|
|
85
|
+
}
|
|
71
86
|
```
|
|
72
87
|
|
|
73
|
-
####
|
|
88
|
+
#### Correct code
|
|
74
89
|
|
|
75
90
|
```js
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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
|
|
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
|
-
####
|
|
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
|
-
####
|
|
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
|
-
### `
|
|
140
|
+
### 𝄃𝄃𝄂𝄂𝄀𝄁𝄃𝄂𝄂𝄃 `require-new` (auto-fixable)
|
|
115
141
|
|
|
116
|
-
|
|
142
|
+
> Requires `new` when calling error constructors (and optionally other built-in constructors).
|
|
117
143
|
|
|
118
|
-
####
|
|
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
|
-
|
|
122
|
-
|
|
123
|
-
return
|
|
124
|
-
}
|
|
125
|
-
}
|
|
152
|
+
throw Error('boom')
|
|
153
|
+
throw TypeError('bad')
|
|
126
154
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
}
|
|
131
|
-
}
|
|
155
|
+
// w/ builtins:
|
|
156
|
+
const arr = Array(),
|
|
157
|
+
m = Map()
|
|
132
158
|
```
|
|
133
159
|
|
|
134
|
-
#### Correct
|
|
160
|
+
#### Correct code
|
|
135
161
|
|
|
136
162
|
```js
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
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
|
|
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
|
+
[](https://codeberg.org/adamlui/eslint-plugin-constructors/#readme)
|
|
199
|
+
```
|
|
147
200
|
|
|
148
201
|
## License
|
|
149
202
|
|
|
150
|
-
|
|
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
|
|
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
package/src/index.js
CHANGED
package/src/rules/require-new.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
const
|
|
1
|
+
const errors = new Set([
|
|
2
2
|
'AggregateError', 'Error', 'EvalError', 'RangeError', 'ReferenceError', 'SyntaxError', 'TypeError', 'URIError'])
|
|
3
3
|
|
|
4
|
-
const
|
|
5
|
-
'Array', 'BigInt', 'Boolean', 'Date', 'Function', '
|
|
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
|
|
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) ||
|
|
36
|
+
: isErrConstructor(calleeName) || builtins.has(calleeName)
|
|
37
37
|
if (shouldReport)
|
|
38
38
|
context.report({
|
|
39
39
|
node, messageId: 'requireNew', data: { name: calleeName },
|