@bigbinary/eslint-plugin-neeto 1.0.15 → 1.0.17

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 +23 -3
  2. package/dist/index.js +414 -42
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -8,11 +8,27 @@ neeto.
8
8
  Install the plugin from npm:
9
9
 
10
10
  ```bash
11
- yarn add -D "@bigbinary/eslint-plugin-neeto@1.0.15"
11
+ yarn add -D "@bigbinary/eslint-plugin-neeto@1.0.17"
12
12
  ```
13
13
 
14
- In your eslintrc.js file, add `neeto` in the `plugins` list and add the required
15
- rules.
14
+ In your eslintrc.js file, add `neeto` in the `plugins` list and add the
15
+ configuration 'recommended' in extends with prefix as 'plugin:'.
16
+
17
+ ```js
18
+ module.exports = {
19
+ // ...other configurations
20
+ plugins: [
21
+ // ...other plugins
22
+ "@bigbinary/neeto",
23
+ ],
24
+ extends: [
25
+ "plugin:@bigbinary/neeto/recommended",
26
+ // ...other plugins/extensions
27
+ ],
28
+ };
29
+ ```
30
+
31
+ Optionally, if you don't need all the recommended rules, follow this approach:
16
32
 
17
33
  ```js
18
34
  module.exports = {
@@ -34,6 +50,8 @@ module.exports = {
34
50
  - [api-connector-should-use-default-export](./docs/src/rules/api-connector-should-use-default-export.md)
35
51
  - [combine-multiple-independent-awaits](./docs/src/rules/combine-multiple-independent-awaits.md)
36
52
  - [consistent-button-link-anchor](./docs/src/rules/consistent-button-link-anchor.md)
53
+ - [ensure-zustand-stores-are-type-annotated](./docs/src/rules/ensure-zustand-stores-are-type-annotated.md)
54
+ - [ensure-zustand-import-names-are-consistent](./docs/src/rules/ensure-zustand-import-names-are-consistent.md)
37
55
  - [file-name-and-export-name-standards](./docs/src/rules/file-name-and-export-name-standards.md)
38
56
  - [minimize-zustand-store-access-scope](./docs/src/rules/minimize-zustand-store-access-scope.md)
39
57
  - [neetocommons-tips](./docs/src/rules/neetocommons-tips.md)
@@ -60,6 +78,8 @@ module.exports = {
60
78
  - [use-snake-case-for-api-connector-filename](./docs/src/rules/use-snake-case-for-api-connector-filename.md)
61
79
  - [use-webpack-alias](./docs/src/rules/use-webpack-alias.md)
62
80
  - [webpack-aliases-and-jsconfig-paths-should-be-in-sync](./docs/src/rules/webpack-aliases-and-jsconfig-paths-should-be-in-sync.md)
81
+ - [zustand-recommend-comparator-function-usage](./docs/src/rules/zustand-recommend-comparator-function-usage.md)
82
+ - [zustand-use-prop-to-pick-single-item](./docs/src/rules/zustand-use-prop-to-pick-single-item.md)
63
83
 
64
84
  ## Development setup
65
85
 
package/dist/index.js CHANGED
@@ -1,14 +1,107 @@
1
1
  (function (global, factory) {
2
2
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('path'), require('ramda'), require('fs')) :
3
3
  typeof define === 'function' && define.amd ? define(['path', 'ramda', 'fs'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global["@bigbinary/eslint-plugin-neeto"] = factory(global.path$2, global.ramda, global.fs));
5
- })(this, (function (path$2, ramda, fs) { 'use strict';
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global["@bigbinary/eslint-plugin-neeto"] = factory(global.path$3, global.ramda, global.fs));
5
+ })(this, (function (path$3, ramda, fs) { 'use strict';
6
6
 
7
7
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
8
 
9
- var path__default = /*#__PURE__*/_interopDefaultLegacy(path$2);
9
+ var path__default = /*#__PURE__*/_interopDefaultLegacy(path$3);
10
10
  var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
11
11
 
12
+ const path$2 = require("path");
13
+
14
+ const webpackAliasConfig = require(path$2.resolve(__dirname, "../../../..", "config/webpack/resolve.js"));
15
+
16
+ var rules = {
17
+ "@bigbinary/neeto/api-connector-name-should-match-filename": "error",
18
+ "@bigbinary/neeto/api-connector-should-use-default-export": "error",
19
+ "@bigbinary/neeto/combine-multiple-independent-awaits": "warn",
20
+ "@bigbinary/neeto/consistent-button-link-anchor": "warn",
21
+ "@bigbinary/neeto/ensure-zustand-import-names-are-consistent": "error",
22
+ "@bigbinary/neeto/ensure-zustand-stores-are-type-annotated": "error",
23
+ "@bigbinary/neeto/file-name-and-export-name-standards": ["warn", {
24
+ basePath: path$2.resolve(__dirname, "../../../..", "app/javascript/src"),
25
+ default: {
26
+ filenameCase: "camelCase",
27
+ exportnameCase: "camelCase"
28
+ },
29
+ configs: [{
30
+ filenameCase: "camelCase",
31
+ exportnameCase: "CONSTANT_CASE",
32
+ includes: ["/**/constants.{js,jsx}", "/**/constants/**"]
33
+ }, {
34
+ filenameCase: "camelCase",
35
+ includes: ["/**/utils.{js,jsx}", "/**/utils/**"]
36
+ }, {
37
+ filenameCase: "snake_case",
38
+ exportnameCase: "camelCase",
39
+ includes: "/apis/**"
40
+ }, {
41
+ filenameCase: "camelCase",
42
+ exportnameCase: "camelCase",
43
+ includes: "/channels/**"
44
+ }, {
45
+ filenameCase: "camelCase",
46
+ exportnameCase: "camelCase",
47
+ includes: "/**/hooks/**"
48
+ }, {
49
+ filenameCase: "camelCase",
50
+ includes: "/lib/**"
51
+ }, {
52
+ filenameCase: "camelCase",
53
+ exportnameCase: "camelCase",
54
+ includes: "/**/stores/**"
55
+ }, {
56
+ filenameCase: "PascalCase",
57
+ exportnameCase: "PascalCase",
58
+ includes: ["/components/**", "/*.jsx"]
59
+ }]
60
+ }],
61
+ "@bigbinary/neeto/neetocommons-tips": "warn",
62
+ "@bigbinary/neeto/no-axios-import-outside-apis": ["error", webpackAliasConfig.alias],
63
+ "@bigbinary/neeto/no-blacklisted-imports": "warn",
64
+ "@bigbinary/neeto/no-dangling-constants": "error",
65
+ "@bigbinary/neeto/no-excess-function-arguments": "warn",
66
+ "@bigbinary/neeto/no-globalProps-destructure": "error",
67
+ "@bigbinary/neeto/no-globalProps-reassignment": "error",
68
+ "@bigbinary/neeto/no-missing-localization": ["error", {
69
+ name: "en.json",
70
+ lng: "en",
71
+ content: require(path$2.resolve(__dirname, "../../../..", "app/javascript/src/translations/en.json"))
72
+ }],
73
+ "@bigbinary/neeto/no-namespace-specifier-import-for-ramda": "error",
74
+ "@bigbinary/neeto/no-redundant-jsx-expression": "error",
75
+ "@bigbinary/neeto/no-redundant-string-templates": "error",
76
+ "@bigbinary/neeto/no-routes-destructure-and-reassignment": "error",
77
+ "@bigbinary/neeto/prefix-neeto-ui-import-alias": ["error", webpackAliasConfig.alias],
78
+ "@bigbinary/neeto/ramda-tips": "warn",
79
+ "@bigbinary/neeto/routes-should-be-declared-and-default-exported": "error",
80
+ "@bigbinary/neeto/routes-should-match-object-path": "error",
81
+ "@bigbinary/neeto/use-axios-config-for-passing-query-parameters": "warn",
82
+ "@bigbinary/neeto/use-common-routes": "error",
83
+ "@bigbinary/neeto/use-snake-case-for-api-connector-filename": "error",
84
+ "@bigbinary/neeto/use-webpack-alias": ["error", webpackAliasConfig.alias],
85
+ "@bigbinary/neeto/webpack-aliases-and-jsconfig-paths-should-be-in-sync": ["error", {
86
+ jsconfigFilePath: path$2.resolve(__dirname, "../../../..", "app/javascript/jsconfig.json"),
87
+ aliasFilePath: path$2.resolve(__dirname, "../../../..", "config/webpack/resolve.js")
88
+ }],
89
+ "@bigbinary/neeto/zustand-recommend-comparator-function-usage": "warn",
90
+ "@bigbinary/neeto/zustand-use-prop-to-pick-single-item": "error"
91
+ };
92
+
93
+ var recommended = {
94
+ plugins: ["@bigbinary/neeto"],
95
+ env: {
96
+ browser: true,
97
+ es2020: true,
98
+ es6: true,
99
+ commonjs: true,
100
+ node: true
101
+ },
102
+ rules
103
+ };
104
+
12
105
  function _arrayWithHoles(arr) {
13
106
  if (Array.isArray(arr)) return arr;
14
107
  }
@@ -175,7 +268,7 @@
175
268
  });
176
269
 
177
270
  const APIS_DIRECTORY = "app/javascript/src/apis";
178
- const ROUTES_FILE_PATH = "app/javascript/src/common/routes.js";
271
+ const ROUTES_FILE_PATH = "app/javascript/src/routes.js";
179
272
  const BLACKLISTED_PACKAGES = ["moment", "bootstrap", "blueprint", "react-spinners", "react-table", "remixicon", "mjml", "react-select", "antd", "@bigbinary/neeto-utils"];
180
273
  const TYPES = {
181
274
  ARRAY_EXPRESSION: "ArrayExpression",
@@ -426,6 +519,15 @@
426
519
  const USE_NEETOUI_BOX_SHADOW = "Use neetoUI border shadow. Refer https://bit.ly/box-shadows-page for more details.";
427
520
  const USE_NEETOUI_TEXT_COLOR = "Use neetoUI text color. Refer https://bit.ly/text-color-page for more details.";
428
521
  const DONT_USE_ZUSTAND_STORE_WITHOUT_SELECTOR = "Don't use zustand store hook without a selector function. It will impact the performance";
522
+ const DONT_USE_ZUSTAND_STORE_WITHOUT_COMPARATOR_FUNCTION = "Don't pick multiple zustand state items without comparator function";
523
+ const USE_PROP_FOR_PICKING_SINGLE_ITEM = "Use prop instead of pick when picking single item";
524
+ const ENSURE_ZUSTAND_STORES_ARE_TYPE_ANNOTATED = `
525
+ Zustand stores should be type annotated like this:
526
+ /** @type {import(”neetocommons/react-utils”).ZustandStoreHook} */
527
+ const useUserStore = create(...)
528
+ `;
529
+ const ENSURE_ZUSTAND_CREATE_IMPORT_ARE_CONSISTENT = "default import from zustand should always be named create";
530
+ const ENSURE_ZUSTAND_SHALLOW_IMPORT_ARE_CONSISTENT = "default import from zustand/shallow should always be named shallow";
429
531
 
430
532
  const toCamelCase = text => {
431
533
  const capitalized = text.split(/[^0-9a-z]/i).map(part => `${part[0].toUpperCase()}${part.slice(1)}`).join("");
@@ -606,13 +708,13 @@
606
708
  })
607
709
  };
608
710
 
609
- const SPECIAL_KEYS = ["home", "index", "show"]; // import routes from "common/routes", import routes from "routes"
711
+ const SPECIAL_KEYS = ["home", "index", "show"]; // import routes from "routes", import routes from "src/routes"
610
712
 
611
713
  const ROUTES_IMPORT_PATTERN = {
612
714
  type: TYPES.IMPORT_DECLARATION,
613
715
  source: {
614
716
  type: TYPES.LITERAL,
615
- value: matchesAny(["routes", "common/routes"])
717
+ value: matchesAny(["routes", "src/routes"])
616
718
  },
617
719
  specifiers: [{
618
720
  type: TYPES.IMPORT_DEFAULT_SPECIFIER,
@@ -623,7 +725,7 @@
623
725
  }]
624
726
  }; // const routes = { home: "/" }, const routes = "/", const routes = ["/"] etc
625
727
 
626
- const DECALARATION_PATTERN = {
728
+ const DECALARATION_PATTERN$1 = {
627
729
  type: TYPES.VARIABLE_DECLARATION,
628
730
  declarations: [{
629
731
  type: TYPES.VARIABLE_DECLARATOR,
@@ -636,7 +738,7 @@
636
738
 
637
739
  const OBJECT_DECALARATION_PATTERN = ramda.assocPath(["declarations", 0, "init"], {
638
740
  type: TYPES.OBJECT_EXPRESSION
639
- }, DECALARATION_PATTERN); // export const routes = { home: "/" }
741
+ }, DECALARATION_PATTERN$1); // export const routes = { home: "/" }
640
742
 
641
743
  const EXPORT_NAMED_DECLARATION_PATTERN = {
642
744
  type: TYPES.EXPORT_NAMED_DECLARATION,
@@ -755,7 +857,7 @@
755
857
  },
756
858
  create: context => ({
757
859
  VariableDeclaration(node) {
758
- if (matches(DECALARATION_PATTERN, node) && isRoutesImportPresent(context)) {
860
+ if (matches(DECALARATION_PATTERN$1, node) && isRoutesImportPresent(context)) {
759
861
  context.report(node, ROUTES_HAS_ALREADY_BEEN_DECLARED);
760
862
  }
761
863
  },
@@ -784,7 +886,7 @@
784
886
  meta: {
785
887
  type: "problem",
786
888
  docs: {
787
- description: "common/routes.js file should declare a constant named routes and should be exported using default export"
889
+ description: "routes.js file should declare a constant named routes and should be exported using default export"
788
890
  }
789
891
  },
790
892
  create: context => ({
@@ -865,7 +967,7 @@
865
967
  },
866
968
  create: context => ({
867
969
  VariableDeclaration(node) {
868
- if (isInsideRoutesFile(context) && matches(DECALARATION_PATTERN, node)) {
970
+ if (isInsideRoutesFile(context) && matches(DECALARATION_PATTERN$1, node)) {
869
971
  const object = node.declarations[0].init;
870
972
  checkObjectPath(context, object);
871
973
  }
@@ -1121,7 +1223,7 @@
1121
1223
  };
1122
1224
 
1123
1225
  const CONSTANT_CASE_PATTERN = /^[A-Z0-9_]*$/;
1124
- const CONSTANTS_FILES = [/.*constants.(js|jsx)$/i, /app\/javascript\/src\/common\/urls.js$/];
1226
+ const CONSTANTS_FILES = [/constants\//, /.*constants.(js|jsx)$/i];
1125
1227
  const CONSTANT_DECALARATION_PATTERN = {
1126
1228
  kind: "const",
1127
1229
  declarations: [{
@@ -1193,6 +1295,117 @@
1193
1295
  })
1194
1296
  };
1195
1297
 
1298
+ const ZUSTAND_CREATE_IMPORT_PATTERN = {
1299
+ specifiers: [{
1300
+ type: TYPES.IMPORT_DEFAULT_SPECIFIER,
1301
+ local: {
1302
+ type: TYPES.IDENTIFIER,
1303
+ name: name => name !== "create"
1304
+ }
1305
+ }],
1306
+ source: {
1307
+ type: TYPES.LITERAL,
1308
+ value: "zustand"
1309
+ }
1310
+ }; // import somethingOtherThan_shallow from "zustand/shallow";
1311
+
1312
+ const ZUSTAND_SHALLOW_IMPORT_PATTERN = {
1313
+ specifiers: [{
1314
+ type: TYPES.IMPORT_DEFAULT_SPECIFIER,
1315
+ local: {
1316
+ type: TYPES.IDENTIFIER,
1317
+ name: name => name !== "shallow"
1318
+ }
1319
+ }],
1320
+ source: {
1321
+ type: TYPES.LITERAL,
1322
+ value: "zustand/shallow"
1323
+ }
1324
+ };
1325
+
1326
+ var ensureZustandImportNamesAreConsistent = {
1327
+ meta: {
1328
+ type: "error",
1329
+ docs: {
1330
+ description: "Throw error if zustand import names are not consistent"
1331
+ }
1332
+ },
1333
+ create: context => ({
1334
+ ImportDeclaration(node) {
1335
+ if (matches(ZUSTAND_CREATE_IMPORT_PATTERN, node)) {
1336
+ context.report({
1337
+ node,
1338
+ message: ENSURE_ZUSTAND_CREATE_IMPORT_ARE_CONSISTENT
1339
+ });
1340
+ } else if (matches(ZUSTAND_SHALLOW_IMPORT_PATTERN, node)) {
1341
+ context.report({
1342
+ node,
1343
+ message: ENSURE_ZUSTAND_SHALLOW_IMPORT_ARE_CONSISTENT
1344
+ });
1345
+ }
1346
+ }
1347
+
1348
+ })
1349
+ };
1350
+
1351
+ const storeImportPattern = () => ({
1352
+ specifiers: [{
1353
+ type: TYPES.IMPORT_DEFAULT_SPECIFIER,
1354
+ local: {
1355
+ type: TYPES.IDENTIFIER
1356
+ }
1357
+ }],
1358
+ source: {
1359
+ type: TYPES.LITERAL,
1360
+ value: "zustand"
1361
+ }
1362
+ });
1363
+
1364
+ const DECALARATION_PATTERN = [{
1365
+ type: TYPES.VARIABLE_DECLARATOR,
1366
+ init: {
1367
+ type: TYPES.CALL_EXPRESSION,
1368
+ callee: {
1369
+ type: TYPES.IDENTIFIER,
1370
+ name: (name, root) => name === "create" && existsBy$1(storeImportPattern(), getAllImports(root))
1371
+ },
1372
+ optional: false
1373
+ }
1374
+ }];
1375
+ const TYPE_ANNOTATION_COMMENT_PATTERN = [{
1376
+ type: "Block",
1377
+ value: "* @type {import(”neetocommons/react-utils”).ZustandStoreHook} "
1378
+ }]; // .....
1379
+ // const useCurrentUserStore = create(....);
1380
+
1381
+ const ZUSTAND_STORE_CREATE_PATTERN = {
1382
+ kind: "const",
1383
+ declarations: DECALARATION_PATTERN,
1384
+ parent: {
1385
+ type: TYPES.PROGRAM,
1386
+ comments: comments => !matches(TYPE_ANNOTATION_COMMENT_PATTERN, comments)
1387
+ }
1388
+ };
1389
+
1390
+ var ensureZustandStoresAreTypeAnnotated = {
1391
+ meta: {
1392
+ type: "error",
1393
+ docs: {
1394
+ description: "Throw error if zustand stores are not type annotated via JSDoc comments"
1395
+ }
1396
+ },
1397
+ create: context => ({
1398
+ VariableDeclaration(node) {
1399
+ if (!matches(ZUSTAND_STORE_CREATE_PATTERN, node)) return;
1400
+ context.report({
1401
+ node,
1402
+ message: ENSURE_ZUSTAND_STORES_ARE_TYPE_ANNOTATED
1403
+ });
1404
+ }
1405
+
1406
+ })
1407
+ };
1408
+
1196
1409
  const isWindows = typeof process === 'object' &&
1197
1410
  process &&
1198
1411
  process.platform === 'win32';
@@ -2373,7 +2586,7 @@
2373
2586
  var minimatch$1 = minimatch_1;
2374
2587
 
2375
2588
  const CAMEL_CASE_REGEX = /^[a-z][A-Za-z0-9]*$/;
2376
- const CONSTANT_CASE_REGEX = /^[A-Z_]+$/;
2589
+ const CONSTANT_CASE_REGEX = /^[A-Z0-9_]+$/;
2377
2590
  const PASCAL_CASE_REGEX = /^[A-Z][A-Za-z0-9]*$/;
2378
2591
  const SNAKE_CASE_REGEX$1 = /^[a-z0-9_]+$/;
2379
2592
  const EXPORT_PATTERN = {
@@ -2402,10 +2615,11 @@
2402
2615
 
2403
2616
  const findMatchedConfig = (context, configs) => {
2404
2617
  const fullFilePath = context.getFilename();
2618
+ const basePath = context.options[0].basePath;
2405
2619
  const matchedConfig = configs.find(config => {
2406
- const isIncluded = asArray(config.includes).some(pathPattern => minimatch$1(fullFilePath, pathPattern));
2620
+ const isIncluded = asArray(config.includes).some(pathPattern => minimatch$1(fullFilePath, basePath + pathPattern));
2407
2621
  if (config.includes && !isIncluded) return false;
2408
- const isExcluded = asArray(config.excludes).some(pathPattern => minimatch$1(fullFilePath, pathPattern));
2622
+ const isExcluded = asArray(config.excludes).some(pathPattern => minimatch$1(fullFilePath, basePath + pathPattern));
2409
2623
  if (config.excludes && isExcluded) return false;
2410
2624
  return true;
2411
2625
  });
@@ -2647,24 +2861,7 @@
2647
2861
  })
2648
2862
  };
2649
2863
 
2650
- const storeImportPattern = name => ({
2651
- specifiers: [{
2652
- type: "ImportDefaultSpecifier",
2653
- local: {
2654
- type: TYPES.IDENTIFIER,
2655
- name
2656
- }
2657
- }],
2658
- source: {
2659
- type: TYPES.LITERAL,
2660
- value: ramda.startsWith("stores/")
2661
- }
2662
- }); // import useSampleStore from "stores/sampleStore";
2663
- // .....
2664
- // const { order } = useSampleStore();
2665
-
2666
-
2667
- const ZUSTAND_HOOK_USAGE_PATTERN = {
2864
+ const ZUSTAND_HOOK_USAGE_PATTERN$1 = {
2668
2865
  kind: "const",
2669
2866
  declarations: [{
2670
2867
  type: TYPES.VARIABLE_DECLARATOR,
@@ -2672,7 +2869,7 @@
2672
2869
  type: TYPES.CALL_EXPRESSION,
2673
2870
  callee: {
2674
2871
  type: TYPES.IDENTIFIER,
2675
- name: (name, root) => name.match(/use[a-zA-z0-9].+Store/) && existsBy$1(storeImportPattern(name), getAllImports(root))
2872
+ name: name => name.match(/use[a-zA-z0-9].+Store/)
2676
2873
  },
2677
2874
  arguments: ramda.isEmpty,
2678
2875
  optional: false
@@ -2689,7 +2886,7 @@
2689
2886
  },
2690
2887
  create: context => ({
2691
2888
  VariableDeclaration(node) {
2692
- if (!matches(ZUSTAND_HOOK_USAGE_PATTERN, node)) return;
2889
+ if (!matches(ZUSTAND_HOOK_USAGE_PATTERN$1, node)) return;
2693
2890
  context.report({
2694
2891
  node,
2695
2892
  message: DONT_USE_ZUSTAND_STORE_WITHOUT_SELECTOR
@@ -4458,9 +4655,9 @@
4458
4655
  return name === ((_root$params$9 = root.params[0]) === null || _root$params$9 === void 0 ? void 0 : _root$params$9.name);
4459
4656
  }
4460
4657
  },
4461
- property: {
4462
- type: TYPES.IDENTIFIER
4463
- }
4658
+ property: property => matches({
4659
+ type: ramda.includes(ramda.__, [TYPES.IDENTIFIER, TYPES.LITERAL])
4660
+ }, property)
4464
4661
  }
4465
4662
  },
4466
4663
  DESTRUCTURE: {
@@ -4557,7 +4754,7 @@
4557
4754
  value: 0
4558
4755
  }]
4559
4756
  };
4560
- const RAMDA_IMPORT_PATTERN = {
4757
+ const RAMDA_IMPORT_PATTERN$1 = {
4561
4758
  type: TYPES.IMPORT_DECLARATION,
4562
4759
  source: {
4563
4760
  type: TYPES.LITERAL,
@@ -4610,7 +4807,7 @@
4610
4807
  }
4611
4808
  }];
4612
4809
 
4613
- const replacer = nodeReplacerWithImports(RAMDA_IMPORT_PATTERN);
4810
+ const replacer = nodeReplacerWithImports(RAMDA_IMPORT_PATTERN$1);
4614
4811
  const reportSuggestion = ({
4615
4812
  context,
4616
4813
  node,
@@ -4799,7 +4996,9 @@
4799
4996
  let fix = null;
4800
4997
 
4801
4998
  if (matches(PLUCK_PATTERNS.IDENTIFIER, func)) {
4802
- fix = `${callee}("${func.body.property.name}", ${code})`;
4999
+ const property = func.body.property;
5000
+ const subtitute = property.type === TYPES.IDENTIFIER && func.body.computed ? property.name : `"${func.body.computed ? property.value : property.name}"`;
5001
+ fix = `${callee}(${subtitute}, ${code})`;
4803
5002
  } else if (matches(PLUCK_PATTERNS.DESTRUCTURE, func)) {
4804
5003
  const propertyName = func.params[0].properties[0].value.name;
4805
5004
  fix = `${callee}("${propertyName}", ${code})`;
@@ -5274,6 +5473,172 @@
5274
5473
  })
5275
5474
  };
5276
5475
 
5476
+ const PICK_PATTERN$1 = [{
5477
+ type: TYPES.CALL_EXPRESSION,
5478
+ callee: {
5479
+ type: TYPES.IDENTIFIER,
5480
+ name: "pick"
5481
+ },
5482
+ optional: false
5483
+ }]; // const { order, user } = useSampleStore((state) => ({ order: state.order, user: state.user }));
5484
+ // const [ order, user ] = useSampleStore((state) => [state.order, state.user]);
5485
+
5486
+ const ARROW_FUNCTION_PATTERN = [{
5487
+ type: TYPES.ARROW_FUNCTION_EXPRESSION,
5488
+ params: [{
5489
+ type: TYPES.IDENTIFIER
5490
+ }]
5491
+ }];
5492
+ const ZUSTAND_HOOK_USAGE_PATTERN = {
5493
+ kind: "const",
5494
+ declarations: [{
5495
+ type: TYPES.VARIABLE_DECLARATOR,
5496
+ init: {
5497
+ type: TYPES.CALL_EXPRESSION,
5498
+ callee: {
5499
+ type: TYPES.IDENTIFIER,
5500
+ name: name => name.match(/use[a-zA-z0-9].+Store/)
5501
+ },
5502
+ arguments: args => args.length === 1 && (matches(PICK_PATTERN$1, args) || matches(ARROW_FUNCTION_PATTERN, args))
5503
+ }
5504
+ }]
5505
+ };
5506
+
5507
+ var zustandRecommendComparatorFunctionUsage = {
5508
+ meta: {
5509
+ type: "warn",
5510
+ docs: {
5511
+ description: "Warn about the usage of picking multiple state items without comparator function from inside zustand store hooks"
5512
+ }
5513
+ },
5514
+ create: context => ({
5515
+ VariableDeclaration(node) {
5516
+ if (!matches(ZUSTAND_HOOK_USAGE_PATTERN, node)) return;
5517
+ context.report({
5518
+ node,
5519
+ message: DONT_USE_ZUSTAND_STORE_WITHOUT_COMPARATOR_FUNCTION
5520
+ });
5521
+ }
5522
+
5523
+ })
5524
+ };
5525
+
5526
+ // const order = useSampleStore(pick(["order"]), shallow);
5527
+ // const order = useSampleStore(pick([order]));
5528
+ // const order = useSampleStore(pick([order]), shallow);
5529
+
5530
+ const PICK_PATTERN = [{
5531
+ type: TYPES.CALL_EXPRESSION,
5532
+ callee: {
5533
+ type: TYPES.IDENTIFIER,
5534
+ name: "pick"
5535
+ },
5536
+ arguments: [{
5537
+ type: TYPES.ARRAY_EXPRESSION,
5538
+ elements: elements => elements.length === 1 && (matches([{
5539
+ type: TYPES.LITERAL
5540
+ }], elements) || matches([{
5541
+ type: TYPES.IDENTIFIER
5542
+ }], elements))
5543
+ }]
5544
+ }];
5545
+ const ZUSTAND_PICK_USAGE_PATTERN = {
5546
+ init: {
5547
+ type: TYPES.CALL_EXPRESSION,
5548
+ callee: {
5549
+ type: TYPES.IDENTIFIER,
5550
+ name: name => name.match(/use[a-zA-z0-9].+Store/)
5551
+ },
5552
+ arguments: PICK_PATTERN,
5553
+ optional: false
5554
+ }
5555
+ }; // import { ... } from 'ramda';
5556
+
5557
+ const RAMDA_IMPORT_PATTERN = {
5558
+ type: TYPES.IMPORT_DECLARATION,
5559
+ specifiers: [{
5560
+ type: TYPES.IMPORT_SPECIFIER,
5561
+ local: {
5562
+ type: TYPES.IDENTIFIER
5563
+ }
5564
+ }],
5565
+ source: {
5566
+ type: TYPES.LITERAL,
5567
+ value: "ramda"
5568
+ }
5569
+ }; // pick()
5570
+
5571
+ const PICK_PATTERN_SHORT = {
5572
+ type: TYPES.CALL_EXPRESSION,
5573
+ callee: {
5574
+ type: TYPES.IDENTIFIER,
5575
+ name: "pick"
5576
+ }
5577
+ }; // import { prop }
5578
+
5579
+ const PROP_IMPORT_PATTERN = {
5580
+ type: TYPES.IMPORT_SPECIFIER,
5581
+ imported: {
5582
+ type: TYPES.IDENTIFIER,
5583
+ name: "prop"
5584
+ }
5585
+ }; // useCurrentUserStore()
5586
+
5587
+ const findProgramNode = node => {
5588
+ const parentNode = node === null || node === void 0 ? void 0 : node.parent;
5589
+ if ((parentNode === null || parentNode === void 0 ? void 0 : parentNode.type) === TYPES.PROGRAM) return parentNode;
5590
+ return findProgramNode(parentNode);
5591
+ };
5592
+ const findRamdaSpecifiers = programNode => {
5593
+ const ramdaImportDeclaration = programNode.body.find(matches(RAMDA_IMPORT_PATTERN));
5594
+ const {
5595
+ specifiers: specifiersNode
5596
+ } = ramdaImportDeclaration;
5597
+ return specifiersNode[0];
5598
+ };
5599
+
5600
+ var zustandUsePropToPickSingleItem = {
5601
+ meta: {
5602
+ type: "error",
5603
+ docs: {
5604
+ description: "Throw error if pick function is used to pick a single item"
5605
+ },
5606
+ fixable: "code"
5607
+ },
5608
+ create: context => ({
5609
+ VariableDeclarator(node) {
5610
+ if (!matches(ZUSTAND_PICK_USAGE_PATTERN, node)) return;
5611
+ const pickNode = findMatchingNodesDeep(node, PICK_PATTERN_SHORT)[0];
5612
+ const item = pickNode.arguments[0].elements[0];
5613
+ const substitute = item.type === TYPES.IDENTIFIER ? item.name : `"${item.value}"`;
5614
+ const fix = `prop(${substitute})`;
5615
+ const programNode = findProgramNode(node);
5616
+ const importSpecifierNode = findRamdaSpecifiers(programNode);
5617
+ const comparatorFunctionNode = node.init.arguments[1];
5618
+ const propImportNode = findMatchingNodesDeep(context.getSourceCode().ast.body, PROP_IMPORT_PATTERN);
5619
+ context.report({
5620
+ node,
5621
+ message: USE_PROP_FOR_PICKING_SINGLE_ITEM,
5622
+ fix: fixer => {
5623
+ const fixerArray = [];
5624
+ fixerArray.push(fixer.replaceText(pickNode, fix));
5625
+
5626
+ if (propImportNode.length === 0) {
5627
+ fixerArray.push(fixer.insertTextAfter(importSpecifierNode, ", prop"));
5628
+ }
5629
+
5630
+ if (comparatorFunctionNode) {
5631
+ fixerArray.push(fixer.remove(comparatorFunctionNode));
5632
+ }
5633
+
5634
+ return fixerArray;
5635
+ }
5636
+ });
5637
+ }
5638
+
5639
+ })
5640
+ };
5641
+
5277
5642
  var index = {
5278
5643
  rules: {
5279
5644
  "minimize-zustand-store-access-scope": minimizeZustandStoreAccessScope,
@@ -5281,6 +5646,8 @@
5281
5646
  "api-connector-should-use-default-export": apiConnectorShouldUseDefaultExport,
5282
5647
  "combine-multiple-independent-awaits": combineMultipleIndependentAwaits,
5283
5648
  "consistent-button-link-anchor": consistentButtonLinkAnchor,
5649
+ "ensure-zustand-stores-are-type-annotated": ensureZustandStoresAreTypeAnnotated,
5650
+ "ensure-zustand-import-names-are-consistent": ensureZustandImportNamesAreConsistent,
5284
5651
  "file-name-and-export-name-standards": fileNameAndExportNameStandards,
5285
5652
  "neetocommons-tips": neetocommonsTips,
5286
5653
  "no-blacklisted-imports": noBlacklistedImports,
@@ -5305,7 +5672,12 @@
5305
5672
  "use-neetoui-classes": useNeetouiClasses,
5306
5673
  "use-snake-case-for-api-connector-filename": useSnakeCaseForApiConnectorFilename,
5307
5674
  "use-webpack-alias": useWebpackAlias,
5308
- "webpack-aliases-and-jsconfig-paths-should-be-in-sync": webpackAliasesAndJsconfigPathsShouldBeInSync
5675
+ "webpack-aliases-and-jsconfig-paths-should-be-in-sync": webpackAliasesAndJsconfigPathsShouldBeInSync,
5676
+ "zustand-recommend-comparator-function-usage": zustandRecommendComparatorFunctionUsage,
5677
+ "zustand-use-prop-to-pick-single-item": zustandUsePropToPickSingleItem
5678
+ },
5679
+ configs: {
5680
+ recommended
5309
5681
  }
5310
5682
  };
5311
5683
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bigbinary/eslint-plugin-neeto",
3
- "version": "1.0.15",
3
+ "version": "1.0.17",
4
4
  "main": "dist/index.js",
5
5
  "license": "UNLICENSED",
6
6
  "scripts": {