eslint-plugin-constructors 1.0.1 → 1.0.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 +26 -24
- package/docs/LICENSE.md +9 -0
- package/docs/SECURITY.md +3 -0
- package/package.json +4 -2
- package/src/index.js +1 -3
- package/src/rules/no-explicit-return.js +14 -14
- package/src/rules/require-new.js +8 -12
package/README.md
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
# eslint-plugin-constructors
|
|
2
2
|
|
|
3
|
-
[](https://www.npmjs.com/package/eslint-plugin-constructors)
|
|
3
|
+
[](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>
|
|
4
6
|
[](https://eslint.org/)
|
|
5
7
|
|
|
6
|
-
ESLint
|
|
8
|
+
ESLint rules to enforce best practices for constructor usage.
|
|
7
9
|
|
|
8
10
|
## Installation
|
|
9
11
|
|
|
@@ -19,15 +21,15 @@ Add `constructors` to your flat config:
|
|
|
19
21
|
import constructors from 'eslint-plugin-constructors'
|
|
20
22
|
|
|
21
23
|
export default [
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
24
|
+
{
|
|
25
|
+
files: ['**/*.js'],
|
|
26
|
+
plugins: { constructors },
|
|
27
|
+
rules: {
|
|
28
|
+
'constructors/no-explicit-return': 'error',
|
|
29
|
+
'constructors/require-error-subclass-name': 'error',
|
|
30
|
+
'constructors/require-new': 'error'
|
|
31
|
+
}
|
|
32
|
+
}
|
|
31
33
|
]
|
|
32
34
|
```
|
|
33
35
|
|
|
@@ -37,10 +39,10 @@ Or use the bundled recommended config:
|
|
|
37
39
|
import constructors from 'eslint-plugin-constructors'
|
|
38
40
|
|
|
39
41
|
export default [
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
{
|
|
43
|
+
files: ['**/*.js'],
|
|
44
|
+
...constructors.configs.recommended
|
|
45
|
+
}
|
|
44
46
|
];
|
|
45
47
|
```
|
|
46
48
|
|
|
@@ -117,15 +119,15 @@ Disallows explicit `return` statements inside class constructors.
|
|
|
117
119
|
|
|
118
120
|
```js
|
|
119
121
|
class A {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
122
|
+
constructor() {
|
|
123
|
+
return
|
|
124
|
+
}
|
|
123
125
|
}
|
|
124
126
|
|
|
125
127
|
class B {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
128
|
+
constructor() {
|
|
129
|
+
return this
|
|
130
|
+
}
|
|
129
131
|
}
|
|
130
132
|
```
|
|
131
133
|
|
|
@@ -133,9 +135,9 @@ class B {
|
|
|
133
135
|
|
|
134
136
|
```js
|
|
135
137
|
class A {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
138
|
+
constructor() {
|
|
139
|
+
this.x = 1
|
|
140
|
+
}
|
|
139
141
|
}
|
|
140
142
|
```
|
|
141
143
|
|
package/docs/LICENSE.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# 🏛️ MIT License
|
|
2
|
+
|
|
3
|
+
**Copyright © 2026 [Adam Lui](https://codeberg.org/adamlui).**
|
|
4
|
+
|
|
5
|
+
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:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
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
|
package/docs/SECURITY.md
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-constructors",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "ESLint rules for constructor usage",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -15,7 +15,8 @@
|
|
|
15
15
|
"license": "MIT",
|
|
16
16
|
"main": "src/index.js",
|
|
17
17
|
"files": [
|
|
18
|
-
"
|
|
18
|
+
"docs/",
|
|
19
|
+
"src/"
|
|
19
20
|
],
|
|
20
21
|
"scripts": {
|
|
21
22
|
"prepare": "husky",
|
|
@@ -46,6 +47,7 @@
|
|
|
46
47
|
"@eslint/markdown": "^8.0.3",
|
|
47
48
|
"@stylistic/eslint-plugin": "^5.10.0",
|
|
48
49
|
"eslint": "^10.10.0",
|
|
50
|
+
"eslint-plugin-eslint-plugin": "^7.6.2",
|
|
49
51
|
"eslint-plugin-import-x": "^4.17.1",
|
|
50
52
|
"eslint-plugin-no-secrets": "^2.3.3",
|
|
51
53
|
"eslint-plugin-no-unsanitized": "^4.1.5",
|
package/src/index.js
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
const reFuncNodeType = /^(?:Arrow)?Function(?:Declaration|Expression)$/
|
|
2
2
|
|
|
3
|
+
function isInsideConstructorFunc(node) {
|
|
4
|
+
let current = node.parent
|
|
5
|
+
while (current) {
|
|
6
|
+
if (reFuncNodeType.test(current.type))
|
|
7
|
+
return ( // whether this func is constructor's val
|
|
8
|
+
current.parent?.type == 'MethodDefinition'
|
|
9
|
+
&& current.parent.kind == 'constructor'
|
|
10
|
+
&& !current.parent.static
|
|
11
|
+
)
|
|
12
|
+
current = current.parent
|
|
13
|
+
}
|
|
14
|
+
return false
|
|
15
|
+
}
|
|
16
|
+
|
|
3
17
|
export default {
|
|
4
18
|
meta: {
|
|
5
19
|
type: 'problem',
|
|
@@ -13,20 +27,6 @@ export default {
|
|
|
13
27
|
},
|
|
14
28
|
|
|
15
29
|
create(context) {
|
|
16
|
-
function isInsideConstructorFunc(node) {
|
|
17
|
-
let current = node.parent
|
|
18
|
-
while (current) {
|
|
19
|
-
if (reFuncNodeType.test(current.type))
|
|
20
|
-
return ( // whether this func is constructor's val
|
|
21
|
-
current.parent?.type == 'MethodDefinition'
|
|
22
|
-
&& current.parent.kind == 'constructor'
|
|
23
|
-
&& !current.parent.static
|
|
24
|
-
)
|
|
25
|
-
current = current.parent
|
|
26
|
-
}
|
|
27
|
-
return false
|
|
28
|
-
}
|
|
29
|
-
|
|
30
30
|
return {
|
|
31
31
|
ReturnStatement(node) {
|
|
32
32
|
if (isInsideConstructorFunc(node))
|
package/src/rules/require-new.js
CHANGED
|
@@ -1,17 +1,8 @@
|
|
|
1
1
|
const ERR_CONSTRUCTORS = new Set([
|
|
2
|
-
'Error',
|
|
3
|
-
'TypeError',
|
|
4
|
-
'RangeError',
|
|
5
|
-
'SyntaxError',
|
|
6
|
-
'ReferenceError',
|
|
7
|
-
'URIError',
|
|
8
|
-
'EvalError',
|
|
9
|
-
'AggregateError'
|
|
10
|
-
])
|
|
2
|
+
'AggregateError', 'Error', 'EvalError', 'RangeError', 'ReferenceError', 'SyntaxError', 'TypeError', 'URIError'])
|
|
11
3
|
|
|
12
4
|
const BUILTIN_CONSTRUCTORS = new Set([
|
|
13
|
-
'Array', 'Boolean', 'Date', 'Function', 'Number', 'Object',
|
|
14
|
-
'RegExp', 'String', 'Symbol', 'BigInt', 'Map', 'Set',
|
|
5
|
+
'Array', 'BigInt', 'Boolean', 'Date', 'Function', 'Number', 'Object', 'RegExp', 'String', 'Symbol', 'Map', 'Set',
|
|
15
6
|
'WeakMap', 'WeakSet'
|
|
16
7
|
])
|
|
17
8
|
|
|
@@ -26,7 +17,12 @@ export default {
|
|
|
26
17
|
recommended: true
|
|
27
18
|
},
|
|
28
19
|
fixable: 'code',
|
|
29
|
-
schema: [{
|
|
20
|
+
schema: [{
|
|
21
|
+
type: 'string',
|
|
22
|
+
enum: ['error-only', 'builtins'],
|
|
23
|
+
description: 'Constructor categories to check: "error-only" (default) or "builtins".'
|
|
24
|
+
}],
|
|
25
|
+
defaultOptions: ['error-only'],
|
|
30
26
|
messages: { requireNew: 'Use `new {{name}}()` instead of `{{name}}()`' }
|
|
31
27
|
},
|
|
32
28
|
|