@kununu/eslint-config 5.2.1 → 5.2.2-beta-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/index.js +39 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -203,6 +203,45 @@ const baseRules = {
|
|
|
203
203
|
|
|
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
|
+
// Enforce early return over nested return blocks
|
|
207
|
+
'prefer-early-return': {
|
|
208
|
+
meta: {
|
|
209
|
+
type: 'suggestion',
|
|
210
|
+
docs: {
|
|
211
|
+
description: 'Prefer early returns over nested returns with logical expressions',
|
|
212
|
+
category: 'Best Practices',
|
|
213
|
+
recommended: true,
|
|
214
|
+
},
|
|
215
|
+
fixable: 'code',
|
|
216
|
+
},
|
|
217
|
+
create(context) {
|
|
218
|
+
return {
|
|
219
|
+
ReturnStatement(node) {
|
|
220
|
+
// Handle both direct LogicalExpression and ParenthesizedExpression
|
|
221
|
+
const argument = node.argument?.type === 'ParenthesizedExpression'
|
|
222
|
+
? node.argument.expression
|
|
223
|
+
: node.argument;
|
|
224
|
+
|
|
225
|
+
if (argument?.type === 'LogicalExpression') {
|
|
226
|
+
context.report({
|
|
227
|
+
node,
|
|
228
|
+
message: 'Prefer early returns over logical expressions in return statements',
|
|
229
|
+
fix(fixer) {
|
|
230
|
+
const sourceCode = context.getSourceCode();
|
|
231
|
+
const condition = sourceCode.getText(argument.left);
|
|
232
|
+
const returnValue = sourceCode.getText(argument.right);
|
|
233
|
+
|
|
234
|
+
return fixer.replaceText(
|
|
235
|
+
node,
|
|
236
|
+
`if (!${condition}) return null;\nreturn ${returnValue};`
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
}
|
|
206
245
|
};
|
|
207
246
|
|
|
208
247
|
module.exports = {
|