@kununu/eslint-config 5.2.3-beta-6 → 5.2.3-beta-7
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 +10 -5
- package/package.json +1 -1
- package/plugins/kununu.js +51 -0
package/index.js
CHANGED
|
@@ -204,12 +204,13 @@ const baseRules = {
|
|
|
204
204
|
// https://react.dev/blog/2024/04/25/react-19-upgrade-guide#removed-proptypes-and-defaultprops
|
|
205
205
|
'react/require-default-props': 0,
|
|
206
206
|
|
|
207
|
-
'no-restricted-syntax': ['error', {
|
|
208
|
-
selector: 'ReturnStatement > LogicalExpression[operator="&&"]',
|
|
209
|
-
message: 'Prefer early returns over logical expressions in return statements. Use if (condition) return value; instead.'
|
|
210
|
-
}],
|
|
211
207
|
};
|
|
212
208
|
|
|
209
|
+
const kununuPlugin = require('./plugins/kununu');
|
|
210
|
+
|
|
211
|
+
// Register it using this prefix
|
|
212
|
+
const pluginPrefix = '@kununu/eslint-config/plugins/kununu';
|
|
213
|
+
|
|
213
214
|
module.exports = {
|
|
214
215
|
extends: [
|
|
215
216
|
'airbnb', // Many strict rules for ECMAScript and React
|
|
@@ -227,6 +228,7 @@ module.exports = {
|
|
|
227
228
|
'@babel',
|
|
228
229
|
'lodash',
|
|
229
230
|
'sort-destructure-keys',
|
|
231
|
+
pluginPrefix
|
|
230
232
|
],
|
|
231
233
|
|
|
232
234
|
env: {
|
|
@@ -236,7 +238,10 @@ module.exports = {
|
|
|
236
238
|
es6: true,
|
|
237
239
|
},
|
|
238
240
|
|
|
239
|
-
rules:
|
|
241
|
+
rules: {
|
|
242
|
+
...baseRules,
|
|
243
|
+
[`${pluginPrefix}/prefer-early-return`]: 'error'
|
|
244
|
+
},
|
|
240
245
|
|
|
241
246
|
overrides: [{
|
|
242
247
|
files: [
|
package/package.json
CHANGED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
rules: {
|
|
3
|
+
'prefer-early-return': {
|
|
4
|
+
meta: {
|
|
5
|
+
type: 'suggestion',
|
|
6
|
+
docs: {
|
|
7
|
+
description: 'Prefer early returns over logical expressions in return statements',
|
|
8
|
+
category: 'Best Practices',
|
|
9
|
+
recommended: true,
|
|
10
|
+
},
|
|
11
|
+
fixable: 'code',
|
|
12
|
+
schema: [] // No options
|
|
13
|
+
},
|
|
14
|
+
create: function(context) {
|
|
15
|
+
return {
|
|
16
|
+
ReturnStatement: function(node) {
|
|
17
|
+
// Handle parenthesized expressions in a way compatible with ESLint 5+
|
|
18
|
+
let argument = node.argument;
|
|
19
|
+
if (argument && argument.type === 'ParenthesizedExpression') {
|
|
20
|
+
argument = argument.expression;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Only handle && operators
|
|
24
|
+
if (argument &&
|
|
25
|
+
argument.type === 'LogicalExpression' &&
|
|
26
|
+
argument.operator === '&&') {
|
|
27
|
+
context.report({
|
|
28
|
+
node: node,
|
|
29
|
+
message: 'Prefer early returns over logical expressions in return statements. Use if (condition) return value; instead.',
|
|
30
|
+
fix: function(fixer) {
|
|
31
|
+
// Get source code in a way that works in ESLint 5+
|
|
32
|
+
const sourceCode = context.getSourceCode ?
|
|
33
|
+
context.getSourceCode() :
|
|
34
|
+
context.getSource;
|
|
35
|
+
|
|
36
|
+
const condition = sourceCode.getText(argument.left);
|
|
37
|
+
const returnValue = sourceCode.getText(argument.right);
|
|
38
|
+
|
|
39
|
+
return fixer.replaceText(
|
|
40
|
+
node,
|
|
41
|
+
"if (" + condition + ") return " + returnValue + ";"
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
};
|