eslint-plugin-constructors 1.0.2 → 1.0.3
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 +115 -51
- package/package.json +24 -4
- package/src/index.d.ts +6 -0
- package/src/rules/require-new.js +1 -1
package/README.md
CHANGED
|
@@ -1,14 +1,19 @@
|
|
|
1
|
+
<a id="top"></a>
|
|
2
|
+
|
|
1
3
|
# eslint-plugin-constructors
|
|
2
4
|
|
|
3
|
-
[]
|
|
4
|
-
|
|
5
|
-
<img src="https://img.shields.io/badge/License-MIT-f99b27.svg?logo=internetarchive&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
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,71 @@ export default [
|
|
|
43
50
|
files: ['**/*.js'],
|
|
44
51
|
...constructors.configs.recommended
|
|
45
52
|
}
|
|
46
|
-
]
|
|
53
|
+
]
|
|
47
54
|
```
|
|
48
55
|
|
|
49
56
|
## Rules
|
|
50
57
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
58
|
+
- [`no-explicit-return`](#no-explicit-return)
|
|
59
|
+
- [`require-error-subclass-name`](#require-error-subclass-name)
|
|
60
|
+
- [`require-new`](#require-new)
|
|
54
61
|
|
|
55
|
-
|
|
62
|
+
---
|
|
56
63
|
|
|
57
|
-
|
|
64
|
+
### `no-explicit-return`
|
|
58
65
|
|
|
59
|
-
|
|
60
|
-
- `"builtins"` – also includes `Array`, `Boolean`, `Date`, `Function`, `Number`, `Object`, `RegExp`, `String`, `Symbol`, `BigInt`, `Map`, `Set`, `WeakMap`, `WeakSet`
|
|
66
|
+
> Disallows explicit `return` statements inside class constructors.
|
|
61
67
|
|
|
62
|
-
####
|
|
68
|
+
#### Incorrect code
|
|
63
69
|
|
|
64
70
|
```js
|
|
65
|
-
|
|
66
|
-
|
|
71
|
+
class A {
|
|
72
|
+
constructor() {
|
|
73
|
+
return
|
|
74
|
+
}
|
|
75
|
+
}
|
|
67
76
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
77
|
+
class B {
|
|
78
|
+
constructor() {
|
|
79
|
+
return this
|
|
80
|
+
}
|
|
81
|
+
}
|
|
71
82
|
```
|
|
72
83
|
|
|
73
|
-
####
|
|
84
|
+
#### Correct code
|
|
74
85
|
|
|
75
86
|
```js
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
m = new Map()
|
|
87
|
+
class A {
|
|
88
|
+
constructor() {
|
|
89
|
+
this.x = 1
|
|
90
|
+
}
|
|
91
|
+
}
|
|
82
92
|
```
|
|
83
93
|
|
|
84
94
|
#### When not to use it
|
|
85
95
|
|
|
86
|
-
If you
|
|
96
|
+
If you need to return a different object from a constructor.
|
|
97
|
+
|
|
98
|
+
<div align="center">
|
|
99
|
+
|
|
100
|
+
[Back to index ↑][epc-rules]
|
|
101
|
+
|
|
102
|
+
</div>
|
|
87
103
|
|
|
88
104
|
---
|
|
89
105
|
|
|
90
106
|
### `require-error-subclass-name`
|
|
91
107
|
|
|
92
|
-
Requires classes extending `Error` have a name ending in `Error`.
|
|
108
|
+
> Requires classes extending `Error` have a name ending in `Error`.
|
|
93
109
|
|
|
94
|
-
####
|
|
110
|
+
#### Incorrect code
|
|
95
111
|
|
|
96
112
|
```js
|
|
97
113
|
class My extends Error {}
|
|
98
114
|
const Custom = class extends Error {}
|
|
99
115
|
```
|
|
100
116
|
|
|
101
|
-
####
|
|
117
|
+
#### Correct code
|
|
102
118
|
|
|
103
119
|
```js
|
|
104
120
|
class MyError extends Error {}
|
|
@@ -109,42 +125,90 @@ const CustomError = class extends Error {}
|
|
|
109
125
|
|
|
110
126
|
If you don't follow the convention of suffixing custom error classes w/ `Error`.
|
|
111
127
|
|
|
128
|
+
<div align="center">
|
|
129
|
+
|
|
130
|
+
[Back to index ↑][epc-rules]
|
|
131
|
+
|
|
132
|
+
</div>
|
|
133
|
+
|
|
112
134
|
---
|
|
113
135
|
|
|
114
|
-
### `
|
|
136
|
+
### `require-new`
|
|
137
|
+
|
|
138
|
+
> Requires `new` when calling error constructors (and optionally other built‑in constructors).
|
|
139
|
+
|
|
140
|
+
#### Options
|
|
115
141
|
|
|
116
|
-
|
|
142
|
+
- `'error-only'` (default) – only error constructors (`AggregateError`, `Error`, `EvalError`, `RangeError`, `ReferenceError`, `SyntaxError`, `TypeError`, `URIError`)
|
|
143
|
+
- `'builtins'` – also includes `Array`, `BigInt`, `Boolean`, `Date`, `Function`, `Map`, `Number`, `Object`, `RegExp`, `Set`, `String`, `Symbol`, `WeakMap`, `WeakSet`
|
|
117
144
|
|
|
118
|
-
#### Incorrect
|
|
145
|
+
#### Incorrect code
|
|
119
146
|
|
|
120
147
|
```js
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
return
|
|
124
|
-
}
|
|
125
|
-
}
|
|
148
|
+
throw Error('boom')
|
|
149
|
+
throw TypeError('bad')
|
|
126
150
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
}
|
|
131
|
-
}
|
|
151
|
+
// w/ builtins:
|
|
152
|
+
const arr = Array(),
|
|
153
|
+
m = Map()
|
|
132
154
|
```
|
|
133
155
|
|
|
134
|
-
#### Correct
|
|
156
|
+
#### Correct code
|
|
135
157
|
|
|
136
158
|
```js
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
159
|
+
throw new Error('boom')
|
|
160
|
+
throw new TypeError('bad')
|
|
161
|
+
|
|
162
|
+
// w/ builtins:
|
|
163
|
+
const arr = new Array(),
|
|
164
|
+
m = new Map()
|
|
142
165
|
```
|
|
143
166
|
|
|
144
167
|
#### When not to use it
|
|
145
168
|
|
|
146
|
-
If you
|
|
169
|
+
If you prefer the convenience of calling built‑ins without `new` (which is allowed by the spec).
|
|
170
|
+
|
|
171
|
+
<div align="center">
|
|
172
|
+
|
|
173
|
+
[Back to index ↑][epc-rules]
|
|
174
|
+
|
|
175
|
+
</div>
|
|
176
|
+
|
|
177
|
+
---
|
|
147
178
|
|
|
148
179
|
## License
|
|
149
180
|
|
|
150
|
-
Copyright © 2026 [Adam Lui](
|
|
181
|
+
> Copyright © 2026 [Adam Lui][adamlui-cb] under the [MIT license](./docs/LICENSE.md).
|
|
182
|
+
|
|
183
|
+
## Related
|
|
184
|
+
|
|
185
|
+
<!-- eslint-plugin-greasemonkey -->
|
|
186
|
+
|
|
187
|
+
### [<img width=16 src="https://www.google.com/s2/favicons?domain=eslint.org&sz=16">][epg-readme] [eslint-plugin-greasemonkey][epg-readme]
|
|
188
|
+
|
|
189
|
+
<details>
|
|
190
|
+
|
|
191
|
+
<summary>ESLint rules for Greasemonkey userscripts.</summary><br>
|
|
192
|
+
|
|
193
|
+
> [<img width=555 src="https://cdn.staticdelivr.com/gl/adamlui/eslint-plugin-greasemonkey/c9ac2985bb/assets/images/screenshots/vs-code/squiggles.png">][epg-readme]
|
|
194
|
+
|
|
195
|
+
> [**Install**][epg-install] / [Readme][epg-readme] / [Report bug][epg-issues]
|
|
196
|
+
|
|
197
|
+
</details>
|
|
198
|
+
|
|
199
|
+
---
|
|
200
|
+
|
|
201
|
+
<div align="center">
|
|
202
|
+
|
|
203
|
+
[**Latest releases**][epc-releases] / [More packages by author][adamlui-npmx] / [Back to top ↑](#top)
|
|
204
|
+
|
|
205
|
+
</div>
|
|
206
|
+
|
|
207
|
+
[adamlui-cb]: https://codeberg.org/adamlui
|
|
208
|
+
[adamlui-npmx]: https://npmx.dev/org/adamlui
|
|
209
|
+
[epc-npm]: https://www.npmjs.com/package/eslint-plugin-constructors
|
|
210
|
+
[epc-rules]: #rules
|
|
211
|
+
[epc-releases]: https://codeberg.org/adamlui/eslint-plugin-constructors/releases
|
|
212
|
+
[epg-install]: https://codeberg.org/adamlui/eslint-plugin-greasemonkey/#installation
|
|
213
|
+
[epg-readme]: https://codeberg.org/adamlui/eslint-plugin-greasemonkey/#readme
|
|
214
|
+
[epg-issues]: https://codeberg.org/adamlui/eslint-plugin-greasemonkey/issues/new
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-constructors",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
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,6 +71,7 @@
|
|
|
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",
|
|
@@ -56,8 +79,5 @@
|
|
|
56
79
|
"globals": "^17.12.0",
|
|
57
80
|
"husky": "^9.1.7",
|
|
58
81
|
"yamllint-js": "^0.2.4"
|
|
59
|
-
},
|
|
60
|
-
"peerDependencies": {
|
|
61
|
-
"eslint": ">=9.0.0"
|
|
62
82
|
}
|
|
63
83
|
}
|
package/src/index.d.ts
ADDED
package/src/rules/require-new.js
CHANGED
|
@@ -2,7 +2,7 @@ const ERR_CONSTRUCTORS = new Set([
|
|
|
2
2
|
'AggregateError', 'Error', 'EvalError', 'RangeError', 'ReferenceError', 'SyntaxError', 'TypeError', 'URIError'])
|
|
3
3
|
|
|
4
4
|
const BUILTIN_CONSTRUCTORS = new Set([
|
|
5
|
-
'Array', 'BigInt', 'Boolean', 'Date', 'Function', '
|
|
5
|
+
'Array', 'BigInt', 'Boolean', 'Date', 'Function', 'Map', 'Number', 'Object', 'RegExp', 'Set', 'String', 'Symbol',
|
|
6
6
|
'WeakMap', 'WeakSet'
|
|
7
7
|
])
|
|
8
8
|
|