@bigbinary/eslint-plugin-neeto 1.0.18 → 1.0.20

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 +8 -118
  2. package/dist/index.js +143 -17
  3. package/package.json +3 -2
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.18"
11
+ yarn add -D "@bigbinary/eslint-plugin-neeto@latest"
12
12
  ```
13
13
 
14
14
  In your eslintrc.js file, add `neeto` in the `plugins` list and add the
@@ -52,6 +52,7 @@ module.exports = {
52
52
  - [consistent-button-link-anchor](./docs/src/rules/consistent-button-link-anchor.md)
53
53
  - [ensure-zustand-stores-are-type-annotated](./docs/src/rules/ensure-zustand-stores-are-type-annotated.md)
54
54
  - [ensure-zustand-import-names-are-consistent](./docs/src/rules/ensure-zustand-import-names-are-consistent.md)
55
+ - [files-inside-react-query-folder-should-end-with-api](./docs/src/rules/files-inside-react-query-folder-should-end-with-api.md)
55
56
  - [file-name-and-export-name-standards](./docs/src/rules/file-name-and-export-name-standards.md)
56
57
  - [minimize-zustand-store-access-scope](./docs/src/rules/minimize-zustand-store-access-scope.md)
57
58
  - [neetocommons-tips](./docs/src/rules/neetocommons-tips.md)
@@ -75,127 +76,16 @@ module.exports = {
75
76
  - [use-axios-config-for-passing-query-parameters](./docs/src/rules/use-axios-config-for-passing-query-parameters.md)
76
77
  - [use-common-routes](./docs/src/rules/use-common-routes.md)
77
78
  - [use-neetoui-classes](./docs/src/rules/use-neetoui-classes.md)
79
+ - [use-react-query-for-managing-remote-data](./docs/src/rules/use-react-query-for-managing-remote-data.md)
78
80
  - [use-snake-case-for-api-connector-filename](./docs/src/rules/use-snake-case-for-api-connector-filename.md)
79
81
  - [use-webpack-alias](./docs/src/rules/use-webpack-alias.md)
80
82
  - [webpack-aliases-and-jsconfig-paths-should-be-in-sync](./docs/src/rules/webpack-aliases-and-jsconfig-paths-should-be-in-sync.md)
81
83
  - [zustand-recommend-comparator-function-usage](./docs/src/rules/zustand-recommend-comparator-function-usage.md)
82
84
  - [zustand-use-prop-to-pick-single-item](./docs/src/rules/zustand-use-prop-to-pick-single-item.md)
83
85
 
84
- ## Development setup
86
+ ## Other references
85
87
 
86
- ### Initialization
87
-
88
- Clone this repo and initialize it by running `yarn install`
89
-
90
- ### Creating new rules
91
-
92
- You can use the script [`bin/generate-rule`](./bin/generate-rule) to scaffold a
93
- new rule.
94
-
95
- #### Usage:
96
-
97
- ```bash
98
- ./bin/generate-rule <rule-name> [--description=<rule-description-inside-quotes>] [--type=<rule-type>] [-P]
99
- ```
100
-
101
- #### Examples:
102
-
103
- ```bash
104
- ./bin/generate-rule "no-string-templates" --description="Disallows the use of string templates in JS" --type="suggestion"
105
- ```
106
-
107
- `-D` is an alias for `--description`<br/> `-T` is an alias for `--type` You can
108
- rewrite the same command as:
109
-
110
- ```bash
111
- ./bin/generate-rule "no-string-templates" -D "Disallows the use of string templates in JS" -T "suggestion"
112
- ```
113
-
114
- If you don't need to generate a `constants.js` file for the rule, you can add
115
- the `-C` (or `--without-constants`) flag:
116
-
117
- ```bash
118
- ./bin/generate-rule "no-string-templates" -P
119
- ```
120
-
121
- If the rule has a fixer, you can add the `-F` (or `--fixable`) flag followed by
122
- `"code"` or `"whitespace"`:
123
-
124
- ```bash
125
- ./bin/generate-rule "no-string-templates" -F "whitespace"
126
- ```
127
-
128
- ### Running tests
129
-
130
- This command will run all tests in this repo:
131
-
132
- ```bash
133
- yarn test
134
- ```
135
-
136
- To run a specific test and for TDD, follow this video:
137
- https://vimeo.com/723326900/785d6fe167
138
-
139
- ## Building and releasing
140
-
141
- Follow these steps to create a new release:
142
-
143
- - Before starting work on the new release, create a branch with the next release
144
- version as its name (`1.X.Y`) from main branch.
145
-
146
- - Update the version of the package in [README.md](README.md) and
147
- [package.json](package.json), and commit the changes in the branch that was
148
- created in the previous step.
149
-
150
- - Create a draft PR of this branch targeting `main` and start working on the new
151
- release.
152
-
153
- Whenever a PR is merged to `main`, `bot-bigbinary` will auto-merge those
154
- changes to this PR as well. This way the branch will stay in sync with `main`.
155
-
156
- - Add a checklist to the PR `1.X.Y` whenever a new change is auto-added to it
157
- (when another feature PR is merged). The checklist will be helpful during
158
- deployment and while writing migration guides.
159
-
160
- - Test the new release using in all the repos using `yalc`. The usage of `yalc`
161
- is explained in [this video](https://vimeo.com/722958162/9e931b640c).
162
-
163
- - In case there is any problem during the test, like any missed edge cases, fix
164
- the problem and run the following to install the latest changes:
165
-
166
- ```bash
167
- yalc push
168
- ```
169
-
170
- - Once all the PRs are created and all issues are rectified, mark the draft PR
171
- we created for the release as ready and get it merged.
172
-
173
- - Create a
174
- [new github release](https://github.com/bigbinary/eslint-plugin-neeto/releases/new)
175
- with the following details:
176
-
177
- Tag name: `1.X.Y` (use `Create new tag on publish` option)
178
-
179
- Target: `main`
180
-
181
- Release Title: `v1.X.Y`
182
-
183
- Click on `Generate release notes` for filling in the description.
184
-
185
- - Whenever a new release is created with a new version number, the github
186
- actions will automatically publish the built package to npm.
187
-
188
- - Immediately open another branch with the next version (`1.X.Y`) so that the
189
- cycle can continue.
190
-
191
- - Now run the following commands to replace the `yalc` based package with our
192
- new release from npm:
193
-
194
- ```bash
195
- yalc remove @bigbinary/eslint-plugin-neeto
196
- yarn add -D @bigbinary/eslint-plugin-neeto@1.X.Y
197
- ```
198
-
199
- You might be able to slightly tweak
200
- [this script](https://gist.github.com/jagannathBhat/42a584748d97fe134f0abadb191ef29a)
201
- to get the job done.
88
+ - [Development instructions](./docs/general/development-instructions.md)
89
+ - [Creating new rules](./docs/general/creating-new-rules.md)
90
+ - [Running test](./docs/general/running-tests.md)
91
+ - [Building and releasing](./docs/general/building-and-releasing.md)
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 {
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",
@@ -68,20 +78,21 @@
68
78
  "@bigbinary/neeto/no-missing-localization": ["error", {
69
79
  name: "en.json",
70
80
  lng: "en",
71
- content: require(path$2.resolve(__dirname, "../../../..", "app/javascript/src/translations/en.json"))
81
+ content: loadJS("app/javascript/src/translations/en.json")
72
82
  }],
73
83
  "@bigbinary/neeto/no-namespace-specifier-import-for-ramda": "error",
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 = {
@@ -345,7 +357,7 @@
345
357
  * also @see matches
346
358
  */
347
359
 
348
- const matchesAny = ramda.curry((list, obj) => list.some(matches(ramda.__, obj)));
360
+ const matchesAny = /*#__PURE__*/ramda.curry((list, obj) => list.some(matches(ramda.__, obj)));
349
361
  /**
350
362
  * Checks if the given pattern exists somewhere deep inside
351
363
  * the given object-tree by partial object matching.
@@ -354,7 +366,7 @@
354
366
  * @param {Object} obj the object tree to check
355
367
  */
356
368
 
357
- const patternExistsDeep = ramda.curry((pattern, obj) => {
369
+ const patternExistsDeep = /*#__PURE__*/ramda.curry((pattern, obj) => {
358
370
  if (typeof obj !== "object" || ramda.isNil(obj)) return false;
359
371
  if (matches(pattern, obj)) return true;
360
372
  return Object.entries(obj).some(([key, value]) => {
@@ -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
1391
  value: '* @type {import("neetocommons/react-utils").ZustandStoreHook} '
1378
- }]; // .....
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,12 +1,13 @@
1
1
  {
2
2
  "name": "@bigbinary/eslint-plugin-neeto",
3
- "version": "1.0.18",
3
+ "version": "1.0.20",
4
4
  "main": "dist/index.js",
5
5
  "license": "UNLICENSED",
6
6
  "scripts": {
7
7
  "prepare": "husky install",
8
8
  "test": "npm run build && jest dist/util-tests.spec.js && node dist/rule-tests.js",
9
- "build": "rollup -c rollup.config.js"
9
+ "build": "rollup -c rollup.config.js",
10
+ "watch": "rollup -c rollup.config.js --watch"
10
11
  },
11
12
  "lint-staged": {
12
13
  "{package.json}": [