@kununu/eslint-config 5.2.2-beta-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.
Files changed (2) hide show
  1. package/index.js +34 -40
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -205,49 +205,43 @@ const baseRules = {
205
205
  'react/require-default-props': 0,
206
206
  // Enforce early return over nested return blocks
207
207
  'prefer-early-return': {
208
- meta: {
209
- type: 'suggestion',
210
- docs: {
211
- description: 'Prefer early returns over nested return blocks',
212
- category: 'Best Practices',
213
- recommended: true,
214
- },
215
- fixable: 'code',
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,
216
214
  },
217
- create(context) {
218
- return {
219
- ReturnStatement(node) {
220
- if (node.argument &&
221
- node.argument.type === 'ObjectExpression' &&
222
- node.argument.properties.some(p => p.type === 'LogicalExpression' || p.value?.type === 'LogicalExpression')) {
223
-
224
- context.report({
225
- node,
226
- message: 'Prefer early returns over nested return blocks',
227
- fix(fixer) {
228
- const sourceCode = context.getSourceCode();
229
- const properties = node.argument.properties;
230
-
231
- // Convert logical expressions to if-returns
232
- const fixes = properties.map(prop => {
233
- if (prop.type === 'LogicalExpression' || prop.value?.type === 'LogicalExpression') {
234
- const logicalExp = prop.type === 'LogicalExpression' ? prop : prop.value;
235
- const condition = sourceCode.getText(logicalExp.left);
236
- const returnValue = sourceCode.getText(logicalExp.right);
237
- return `if (${condition}) return ${returnValue};`;
238
- }
239
- // If it's the last non-logical property, it becomes the default return
240
- return `return ${sourceCode.getText(prop)};`;
241
- });
242
-
243
- return fixer.replaceText(node, fixes.join('\n'));
244
- }
245
- });
246
- }
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
+ });
247
240
  }
248
- };
249
- }
241
+ }
242
+ };
250
243
  }
244
+ }
251
245
  };
252
246
 
253
247
  module.exports = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kununu/eslint-config",
3
- "version": "5.2.2-beta-1",
3
+ "version": "5.2.2-beta-2",
4
4
  "description": "kununu's ESLint config",
5
5
  "main": "index.js",
6
6
  "repository": "kununu/eslint-config",