eslint-plugin-constructors 1.2.0 → 1.2.2
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 +94 -46
- package/package.json +1 -1
- package/src/data/deprecations.js +3 -0
- package/src/index.js +3 -0
- package/src/rules/require-new.js +10 -4
package/README.md
CHANGED
|
@@ -22,7 +22,12 @@ npm i -D eslint-plugin-constructors
|
|
|
22
22
|
|
|
23
23
|
---
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
<h2 id="usage">
|
|
26
|
+
Usage
|
|
27
|
+
<a title="Copy section URL" target="_blank"
|
|
28
|
+
href="https://copy-to-clipboard.onrender.com/copy/https%3A%2F%2Fcodeberg.org%2Fadamlui%2Feslint-plugin-constructors%2F%23usage">
|
|
29
|
+
<img height=17 width=22 alt="[Copy section URL]" src="https://api.iconify.design/mdi/content-copy.svg?color=%23cccccc" align="bottom"></a>
|
|
30
|
+
</h2>
|
|
26
31
|
|
|
27
32
|
Add `constructors` to your flat config:
|
|
28
33
|
|
|
@@ -59,44 +64,63 @@ export default [
|
|
|
59
64
|
|
|
60
65
|
---
|
|
61
66
|
|
|
62
|
-
|
|
67
|
+
<h2 id="supported-rules">
|
|
68
|
+
Supported rules
|
|
69
|
+
<a title="Copy section URL" target="_blank"
|
|
70
|
+
href="https://copy-to-clipboard.onrender.com/copy/https%3A%2F%2Fcodeberg.org%2Fadamlui%2Feslint-plugin-constructors%2F%23supported-rules">
|
|
71
|
+
<img height=17 width=22 alt="[Copy section URL]" src="https://api.iconify.design/mdi/content-copy.svg?color=%23cccccc" align="bottom"></a>
|
|
72
|
+
</h2>
|
|
63
73
|
|
|
64
74
|
| ID | Description | Auto-fixable | Recommended |
|
|
65
75
|
| --- | --- | :-: | :-: |
|
|
66
76
|
| [`no-explicit-return`](#no-explicit-return) | Disallows explicit `return` statements in class constructors | ❌ | ✅ |
|
|
67
77
|
| [`require-error-subclass-name`](#require-error-subclass-name) | Requires classes extending `Error` to have names ending in `Error` | ❌ | ✅ |
|
|
68
|
-
| [`require-new`](#require-new
|
|
78
|
+
| [`require-new`](#require-new) | Requires `new` when calling error constructors | ✅ | ✅ |
|
|
69
79
|
|
|
70
80
|
---
|
|
71
81
|
|
|
72
|
-
|
|
82
|
+
<h3 id="no-explicit-return">
|
|
83
|
+
<code>no-explicit-return</code>
|
|
84
|
+
<a title="Included in recommended config" href="#usage">
|
|
85
|
+
<img height=18 alt="[Recommended]" src="https://img.shields.io/badge/Recommended-f1f20a?style=flat-square" align="middle"></a>
|
|
86
|
+
<a title="Copy rule URL" target="_blank"
|
|
87
|
+
href="https://copy-to-clipboard.onrender.com/copy/https%3A%2F%2Fcodeberg.org%2Fadamlui%2Feslint-plugin-constructors%2F%23no-explicit-return">
|
|
88
|
+
<img height=16 width=21 alt="[Copy rule URL]" src="https://api.iconify.design/mdi/content-copy.svg?color=%23cccccc" align="middle"></a>
|
|
89
|
+
</h3>
|
|
73
90
|
|
|
74
91
|
> Disallows explicit `return` statements inside class constructors.
|
|
75
92
|
|
|
76
93
|
#### Incorrect code
|
|
77
94
|
|
|
78
|
-
```
|
|
79
|
-
class A {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
class B {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
95
|
+
```diff
|
|
96
|
+
class A {
|
|
97
|
+
constructor() {
|
|
98
|
+
! return
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
class B {
|
|
103
|
+
constructor() {
|
|
104
|
+
! return this
|
|
105
|
+
}
|
|
106
|
+
}
|
|
90
107
|
```
|
|
91
108
|
|
|
92
109
|
#### Correct code
|
|
93
110
|
|
|
94
|
-
```
|
|
95
|
-
class A {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}
|
|
111
|
+
```diff
|
|
112
|
+
class A {
|
|
113
|
+
constructor() {
|
|
114
|
+
- return
|
|
115
|
+
+ this.x = 1
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
class B {
|
|
119
|
+
constructor() {
|
|
120
|
+
- return this
|
|
121
|
+
+ this.x = 1
|
|
122
|
+
}
|
|
123
|
+
}
|
|
100
124
|
```
|
|
101
125
|
|
|
102
126
|
#### When not to use it
|
|
@@ -111,22 +135,31 @@ If you need to return a different object from a constructor.
|
|
|
111
135
|
|
|
112
136
|
---
|
|
113
137
|
|
|
114
|
-
|
|
138
|
+
<h3 id="require-error-subclass-name">
|
|
139
|
+
<code>require-error-subclass-name</code>
|
|
140
|
+
<a title="Included in recommended config" href="#usage">
|
|
141
|
+
<img height=18 alt="[Recommended]" src="https://img.shields.io/badge/Recommended-f1f20a?style=flat-square" align="middle"></a>
|
|
142
|
+
<a title="Copy rule URL" target="_blank"
|
|
143
|
+
href="https://copy-to-clipboard.onrender.com/copy/https%3A%2F%2Fcodeberg.org%2Fadamlui%2Feslint-plugin-constructors%2F%23require-error-subclass-name">
|
|
144
|
+
<img height=16 width=21 alt="[Copy rule URL]" src="https://api.iconify.design/mdi/content-copy.svg?color=%23cccccc" align="middle"></a>
|
|
145
|
+
</h3>
|
|
115
146
|
|
|
116
147
|
> Requires classes extending `Error` have a name ending in `Error`.
|
|
117
148
|
|
|
118
149
|
#### Incorrect code
|
|
119
150
|
|
|
120
|
-
```
|
|
121
|
-
class My extends Error {}
|
|
122
|
-
const Custom = class extends Error {}
|
|
151
|
+
```diff
|
|
152
|
+
! class My extends Error {}
|
|
153
|
+
! const Custom = class extends Error {}
|
|
123
154
|
```
|
|
124
155
|
|
|
125
156
|
#### Correct code
|
|
126
157
|
|
|
127
|
-
```
|
|
128
|
-
class
|
|
129
|
-
const
|
|
158
|
+
```diff
|
|
159
|
+
- class My extends Error {}
|
|
160
|
+
- const Custom = class extends Error {}
|
|
161
|
+
+ class MyError extends Error {}
|
|
162
|
+
+ const CustomError = class extends Error {}
|
|
130
163
|
```
|
|
131
164
|
|
|
132
165
|
#### When not to use it
|
|
@@ -141,7 +174,16 @@ If you don't follow the convention of suffixing custom error classes w/ `Error`.
|
|
|
141
174
|
|
|
142
175
|
---
|
|
143
176
|
|
|
144
|
-
|
|
177
|
+
<h3 id="require-new">
|
|
178
|
+
<code>require-new</code>
|
|
179
|
+
<a title="Included in recommended config" href="#usage">
|
|
180
|
+
<img height=18 alt="[Recommended]" src="https://img.shields.io/badge/Recommended-f1f20a?style=flat-square" align="middle"></a>
|
|
181
|
+
<a title="Accepts eslint --fix" target="_blank" href="https://eslint.org/docs/latest/use/command-line-interface#--fix">
|
|
182
|
+
<img height=18 alt="[Auto-fixable]" src="https://img.shields.io/badge/Auto--fixable-e8357a?style=flat-square" align="middle"></a>
|
|
183
|
+
<a title="Copy rule URL" target="_blank"
|
|
184
|
+
href="https://copy-to-clipboard.onrender.com/copy/https%3A%2F%2Fcodeberg.org%2Fadamlui%2Feslint-plugin-constructors%2F%23require-new">
|
|
185
|
+
<img height=16 width=21 alt="[Copy rule URL]" src="https://api.iconify.design/mdi/content-copy.svg?color=%23cccccc" align="middle"></a>
|
|
186
|
+
</h3>
|
|
145
187
|
|
|
146
188
|
> Requires `new` when calling error constructors (and optionally other built-ins).
|
|
147
189
|
|
|
@@ -155,25 +197,31 @@ Excluded: `Boolean`, `Number`, `String` (calling w/ `new` returns wrapper object
|
|
|
155
197
|
|
|
156
198
|
#### Incorrect code
|
|
157
199
|
|
|
158
|
-
```
|
|
159
|
-
|
|
160
|
-
throw Error('boom')
|
|
200
|
+
```diff
|
|
201
|
+
/* eslint constructors/require-new: ['error', 'errors'] */
|
|
202
|
+
! throw Error('boom')
|
|
161
203
|
|
|
162
|
-
|
|
163
|
-
const m = Map()
|
|
204
|
+
/* eslint constructors/require-new: ['error', 'throwing'] */
|
|
205
|
+
! const m = Map()
|
|
164
206
|
|
|
165
|
-
|
|
166
|
-
const arr = Array()
|
|
207
|
+
/* eslint constructors/require-new: ['error', 'all'] */
|
|
208
|
+
! const arr = Array()
|
|
167
209
|
```
|
|
168
210
|
|
|
169
211
|
#### Correct code
|
|
170
212
|
|
|
171
|
-
```
|
|
172
|
-
|
|
213
|
+
```diff
|
|
214
|
+
/* eslint constructors/require-new: ['error', 'errors'] */
|
|
215
|
+
- throw Error('boom')
|
|
216
|
+
+ throw new Error('boom')
|
|
173
217
|
|
|
174
|
-
|
|
218
|
+
/* eslint constructors/require-new: ['error', 'throwing'] */
|
|
219
|
+
- const m = Map()
|
|
220
|
+
+ const m = new Map()
|
|
175
221
|
|
|
176
|
-
|
|
222
|
+
/* eslint constructors/require-new: ['error', 'all'] */
|
|
223
|
+
- const arr = Array()
|
|
224
|
+
+ const arr = new Array()
|
|
177
225
|
```
|
|
178
226
|
|
|
179
227
|
#### When not to use it
|
|
@@ -188,22 +236,22 @@ If you prefer calling built-ins w/o `new` (which the runtime allows for some of
|
|
|
188
236
|
|
|
189
237
|
---
|
|
190
238
|
|
|
191
|
-
## Shield
|
|
239
|
+
## Shield embed
|
|
192
240
|
|
|
193
241
|
<a href="https://codeberg.org/adamlui/eslint-plugin-constructors/#readme">
|
|
194
|
-
<img height=32 alt="[Linted by eslint-plugin-constructors]" src="https://img.shields.io/badge/Linted_by-eslint--plugin--constructors-
|
|
242
|
+
<img height=32 alt="[Linted by eslint-plugin-constructors]" src="https://img.shields.io/badge/Linted_by-eslint--plugin--constructors-black?logo=eslint&logoColor=white&labelColor=4B32C3&style=for-the-badge"></a>
|
|
195
243
|
|
|
196
244
|
#### HTML
|
|
197
245
|
|
|
198
246
|
```html
|
|
199
247
|
<a href="https://codeberg.org/adamlui/eslint-plugin-constructors/#readme">
|
|
200
|
-
<img height=32 alt="[Linted by eslint-plugin-constructors]" src="https://img.shields.io/badge/Linted_by-eslint--plugin--constructors-
|
|
248
|
+
<img height=32 alt="[Linted by eslint-plugin-constructors]" src="https://img.shields.io/badge/Linted_by-eslint--plugin--constructors-black?logo=eslint&logoColor=white&labelColor=4B32C3&style=for-the-badge"></a>
|
|
201
249
|
```
|
|
202
250
|
|
|
203
251
|
#### Markdown
|
|
204
252
|
|
|
205
253
|
```md
|
|
206
|
-
[](https://codeberg.org/adamlui/eslint-plugin-constructors/#readme)
|
|
207
255
|
```
|
|
208
256
|
|
|
209
257
|
---
|
|
@@ -214,7 +262,7 @@ If you prefer calling built-ins w/o `new` (which the runtime allows for some of
|
|
|
214
262
|
|
|
215
263
|
<details>
|
|
216
264
|
|
|
217
|
-
<summary>Copyright © 2026 <a href="https://codeberg.org/adamlui">Adam Lui</a> under the <a href="https://codeberg.org/adamlui/eslint-plugin-constructors/src/branch/main/docs/LICENSE.md">MIT license</a>.</summary><br>
|
|
265
|
+
<summary>Copyright © 2026 <a href="https://codeberg.org/adamlui"><b>Adam Lui</b></a> under the <a href="https://codeberg.org/adamlui/eslint-plugin-constructors/src/branch/main/docs/LICENSE.md"><b>MIT license</b></a>.</summary><br>
|
|
218
266
|
|
|
219
267
|
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:
|
|
220
268
|
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -10,6 +10,9 @@ const plugin = {
|
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
+
for (const [ruleId, rule] of Object.entries(plugin.rules))
|
|
14
|
+
rule.meta.docs.url = `https://codeberg.org/adamlui/eslint-plugin-constructors/#${ruleId}`
|
|
15
|
+
|
|
13
16
|
plugin.configs = {
|
|
14
17
|
recommended: {
|
|
15
18
|
plugins: { constructors: plugin },
|
package/src/rules/require-new.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import deprecations from '../data/deprecations.js'
|
|
1
2
|
import globals from 'globals'
|
|
2
3
|
|
|
3
4
|
const errorConstructors = new Set(Object.keys(globals.builtin).filter(name => name.endsWith('Error')))
|
|
@@ -37,10 +38,15 @@ export default {
|
|
|
37
38
|
},
|
|
38
39
|
|
|
39
40
|
create(context) {
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
41
|
+
const optionAliases = deprecations.options['require-new'] || {},
|
|
42
|
+
rawMode = context.options[0] || 'errors'
|
|
43
|
+
/* eslint-disable security/detect-object-injection */
|
|
44
|
+
let mode = optionAliases[rawMode] || rawMode, deprecation
|
|
45
|
+
if (optionAliases[rawMode])
|
|
46
|
+
deprecation = { old: rawMode, new: optionAliases[rawMode] }
|
|
47
|
+
/* eslint-enable security/detect-object-injection */
|
|
48
|
+
else if (rawMode == 'builtins')
|
|
49
|
+
mode = 'legacy-builtins'
|
|
44
50
|
|
|
45
51
|
function shouldReportName(name) {
|
|
46
52
|
return mode == 'errors' ? errorConstructors.has(name)
|