@bigbinary/eslint-plugin-neeto 1.1.20 → 1.1.22

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 (3) hide show
  1. package/README.md +2 -0
  2. package/dist/index.js +319 -7
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -46,6 +46,7 @@ module.exports = {
46
46
 
47
47
  ## Exported rules
48
48
 
49
+ - [use-use-mutation-with-invalidation-hook](./docs/src/rules/use-use-mutation-with-invalidation-hook.md)
49
50
  - [no-withT-for-smart-components](./docs/src/rules/no-withT-for-smart-components.md)
50
51
  - [no-large-functions-inside-jsx](./docs/src/rules/no-large-functions-inside-jsx.md)
51
52
  - [use-standard-date-time-formats](./docs/src/rules/use-standard-date-time-formats.md)
@@ -113,6 +114,7 @@ module.exports = {
113
114
  - [use-functions-and-hooks-from-neeto-commons-frontend.md](./docs/src/rules/use-functions-and-hooks-from-neeto-commons-frontend.md)
114
115
  - [zustand-use-with-immutable-actions-wrapper-inside-create](./docs/src/rules/zustand-use-with-immutable-actions-wrapper-inside-create.md)
115
116
  - [add-required-peer-dependencies](./docs/src/rules/add-required-peer-dependencies.md)
117
+ - [move-spread-attributes-to-top-position](./docs/src/rules/move-spread-attributes-to-top-position.md)
116
118
 
117
119
  ## Other references
118
120
 
package/dist/index.js CHANGED
@@ -42,6 +42,7 @@
42
42
  var commonResolve$2 = loadJS$2("node_modules/@bigbinary/neeto-commons-frontend/configs/webpack/resolve.js");
43
43
  var resolve$2 = ramda.mergeDeepLeft(projectResolve$2, commonResolve$2);
44
44
  var rules = {
45
+ "@bigbinary/neeto/use-use-mutation-with-invalidation-hook": "error",
45
46
  "@bigbinary/neeto/no-withT-for-smart-components": "error",
46
47
  "@bigbinary/neeto/no-large-functions-inside-jsx": "error",
47
48
  "@bigbinary/neeto/use-standard-date-time-formats": "error",
@@ -151,7 +152,8 @@
151
152
  "@bigbinary/neeto/merge-spread-attributes": "error",
152
153
  "@bigbinary/neeto/props-inside-spread-attributes-should-be-sorted": "error",
153
154
  "@bigbinary/neeto/props-with-different-key-value-should-be-outside-spread-attribute": "error",
154
- "@bigbinary/neeto/use-common-constants": "warn"
155
+ "@bigbinary/neeto/use-common-constants": "warn",
156
+ "@bigbinary/neeto/move-spread-attributes-to-top-position": "error"
155
157
  };
156
158
 
157
159
  var recommended = {
@@ -547,6 +549,8 @@
547
549
  var USE_WITH_T_HOC_IN_DUMB_COMPONENTS = 'Use "withT" HOC from "neeto-commons-frontend" instead of useTranslation hook in Dumb components';
548
550
  var NO_WITH_T_IN_SMART_COMPONENTS = '"withT" HOC should only be used in dumb components. "useTranslation" hook should be used here.';
549
551
  var USE_STANDARD_DATE_TIME_FORMATS = 'Use "dateFormat/timeFormat" from "@bigbinary/neeto-commons-frontend/utils" instead. Refer: https://github.com/bigbinary/neeto-commons-frontend/blob/main/docs/utils/README.md#dateformattimeformat';
552
+ var MOVE_SPREAD_ATTRIBUTES_TO_TOP = "Move spread attribute to top";
553
+ var USE_USE_MUTATION_WITH_INVALIDATION = "Use useMutationWithInvalidation from 'neetocommons/react-utils'";
550
554
 
551
555
  var PACKAGES_TO_CHECK = ["@bigbinary/neeto-molecules", "@bigbinary/neetoui", "@bigbinary/neeto-commons-frontend"];
552
556
 
@@ -5366,7 +5370,7 @@
5366
5370
  };
5367
5371
 
5368
5372
  //import { } from "neetocommons/react-utils";
5369
- var REACT_UTILS_IMPORT_PATTERN$1 = {
5373
+ var REACT_UTILS_IMPORT_PATTERN$2 = {
5370
5374
  type: TYPES.IMPORT_DECLARATION,
5371
5375
  source: {
5372
5376
  type: TYPES.LITERAL,
@@ -5476,7 +5480,7 @@
5476
5480
  context: context,
5477
5481
  fixer: fixer,
5478
5482
  functions: ["handleMetaClick"],
5479
- importPattern: REACT_UTILS_IMPORT_PATTERN$1
5483
+ importPattern: REACT_UTILS_IMPORT_PATTERN$2
5480
5484
  });
5481
5485
  if (importStatement) fixerArray.push(importStatement);
5482
5486
  return fixerArray;
@@ -6646,7 +6650,7 @@
6646
6650
  var restOfString = inputString.slice(position + 1);
6647
6651
  return firstCharacter + textToInsert + restOfString;
6648
6652
  };
6649
- var reportError = function reportError(_ref) {
6653
+ var reportError$1 = function reportError(_ref) {
6650
6654
  var context = _ref.context,
6651
6655
  node = _ref.node,
6652
6656
  arrowFunctionExpression = _ref.arrowFunctionExpression,
@@ -6687,7 +6691,7 @@
6687
6691
  componentDeclaration = node;
6688
6692
  if (!pure.matches(WITH_T_HOC_PATTERN, node)) return;
6689
6693
  var arrowFunctionExpression = node.declarations[0].init.arguments[0];
6690
- reportError({
6694
+ reportError$1({
6691
6695
  context: context,
6692
6696
  node: node,
6693
6697
  arrowFunctionExpression: arrowFunctionExpression
@@ -6696,7 +6700,7 @@
6696
6700
  ExportDefaultDeclaration: function ExportDefaultDeclaration(node) {
6697
6701
  if (!pure.matches(EXPORTED_WITH_T_PATTERN, node)) return;
6698
6702
  if (!pure.matches(SMART_COMPONENT_PATTERN, componentDeclaration)) return;
6699
- reportError({
6703
+ reportError$1({
6700
6704
  context: context,
6701
6705
  node: node,
6702
6706
  arrowFunctionExpression: componentDeclaration.declarations[0].init,
@@ -6879,6 +6883,13 @@
6879
6883
  array.splice(index, 1);
6880
6884
  }
6881
6885
  };
6886
+ var findIndexToInsert = function findIndexToInsert(endIndex, attributes) {
6887
+ var index = endIndex - 1;
6888
+ for (; index >= 0; --index) {
6889
+ if (attributes[index].type === TYPES.JSX_SPREAD_ATTRIBUTE) return index + 1;
6890
+ }
6891
+ return index + 1;
6892
+ };
6882
6893
 
6883
6894
  var mergeSpreadAttributes = {
6884
6895
  meta: {
@@ -7004,6 +7015,43 @@
7004
7015
  }
7005
7016
  };
7006
7017
 
7018
+ var moveSpreadAttributesToTopPosition = {
7019
+ meta: {
7020
+ type: "problem",
7021
+ docs: {
7022
+ url: "https://github.com/bigbinary/eslint-plugin-neeto/blob/main/docs/src/rules/move-spread-attributes-to-top-position.md",
7023
+ description: "Enforce moving spread attributes to top most position in oprning elements"
7024
+ },
7025
+ fixable: "code"
7026
+ },
7027
+ create: function create(context) {
7028
+ return {
7029
+ JSXSpreadAttribute: function JSXSpreadAttribute(node) {
7030
+ var _node$argument$proper;
7031
+ if (!pure.matches(SPREAD_ATTRIBUTE_WITH_OBJECT_EXPRESSION, node) || (_node$argument$proper = node.argument.properties) !== null && _node$argument$proper !== void 0 && _node$argument$proper.find(pure.matches(OTHER_PROPS_INSIDE_SPREAD_ATTRIBUTE))) {
7032
+ return;
7033
+ }
7034
+ var attributes = node.parent.attributes;
7035
+ var nodeIndex = pure.findIndexBy({
7036
+ range: node.range
7037
+ }, attributes);
7038
+ var indexToInsert = findIndexToInsert(nodeIndex, attributes);
7039
+ if (indexToInsert === nodeIndex) return;
7040
+ var indentLevel = context.getSourceCode().getFirstToken(attributes[0]).loc.start.column;
7041
+ context.report({
7042
+ node: node,
7043
+ message: MOVE_SPREAD_ATTRIBUTES_TO_TOP,
7044
+ fix: function fix(fixer) {
7045
+ var removeNode = fixer.remove(node);
7046
+ var moveNode = fixer.insertTextBefore(attributes[indexToInsert], "".concat(context.getSourceCode().getText(node), "\n").concat(" ".repeat(indentLevel)));
7047
+ return [moveNode, removeNode];
7048
+ }
7049
+ });
7050
+ }
7051
+ };
7052
+ }
7053
+ };
7054
+
7007
7055
  var noRepetitionInPropKeyAndValueName = {
7008
7056
  meta: {
7009
7057
  type: "problem",
@@ -26252,6 +26300,268 @@
26252
26300
  }
26253
26301
  };
26254
26302
 
26303
+ var ONSUCCESS_OPTION = {
26304
+ type: TYPES.PROPERTY,
26305
+ key: {
26306
+ name: "onSuccess"
26307
+ }
26308
+ };
26309
+
26310
+ // keysToInvalidate: [...]
26311
+ var KEYS_TO_INVALIDATE_OPTION = {
26312
+ type: TYPES.PROPERTY,
26313
+ key: {
26314
+ name: "keysToInvalidate"
26315
+ }
26316
+ };
26317
+
26318
+ // {
26319
+ // onSuccess: (...) => {....},
26320
+ // ...,
26321
+ // });
26322
+ var OPTIONS = {
26323
+ type: TYPES.OBJECT_EXPRESSION,
26324
+ properties: pure.existsBy(ONSUCCESS_OPTION)
26325
+ };
26326
+
26327
+ // useMutation(api.update, {
26328
+ // onSuccess: (...) => {....},
26329
+ // ...,
26330
+ // });
26331
+ var USE_MUTATION_HOOK_WITH_ONSUCCESS = {
26332
+ callee: {
26333
+ type: TYPES.IDENTIFIER,
26334
+ name: ramda.includes(ramda.__, ["useMutation", "useMutationWithInvalidation"])
26335
+ },
26336
+ arguments: pure.existsBy(OPTIONS)
26337
+ };
26338
+
26339
+ // useMutation(api.update, {
26340
+ // onSuccess: (...) => {....queryClient....},
26341
+ // ...,
26342
+ // });
26343
+ var INVALIDATION_PATTERN = {
26344
+ type: TYPES.EXPRESSION_STATEMENT,
26345
+ expression: {
26346
+ type: TYPES.CALL_EXPRESSION,
26347
+ callee: {
26348
+ type: TYPES.MEMBER_EXPRESSION,
26349
+ object: {
26350
+ type: TYPES.IDENTIFIER,
26351
+ name: "queryClient"
26352
+ }
26353
+ }
26354
+ }
26355
+ };
26356
+
26357
+ // () => queryClient.invalidateQueries(CUSTOMERS)
26358
+ var SINGLE_INVALIDATION_PATTERN = {
26359
+ type: TYPES.CALL_EXPRESSION,
26360
+ callee: {
26361
+ type: TYPES.MEMBER_EXPRESSION,
26362
+ object: {
26363
+ type: TYPES.IDENTIFIER,
26364
+ name: "queryClient"
26365
+ },
26366
+ property: {
26367
+ type: TYPES.IDENTIFIER,
26368
+ name: "invalidateQueries"
26369
+ }
26370
+ }
26371
+ };
26372
+
26373
+ // import { useMutationWithInvalidation } from "neetocommons/react-utils";
26374
+ var USE_MUTATION_WITH_INVALIDATION_IMPORT_PATTERN = {
26375
+ type: TYPES.IMPORT_DECLARATION,
26376
+ specifiers: pure.existsBy({
26377
+ type: TYPES.IMPORT_SPECIFIER,
26378
+ imported: {
26379
+ type: TYPES.IDENTIFIER,
26380
+ name: "useMutationWithInvalidation"
26381
+ }
26382
+ }),
26383
+ source: {
26384
+ type: TYPES.LITERAL,
26385
+ value: "neetocommons/react-utils"
26386
+ }
26387
+ };
26388
+
26389
+ //import { } from "neetocommons/react-utils";
26390
+ var REACT_UTILS_IMPORT_PATTERN$1 = {
26391
+ type: TYPES.IMPORT_DECLARATION,
26392
+ source: {
26393
+ type: TYPES.LITERAL,
26394
+ value: "neetocommons/react-utils"
26395
+ },
26396
+ specifiers: [{
26397
+ type: TYPES.IMPORT_SPECIFIER
26398
+ }]
26399
+ };
26400
+
26401
+ // queryClient.setQueryData(...);
26402
+ var OTHER_QUERY_CLIENT_USAGE_PATTERN = {
26403
+ callee: {
26404
+ property: {
26405
+ type: TYPES.IDENTIFIER,
26406
+ name: pure.isNot("invalidateQueries")
26407
+ }
26408
+ }
26409
+ };
26410
+
26411
+ var reportError = function reportError(context, node) {
26412
+ context.report({
26413
+ node: node,
26414
+ message: USE_USE_MUTATION_WITH_INVALIDATION
26415
+ });
26416
+ };
26417
+ var getInitialFixerArray = function getInitialFixerArray(_ref) {
26418
+ var node = _ref.node,
26419
+ context = _ref.context,
26420
+ fixer = _ref.fixer,
26421
+ mutationOptions = _ref.mutationOptions,
26422
+ existingKeysToInvalidate = _ref.existingKeysToInvalidate,
26423
+ keys = _ref.keys,
26424
+ importExists = _ref.importExists;
26425
+ var replaceCallee = fixer.replaceText(node.callee, "useMutationWithInvalidation");
26426
+ var indentLevel = context.getSourceCode().getFirstToken(mutationOptions.properties[0]).loc.start.column;
26427
+ var addKeysToInvalidate;
26428
+ if (existingKeysToInvalidate) {
26429
+ addKeysToInvalidate = fixer.insertTextAfter(ramda.last(existingKeysToInvalidate.value.elements), ",".concat(keys));
26430
+ } else {
26431
+ addKeysToInvalidate = fixer.insertTextBefore(mutationOptions.properties[0], "keysToInvalidate: [".concat(keys, "],\n").concat(" ".repeat(indentLevel)));
26432
+ }
26433
+ var fixerArray = [replaceCallee, addKeysToInvalidate];
26434
+ if (!importExists) {
26435
+ var addImport = addFunctionImport({
26436
+ context: context,
26437
+ fixer: fixer,
26438
+ functions: ["useMutationWithInvalidation"],
26439
+ importPattern: REACT_UTILS_IMPORT_PATTERN$1
26440
+ });
26441
+ fixerArray.push(addImport);
26442
+ }
26443
+ return fixerArray;
26444
+ };
26445
+ var getRemoveCommaFix = function getRemoveCommaFix(context, fixer, onSuccess) {
26446
+ var tokenAfter = context.getTokenAfter(onSuccess);
26447
+ return tokenAfter.value === "," ? fixer.remove(tokenAfter) : null;
26448
+ };
26449
+ var getVariables = function getVariables(context, params) {
26450
+ var _params$arguments$0$e;
26451
+ var variables = (_params$arguments$0$e = params.arguments[0].elements) === null || _params$arguments$0$e === void 0 ? void 0 : _params$arguments$0$e.filter(function (element) {
26452
+ return element.type !== TYPES.LITERAL && /[a-z]/.test(context.getSourceCode().getText(element));
26453
+ });
26454
+ if (!params.arguments[0].elements && /[a-z]/.test(context.getSourceCode().getText(params.arguments[0]))) {
26455
+ variables = params.arguments[0];
26456
+ }
26457
+ return variables;
26458
+ };
26459
+
26460
+ var useUseMutationWithInvalidationHook = {
26461
+ meta: {
26462
+ type: "problem",
26463
+ docs: {
26464
+ url: "https://github.com/bigbinary/eslint-plugin-neeto/blob/main/docs/src/rules/use-use-mutation-with-invalidation-hook.md",
26465
+ description: "Enforce use of useMutationWithInvalidation"
26466
+ },
26467
+ fixable: "code"
26468
+ },
26469
+ create: function create(context) {
26470
+ return {
26471
+ CallExpression: function CallExpression(node) {
26472
+ if (!pure.matches(USE_MUTATION_HOOK_WITH_ONSUCCESS, node)) return;
26473
+ var mutationOptions = pure.findBy({
26474
+ type: TYPES.OBJECT_EXPRESSION
26475
+ }, node.arguments);
26476
+ var onSuccess = pure.findBy(ONSUCCESS_OPTION, mutationOptions.properties);
26477
+ var existingKeysToInvalidate = pure.findBy(KEYS_TO_INVALIDATE_OPTION, mutationOptions.properties);
26478
+ if (!onSuccess.value.body) return;
26479
+ var importExists = pure.existsBy(USE_MUTATION_WITH_INVALIDATION_IMPORT_PATTERN, getAllImports(node));
26480
+ if (onSuccess.value.body.type === TYPES.CALL_EXPRESSION) {
26481
+ var params = onSuccess.value.body;
26482
+ if (!pure.matches(SINGLE_INVALIDATION_PATTERN, params)) return;
26483
+ var variables = getVariables(context, params);
26484
+ if (pure.isPresent(variables)) {
26485
+ reportError(context, node);
26486
+ } else {
26487
+ context.report({
26488
+ node: node,
26489
+ message: USE_USE_MUTATION_WITH_INVALIDATION,
26490
+ fix: function fix(fixer) {
26491
+ var fixerArray = getInitialFixerArray({
26492
+ node: node,
26493
+ context: context,
26494
+ fixer: fixer,
26495
+ mutationOptions: mutationOptions,
26496
+ existingKeysToInvalidate: existingKeysToInvalidate,
26497
+ keys: context.getSourceCode().getText(params.arguments[0]),
26498
+ importExists: importExists
26499
+ });
26500
+ var removeInvalidations = fixer.remove(onSuccess);
26501
+ fixerArray.push(removeInvalidations);
26502
+ var removeComma = getRemoveCommaFix(context, fixer, onSuccess);
26503
+ if (removeComma) fixerArray.push(removeComma);
26504
+ return fixerArray;
26505
+ }
26506
+ });
26507
+ }
26508
+ } else if (onSuccess.value.body.type === TYPES.BLOCK_STATEMENT) {
26509
+ var _params = onSuccess.value.body.body;
26510
+ var invalidations = pure._filterBy(INVALIDATION_PATTERN, _params);
26511
+ if (pure.isNotPresent(invalidations)) return;
26512
+ var otherOptions = pure._removeBy(INVALIDATION_PATTERN, _params);
26513
+ var queryClientInOtherOptions = otherOptions === null || otherOptions === void 0 ? void 0 : otherOptions.find(function (item) {
26514
+ return context.getSourceCode().getText(item).includes("queryClient");
26515
+ });
26516
+ if (queryClientInOtherOptions || pure.findBy(OTHER_QUERY_CLIENT_USAGE_PATTERN, ramda.pluck("expression", invalidations))) {
26517
+ return;
26518
+ }
26519
+ var invalidationsWithoutVariables = invalidations.filter(function (item) {
26520
+ var variables = getVariables(context, item.expression);
26521
+ return pure.isNotPresent(variables);
26522
+ });
26523
+ if (pure.isNotPresent(invalidationsWithoutVariables)) {
26524
+ reportError(context, node);
26525
+ } else {
26526
+ var keys = ramda.pluck("expression", invalidationsWithoutVariables).flatMap(function (item) {
26527
+ return item.arguments.map(function (arg) {
26528
+ return context.getSourceCode().getText(arg);
26529
+ });
26530
+ });
26531
+ context.report({
26532
+ node: node,
26533
+ message: USE_USE_MUTATION_WITH_INVALIDATION,
26534
+ fix: function fix(fixer) {
26535
+ var fixerArray = getInitialFixerArray({
26536
+ node: node,
26537
+ context: context,
26538
+ fixer: fixer,
26539
+ mutationOptions: mutationOptions,
26540
+ existingKeysToInvalidate: existingKeysToInvalidate,
26541
+ keys: keys.join(", "),
26542
+ importExists: importExists
26543
+ });
26544
+ if (_params.length === invalidationsWithoutVariables.length) {
26545
+ var _removeInvalidations = fixer.remove(onSuccess);
26546
+ fixerArray.push(_removeInvalidations);
26547
+ var removeComma = getRemoveCommaFix(context, fixer, onSuccess);
26548
+ if (removeComma) fixerArray.push(removeComma);
26549
+ return fixerArray;
26550
+ }
26551
+ var removeInvalidations = invalidationsWithoutVariables.map(function (item) {
26552
+ return fixer.remove(item);
26553
+ });
26554
+ fixerArray.push.apply(fixerArray, _toConsumableArray(removeInvalidations));
26555
+ return fixerArray;
26556
+ }
26557
+ });
26558
+ }
26559
+ }
26560
+ }
26561
+ };
26562
+ }
26563
+ };
26564
+
26255
26565
  var useWebpackAlias = {
26256
26566
  meta: {
26257
26567
  type: "suggestion",
@@ -26835,6 +27145,7 @@
26835
27145
 
26836
27146
  var index = {
26837
27147
  rules: {
27148
+ "use-use-mutation-with-invalidation-hook": useUseMutationWithInvalidationHook,
26838
27149
  "no-withT-for-smart-components": noWithTForSmartComponents,
26839
27150
  "no-large-functions-inside-jsx": noLargeFunctionsInsideJsx,
26840
27151
  "use-standard-date-time-formats": useStandardDateTimeFormats,
@@ -26901,7 +27212,8 @@
26901
27212
  "no-unnecessary-if-condition": noUnnecessaryIfCondition,
26902
27213
  "zustand-use-with-immutable-actions-wrapper-inside-create": useWithImmutableActionsWrapperInsideCreate,
26903
27214
  "add-required-peer-dependencies": addRequiredPeerDependencies,
26904
- "use-common-constants": useCommonConstants
27215
+ "use-common-constants": useCommonConstants,
27216
+ "move-spread-attributes-to-top-position": moveSpreadAttributesToTopPosition
26905
27217
  },
26906
27218
  configs: {
26907
27219
  recommended: recommended,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bigbinary/eslint-plugin-neeto",
3
- "version": "1.1.20",
3
+ "version": "1.1.22",
4
4
  "main": "dist/index.js",
5
5
  "license": "UNLICENSED",
6
6
  "scripts": {