eslint-plugin-hyoban 0.0.1
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 -0
- package/README.md +19 -0
- package/dist/index.cjs +126 -0
- package/dist/index.d.cts +14 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +124 -0
- package/package.json +73 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
|
+
|
|
3
|
+
var utils = require('@typescript-eslint/utils');
|
|
4
|
+
|
|
5
|
+
var version = "0.0.1";
|
|
6
|
+
|
|
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 expressionTypesNoCheck = new Set([
|
|
11
|
+
utils.AST_NODE_TYPES.ConditionalExpression,
|
|
12
|
+
utils.AST_NODE_TYPES.JSXElement,
|
|
13
|
+
utils.AST_NODE_TYPES.TSAsExpression
|
|
14
|
+
]);
|
|
15
|
+
const rule$1 = createRule({
|
|
16
|
+
name: 'no-extra-space-jsx-expression',
|
|
17
|
+
meta: {
|
|
18
|
+
type: 'layout',
|
|
19
|
+
fixable: 'whitespace',
|
|
20
|
+
docs: {
|
|
21
|
+
description: 'No extra space in jsx expression'
|
|
22
|
+
},
|
|
23
|
+
messages: {
|
|
24
|
+
noExtraSpaceJsxExpression: 'No extra space in jsx expression'
|
|
25
|
+
},
|
|
26
|
+
schema: []
|
|
27
|
+
},
|
|
28
|
+
defaultOptions: [],
|
|
29
|
+
create (context) {
|
|
30
|
+
function check(node, isExit) {
|
|
31
|
+
const { expression } = node;
|
|
32
|
+
if (expressionTypesNoCheck.has(expression.type) || expression.type === utils.AST_NODE_TYPES.ArrowFunctionExpression && expression.body.type !== utils.AST_NODE_TYPES.BlockStatement) return;
|
|
33
|
+
const containerRange = node.range;
|
|
34
|
+
const expressionRange = expression.range;
|
|
35
|
+
const noSpace = isExit ? containerRange[1] - expressionRange[1] === 1 : expressionRange[0] - containerRange[0] === 1;
|
|
36
|
+
if (noSpace) return;
|
|
37
|
+
const rangeToRemove = isExit ? [
|
|
38
|
+
expressionRange[1],
|
|
39
|
+
containerRange[1] - 1
|
|
40
|
+
] : [
|
|
41
|
+
containerRange[0] + 1,
|
|
42
|
+
expressionRange[0]
|
|
43
|
+
];
|
|
44
|
+
context.report({
|
|
45
|
+
node,
|
|
46
|
+
loc: {
|
|
47
|
+
start: context.sourceCode.getLocFromIndex(rangeToRemove[0]),
|
|
48
|
+
end: context.sourceCode.getLocFromIndex(rangeToRemove[1])
|
|
49
|
+
},
|
|
50
|
+
messageId: 'noExtraSpaceJsxExpression',
|
|
51
|
+
fix: (fixer)=>fixer.removeRange(rangeToRemove)
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
'JSXExpressionContainer:exit' (node) {
|
|
56
|
+
check(node, true);
|
|
57
|
+
},
|
|
58
|
+
JSXExpressionContainer (node) {
|
|
59
|
+
check(node);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// eslint-disable-next-line unused-imports/no-unused-imports
|
|
66
|
+
function isConditionRevertNeedBracket(node) {
|
|
67
|
+
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);
|
|
68
|
+
}
|
|
69
|
+
function getIndentation(node) {
|
|
70
|
+
return ' '.repeat(node.loc.start.column);
|
|
71
|
+
}
|
|
72
|
+
function isNodeNeedEarlyReturn(node) {
|
|
73
|
+
return node.type === utils.AST_NODE_TYPES.ReturnStatement || node.type === utils.AST_NODE_TYPES.ThrowStatement || node.type === utils.AST_NODE_TYPES.ContinueStatement;
|
|
74
|
+
}
|
|
75
|
+
const rule = createRule({
|
|
76
|
+
name: 'prefer-early-return',
|
|
77
|
+
meta: {
|
|
78
|
+
docs: {
|
|
79
|
+
description: 'Prefer early return pattern to clean if else statement'
|
|
80
|
+
},
|
|
81
|
+
messages: {
|
|
82
|
+
preferEarlyReturn: 'Return early to clean this if else statement'
|
|
83
|
+
},
|
|
84
|
+
type: 'suggestion',
|
|
85
|
+
schema: [],
|
|
86
|
+
fixable: 'code'
|
|
87
|
+
},
|
|
88
|
+
defaultOptions: [],
|
|
89
|
+
create (context) {
|
|
90
|
+
return {
|
|
91
|
+
IfStatement (node) {
|
|
92
|
+
if (!node.alternate) return;
|
|
93
|
+
if (isNodeNeedEarlyReturn(node.alternate) || node.alternate.type === utils.AST_NODE_TYPES.BlockStatement && node.alternate.body.some((statement)=>isNodeNeedEarlyReturn(statement))) {
|
|
94
|
+
context.report({
|
|
95
|
+
node: node.alternate,
|
|
96
|
+
messageId: 'preferEarlyReturn',
|
|
97
|
+
fix (fixer) {
|
|
98
|
+
const condition = context.sourceCode.getText(node.test);
|
|
99
|
+
const revertCondition = isConditionRevertNeedBracket(node.test) ? `!(${condition})` : `!${condition}`;
|
|
100
|
+
let ifText = context.sourceCode.getText(node.consequent);
|
|
101
|
+
ifText = ifText.startsWith('{') && ifText.endsWith('}') ? ifText.replace(/^{/, '').replace(/}$/, '').replaceAll('\n ', '\n').slice(1, -1) : `${getIndentation(node)}${ifText}`;
|
|
102
|
+
const elseText = context.sourceCode.getText(node.alternate);
|
|
103
|
+
return [
|
|
104
|
+
fixer.replaceText(node, `if (${revertCondition}) ${elseText}\n${ifText}`)
|
|
105
|
+
];
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
// eslint-disable-next-line unused-imports/no-unused-imports
|
|
115
|
+
var index = {
|
|
116
|
+
meta: {
|
|
117
|
+
name: 'hyoban',
|
|
118
|
+
version
|
|
119
|
+
},
|
|
120
|
+
rules: {
|
|
121
|
+
'prefer-early-return': rule,
|
|
122
|
+
'no-extra-space-jsx-expression': rule$1
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
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
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.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { ESLintUtils, AST_NODE_TYPES } from '@typescript-eslint/utils';
|
|
2
|
+
|
|
3
|
+
var version = "0.0.1";
|
|
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 expressionTypesNoCheck = new Set([
|
|
9
|
+
AST_NODE_TYPES.ConditionalExpression,
|
|
10
|
+
AST_NODE_TYPES.JSXElement,
|
|
11
|
+
AST_NODE_TYPES.TSAsExpression
|
|
12
|
+
]);
|
|
13
|
+
const rule$1 = createRule({
|
|
14
|
+
name: 'no-extra-space-jsx-expression',
|
|
15
|
+
meta: {
|
|
16
|
+
type: 'layout',
|
|
17
|
+
fixable: 'whitespace',
|
|
18
|
+
docs: {
|
|
19
|
+
description: 'No extra space in jsx expression'
|
|
20
|
+
},
|
|
21
|
+
messages: {
|
|
22
|
+
noExtraSpaceJsxExpression: 'No extra space in jsx expression'
|
|
23
|
+
},
|
|
24
|
+
schema: []
|
|
25
|
+
},
|
|
26
|
+
defaultOptions: [],
|
|
27
|
+
create (context) {
|
|
28
|
+
function check(node, isExit) {
|
|
29
|
+
const { expression } = node;
|
|
30
|
+
if (expressionTypesNoCheck.has(expression.type) || expression.type === AST_NODE_TYPES.ArrowFunctionExpression && expression.body.type !== AST_NODE_TYPES.BlockStatement) return;
|
|
31
|
+
const containerRange = node.range;
|
|
32
|
+
const expressionRange = expression.range;
|
|
33
|
+
const noSpace = isExit ? containerRange[1] - expressionRange[1] === 1 : expressionRange[0] - containerRange[0] === 1;
|
|
34
|
+
if (noSpace) return;
|
|
35
|
+
const rangeToRemove = isExit ? [
|
|
36
|
+
expressionRange[1],
|
|
37
|
+
containerRange[1] - 1
|
|
38
|
+
] : [
|
|
39
|
+
containerRange[0] + 1,
|
|
40
|
+
expressionRange[0]
|
|
41
|
+
];
|
|
42
|
+
context.report({
|
|
43
|
+
node,
|
|
44
|
+
loc: {
|
|
45
|
+
start: context.sourceCode.getLocFromIndex(rangeToRemove[0]),
|
|
46
|
+
end: context.sourceCode.getLocFromIndex(rangeToRemove[1])
|
|
47
|
+
},
|
|
48
|
+
messageId: 'noExtraSpaceJsxExpression',
|
|
49
|
+
fix: (fixer)=>fixer.removeRange(rangeToRemove)
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
'JSXExpressionContainer:exit' (node) {
|
|
54
|
+
check(node, true);
|
|
55
|
+
},
|
|
56
|
+
JSXExpressionContainer (node) {
|
|
57
|
+
check(node);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// eslint-disable-next-line unused-imports/no-unused-imports
|
|
64
|
+
function isConditionRevertNeedBracket(node) {
|
|
65
|
+
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);
|
|
66
|
+
}
|
|
67
|
+
function getIndentation(node) {
|
|
68
|
+
return ' '.repeat(node.loc.start.column);
|
|
69
|
+
}
|
|
70
|
+
function isNodeNeedEarlyReturn(node) {
|
|
71
|
+
return node.type === AST_NODE_TYPES.ReturnStatement || node.type === AST_NODE_TYPES.ThrowStatement || node.type === AST_NODE_TYPES.ContinueStatement;
|
|
72
|
+
}
|
|
73
|
+
const rule = createRule({
|
|
74
|
+
name: 'prefer-early-return',
|
|
75
|
+
meta: {
|
|
76
|
+
docs: {
|
|
77
|
+
description: 'Prefer early return pattern to clean if else statement'
|
|
78
|
+
},
|
|
79
|
+
messages: {
|
|
80
|
+
preferEarlyReturn: 'Return early to clean this if else statement'
|
|
81
|
+
},
|
|
82
|
+
type: 'suggestion',
|
|
83
|
+
schema: [],
|
|
84
|
+
fixable: 'code'
|
|
85
|
+
},
|
|
86
|
+
defaultOptions: [],
|
|
87
|
+
create (context) {
|
|
88
|
+
return {
|
|
89
|
+
IfStatement (node) {
|
|
90
|
+
if (!node.alternate) return;
|
|
91
|
+
if (isNodeNeedEarlyReturn(node.alternate) || node.alternate.type === AST_NODE_TYPES.BlockStatement && node.alternate.body.some((statement)=>isNodeNeedEarlyReturn(statement))) {
|
|
92
|
+
context.report({
|
|
93
|
+
node: node.alternate,
|
|
94
|
+
messageId: 'preferEarlyReturn',
|
|
95
|
+
fix (fixer) {
|
|
96
|
+
const condition = context.sourceCode.getText(node.test);
|
|
97
|
+
const revertCondition = isConditionRevertNeedBracket(node.test) ? `!(${condition})` : `!${condition}`;
|
|
98
|
+
let ifText = context.sourceCode.getText(node.consequent);
|
|
99
|
+
ifText = ifText.startsWith('{') && ifText.endsWith('}') ? ifText.replace(/^{/, '').replace(/}$/, '').replaceAll('\n ', '\n').slice(1, -1) : `${getIndentation(node)}${ifText}`;
|
|
100
|
+
const elseText = context.sourceCode.getText(node.alternate);
|
|
101
|
+
return [
|
|
102
|
+
fixer.replaceText(node, `if (${revertCondition}) ${elseText}\n${ifText}`)
|
|
103
|
+
];
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
// eslint-disable-next-line unused-imports/no-unused-imports
|
|
113
|
+
var index = {
|
|
114
|
+
meta: {
|
|
115
|
+
name: 'hyoban',
|
|
116
|
+
version
|
|
117
|
+
},
|
|
118
|
+
rules: {
|
|
119
|
+
'prefer-early-return': rule,
|
|
120
|
+
'no-extra-space-jsx-expression': rule$1
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
export { index as default };
|
package/package.json
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "eslint-plugin-hyoban",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.1",
|
|
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
|
+
},
|
|
11
|
+
"license": "MIT",
|
|
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
|
+
},
|
|
29
|
+
"main": "./dist/index.cjs",
|
|
30
|
+
"module": "./dist/index.js",
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"typesVersions": {
|
|
33
|
+
"*": {
|
|
34
|
+
"*": [
|
|
35
|
+
"./dist/*",
|
|
36
|
+
"./dist/index.d.ts"
|
|
37
|
+
]
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"files": [
|
|
41
|
+
"dist"
|
|
42
|
+
],
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@typescript-eslint/utils": "^7.2.0"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@types/node": "^20.11.27",
|
|
48
|
+
"@typescript-eslint/rule-tester": "^7.2.0",
|
|
49
|
+
"bunchee": "^4.4.8",
|
|
50
|
+
"dedent": "^1.5.1",
|
|
51
|
+
"eslint": "^8.57.0",
|
|
52
|
+
"eslint-config-hyoban": "^0.2.1",
|
|
53
|
+
"lint-staged": "^15.2.2",
|
|
54
|
+
"release-it": "^17.1.1",
|
|
55
|
+
"release-it-pnpm": "4.0.2",
|
|
56
|
+
"simple-git-hooks": "^2.10.0",
|
|
57
|
+
"typescript": "^5.4.2",
|
|
58
|
+
"vitest": "^1.3.1"
|
|
59
|
+
},
|
|
60
|
+
"simple-git-hooks": {
|
|
61
|
+
"pre-commit": "pnpm lint-staged"
|
|
62
|
+
},
|
|
63
|
+
"lint-staged": {
|
|
64
|
+
"*": "eslint --fix"
|
|
65
|
+
},
|
|
66
|
+
"scripts": {
|
|
67
|
+
"build": "bunchee",
|
|
68
|
+
"dev": "bunchee -w",
|
|
69
|
+
"lint": "eslint .",
|
|
70
|
+
"test": "vitest",
|
|
71
|
+
"typecheck": "tsc"
|
|
72
|
+
}
|
|
73
|
+
}
|