@bigbinary/eslint-plugin-neeto 1.1.25 → 1.1.26

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/README.md CHANGED
@@ -47,7 +47,7 @@ module.exports = {
47
47
  ## Exported rules
48
48
 
49
49
  - [use-use-query-client-hook](./docs/src/rules/use-use-query-client-hook.md)
50
-
50
+ - [use-early-return](./docs/src/rules/use-early-return.md)
51
51
  - [use-classnames-for-dynamic-classes](./docs/src/rules/use-classnames-for-dynamic-classes.md)
52
52
  - [use-use-mutation-with-invalidation-hook](./docs/src/rules/use-use-mutation-with-invalidation-hook.md)
53
53
  - [no-withT-for-smart-components](./docs/src/rules/no-withT-for-smart-components.md)
package/dist/index.js CHANGED
@@ -43,6 +43,7 @@
43
43
  var resolve$2 = ramda.mergeDeepLeft(projectResolve$2, commonResolve$2);
44
44
  var rules = {
45
45
  "@bigbinary/neeto/use-use-query-client-hook": "error",
46
+ "@bigbinary/neeto/use-early-return": "warn",
46
47
  "@bigbinary/neeto/use-classnames-for-dynamic-classes": "error",
47
48
  "@bigbinary/neeto/use-use-mutation-with-invalidation-hook": "error",
48
49
  "@bigbinary/neeto/no-withT-for-smart-components": "error",
@@ -555,6 +556,7 @@
555
556
  var USE_USE_MUTATION_WITH_INVALIDATION = "Use useMutationWithInvalidation from 'neetocommons/react-utils'";
556
557
  var USE_CLASSNAMES_PACKAGE = "Use classnames package for dynamic classes";
557
558
  var USE_USE_QUERY_CLIENT_HOOK = "Use 'useQueryClient' hook from 'react-query'";
559
+ var USE_EARLY_RETURN = "Use early return to avoid nested statement";
558
560
 
559
561
  var PACKAGES_TO_CHECK = ["@bigbinary/neeto-molecules", "@bigbinary/neetoui", "@bigbinary/neeto-commons-frontend"];
560
562
 
@@ -6003,7 +6005,7 @@
6003
6005
  var BREAK_STATEMENT_PATTERN = {
6004
6006
  type: TYPES.BREAK_STATEMENT
6005
6007
  };
6006
- var RETURN_STATEMENT_PATTERN = {
6008
+ var RETURN_STATEMENT_PATTERN$1 = {
6007
6009
  type: TYPES.RETURN_STATEMENT
6008
6010
  };
6009
6011
  var ARRAY_LENGTH_TEST_PATTERN = {
@@ -6046,7 +6048,7 @@
6046
6048
  var _blockStatement$body = _slicedToArray(blockStatement.body, 2),
6047
6049
  firstStatement = _blockStatement$body[0],
6048
6050
  secondStatement = _blockStatement$body[1];
6049
- if (!(pure.matches(USE_TRANSLATION_HOOK_PATTERN, firstStatement) && pure.matches(RETURN_STATEMENT_PATTERN, secondStatement))) {
6051
+ if (!(pure.matches(USE_TRANSLATION_HOOK_PATTERN, firstStatement) && pure.matches(RETURN_STATEMENT_PATTERN$1, secondStatement))) {
6050
6052
  return;
6051
6053
  }
6052
6054
  context.report({
@@ -8135,7 +8137,7 @@
8135
8137
  };
8136
8138
 
8137
8139
  var isForLoopReplacable = function isForLoopReplacable(node) {
8138
- return ramda.not(patternExistsDeep(BREAK_STATEMENT_PATTERN, node.body) || patternExistsDeep(RETURN_STATEMENT_PATTERN, node.body) || patternExistsDeep(CHAR_AT_PATTERN, node.body));
8140
+ return ramda.not(patternExistsDeep(BREAK_STATEMENT_PATTERN, node.body) || patternExistsDeep(RETURN_STATEMENT_PATTERN$1, node.body) || patternExistsDeep(CHAR_AT_PATTERN, node.body));
8139
8141
  };
8140
8142
  var useArrayMethods = {
8141
8143
  meta: {
@@ -8813,6 +8815,69 @@
8813
8815
  }
8814
8816
  };
8815
8817
 
8818
+ var RETURN_STATEMENT_PATTERN = {
8819
+ type: TYPES.RETURN_STATEMENT
8820
+ };
8821
+ var RETURN_BLOCK_PATTERN = {
8822
+ type: TYPES.BLOCK_STATEMENT,
8823
+ body: function body(_body) {
8824
+ return _body.length === 1 && pure.matches(RETURN_STATEMENT_PATTERN, _body[0]);
8825
+ }
8826
+ };
8827
+
8828
+ var useEarlyReturn = {
8829
+ meta: {
8830
+ type: "suggestion",
8831
+ docs: {
8832
+ url: "https://github.com/bigbinary/eslint-plugin-neeto/blob/main/docs/src/rules/use-early-return.md",
8833
+ description: "Use early return to avoid nested statements in a function"
8834
+ },
8835
+ fixable: "code"
8836
+ },
8837
+ create: function create(context) {
8838
+ return {
8839
+ ":matches(FunctionDeclaration, ArrowFunctionExpression) > BlockStatement > IfStatement:first-child": function matchesFunctionDeclarationArrowFunctionExpressionBlockStatementIfStatementFirstChild(node) {
8840
+ // checks if a node is `return` type or `block` with only a `return` type
8841
+ var isReturnBlock = ramda.either(pure.matches(RETURN_STATEMENT_PATTERN), pure.matches(RETURN_BLOCK_PATTERN));
8842
+ var ensurePunctuation = function ensurePunctuation(text) {
8843
+ return text.endsWith(";") ? text : "".concat(text, ";");
8844
+ };
8845
+ if (
8846
+ // ignore if:
8847
+ node.parent.body.length > 1 ||
8848
+ // function includes other statements.
8849
+ node.consequent.type !== TYPES.BLOCK_STATEMENT ||
8850
+ // avoid inline `if` statement.
8851
+ isReturnBlock(node.consequent) ||
8852
+ // `if` block only has return statement.
8853
+ node.alternate && !isReturnBlock(node.alternate) ||
8854
+ // `else` block has statements other than return.
8855
+ node.alternate && node.alternate.type === TYPES.IF_STATEMENT // nested if-else ladder
8856
+ ) {
8857
+ return;
8858
+ }
8859
+ var fix = function fix(fixer) {
8860
+ var condition = context.getSourceCode().getText(node.test);
8861
+ var consequent = context.getSourceCode().getText(node.consequent).replace(/^\{|\}$/g, "");
8862
+ var alternate = "return;";
8863
+ if (node.alternate && isReturnBlock(node.alternate)) {
8864
+ alternate = context.getSourceCode().getText(node.alternate);
8865
+ if (node.alternate.type === TYPES.RETURN_STATEMENT) {
8866
+ alternate = ensurePunctuation(alternate);
8867
+ }
8868
+ }
8869
+ return [fixer.insertTextBefore(node, "if(!(".concat(condition, ")) ").concat(alternate)), fixer.replaceText(node, consequent)];
8870
+ };
8871
+ context.report({
8872
+ node: node,
8873
+ message: USE_EARLY_RETURN,
8874
+ fix: fix
8875
+ });
8876
+ }
8877
+ };
8878
+ }
8879
+ };
8880
+
8816
8881
  const defaultOpts$2 = {
8817
8882
  xml: false,
8818
8883
  decodeEntities: true,
@@ -27353,6 +27418,7 @@
27353
27418
  var index = {
27354
27419
  rules: {
27355
27420
  "use-use-query-client-hook": useUseQueryClientHook,
27421
+ "use-early-return": useEarlyReturn,
27356
27422
  "use-classnames-for-dynamic-classes": useClassnamesForDynamicClasses,
27357
27423
  "use-use-mutation-with-invalidation-hook": useUseMutationWithInvalidationHook,
27358
27424
  "no-withT-for-smart-components": noWithTForSmartComponents,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bigbinary/eslint-plugin-neeto",
3
- "version": "1.1.25",
3
+ "version": "1.1.26",
4
4
  "main": "dist/index.js",
5
5
  "license": "UNLICENSED",
6
6
  "scripts": {