@bigbinary/eslint-plugin-neeto 1.0.61 → 1.0.63

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 +1 -0
  2. package/dist/index.js +219 -30
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -103,6 +103,7 @@ module.exports = {
103
103
  - [props-with-different-key-value-should-be-outside-spread-attribute](./docs/src/rules/props-with-different-key-value-should-be-outside-spread-attribute.md)
104
104
  - [no-repetition-in-prop-key-value-names](./docs/src/rules/no-repetition-in-prop-key-value-names.md)
105
105
  - [use-functions-and-hooks-from-neeto-commons-frontend.md](./docs/src/rules/use-functions-and-hooks-from-neeto-commons-frontend.md)
106
+ - [zustand-use-with-immutable-actions-wrapper-inside-create](./docs/src/rules/zustand-use-with-immutable-actions-wrapper-inside-create.md)
106
107
 
107
108
  ## Other references
108
109
 
package/dist/index.js CHANGED
@@ -96,7 +96,7 @@
96
96
  "@bigbinary/neeto/routes-should-be-declared-and-default-exported": "error",
97
97
  "@bigbinary/neeto/routes-should-match-object-path": "error",
98
98
  "@bigbinary/neeto/use-array-methods": "warn",
99
- "@bigbinary/neeto/use-axios-config-for-passing-query-parameters": "error",
99
+ "@bigbinary/neeto/use-axios-config-for-passing-query-parameters": "warn",
100
100
  "@bigbinary/neeto/use-camel-case-or-pascal-case-for-function-names": "error",
101
101
  "@bigbinary/neeto/use-common-routes": "error",
102
102
  "@bigbinary/neeto/use-components-children-prop-in-trans": "error",
@@ -119,7 +119,8 @@
119
119
  "@bigbinary/neeto/zustand-recommend-comparator-function-usage": "warn",
120
120
  "@bigbinary/neeto/zustand-use-prop-to-pick-single-item": "error",
121
121
  "@bigbinary/neeto/use-functions-and-hooks-from-neeto-commons-frontend": "warn",
122
- "@bigbinary/neeto/no-unnecessary-if-condition": "error"
122
+ "@bigbinary/neeto/no-unnecessary-if-condition": "error",
123
+ "@bigbinary/neeto/zustand-use-with-immutable-actions-wrapper-inside-create": "error"
123
124
  };
124
125
 
125
126
  var recommended = {
@@ -166,19 +167,19 @@
166
167
  }
167
168
  }
168
169
 
169
- function _arrayLikeToArray(arr, len) {
170
+ function _arrayLikeToArray$1(arr, len) {
170
171
  if (len == null || len > arr.length) len = arr.length;
171
172
  for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
172
173
  return arr2;
173
174
  }
174
175
 
175
- function _unsupportedIterableToArray(o, minLen) {
176
+ function _unsupportedIterableToArray$1(o, minLen) {
176
177
  if (!o) return;
177
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
178
+ if (typeof o === "string") return _arrayLikeToArray$1(o, minLen);
178
179
  var n = Object.prototype.toString.call(o).slice(8, -1);
179
180
  if (n === "Object" && o.constructor) n = o.constructor.name;
180
181
  if (n === "Map" || n === "Set") return Array.from(o);
181
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
182
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray$1(o, minLen);
182
183
  }
183
184
 
184
185
  function _nonIterableRest() {
@@ -186,7 +187,7 @@
186
187
  }
187
188
 
188
189
  function _slicedToArray(arr, i) {
189
- return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
190
+ return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray$1(arr, i) || _nonIterableRest();
190
191
  }
191
192
 
192
193
  var regeneratorRuntime$1 = {exports: {}};
@@ -972,6 +973,7 @@
972
973
  var MERGE_ADJACENT_SPREAD_ATTRIBUTES = "This spread attribute can be merged";
973
974
  var DIFFERENT_PROPS_MUST_BE_OUTSIDE = "Props with different key and value should be outside the spread attribute";
974
975
  var PROP_KEY_VALUE_DUPLICATE = "Prop key and value names should not be repeated";
976
+ var USE_WITH_IMMUTABLE_ACTIONS_WRAPPER = 'Use "withImmutableActions" wrapper from "neeto-commons-frontend" inside zustand create. Refer: https://bit.ly/withimmutableactions';
975
977
 
976
978
  var toCamelCase = function toCamelCase(text) {
977
979
  var capitalized = text.split(/[^0-9a-z]/i).map(function (part) {
@@ -1410,7 +1412,7 @@
1410
1412
  };
1411
1413
 
1412
1414
  function _arrayWithoutHoles(arr) {
1413
- if (Array.isArray(arr)) return _arrayLikeToArray(arr);
1415
+ if (Array.isArray(arr)) return _arrayLikeToArray$1(arr);
1414
1416
  }
1415
1417
 
1416
1418
  function _iterableToArray(iter) {
@@ -1422,7 +1424,7 @@
1422
1424
  }
1423
1425
 
1424
1426
  function _toConsumableArray(arr) {
1425
- return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
1427
+ return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray$1(arr) || _nonIterableSpread();
1426
1428
  }
1427
1429
 
1428
1430
  var camelize = function camelize(text) {
@@ -5053,7 +5055,7 @@
5053
5055
  };
5054
5056
 
5055
5057
  //import { } from "neetocommons/react-utils";
5056
- var REACT_UTILS_IMPORT_PATTERN = {
5058
+ var REACT_UTILS_IMPORT_PATTERN$1 = {
5057
5059
  type: TYPES.IMPORT_DECLARATION,
5058
5060
  source: {
5059
5061
  type: TYPES.LITERAL,
@@ -5163,7 +5165,7 @@
5163
5165
  context: context,
5164
5166
  fixer: fixer,
5165
5167
  functions: ["handleMetaClick"],
5166
- importPattern: REACT_UTILS_IMPORT_PATTERN
5168
+ importPattern: REACT_UTILS_IMPORT_PATTERN$1
5167
5169
  });
5168
5170
  if (importStatement) fixerArray.push(importStatement);
5169
5171
  return fixerArray;
@@ -7393,38 +7395,145 @@
7393
7395
  }]
7394
7396
  };
7395
7397
 
7398
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
7399
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
7400
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
7401
+ var extractVariableValue = function extractVariableValue(inputValue) {
7402
+ var match = inputValue.match(/\${([^}]+)}/);
7403
+ return match ? match[1] : "\"".concat(inputValue, "\"");
7404
+ };
7405
+ var getUrlSearchParams = function getUrlSearchParams(urlString) {
7406
+ var params = {};
7407
+ var queryString = urlString.split("?")[1].slice(0, -1);
7408
+ if (queryString) {
7409
+ var searchParams = new URLSearchParams(queryString);
7410
+ var _iterator = _createForOfIteratorHelper(searchParams),
7411
+ _step;
7412
+ try {
7413
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
7414
+ var _step$value = _slicedToArray(_step.value, 2),
7415
+ key = _step$value[0],
7416
+ value = _step$value[1];
7417
+ if (ramda.isEmpty(value)) return {};
7418
+ params[key] = extractVariableValue(value);
7419
+ }
7420
+ } catch (err) {
7421
+ _iterator.e(err);
7422
+ } finally {
7423
+ _iterator.f();
7424
+ }
7425
+ }
7426
+ return params;
7427
+ };
7428
+ var getParamsFixString = function getParamsFixString(params, paramsString) {
7429
+ var keys = Object.keys(params);
7430
+ if (keys.length === 1 && keys[0] === params[keys[0]]) {
7431
+ return "params: ".concat(keys[0]);
7432
+ }
7433
+ return "params: { ".concat(paramsString, "}");
7434
+ };
7435
+ var getFixForParamsObjectAlreadyPresent = function getFixForParamsObjectAlreadyPresent(paramsNode, fixer, paramsString) {
7436
+ if (paramsNode.value.type === TYPES.IDENTIFIER) {
7437
+ return fixer.replaceText(paramsNode, "params: { ...".concat(paramsNode.value.name, ", ").concat(paramsString, " }"));
7438
+ }
7439
+ return fixer.insertTextAfter(ramda.last(paramsNode.value.properties), ", ".concat(paramsString));
7440
+ };
7441
+ var getFix = function getFix(_ref) {
7442
+ var urlNode = _ref.urlNode,
7443
+ context = _ref.context,
7444
+ node = _ref.node,
7445
+ fixer = _ref.fixer;
7446
+ var urlString = context.getSourceCode().getText(urlNode);
7447
+ var fixForParams = getFixForParams({
7448
+ urlNode: urlNode,
7449
+ urlString: urlString,
7450
+ node: node,
7451
+ fixer: fixer
7452
+ });
7453
+ if (!fixForParams) return null;
7454
+ return [getFixForUrl(fixer, urlString, urlNode), fixForParams];
7455
+ };
7456
+ var getFixForUrl = function getFixForUrl(fixer, urlString, urlNode) {
7457
+ var urlWithoutSearchParams = urlString.split("?")[0].slice(1);
7458
+ var match = urlWithoutSearchParams.match(/^\${([^}]+)}$/);
7459
+ var urlFixString = "";
7460
+ if (match) {
7461
+ urlFixString = match[1];
7462
+ } else {
7463
+ urlFixString = urlWithoutSearchParams.includes("${") ? "`".concat(urlWithoutSearchParams, "`") : "\"".concat(urlWithoutSearchParams, "\"");
7464
+ }
7465
+ return fixer.replaceText(urlNode, urlFixString);
7466
+ };
7467
+ var getFixForParams = function getFixForParams(_ref2) {
7468
+ var urlNode = _ref2.urlNode,
7469
+ urlString = _ref2.urlString,
7470
+ node = _ref2.node,
7471
+ fixer = _ref2.fixer;
7472
+ var objectNode = pure.findBy({
7473
+ type: TYPES.OBJECT_EXPRESSION
7474
+ }, node.arguments);
7475
+ var objectProperties = objectNode === null || objectNode === void 0 ? void 0 : objectNode.properties;
7476
+ var paramsNode = objectProperties === null || objectProperties === void 0 ? void 0 : objectProperties.find(function (prop) {
7477
+ return prop.key.name === "params";
7478
+ });
7479
+ var params = getUrlSearchParams(urlString);
7480
+ if (ramda.isEmpty(params)) return null;
7481
+ var paramsString = Object.entries(params).map(function (_ref3) {
7482
+ var _ref4 = _slicedToArray(_ref3, 2),
7483
+ key = _ref4[0],
7484
+ value = _ref4[1];
7485
+ return "".concat(key, ": ").concat(value);
7486
+ }).join(", ");
7487
+ if (objectNode) {
7488
+ if (paramsNode) {
7489
+ return getFixForParamsObjectAlreadyPresent(paramsNode, fixer, paramsString);
7490
+ }
7491
+ var positionToInsert = ramda.last(objectProperties);
7492
+ var _fixString = getParamsFixString(params, paramsString);
7493
+ return fixer.insertTextAfter(positionToInsert, ", ".concat(_fixString));
7494
+ }
7495
+ var fixString = getParamsFixString(params, paramsString);
7496
+ return fixer.insertTextAfter(urlNode, ", { ".concat(fixString, "}"));
7497
+ };
7396
7498
  var getPropValue = function getPropValue(configNode, name) {
7397
7499
  var _configNode$propertie;
7398
- return (_configNode$propertie = configNode.properties.find(function (_ref) {
7399
- var key = _ref.key;
7500
+ return (_configNode$propertie = configNode.properties.find(function (_ref5) {
7501
+ var key = _ref5.key;
7400
7502
  return key.name === name;
7401
7503
  })) === null || _configNode$propertie === void 0 ? void 0 : _configNode$propertie.value;
7402
7504
  };
7403
- var checkAndReport = function checkAndReport(_ref2) {
7404
- var context = _ref2.context,
7405
- node = _ref2.node,
7406
- urlNode = _ref2.urlNode;
7407
- var isQuestionMarkPresent = false;
7408
- if (urlNode.type === TYPES.LITERAL) {
7409
- isQuestionMarkPresent = urlNode.value.includes("?");
7410
- } else {
7411
- isQuestionMarkPresent = urlNode.quasis.some(function (_ref3) {
7412
- var value = _ref3.value;
7413
- return value.cooked.includes("?");
7505
+ var checkAndReport = function checkAndReport(_ref6) {
7506
+ var context = _ref6.context,
7507
+ node = _ref6.node,
7508
+ urlNode = _ref6.urlNode;
7509
+ var isQuestionMarkPresent = urlNode.type === TYPES.LITERAL ? urlNode.value.includes("?") : urlNode.quasis.some(function (_ref7) {
7510
+ var value = _ref7.value;
7511
+ return value.cooked.includes("?");
7512
+ });
7513
+ if (isQuestionMarkPresent) {
7514
+ context.report({
7515
+ node: node,
7516
+ message: USE_AXIOS_CONFIG_FOR_PASSING_QUERY_PARAMETERS_MESSAGE,
7517
+ fix: function fix(fixer) {
7518
+ return getFix({
7519
+ urlNode: urlNode,
7520
+ context: context,
7521
+ node: node,
7522
+ fixer: fixer
7523
+ });
7524
+ }
7414
7525
  });
7415
7526
  }
7416
- if (isQuestionMarkPresent) context.report({
7417
- node: node,
7418
- message: USE_AXIOS_CONFIG_FOR_PASSING_QUERY_PARAMETERS_MESSAGE
7419
- });
7420
7527
  };
7528
+
7421
7529
  var useAxiosConfigForPassingQueryParameters = {
7422
7530
  meta: {
7423
7531
  type: "problem",
7424
7532
  docs: {
7425
7533
  url: "https://github.com/bigbinary/eslint-plugin-neeto/blob/main/docs/src/rules/use-axios-config-for-passing-query-parameters.md",
7426
7534
  description: "Enforces using the config parameter of axios to send query parameters through axios"
7427
- }
7535
+ },
7536
+ fixable: "code"
7428
7537
  },
7429
7538
  create: function create(context) {
7430
7539
  return {
@@ -25169,6 +25278,15 @@
25169
25278
  return matchesAny([PICK_PATTERN_SHORT, ARROW_FUNCTION_PATTERN], args[0]);
25170
25279
  };
25171
25280
 
25281
+ // withImmutableActions(...)
25282
+ var WITH_IMMUTABLE_ACTIONS_WRAPPER_PATTERN = {
25283
+ type: TYPES.CALL_EXPRESSION,
25284
+ callee: {
25285
+ type: TYPES.IDENTIFIER,
25286
+ name: "withImmutableActions"
25287
+ }
25288
+ };
25289
+
25172
25290
  // const order = useSampleStore(pick(["order"]));
25173
25291
  // const order = useSampleStore(pick(["order"]), shallow);
25174
25292
  // const order = useSampleStore(pick([order]));
@@ -25322,6 +25440,34 @@
25322
25440
  }
25323
25441
  };
25324
25442
 
25443
+ // Matches zustand create usages that doesn't contain withImmutableActions wrapper
25444
+ // const useCurrentUserStore = create(....);
25445
+ var ZUSTAND_CREATE_WITHOUT_WRAPPER_PATTERN = {
25446
+ type: TYPES.VARIABLE_DECLARATOR,
25447
+ init: {
25448
+ type: TYPES.CALL_EXPRESSION,
25449
+ callee: {
25450
+ type: TYPES.IDENTIFIER,
25451
+ name: function name(_name2, root) {
25452
+ return _name2 === "create" && pure.existsBy(storeImportPattern(), getAllImports(root));
25453
+ }
25454
+ },
25455
+ arguments: ramda.none(pure.matches(WITH_IMMUTABLE_ACTIONS_WRAPPER_PATTERN))
25456
+ }
25457
+ };
25458
+
25459
+ //import { } from "neetocommons/react-utils";
25460
+ var REACT_UTILS_IMPORT_PATTERN = {
25461
+ type: TYPES.IMPORT_DECLARATION,
25462
+ source: {
25463
+ type: TYPES.LITERAL,
25464
+ value: "neetocommons/react-utils"
25465
+ },
25466
+ specifiers: [{
25467
+ type: TYPES.IMPORT_SPECIFIER
25468
+ }]
25469
+ };
25470
+
25325
25471
  var ensureZustandStoresAreTypeAnnotated = {
25326
25472
  meta: {
25327
25473
  type: "problem",
@@ -25513,6 +25659,48 @@
25513
25659
  }
25514
25660
  };
25515
25661
 
25662
+ var useWithImmutableActionsWrapperInsideCreate = {
25663
+ meta: {
25664
+ type: "problem",
25665
+ docs: {
25666
+ url: "https://github.com/bigbinary/eslint-plugin-neeto/blob/main/docs/src/rules/zustand-use-with-immutable-actions-wrapper-inside-create.md",
25667
+ description: "Enforce the use of withImmutableActions wrapper inside zustand create usages."
25668
+ },
25669
+ fixable: "code"
25670
+ },
25671
+ create: function create(context) {
25672
+ return {
25673
+ VariableDeclaration: function VariableDeclaration(node) {
25674
+ var createUsageWithoutWrapper = node.declarations.find(pure.matches(ZUSTAND_CREATE_WITHOUT_WRAPPER_PATTERN));
25675
+ var argumentsInsideCreate = createUsageWithoutWrapper === null || createUsageWithoutWrapper === void 0 ? void 0 : createUsageWithoutWrapper.init.arguments;
25676
+ if (pure.isNotPresent(argumentsInsideCreate) || argumentsInsideCreate[0].type !== TYPES.ARROW_FUNCTION_EXPRESSION) {
25677
+ return;
25678
+ }
25679
+ context.report({
25680
+ node: node,
25681
+ message: USE_WITH_IMMUTABLE_ACTIONS_WRAPPER,
25682
+ fix: function fix(fixer) {
25683
+ var argumentsString = argumentsInsideCreate.map(function (argument) {
25684
+ return context.getSourceCode().getText(argument);
25685
+ }).join(", ");
25686
+ var fixerArray = [];
25687
+ var addWrapperFixer = "create(withImmutableActions(".concat(argumentsString, "))");
25688
+ fixerArray.push(fixer.replaceText(createUsageWithoutWrapper.init, addWrapperFixer));
25689
+ var addImportFixer = addFunctionImport({
25690
+ context: context,
25691
+ fixer: fixer,
25692
+ functions: ["withImmutableActions"],
25693
+ importPattern: REACT_UTILS_IMPORT_PATTERN
25694
+ });
25695
+ if (addImportFixer) fixerArray.push(addImportFixer);
25696
+ return fixerArray;
25697
+ }
25698
+ });
25699
+ }
25700
+ };
25701
+ }
25702
+ };
25703
+
25516
25704
  var index = {
25517
25705
  rules: {
25518
25706
  "api-connector-name-should-match-filename": apiConnectorNameShouldMatchFilename,
@@ -25572,7 +25760,8 @@
25572
25760
  "props-with-different-key-value-should-be-outside-spread-attribute": propsWithDifferentKeyValueShouldBeOutsideSpreadAttribute,
25573
25761
  "no-repetition-in-prop-key-value-names": noRepetitionInPropKeyAndValueName,
25574
25762
  "use-functions-and-hooks-from-neeto-commons-frontend": useFunctionsAndHooksFromNeetoCommonsFrontend,
25575
- "no-unnecessary-if-condition": noUnnecessaryIfCondition
25763
+ "no-unnecessary-if-condition": noUnnecessaryIfCondition,
25764
+ "zustand-use-with-immutable-actions-wrapper-inside-create": useWithImmutableActionsWrapperInsideCreate
25576
25765
  },
25577
25766
  configs: {
25578
25767
  recommended: recommended
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bigbinary/eslint-plugin-neeto",
3
- "version": "1.0.61",
3
+ "version": "1.0.63",
4
4
  "main": "dist/index.js",
5
5
  "license": "UNLICENSED",
6
6
  "scripts": {