eslint-plugin-hyoban 0.1.10 → 0.2.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/LICENSE +21 -21
- package/README.md +19 -0
- package/dist/index.cjs +98 -217
- package/dist/index.d.cts +14 -0
- package/dist/index.d.ts +7 -5
- package/dist/index.js +115 -0
- package/package.json +73 -11
- package/dist/index.mjs +0 -234
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c)
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Stephen Zhou
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# eslint-plugin-hyoban
|
|
2
|
+
|
|
3
|
+
[![npm version][npm-version-src]][npm-version-href]
|
|
4
|
+
[![npm downloads][npm-downloads-src]][npm-downloads-href]
|
|
5
|
+
[![bundle][bundle-src]][bundle-href]
|
|
6
|
+
[![JSDocs][jsdocs-src]][jsdocs-href]
|
|
7
|
+
|
|
8
|
+
Prefer early return pattern to clean if else statement
|
|
9
|
+
|
|
10
|
+
<!-- Badges -->
|
|
11
|
+
|
|
12
|
+
[npm-version-src]: https://img.shields.io/npm/v/eslint-plugin-hyoban?style=flat&colorA=080f12&colorB=1fa669
|
|
13
|
+
[npm-version-href]: https://npmjs.com/package/eslint-plugin-hyoban
|
|
14
|
+
[npm-downloads-src]: https://img.shields.io/npm/dm/eslint-plugin-hyoban?style=flat&colorA=080f12&colorB=1fa669
|
|
15
|
+
[npm-downloads-href]: https://npmjs.com/package/eslint-plugin-hyoban
|
|
16
|
+
[bundle-src]: https://img.shields.io/bundlephobia/minzip/eslint-plugin-hyoban?style=flat&colorA=080f12&colorB=1fa669&label=minzip
|
|
17
|
+
[bundle-href]: https://bundlephobia.com/result?p=eslint-plugin-hyoban
|
|
18
|
+
[jsdocs-src]: https://img.shields.io/badge/jsdocs-reference-080f12?style=flat&colorA=080f12&colorB=1fa669
|
|
19
|
+
[jsdocs-href]: https://www.jsdocs.io/package/eslint-plugin-hyoban
|
package/dist/index.cjs
CHANGED
|
@@ -1,236 +1,117 @@
|
|
|
1
|
-
'
|
|
1
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
var utils = require('@typescript-eslint/utils');
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
(ruleName) => ruleName
|
|
7
|
-
);
|
|
5
|
+
var version = "0.2.0";
|
|
8
6
|
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
7
|
+
const createRule = utils.ESLintUtils.RuleCreator((name)=>`https://github.com/hyoban/eslint-plugin-hyoban/blob/main/src/rules/${name}.`);
|
|
8
|
+
|
|
9
|
+
// eslint-disable-next-line unused-imports/no-unused-imports
|
|
10
|
+
const rule$1 = createRule({
|
|
11
|
+
name: 'no-extra-space-jsx-expression',
|
|
12
|
+
meta: {
|
|
13
|
+
type: 'layout',
|
|
14
|
+
fixable: 'whitespace',
|
|
15
|
+
docs: {
|
|
16
|
+
description: 'No extra space in jsx expression'
|
|
17
|
+
},
|
|
18
|
+
messages: {
|
|
19
|
+
noExtraSpaceJsxExpression: 'No extra space in jsx expression'
|
|
20
|
+
},
|
|
21
|
+
schema: []
|
|
17
22
|
},
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const sourceCode = context.getSourceCode();
|
|
27
|
-
return {
|
|
28
|
-
TSTypeParameterDeclaration: (node) => {
|
|
29
|
-
if (!["TSCallSignatureDeclaration", "ArrowFunctionExpression", "TSFunctionType"].includes(node.parent.type)) {
|
|
30
|
-
const pre = sourceCode.text.slice(0, node.range[0]);
|
|
31
|
-
const preSpace = pre.match(/(\s+)$/)?.[0];
|
|
32
|
-
if (preSpace && preSpace.length) {
|
|
23
|
+
defaultOptions: [],
|
|
24
|
+
create (context) {
|
|
25
|
+
function check(node, isExit) {
|
|
26
|
+
const { expression } = node;
|
|
27
|
+
if (expression.type !== utils.AST_NODE_TYPES.CallExpression && expression.type !== utils.AST_NODE_TYPES.ChainExpression) return;
|
|
28
|
+
const containerRange = node.range;
|
|
29
|
+
const expressionRange = expression.range;
|
|
30
|
+
if (containerRange[1] - expressionRange[1] === 1 && expressionRange[0] - containerRange[0] === 1) return;
|
|
33
31
|
context.report({
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
32
|
+
node,
|
|
33
|
+
messageId: 'noExtraSpaceJsxExpression',
|
|
34
|
+
fix (fixer) {
|
|
35
|
+
return isExit ? fixer.removeRange([
|
|
36
|
+
expressionRange[1],
|
|
37
|
+
containerRange[1] - 1
|
|
38
|
+
]) : fixer.removeRange([
|
|
39
|
+
containerRange[0] + 1,
|
|
40
|
+
expressionRange[0]
|
|
41
|
+
]);
|
|
42
|
+
}
|
|
39
43
|
});
|
|
40
|
-
}
|
|
41
44
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const current = params[i];
|
|
46
|
-
const from = prev.range[1];
|
|
47
|
-
const to = current.range[0];
|
|
48
|
-
const span = sourceCode.text.slice(from, to);
|
|
49
|
-
if (span !== ", " && !span.match(/,\n/)) {
|
|
50
|
-
context.report({
|
|
51
|
-
*fix(fixer) {
|
|
52
|
-
yield fixer.replaceTextRange([from, to], ", ");
|
|
53
|
-
},
|
|
54
|
-
loc: {
|
|
55
|
-
start: prev.loc.end,
|
|
56
|
-
end: current.loc.start
|
|
57
|
-
},
|
|
58
|
-
messageId: "genericSpacingMismatch",
|
|
59
|
-
node
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
},
|
|
64
|
-
TSTypeParameter: (node) => {
|
|
65
|
-
if (!node.default)
|
|
66
|
-
return;
|
|
67
|
-
const endNode = node.constraint || node.name;
|
|
68
|
-
const from = endNode.range[1];
|
|
69
|
-
const to = node.default.range[0];
|
|
70
|
-
if (sourceCode.text.slice(from, to) !== " = ") {
|
|
71
|
-
context.report({
|
|
72
|
-
*fix(fixer) {
|
|
73
|
-
yield fixer.replaceTextRange([from, to], " = ");
|
|
74
|
-
},
|
|
75
|
-
loc: {
|
|
76
|
-
start: endNode.loc.end,
|
|
77
|
-
end: node.default.loc.start
|
|
45
|
+
return {
|
|
46
|
+
'JSXExpressionContainer:exit' (node) {
|
|
47
|
+
check(node, true);
|
|
78
48
|
},
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
const RULE_NAME$2 = "if-newline";
|
|
89
|
-
const ifNewline = createEslintRule({
|
|
90
|
-
name: RULE_NAME$2,
|
|
91
|
-
meta: {
|
|
92
|
-
type: "problem",
|
|
93
|
-
docs: {
|
|
94
|
-
description: "Newline after if",
|
|
95
|
-
recommended: "error"
|
|
96
|
-
},
|
|
97
|
-
fixable: "code",
|
|
98
|
-
schema: [],
|
|
99
|
-
messages: {
|
|
100
|
-
missingIfNewline: "Expect newline after if"
|
|
101
|
-
}
|
|
102
|
-
},
|
|
103
|
-
defaultOptions: [],
|
|
104
|
-
create: (context) => {
|
|
105
|
-
return {
|
|
106
|
-
IfStatement(node) {
|
|
107
|
-
if (!node.consequent)
|
|
108
|
-
return;
|
|
109
|
-
if (node.consequent.type === "BlockStatement")
|
|
110
|
-
return;
|
|
111
|
-
if (node.test.loc.end.line === node.consequent.loc.start.line) {
|
|
112
|
-
context.report({
|
|
113
|
-
node,
|
|
114
|
-
loc: {
|
|
115
|
-
start: node.test.loc.end,
|
|
116
|
-
end: node.consequent.loc.start
|
|
117
|
-
},
|
|
118
|
-
messageId: "missingIfNewline",
|
|
119
|
-
fix(fixer) {
|
|
120
|
-
return fixer.replaceTextRange([node.consequent.range[0], node.consequent.range[0]], "\n");
|
|
49
|
+
JSXExpressionContainer (node) {
|
|
50
|
+
check(node);
|
|
121
51
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
}
|
|
125
|
-
};
|
|
126
|
-
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
127
54
|
});
|
|
128
55
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
56
|
+
// eslint-disable-next-line unused-imports/no-unused-imports
|
|
57
|
+
function isConditionRevertNeedBracket(node) {
|
|
58
|
+
return !(node.type === utils.AST_NODE_TYPES.Identifier || node.type === utils.AST_NODE_TYPES.Literal || node.type === utils.AST_NODE_TYPES.MemberExpression || node.type === utils.AST_NODE_TYPES.CallExpression);
|
|
59
|
+
}
|
|
60
|
+
function getIndentation(node) {
|
|
61
|
+
return ' '.repeat(node.loc.start.column);
|
|
62
|
+
}
|
|
63
|
+
function isNodeNeedEarlyReturn(node) {
|
|
64
|
+
return node.type === utils.AST_NODE_TYPES.ReturnStatement || node.type === utils.AST_NODE_TYPES.ThrowStatement || node.type === utils.AST_NODE_TYPES.ContinueStatement;
|
|
65
|
+
}
|
|
66
|
+
const rule = createRule({
|
|
67
|
+
name: 'prefer-early-return',
|
|
68
|
+
meta: {
|
|
69
|
+
docs: {
|
|
70
|
+
description: 'Prefer early return pattern to clean if else statement'
|
|
71
|
+
},
|
|
72
|
+
messages: {
|
|
73
|
+
preferEarlyReturn: 'Return early to clean this if else statement'
|
|
74
|
+
},
|
|
75
|
+
type: 'suggestion',
|
|
76
|
+
schema: [],
|
|
77
|
+
fixable: 'code'
|
|
137
78
|
},
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
79
|
+
defaultOptions: [],
|
|
80
|
+
create (context) {
|
|
81
|
+
return {
|
|
82
|
+
IfStatement (node) {
|
|
83
|
+
if (!node.alternate) return;
|
|
84
|
+
if (isNodeNeedEarlyReturn(node.alternate) || node.alternate.type === utils.AST_NODE_TYPES.BlockStatement && node.alternate.body.some((statement)=>isNodeNeedEarlyReturn(statement))) {
|
|
85
|
+
context.report({
|
|
86
|
+
node: node.alternate,
|
|
87
|
+
messageId: 'preferEarlyReturn',
|
|
88
|
+
fix (fixer) {
|
|
89
|
+
const condition = context.sourceCode.getText(node.test);
|
|
90
|
+
const revertCondition = isConditionRevertNeedBracket(node.test) ? `!(${condition})` : `!${condition}`;
|
|
91
|
+
let ifText = context.sourceCode.getText(node.consequent);
|
|
92
|
+
ifText = ifText.startsWith('{') && ifText.endsWith('}') ? ifText.replace(/^{/, '').replace(/}$/, '').replaceAll('\n ', '\n').slice(1, -1) : `${getIndentation(node)}${ifText}`;
|
|
93
|
+
const elseText = context.sourceCode.getText(node.alternate);
|
|
94
|
+
return [
|
|
95
|
+
fixer.replaceText(node, `if (${revertCondition}) ${elseText}\n${ifText}`)
|
|
96
|
+
];
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
};
|
|
142
102
|
}
|
|
143
|
-
},
|
|
144
|
-
defaultOptions: [],
|
|
145
|
-
create: (context) => {
|
|
146
|
-
return {
|
|
147
|
-
ImportDeclaration(node) {
|
|
148
|
-
if (node.specifiers.length <= 1)
|
|
149
|
-
return;
|
|
150
|
-
const names = /* @__PURE__ */ new Set();
|
|
151
|
-
node.specifiers.forEach((n) => {
|
|
152
|
-
const id = n.local.name;
|
|
153
|
-
if (names.has(id)) {
|
|
154
|
-
context.report({
|
|
155
|
-
node,
|
|
156
|
-
loc: {
|
|
157
|
-
start: n.loc.end,
|
|
158
|
-
end: n.loc.start
|
|
159
|
-
},
|
|
160
|
-
messageId: "importDedupe",
|
|
161
|
-
fix(fixer) {
|
|
162
|
-
const s = n.range[0];
|
|
163
|
-
let e = n.range[1];
|
|
164
|
-
if (context.getSourceCode().text[e] === ",")
|
|
165
|
-
e += 1;
|
|
166
|
-
return fixer.removeRange([s, e]);
|
|
167
|
-
}
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
names.add(id);
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
};
|
|
174
|
-
}
|
|
175
103
|
});
|
|
176
104
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
docs: {
|
|
183
|
-
description: "Newline after if",
|
|
184
|
-
recommended: "error"
|
|
105
|
+
// eslint-disable-next-line unused-imports/no-unused-imports
|
|
106
|
+
var index = {
|
|
107
|
+
meta: {
|
|
108
|
+
name: 'hyoban',
|
|
109
|
+
version
|
|
185
110
|
},
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
preferInlineTypeImport: "Prefer inline type import"
|
|
111
|
+
rules: {
|
|
112
|
+
'prefer-early-return': rule,
|
|
113
|
+
'no-extra-space-jsx-expression': rule$1
|
|
190
114
|
}
|
|
191
|
-
},
|
|
192
|
-
defaultOptions: [],
|
|
193
|
-
create: (context) => {
|
|
194
|
-
const sourceCode = context.getSourceCode();
|
|
195
|
-
return {
|
|
196
|
-
ImportDeclaration: (node) => {
|
|
197
|
-
if (node.specifiers.length === 1 && ["ImportNamespaceSpecifier", "ImportDefaultSpecifier"].includes(node.specifiers[0].type))
|
|
198
|
-
return;
|
|
199
|
-
if (node.importKind === "type") {
|
|
200
|
-
context.report({
|
|
201
|
-
*fix(fixer) {
|
|
202
|
-
yield* removeTypeSpecifier(fixer, sourceCode, node);
|
|
203
|
-
for (const specifier of node.specifiers)
|
|
204
|
-
yield fixer.insertTextBefore(specifier, "type ");
|
|
205
|
-
},
|
|
206
|
-
loc: node.loc,
|
|
207
|
-
messageId: "preferInlineTypeImport",
|
|
208
|
-
node
|
|
209
|
-
});
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
};
|
|
213
|
-
}
|
|
214
|
-
});
|
|
215
|
-
function* removeTypeSpecifier(fixer, sourceCode, node) {
|
|
216
|
-
const importKeyword = sourceCode.getFirstToken(node);
|
|
217
|
-
const typeIdentifier = sourceCode.getTokenAfter(importKeyword);
|
|
218
|
-
yield fixer.remove(typeIdentifier);
|
|
219
|
-
if (importKeyword.loc.end.column + 1 === typeIdentifier.loc.start.column) {
|
|
220
|
-
yield fixer.removeRange([
|
|
221
|
-
importKeyword.range[1],
|
|
222
|
-
importKeyword.range[1] + 1
|
|
223
|
-
]);
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
const index = {
|
|
228
|
-
rules: {
|
|
229
|
-
"if-newline": ifNewline,
|
|
230
|
-
"import-dedupe": importDedupe,
|
|
231
|
-
"prefer-inline-type-import": preferInlineTypeImport,
|
|
232
|
-
"generic-spacing": genericSpacing
|
|
233
|
-
}
|
|
234
115
|
};
|
|
235
116
|
|
|
236
|
-
|
|
117
|
+
exports.default = index;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ESLintUtils } from '@typescript-eslint/utils';
|
|
2
|
+
|
|
3
|
+
declare const _default: {
|
|
4
|
+
meta: {
|
|
5
|
+
name: string;
|
|
6
|
+
version: string;
|
|
7
|
+
};
|
|
8
|
+
rules: {
|
|
9
|
+
'prefer-early-return': ESLintUtils.RuleModule<"preferEarlyReturn", [], ESLintUtils.RuleListener>;
|
|
10
|
+
'no-extra-space-jsx-expression': ESLintUtils.RuleModule<"noExtraSpaceJsxExpression", [], ESLintUtils.RuleListener>;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export { _default as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { ESLintUtils } from '@typescript-eslint/utils';
|
|
2
2
|
|
|
3
3
|
declare const _default: {
|
|
4
|
+
meta: {
|
|
5
|
+
name: string;
|
|
6
|
+
version: string;
|
|
7
|
+
};
|
|
4
8
|
rules: {
|
|
5
|
-
'
|
|
6
|
-
'
|
|
7
|
-
'prefer-inline-type-import': _typescript_eslint_utils_dist_ts_eslint_Rule.RuleModule<"preferInlineTypeImport", [], _typescript_eslint_utils_dist_ts_eslint_Rule.RuleListener>;
|
|
8
|
-
'generic-spacing': _typescript_eslint_utils_dist_ts_eslint_Rule.RuleModule<"genericSpacingMismatch", [], _typescript_eslint_utils_dist_ts_eslint_Rule.RuleListener>;
|
|
9
|
+
'prefer-early-return': ESLintUtils.RuleModule<"preferEarlyReturn", [], ESLintUtils.RuleListener>;
|
|
10
|
+
'no-extra-space-jsx-expression': ESLintUtils.RuleModule<"noExtraSpaceJsxExpression", [], ESLintUtils.RuleListener>;
|
|
9
11
|
};
|
|
10
12
|
};
|
|
11
13
|
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { ESLintUtils, AST_NODE_TYPES } from '@typescript-eslint/utils';
|
|
2
|
+
|
|
3
|
+
var version = "0.2.0";
|
|
4
|
+
|
|
5
|
+
const createRule = ESLintUtils.RuleCreator((name)=>`https://github.com/hyoban/eslint-plugin-hyoban/blob/main/src/rules/${name}.`);
|
|
6
|
+
|
|
7
|
+
// eslint-disable-next-line unused-imports/no-unused-imports
|
|
8
|
+
const rule$1 = createRule({
|
|
9
|
+
name: 'no-extra-space-jsx-expression',
|
|
10
|
+
meta: {
|
|
11
|
+
type: 'layout',
|
|
12
|
+
fixable: 'whitespace',
|
|
13
|
+
docs: {
|
|
14
|
+
description: 'No extra space in jsx expression'
|
|
15
|
+
},
|
|
16
|
+
messages: {
|
|
17
|
+
noExtraSpaceJsxExpression: 'No extra space in jsx expression'
|
|
18
|
+
},
|
|
19
|
+
schema: []
|
|
20
|
+
},
|
|
21
|
+
defaultOptions: [],
|
|
22
|
+
create (context) {
|
|
23
|
+
function check(node, isExit) {
|
|
24
|
+
const { expression } = node;
|
|
25
|
+
if (expression.type !== AST_NODE_TYPES.CallExpression && expression.type !== AST_NODE_TYPES.ChainExpression) return;
|
|
26
|
+
const containerRange = node.range;
|
|
27
|
+
const expressionRange = expression.range;
|
|
28
|
+
if (containerRange[1] - expressionRange[1] === 1 && expressionRange[0] - containerRange[0] === 1) return;
|
|
29
|
+
context.report({
|
|
30
|
+
node,
|
|
31
|
+
messageId: 'noExtraSpaceJsxExpression',
|
|
32
|
+
fix (fixer) {
|
|
33
|
+
return isExit ? fixer.removeRange([
|
|
34
|
+
expressionRange[1],
|
|
35
|
+
containerRange[1] - 1
|
|
36
|
+
]) : fixer.removeRange([
|
|
37
|
+
containerRange[0] + 1,
|
|
38
|
+
expressionRange[0]
|
|
39
|
+
]);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
return {
|
|
44
|
+
'JSXExpressionContainer:exit' (node) {
|
|
45
|
+
check(node, true);
|
|
46
|
+
},
|
|
47
|
+
JSXExpressionContainer (node) {
|
|
48
|
+
check(node);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// eslint-disable-next-line unused-imports/no-unused-imports
|
|
55
|
+
function isConditionRevertNeedBracket(node) {
|
|
56
|
+
return !(node.type === AST_NODE_TYPES.Identifier || node.type === AST_NODE_TYPES.Literal || node.type === AST_NODE_TYPES.MemberExpression || node.type === AST_NODE_TYPES.CallExpression);
|
|
57
|
+
}
|
|
58
|
+
function getIndentation(node) {
|
|
59
|
+
return ' '.repeat(node.loc.start.column);
|
|
60
|
+
}
|
|
61
|
+
function isNodeNeedEarlyReturn(node) {
|
|
62
|
+
return node.type === AST_NODE_TYPES.ReturnStatement || node.type === AST_NODE_TYPES.ThrowStatement || node.type === AST_NODE_TYPES.ContinueStatement;
|
|
63
|
+
}
|
|
64
|
+
const rule = createRule({
|
|
65
|
+
name: 'prefer-early-return',
|
|
66
|
+
meta: {
|
|
67
|
+
docs: {
|
|
68
|
+
description: 'Prefer early return pattern to clean if else statement'
|
|
69
|
+
},
|
|
70
|
+
messages: {
|
|
71
|
+
preferEarlyReturn: 'Return early to clean this if else statement'
|
|
72
|
+
},
|
|
73
|
+
type: 'suggestion',
|
|
74
|
+
schema: [],
|
|
75
|
+
fixable: 'code'
|
|
76
|
+
},
|
|
77
|
+
defaultOptions: [],
|
|
78
|
+
create (context) {
|
|
79
|
+
return {
|
|
80
|
+
IfStatement (node) {
|
|
81
|
+
if (!node.alternate) return;
|
|
82
|
+
if (isNodeNeedEarlyReturn(node.alternate) || node.alternate.type === AST_NODE_TYPES.BlockStatement && node.alternate.body.some((statement)=>isNodeNeedEarlyReturn(statement))) {
|
|
83
|
+
context.report({
|
|
84
|
+
node: node.alternate,
|
|
85
|
+
messageId: 'preferEarlyReturn',
|
|
86
|
+
fix (fixer) {
|
|
87
|
+
const condition = context.sourceCode.getText(node.test);
|
|
88
|
+
const revertCondition = isConditionRevertNeedBracket(node.test) ? `!(${condition})` : `!${condition}`;
|
|
89
|
+
let ifText = context.sourceCode.getText(node.consequent);
|
|
90
|
+
ifText = ifText.startsWith('{') && ifText.endsWith('}') ? ifText.replace(/^{/, '').replace(/}$/, '').replaceAll('\n ', '\n').slice(1, -1) : `${getIndentation(node)}${ifText}`;
|
|
91
|
+
const elseText = context.sourceCode.getText(node.alternate);
|
|
92
|
+
return [
|
|
93
|
+
fixer.replaceText(node, `if (${revertCondition}) ${elseText}\n${ifText}`)
|
|
94
|
+
];
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
// eslint-disable-next-line unused-imports/no-unused-imports
|
|
104
|
+
var index = {
|
|
105
|
+
meta: {
|
|
106
|
+
name: 'hyoban',
|
|
107
|
+
version
|
|
108
|
+
},
|
|
109
|
+
rules: {
|
|
110
|
+
'prefer-early-return': rule,
|
|
111
|
+
'no-extra-space-jsx-expression': rule$1
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
export { index as default };
|
package/package.json
CHANGED
|
@@ -1,26 +1,88 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-hyoban",
|
|
3
|
-
"
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.2.0",
|
|
5
|
+
"packageManager": "pnpm@8.15.4",
|
|
6
|
+
"description": "Prefer early return pattern to clean if else statement",
|
|
7
|
+
"author": {
|
|
8
|
+
"name": "hyoban",
|
|
9
|
+
"url": "https://github.com/hyoban"
|
|
10
|
+
},
|
|
4
11
|
"license": "MIT",
|
|
5
|
-
"homepage": "https://github.com/hyoban/eslint-
|
|
12
|
+
"homepage": "https://github.com/hyoban/eslint-plugin-hyoban#readme",
|
|
13
|
+
"repository": "hyoban/eslint-plugin-hyoban",
|
|
14
|
+
"bugs": "https://github.com/hyoban/eslint-plugin-hyoban/issues",
|
|
15
|
+
"keywords": [],
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"import": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"default": "./dist/index.js"
|
|
22
|
+
},
|
|
23
|
+
"require": {
|
|
24
|
+
"types": "./dist/index.d.cts",
|
|
25
|
+
"default": "./dist/index.cjs"
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
},
|
|
6
29
|
"main": "./dist/index.cjs",
|
|
7
|
-
"module": "./dist/index.
|
|
30
|
+
"module": "./dist/index.js",
|
|
8
31
|
"types": "./dist/index.d.ts",
|
|
32
|
+
"typesVersions": {
|
|
33
|
+
"*": {
|
|
34
|
+
"*": [
|
|
35
|
+
"./dist/*",
|
|
36
|
+
"./dist/index.d.ts"
|
|
37
|
+
]
|
|
38
|
+
}
|
|
39
|
+
},
|
|
9
40
|
"files": [
|
|
10
41
|
"dist"
|
|
11
42
|
],
|
|
43
|
+
"release-it": {
|
|
44
|
+
"plugins": {
|
|
45
|
+
"release-it-pnpm": {}
|
|
46
|
+
},
|
|
47
|
+
"git": {
|
|
48
|
+
"commitMessage": "chore: release v${version}"
|
|
49
|
+
},
|
|
50
|
+
"hooks": {
|
|
51
|
+
"before:init": [
|
|
52
|
+
"pnpm run lint",
|
|
53
|
+
"pnpm run typecheck",
|
|
54
|
+
"pnpm run test --run"
|
|
55
|
+
]
|
|
56
|
+
}
|
|
57
|
+
},
|
|
12
58
|
"dependencies": {
|
|
13
|
-
"@typescript-eslint/utils": "^
|
|
59
|
+
"@typescript-eslint/utils": "^7.1.1"
|
|
14
60
|
},
|
|
15
61
|
"devDependencies": {
|
|
16
|
-
"@types/node": "^
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
62
|
+
"@types/node": "^20.11.25",
|
|
63
|
+
"@typescript-eslint/rule-tester": "^7.1.1",
|
|
64
|
+
"bunchee": "^4.4.8",
|
|
65
|
+
"dedent": "^1.5.1",
|
|
66
|
+
"eslint": "^8.57.0",
|
|
67
|
+
"eslint-config-hyoban": "^0.1.33",
|
|
68
|
+
"lint-staged": "^15.2.2",
|
|
69
|
+
"release-it": "^17.1.1",
|
|
70
|
+
"release-it-pnpm": "4.0.0-beta.2",
|
|
71
|
+
"simple-git-hooks": "^2.10.0",
|
|
72
|
+
"typescript": "^5.4.2",
|
|
73
|
+
"vitest": "^1.3.1"
|
|
74
|
+
},
|
|
75
|
+
"simple-git-hooks": {
|
|
76
|
+
"pre-commit": "pnpm lint-staged"
|
|
77
|
+
},
|
|
78
|
+
"lint-staged": {
|
|
79
|
+
"*": "eslint --fix"
|
|
20
80
|
},
|
|
21
81
|
"scripts": {
|
|
22
|
-
"build": "
|
|
23
|
-
"
|
|
24
|
-
"
|
|
82
|
+
"build": "bunchee",
|
|
83
|
+
"dev": "bunchee -w",
|
|
84
|
+
"lint": "eslint .",
|
|
85
|
+
"test": "vitest",
|
|
86
|
+
"typecheck": "tsc"
|
|
25
87
|
}
|
|
26
88
|
}
|
package/dist/index.mjs
DELETED
|
@@ -1,234 +0,0 @@
|
|
|
1
|
-
import { ESLintUtils } from '@typescript-eslint/utils';
|
|
2
|
-
|
|
3
|
-
const createEslintRule = ESLintUtils.RuleCreator(
|
|
4
|
-
(ruleName) => ruleName
|
|
5
|
-
);
|
|
6
|
-
|
|
7
|
-
const RULE_NAME$3 = "generic-spacing";
|
|
8
|
-
const genericSpacing = createEslintRule({
|
|
9
|
-
name: RULE_NAME$3,
|
|
10
|
-
meta: {
|
|
11
|
-
type: "suggestion",
|
|
12
|
-
docs: {
|
|
13
|
-
description: "Spaces around generic type parameters",
|
|
14
|
-
recommended: "error"
|
|
15
|
-
},
|
|
16
|
-
fixable: "code",
|
|
17
|
-
schema: [],
|
|
18
|
-
messages: {
|
|
19
|
-
genericSpacingMismatch: "Generic spaces mismatch"
|
|
20
|
-
}
|
|
21
|
-
},
|
|
22
|
-
defaultOptions: [],
|
|
23
|
-
create: (context) => {
|
|
24
|
-
const sourceCode = context.getSourceCode();
|
|
25
|
-
return {
|
|
26
|
-
TSTypeParameterDeclaration: (node) => {
|
|
27
|
-
if (!["TSCallSignatureDeclaration", "ArrowFunctionExpression", "TSFunctionType"].includes(node.parent.type)) {
|
|
28
|
-
const pre = sourceCode.text.slice(0, node.range[0]);
|
|
29
|
-
const preSpace = pre.match(/(\s+)$/)?.[0];
|
|
30
|
-
if (preSpace && preSpace.length) {
|
|
31
|
-
context.report({
|
|
32
|
-
node,
|
|
33
|
-
messageId: "genericSpacingMismatch",
|
|
34
|
-
*fix(fixer) {
|
|
35
|
-
yield fixer.replaceTextRange([node.range[0] - preSpace.length, node.range[0]], "");
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
const params = node.params;
|
|
41
|
-
for (let i = 1; i < params.length; i++) {
|
|
42
|
-
const prev = params[i - 1];
|
|
43
|
-
const current = params[i];
|
|
44
|
-
const from = prev.range[1];
|
|
45
|
-
const to = current.range[0];
|
|
46
|
-
const span = sourceCode.text.slice(from, to);
|
|
47
|
-
if (span !== ", " && !span.match(/,\n/)) {
|
|
48
|
-
context.report({
|
|
49
|
-
*fix(fixer) {
|
|
50
|
-
yield fixer.replaceTextRange([from, to], ", ");
|
|
51
|
-
},
|
|
52
|
-
loc: {
|
|
53
|
-
start: prev.loc.end,
|
|
54
|
-
end: current.loc.start
|
|
55
|
-
},
|
|
56
|
-
messageId: "genericSpacingMismatch",
|
|
57
|
-
node
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
},
|
|
62
|
-
TSTypeParameter: (node) => {
|
|
63
|
-
if (!node.default)
|
|
64
|
-
return;
|
|
65
|
-
const endNode = node.constraint || node.name;
|
|
66
|
-
const from = endNode.range[1];
|
|
67
|
-
const to = node.default.range[0];
|
|
68
|
-
if (sourceCode.text.slice(from, to) !== " = ") {
|
|
69
|
-
context.report({
|
|
70
|
-
*fix(fixer) {
|
|
71
|
-
yield fixer.replaceTextRange([from, to], " = ");
|
|
72
|
-
},
|
|
73
|
-
loc: {
|
|
74
|
-
start: endNode.loc.end,
|
|
75
|
-
end: node.default.loc.start
|
|
76
|
-
},
|
|
77
|
-
messageId: "genericSpacingMismatch",
|
|
78
|
-
node
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
const RULE_NAME$2 = "if-newline";
|
|
87
|
-
const ifNewline = createEslintRule({
|
|
88
|
-
name: RULE_NAME$2,
|
|
89
|
-
meta: {
|
|
90
|
-
type: "problem",
|
|
91
|
-
docs: {
|
|
92
|
-
description: "Newline after if",
|
|
93
|
-
recommended: "error"
|
|
94
|
-
},
|
|
95
|
-
fixable: "code",
|
|
96
|
-
schema: [],
|
|
97
|
-
messages: {
|
|
98
|
-
missingIfNewline: "Expect newline after if"
|
|
99
|
-
}
|
|
100
|
-
},
|
|
101
|
-
defaultOptions: [],
|
|
102
|
-
create: (context) => {
|
|
103
|
-
return {
|
|
104
|
-
IfStatement(node) {
|
|
105
|
-
if (!node.consequent)
|
|
106
|
-
return;
|
|
107
|
-
if (node.consequent.type === "BlockStatement")
|
|
108
|
-
return;
|
|
109
|
-
if (node.test.loc.end.line === node.consequent.loc.start.line) {
|
|
110
|
-
context.report({
|
|
111
|
-
node,
|
|
112
|
-
loc: {
|
|
113
|
-
start: node.test.loc.end,
|
|
114
|
-
end: node.consequent.loc.start
|
|
115
|
-
},
|
|
116
|
-
messageId: "missingIfNewline",
|
|
117
|
-
fix(fixer) {
|
|
118
|
-
return fixer.replaceTextRange([node.consequent.range[0], node.consequent.range[0]], "\n");
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
const RULE_NAME$1 = "import-dedupe";
|
|
128
|
-
const importDedupe = createEslintRule({
|
|
129
|
-
name: RULE_NAME$1,
|
|
130
|
-
meta: {
|
|
131
|
-
type: "problem",
|
|
132
|
-
docs: {
|
|
133
|
-
description: "Fix duplication in imports",
|
|
134
|
-
recommended: "error"
|
|
135
|
-
},
|
|
136
|
-
fixable: "code",
|
|
137
|
-
schema: [],
|
|
138
|
-
messages: {
|
|
139
|
-
importDedupe: "Expect no duplication in imports"
|
|
140
|
-
}
|
|
141
|
-
},
|
|
142
|
-
defaultOptions: [],
|
|
143
|
-
create: (context) => {
|
|
144
|
-
return {
|
|
145
|
-
ImportDeclaration(node) {
|
|
146
|
-
if (node.specifiers.length <= 1)
|
|
147
|
-
return;
|
|
148
|
-
const names = /* @__PURE__ */ new Set();
|
|
149
|
-
node.specifiers.forEach((n) => {
|
|
150
|
-
const id = n.local.name;
|
|
151
|
-
if (names.has(id)) {
|
|
152
|
-
context.report({
|
|
153
|
-
node,
|
|
154
|
-
loc: {
|
|
155
|
-
start: n.loc.end,
|
|
156
|
-
end: n.loc.start
|
|
157
|
-
},
|
|
158
|
-
messageId: "importDedupe",
|
|
159
|
-
fix(fixer) {
|
|
160
|
-
const s = n.range[0];
|
|
161
|
-
let e = n.range[1];
|
|
162
|
-
if (context.getSourceCode().text[e] === ",")
|
|
163
|
-
e += 1;
|
|
164
|
-
return fixer.removeRange([s, e]);
|
|
165
|
-
}
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
names.add(id);
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
};
|
|
172
|
-
}
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
const RULE_NAME = "prefer-inline-type-import";
|
|
176
|
-
const preferInlineTypeImport = createEslintRule({
|
|
177
|
-
name: RULE_NAME,
|
|
178
|
-
meta: {
|
|
179
|
-
type: "suggestion",
|
|
180
|
-
docs: {
|
|
181
|
-
description: "Newline after if",
|
|
182
|
-
recommended: "error"
|
|
183
|
-
},
|
|
184
|
-
fixable: "code",
|
|
185
|
-
schema: [],
|
|
186
|
-
messages: {
|
|
187
|
-
preferInlineTypeImport: "Prefer inline type import"
|
|
188
|
-
}
|
|
189
|
-
},
|
|
190
|
-
defaultOptions: [],
|
|
191
|
-
create: (context) => {
|
|
192
|
-
const sourceCode = context.getSourceCode();
|
|
193
|
-
return {
|
|
194
|
-
ImportDeclaration: (node) => {
|
|
195
|
-
if (node.specifiers.length === 1 && ["ImportNamespaceSpecifier", "ImportDefaultSpecifier"].includes(node.specifiers[0].type))
|
|
196
|
-
return;
|
|
197
|
-
if (node.importKind === "type") {
|
|
198
|
-
context.report({
|
|
199
|
-
*fix(fixer) {
|
|
200
|
-
yield* removeTypeSpecifier(fixer, sourceCode, node);
|
|
201
|
-
for (const specifier of node.specifiers)
|
|
202
|
-
yield fixer.insertTextBefore(specifier, "type ");
|
|
203
|
-
},
|
|
204
|
-
loc: node.loc,
|
|
205
|
-
messageId: "preferInlineTypeImport",
|
|
206
|
-
node
|
|
207
|
-
});
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
};
|
|
211
|
-
}
|
|
212
|
-
});
|
|
213
|
-
function* removeTypeSpecifier(fixer, sourceCode, node) {
|
|
214
|
-
const importKeyword = sourceCode.getFirstToken(node);
|
|
215
|
-
const typeIdentifier = sourceCode.getTokenAfter(importKeyword);
|
|
216
|
-
yield fixer.remove(typeIdentifier);
|
|
217
|
-
if (importKeyword.loc.end.column + 1 === typeIdentifier.loc.start.column) {
|
|
218
|
-
yield fixer.removeRange([
|
|
219
|
-
importKeyword.range[1],
|
|
220
|
-
importKeyword.range[1] + 1
|
|
221
|
-
]);
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
const index = {
|
|
226
|
-
rules: {
|
|
227
|
-
"if-newline": ifNewline,
|
|
228
|
-
"import-dedupe": importDedupe,
|
|
229
|
-
"prefer-inline-type-import": preferInlineTypeImport,
|
|
230
|
-
"generic-spacing": genericSpacing
|
|
231
|
-
}
|
|
232
|
-
};
|
|
233
|
-
|
|
234
|
-
export { index as default };
|