@bigbinary/eslint-plugin-neeto 1.0.17 → 1.0.19

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 +3 -1
  2. package/dist/index.js +141 -15
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -8,7 +8,7 @@ neeto.
8
8
  Install the plugin from npm:
9
9
 
10
10
  ```bash
11
- yarn add -D "@bigbinary/eslint-plugin-neeto@1.0.17"
11
+ yarn add -D "@bigbinary/eslint-plugin-neeto@1.0.19"
12
12
  ```
13
13
 
14
14
  In your eslintrc.js file, add `neeto` in the `plugins` list and add the
@@ -80,6 +80,8 @@ module.exports = {
80
80
  - [webpack-aliases-and-jsconfig-paths-should-be-in-sync](./docs/src/rules/webpack-aliases-and-jsconfig-paths-should-be-in-sync.md)
81
81
  - [zustand-recommend-comparator-function-usage](./docs/src/rules/zustand-recommend-comparator-function-usage.md)
82
82
  - [zustand-use-prop-to-pick-single-item](./docs/src/rules/zustand-use-prop-to-pick-single-item.md)
83
+ - [files-inside-react-query-folder-should-end-with-api](./docs/src/rules/files-inside-react-query-folder-should-end-with-api.md)
84
+ - [use-react-query-for-managing-remote-data](./docs/src/rules/use-react-query-for-managing-remote-data.md)
83
85
 
84
86
  ## Development setup
85
87
 
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('path'), require('ramda'), require('fs')) :
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$3, global.ramda, global.fs));
5
- })(this, (function (path$3, ramda, fs) { 'use strict';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('ramda'), require('path'), require('fs')) :
3
+ typeof define === 'function' && define.amd ? define(['ramda', 'path', 'fs'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global["@bigbinary/eslint-plugin-neeto"] = factory(global.ramda, global.path$3, global.fs));
5
+ })(this, (function (ramda, path$3, fs) { 'use strict';
6
6
 
7
7
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
8
 
@@ -11,8 +11,17 @@
11
11
 
12
12
  const path$2 = require("path");
13
13
 
14
- const webpackAliasConfig = require(path$2.resolve(__dirname, "../../../..", "config/webpack/resolve.js"));
14
+ const loadJS = jsPath => {
15
+ try {
16
+ return require(path$2.resolve(__dirname, "../../../..", jsPath));
17
+ } catch (error) {
18
+ return {};
19
+ }
20
+ };
15
21
 
22
+ const projectResolve = loadJS("config/webpack/resolve.js");
23
+ const commonResolve = loadJS("node_modules/@bigbinary/neeto-commons-frontend/configs/webpack/resolve.js");
24
+ const resolve = ramda.mergeDeepLeft(projectResolve, commonResolve);
16
25
  var rules = {
17
26
  "@bigbinary/neeto/api-connector-name-should-match-filename": "error",
18
27
  "@bigbinary/neeto/api-connector-should-use-default-export": "error",
@@ -20,6 +29,7 @@
20
29
  "@bigbinary/neeto/consistent-button-link-anchor": "warn",
21
30
  "@bigbinary/neeto/ensure-zustand-import-names-are-consistent": "error",
22
31
  "@bigbinary/neeto/ensure-zustand-stores-are-type-annotated": "error",
32
+ "@bigbinary/neeto/files-inside-react-query-folder-should-end-with-api": "warn",
23
33
  "@bigbinary/neeto/file-name-and-export-name-standards": ["warn", {
24
34
  basePath: path$2.resolve(__dirname, "../../../..", "app/javascript/src"),
25
35
  default: {
@@ -59,7 +69,7 @@
59
69
  }]
60
70
  }],
61
71
  "@bigbinary/neeto/neetocommons-tips": "warn",
62
- "@bigbinary/neeto/no-axios-import-outside-apis": ["error", webpackAliasConfig.alias],
72
+ "@bigbinary/neeto/no-axios-import-outside-apis": ["error", resolve.alias],
63
73
  "@bigbinary/neeto/no-blacklisted-imports": "warn",
64
74
  "@bigbinary/neeto/no-dangling-constants": "error",
65
75
  "@bigbinary/neeto/no-excess-function-arguments": "warn",
@@ -74,14 +84,15 @@
74
84
  "@bigbinary/neeto/no-redundant-jsx-expression": "error",
75
85
  "@bigbinary/neeto/no-redundant-string-templates": "error",
76
86
  "@bigbinary/neeto/no-routes-destructure-and-reassignment": "error",
77
- "@bigbinary/neeto/prefix-neeto-ui-import-alias": ["error", webpackAliasConfig.alias],
87
+ "@bigbinary/neeto/prefix-neeto-ui-import-alias": ["error", resolve.alias],
78
88
  "@bigbinary/neeto/ramda-tips": "warn",
79
89
  "@bigbinary/neeto/routes-should-be-declared-and-default-exported": "error",
80
90
  "@bigbinary/neeto/routes-should-match-object-path": "error",
81
91
  "@bigbinary/neeto/use-axios-config-for-passing-query-parameters": "warn",
82
92
  "@bigbinary/neeto/use-common-routes": "error",
93
+ "@bigbinary/neeto/use-react-query-for-managing-remote-data": "warn",
83
94
  "@bigbinary/neeto/use-snake-case-for-api-connector-filename": "error",
84
- "@bigbinary/neeto/use-webpack-alias": ["error", webpackAliasConfig.alias],
95
+ "@bigbinary/neeto/use-webpack-alias": ["error", resolve.alias],
85
96
  "@bigbinary/neeto/webpack-aliases-and-jsconfig-paths-should-be-in-sync": ["error", {
86
97
  jsconfigFilePath: path$2.resolve(__dirname, "../../../..", "app/javascript/jsconfig.json"),
87
98
  aliasFilePath: path$2.resolve(__dirname, "../../../..", "config/webpack/resolve.js")
@@ -268,6 +279,7 @@
268
279
  });
269
280
 
270
281
  const APIS_DIRECTORY = "app/javascript/src/apis";
282
+ const REACT_QUERY_DIRECTORY = "app/javascript/src/hooks/reactQuery";
271
283
  const ROUTES_FILE_PATH = "app/javascript/src/routes.js";
272
284
  const BLACKLISTED_PACKAGES = ["moment", "bootstrap", "blueprint", "react-spinners", "react-table", "remixicon", "mjml", "react-select", "antd", "@bigbinary/neeto-utils"];
273
285
  const TYPES = {
@@ -508,7 +520,7 @@
508
520
  const ROUTES_HAS_ALREADY_BEEN_DECLARED = "Identifier `routes` has already been declared";
509
521
  const ROUTES_CONSTANT_SHOULD_BE_DECLARED = "A constant object named `routes` should be declared inside routes.js";
510
522
  const ROUTES_SHOULD_USE_DEFAULT_EXPORT = "`routes` should be exported using the default export method.";
511
- const USE_ARRAY_METHODS = "Use array methods (forEach, map, filter, reduce, etc) instead of 'for' loop. Refer 'Advanced Array Methods' section in https://bit.ly/learn-advanced-javascript";
523
+ const USE_ARRAY_METHODS = "Use array methods (forEach, map, filter, reduce, etc) instead of 'for' loop. Refer 'Advanced Array Methods' section in https://bit.ly/array-methods-page-1";
512
524
  const USE_AXIOS_CONFIG_FOR_PASSING_QUERY_PARAMETERS_MESSAGE = "Use 'params attribute in axios configuration' to pass query parameters.";
513
525
  const USE_LINK_FOR_NAVIGATION = "Use Link or NavLink component from 'react-router-dom' for click-navigation instead of onClick handler";
514
526
  const USE_ANCHOR_FOR_NAVIGATION = "Use Anchor element (<a>) for click-navigation instead of onClick handler";
@@ -523,11 +535,13 @@
523
535
  const USE_PROP_FOR_PICKING_SINGLE_ITEM = "Use prop instead of pick when picking single item";
524
536
  const ENSURE_ZUSTAND_STORES_ARE_TYPE_ANNOTATED = `
525
537
  Zustand stores should be type annotated like this:
526
- /** @type {import(”neetocommons/react-utils”).ZustandStoreHook} */
538
+ /** @type {import("neetocommons/react-utils").ZustandStoreHook} */
527
539
  const useUserStore = create(...)
528
540
  `;
529
541
  const ENSURE_ZUSTAND_CREATE_IMPORT_ARE_CONSISTENT = "default import from zustand should always be named create";
530
542
  const ENSURE_ZUSTAND_SHALLOW_IMPORT_ARE_CONSISTENT = "default import from zustand/shallow should always be named shallow";
543
+ const USE_REACT_QUERY_FOR_MANAGING_REMOTE_DATA = "API calls should be managed using React Query";
544
+ const FILES_INSIDE_REACT_QUERY_FOLDER_SHOULD_END_WITH_API = "Files inside reactQuery folder should end with Api";
531
545
 
532
546
  const toCamelCase = text => {
533
547
  const capitalized = text.split(/[^0-9a-z]/i).map(part => `${part[0].toUpperCase()}${part.slice(1)}`).join("");
@@ -1372,10 +1386,10 @@
1372
1386
  optional: false
1373
1387
  }
1374
1388
  }];
1375
- const TYPE_ANNOTATION_COMMENT_PATTERN = [{
1389
+ const TYPE_ANNOTATION_COMMENT_PATTERN = {
1376
1390
  type: "Block",
1377
- value: "* @type {import(”neetocommons/react-utils”).ZustandStoreHook} "
1378
- }]; // .....
1391
+ value: '* @type {import("neetocommons/react-utils").ZustandStoreHook} '
1392
+ }; // .....
1379
1393
  // const useCurrentUserStore = create(....);
1380
1394
 
1381
1395
  const ZUSTAND_STORE_CREATE_PATTERN = {
@@ -1383,7 +1397,7 @@
1383
1397
  declarations: DECALARATION_PATTERN,
1384
1398
  parent: {
1385
1399
  type: TYPES.PROGRAM,
1386
- comments: comments => !matches(TYPE_ANNOTATION_COMMENT_PATTERN, comments)
1400
+ comments: ramda.none(matches(TYPE_ANNOTATION_COMMENT_PATTERN))
1387
1401
  }
1388
1402
  };
1389
1403
 
@@ -5121,6 +5135,116 @@
5121
5135
  }
5122
5136
  };
5123
5137
 
5138
+ const USE_QUERY_PATTERN = {
5139
+ callee: {
5140
+ name: "useQuery"
5141
+ }
5142
+ };
5143
+ const USE_MUTATION_PATTERN = {
5144
+ callee: {
5145
+ name: "useMutation"
5146
+ }
5147
+ };
5148
+ const USE_INFINITE_QUERY_PATTERN = {
5149
+ callee: {
5150
+ name: "useInfiniteQuery"
5151
+ }
5152
+ };
5153
+ const IMPORT_DECLARATION_PATTERN = {
5154
+ type: TYPES.IMPORT_DECLARATION
5155
+ };
5156
+ const JSX_ATTRIBUTE_PATTERN = {
5157
+ type: TYPES.JSX_ATTRIBUTE
5158
+ };
5159
+ const QUERY_CLIENT_PATTERN = {
5160
+ callee: {
5161
+ object: {
5162
+ name: "queryClient"
5163
+ }
5164
+ }
5165
+ };
5166
+ const USE_PAGINATED_QUERY_PATTERN = {
5167
+ callee: {
5168
+ name: "usePaginatedQuery"
5169
+ }
5170
+ };
5171
+ const PROPERTY_PATTERN = {
5172
+ type: TYPES.MEMBER_EXPRESSION,
5173
+ property: {
5174
+ type: TYPES.IDENTIFIER
5175
+ }
5176
+ };
5177
+ const FILENAME_SUFFIX = "Api.js";
5178
+ const IGNORE_LIST = [USE_QUERY_PATTERN, USE_MUTATION_PATTERN, USE_INFINITE_QUERY_PATTERN, IMPORT_DECLARATION_PATTERN, JSX_ATTRIBUTE_PATTERN, QUERY_CLIENT_PATTERN, USE_PAGINATED_QUERY_PATTERN];
5179
+ const WHITELISTED_FOLDER_NAMES = ["constants", "utils"];
5180
+ const WHITELISTED_FILE_NAMES = ["constants", "utils"];
5181
+
5182
+ const getFolderName = fileName => path__default["default"].basename(path__default["default"].dirname(fileName));
5183
+
5184
+ var filesInsideReactQueryFolderShouldEndWithApi = {
5185
+ meta: {
5186
+ type: "problem",
5187
+ docs: {
5188
+ description: "Files inside reactQuery folder should end with Api"
5189
+ }
5190
+ },
5191
+ create: context => ({
5192
+ Program(node) {
5193
+ const filePath = context.getFilename();
5194
+ if (!filePath.includes(REACT_QUERY_DIRECTORY)) return;
5195
+ if (filePath.endsWith(FILENAME_SUFFIX)) return;
5196
+ if (WHITELISTED_FOLDER_NAMES.includes(getFolderName(filePath))) return;
5197
+ const [fileName] = getFilenameAndExtension(context);
5198
+ if (WHITELISTED_FILE_NAMES.includes(fileName)) return;
5199
+ context.report({
5200
+ node,
5201
+ message: FILES_INSIDE_REACT_QUERY_FOLDER_SHOULD_END_WITH_API
5202
+ });
5203
+ }
5204
+
5205
+ })
5206
+ };
5207
+
5208
+ const traverseParentsAndMatch = (patterns, node) => {
5209
+ if (matchesAny(patterns, node)) return true;
5210
+ if (node.parent) return traverseParentsAndMatch(patterns, node.parent);
5211
+ return false;
5212
+ };
5213
+
5214
+ const isInsideReactQueryDirectory = context => {
5215
+ const filename = context.getFilename();
5216
+ const reactQueryDirectory = path__default["default"].resolve(REACT_QUERY_DIRECTORY);
5217
+ return filename.startsWith(reactQueryDirectory);
5218
+ };
5219
+
5220
+ var useReactQueryForManagingRemoteData = {
5221
+ meta: {
5222
+ type: "problem",
5223
+ docs: {
5224
+ description: "API calls should be managed with React Query"
5225
+ }
5226
+ },
5227
+ create: context => ({
5228
+ Identifier(node) {
5229
+ if (isInsideApisDirectory(context) || isInsideReactQueryDirectory(context)) {
5230
+ return;
5231
+ }
5232
+
5233
+ const isProperty = traverseParentsAndMatch([PROPERTY_PATTERN], node);
5234
+
5235
+ if (node.name.endsWith("Api") && isProperty) {
5236
+ const hasAnyMatch = traverseParentsAndMatch(IGNORE_LIST, node);
5237
+ if (hasAnyMatch) return;
5238
+ context.report({
5239
+ node,
5240
+ message: USE_REACT_QUERY_FOR_MANAGING_REMOTE_DATA
5241
+ });
5242
+ }
5243
+ }
5244
+
5245
+ })
5246
+ };
5247
+
5124
5248
  const BREAK_STATEMENT_PATTERN = {
5125
5249
  type: TYPES.BREAK_STATEMENT
5126
5250
  };
@@ -5674,7 +5798,9 @@
5674
5798
  "use-webpack-alias": useWebpackAlias,
5675
5799
  "webpack-aliases-and-jsconfig-paths-should-be-in-sync": webpackAliasesAndJsconfigPathsShouldBeInSync,
5676
5800
  "zustand-recommend-comparator-function-usage": zustandRecommendComparatorFunctionUsage,
5677
- "zustand-use-prop-to-pick-single-item": zustandUsePropToPickSingleItem
5801
+ "zustand-use-prop-to-pick-single-item": zustandUsePropToPickSingleItem,
5802
+ "files-inside-react-query-folder-should-end-with-api": filesInsideReactQueryFolderShouldEndWithApi,
5803
+ "use-react-query-for-managing-remote-data": useReactQueryForManagingRemoteData
5678
5804
  },
5679
5805
  configs: {
5680
5806
  recommended
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bigbinary/eslint-plugin-neeto",
3
- "version": "1.0.17",
3
+ "version": "1.0.19",
4
4
  "main": "dist/index.js",
5
5
  "license": "UNLICENSED",
6
6
  "scripts": {