@kununu/eslint-config 5.2.3-beta-4 → 5.2.3-beta-5
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/index.js +45 -1
- package/package.json +1 -1
- package/customRules/prefer-early-return.js +0 -38
package/index.js
CHANGED
|
@@ -1,4 +1,48 @@
|
|
|
1
|
-
const
|
|
1
|
+
const preferEarlyReturnRule = {
|
|
2
|
+
meta: {
|
|
3
|
+
type: 'suggestion',
|
|
4
|
+
docs: {
|
|
5
|
+
description: 'Prefer early returns over logical expressions in return statements',
|
|
6
|
+
category: 'Best Practices',
|
|
7
|
+
recommended: true,
|
|
8
|
+
},
|
|
9
|
+
fixable: 'code',
|
|
10
|
+
},
|
|
11
|
+
create(context) {
|
|
12
|
+
return {
|
|
13
|
+
ReturnStatement(node) {
|
|
14
|
+
const argument = node.argument?.type === 'ParenthesizedExpression'
|
|
15
|
+
? node.argument.expression
|
|
16
|
+
: node.argument;
|
|
17
|
+
|
|
18
|
+
// Only handle && operators
|
|
19
|
+
if (argument?.type === 'LogicalExpression' && argument.operator === '&&') {
|
|
20
|
+
context.report({
|
|
21
|
+
node,
|
|
22
|
+
message: 'Prefer early returns over logical expressions in return statements',
|
|
23
|
+
fix(fixer) {
|
|
24
|
+
const sourceCode = context.getSourceCode();
|
|
25
|
+
const condition = sourceCode.getText(argument.left);
|
|
26
|
+
const returnValue = sourceCode.getText(argument.right);
|
|
27
|
+
|
|
28
|
+
return fixer.replaceText(
|
|
29
|
+
node,
|
|
30
|
+
`if (${condition}) return ${returnValue};`
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// Create the plugin with our rule
|
|
41
|
+
const customPlugin = {
|
|
42
|
+
rules: {
|
|
43
|
+
'prefer-early-return': preferEarlyReturnRule
|
|
44
|
+
}
|
|
45
|
+
};
|
|
2
46
|
|
|
3
47
|
// Register the custom plugin
|
|
4
48
|
try {
|
package/package.json
CHANGED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
meta: {
|
|
3
|
-
type: 'suggestion',
|
|
4
|
-
docs: {
|
|
5
|
-
description: 'Prefer early returns over logical expressions in return statements',
|
|
6
|
-
category: 'Best Practices',
|
|
7
|
-
recommended: true,
|
|
8
|
-
},
|
|
9
|
-
fixable: 'code',
|
|
10
|
-
},
|
|
11
|
-
create(context) {
|
|
12
|
-
return {
|
|
13
|
-
ReturnStatement(node) {
|
|
14
|
-
const argument = node.argument?.type === 'ParenthesizedExpression'
|
|
15
|
-
? node.argument.expression
|
|
16
|
-
: node.argument;
|
|
17
|
-
|
|
18
|
-
// Only handle && operators
|
|
19
|
-
if (argument?.type === 'LogicalExpression' && argument.operator === '&&') {
|
|
20
|
-
context.report({
|
|
21
|
-
node,
|
|
22
|
-
message: 'Prefer early returns over logical expressions in return statements',
|
|
23
|
-
fix(fixer) {
|
|
24
|
-
const sourceCode = context.getSourceCode();
|
|
25
|
-
const condition = sourceCode.getText(argument.left);
|
|
26
|
-
const returnValue = sourceCode.getText(argument.right);
|
|
27
|
-
|
|
28
|
-
return fixer.replaceText(
|
|
29
|
-
node,
|
|
30
|
-
`if (${condition}) return ${returnValue};`
|
|
31
|
-
);
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
};
|