@bigbinary/eslint-plugin-neeto 1.0.12 → 1.0.14

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 +7 -1
  2. package/dist/index.js +1051 -483
  3. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -1,12 +1,13 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('path'), require('ramda')) :
3
- typeof define === 'function' && define.amd ? define(['path', 'ramda'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global["@bigbinary/eslint-plugin-neeto"] = factory(global.path, global.ramda));
5
- })(this, (function (path, ramda) { 'use strict';
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, global.ramda, global.fs));
5
+ })(this, (function (path, 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
9
  var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
10
+ var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
10
11
 
11
12
  function _arrayWithHoles(arr) {
12
13
  if (Array.isArray(arr)) return arr;
@@ -78,159 +79,36 @@
78
79
  return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
79
80
  }, _typeof(obj);
80
81
  }
81
- var matches = ramda.curry(function (pattern, object) {
82
+
83
+ var matchesImpl = function matchesImpl(pattern, object) {
82
84
  var __parent = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : object;
83
85
 
84
86
  if (object === pattern) return true;
85
87
  if (typeof pattern === "function" && pattern(object, __parent)) return true;
86
88
  if (ramda.isNil(pattern) || ramda.isNil(object)) return false;
87
89
  if (_typeof(pattern) !== "object") return false;
88
- return Object.entries(pattern).every(function (_ref5) {
89
- var _ref6 = _slicedToArray(_ref5, 2),
90
- key = _ref6[0],
91
- value = _ref6[1];
92
-
93
- return matches(value, object[key], __parent);
94
- });
95
- });
96
-
97
- function _defineProperty(obj, key, value) {
98
- if (key in obj) {
99
- Object.defineProperty(obj, key, {
100
- value: value,
101
- enumerable: true,
102
- configurable: true,
103
- writable: true
104
- });
105
- } else {
106
- obj[key] = value;
107
- }
108
-
109
- return obj;
110
- }
111
-
112
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
90
+ return Object.entries(pattern).every(function (_ref) {
91
+ var _ref2 = _slicedToArray(_ref, 2),
92
+ key = _ref2[0],
93
+ value = _ref2[1];
113
94
 
114
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
115
- ramda.curry(function (id, array) {
116
- return array.filter(function (item) {
117
- return item.id !== id;
118
- });
119
- });
120
- ramda.curry(function (id, array) {
121
- return array.find(function (item) {
122
- return item.id === id;
123
- });
124
- });
125
- ramda.curry(function (id, newItem, array) {
126
- return array.map(function (item) {
127
- return item.id === id ? newItem : item;
128
- });
129
- });
130
- ramda.curry(function (id, modifier, array) {
131
- return array.map(function (item) {
132
- return item.id === id ? modifier(item) : item;
133
- });
134
- });
135
- ramda.curry(function (pattern, array) {
136
- return array.find(matches(pattern));
137
- });
138
- ramda.curry(function (pattern, array) {
139
- return array.filter(ramda.complement(matches(pattern)));
140
- });
141
- ramda.curry(function (pattern, newItem, array) {
142
- return array.map(function (item) {
143
- return matches(pattern, item) ? newItem : item;
144
- });
145
- });
146
- ramda.curry(function (pattern, modifier, array) {
147
- return array.map(function (item) {
148
- return matches(pattern, item) ? modifier(item) : item;
149
- });
150
- });
151
- ramda.curry(function (id, array) {
152
- return array.some(function (item) {
153
- return item.id === id;
95
+ return matchesImpl(value, object[key], __parent);
154
96
  });
97
+ };
98
+ var matches = /*#__PURE__*/ramda.curry(function (pattern, object) {
99
+ return matchesImpl(pattern, object);
155
100
  });
156
- var existsBy$1 = ramda.curry(function (pattern, array) {
101
+ var existsBy$1 = /*#__PURE__*/ramda.curry(function (pattern, array) {
157
102
  return array.some(matches(pattern));
158
103
  });
159
- ramda.curry(function (pattern, array) {
160
- return ramda.findLast(matches(pattern), array);
161
- });
162
- ramda.curry(function (id, array) {
163
- return array.findIndex(function (item) {
164
- return item.id === id;
165
- });
166
- });
167
- ramda.curry(function (pattern, array) {
168
- return array.findIndex(matches(pattern));
169
- });
170
- ramda.curry(function (pattern, array) {
171
- return ramda.findLastIndex(matches(pattern), array);
172
- });
173
- ramda.curry(function (pattern, array) {
174
- return array.filter(matches(pattern));
175
- });
176
- ramda.curry(function (pattern, array) {
177
- return ramda.count(matches(pattern), array);
178
- });
179
- ramda.curry(function (keyMap, objectArray) {
180
- return objectArray.map(function (object) {
181
- var shallowCopy = _objectSpread({}, object);
182
-
183
- for (var source in keyMap) {
184
- shallowCopy[keyMap[source]] = object[source];
185
- }
186
-
187
- return shallowCopy;
188
- });
189
- });
190
- ramda.curry(function (keyMap, objectArray) {
191
- return objectArray.map(function (object) {
192
- var shallowCopy = _objectSpread({}, object);
193
-
194
- for (var source in keyMap) {
195
- shallowCopy[keyMap[source]] = object[source];
196
- delete shallowCopy[source];
197
- }
198
-
199
- return shallowCopy;
200
- });
201
- });
202
- ramda.curry(function (keyMap, objectArray) {
203
- var copyKeysSingleObject = function copyKeysSingleObject(object, keyMap) {
204
- var root = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : object;
205
- return _objectSpread(_objectSpread({}, object), ramda.fromPairs(ramda.toPairs(keyMap).map(function (_ref) {
206
- var _ref2 = _slicedToArray(_ref, 2),
207
- destination = _ref2[0],
208
- source = _ref2[1];
209
-
210
- if (typeof source === "function") {
211
- return [destination, source(object[destination], root)];
212
- } else if (Array.isArray(source)) {
213
- return [destination, ramda.path(source, root)];
214
- } else if (_typeof(source) === "object") {
215
- return [destination, copyKeysSingleObject(object[destination], source, root)];
216
- }
217
-
218
- return [destination, object[source]];
219
- })));
220
- };
221
-
222
- return objectArray.map(function (object) {
223
- return copyKeysSingleObject(object, keyMap);
224
- });
225
- });
226
- ramda.complement(ramda.isNil);
227
- ramda.complement(ramda.isEmpty);
228
- ramda.curry(function (x, y) {
104
+ var isNotEmpty$1 = /*#__PURE__*/ramda.complement(ramda.isEmpty);
105
+ var notEquals = /*#__PURE__*/ramda.curry(function (x, y) {
229
106
  return x !== y;
230
107
  });
231
- ramda.complement(ramda.equals);
232
108
 
233
109
  const APIS_DIRECTORY = "app/javascript/src/apis";
110
+ const ROUTES_FILE_PATH = "app/javascript/src/common/routes.js";
111
+ const WEBPACK_ALIAS_PATH = "config/webpack/alias.js";
234
112
  const BLACKLISTED_PACKAGES = ["moment", "bootstrap", "blueprint", "react-spinners", "react-table", "remixicon", "mjml", "react-select", "antd"];
235
113
  const TYPES = {
236
114
  ARRAY_EXPRESSION: "ArrayExpression",
@@ -240,8 +118,18 @@
240
118
  BINARY_EXPRESSION: "BinaryExpression",
241
119
  CALL_EXPRESSION: "CallExpression",
242
120
  EXPORT_NAMED_DECLARATION: "ExportNamedDeclaration",
121
+ EXPORT_DEFAULT_DECLARATION: "ExportDefaultDeclaration",
122
+ FUNCTION_DECLARATION: "FunctionDeclaration",
123
+ FUNCTION_EXPRESSION: "FunctionExpression",
243
124
  IDENTIFIER: "Identifier",
244
125
  IF_STATEMENT: "IfStatement",
126
+ IMPORT_DECLARATION: "ImportDeclaration",
127
+ IMPORT_DEFAULT_SPECIFIER: "ImportDefaultSpecifier",
128
+ IMPORT_SPECIFIER: "ImportSpecifier",
129
+ JSX_ATTRIBUTE: "JSXAttribute",
130
+ JSX_ELEMENT: "JSXElement",
131
+ JSX_EXPRESSION_CONTAINER: "JSXExpressionContainer",
132
+ JSX_IDENTIFIER: "JSXIdentifier",
245
133
  LITERAL: "Literal",
246
134
  LOGICAL_EXPRESSION: "LogicalExpression",
247
135
  MEMBER_EXPRESSION: "MemberExpression",
@@ -252,19 +140,17 @@
252
140
  SPREAD_ELEMENT: "SpreadElement",
253
141
  TEMPLATE_LITERAL: "TemplateLiteral",
254
142
  UNARY_EXPRESSION: "UnaryExpression",
143
+ VARIABLE_DECLARATION: "VariableDeclaration",
255
144
  VARIABLE_DECLARATOR: "VariableDeclarator",
256
- WHILE_STATEMENT: "WhileStatement",
257
- JSX_ATTRIBUTE: "JSXAttribute",
258
- JSX_ELEMENT: "JSXElement",
259
- JSX_EXPRESSION_CONTAINER: "JSXExpressionContainer"
145
+ WHILE_STATEMENT: "WhileStatement"
260
146
  };
261
147
  const IMPORT_DECLARATION = {
262
- type: "ImportDeclaration",
148
+ type: TYPES.IMPORT_DECLARATION,
263
149
  source: {
264
- type: "Literal"
150
+ type: TYPES.LITERAL
265
151
  },
266
152
  specifiers: [{
267
- type: "ImportSpecifier"
153
+ type: TYPES.IMPORT_SPECIFIER
268
154
  }]
269
155
  };
270
156
 
@@ -274,7 +160,7 @@
274
160
  * @returns the source code corresponding to the supplied node.
275
161
  */
276
162
 
277
- const getCode = (context, node) => context.getSourceCode().text.substr(node.range[0], node.range[1] - node.range[0]);
163
+ const getCode$1 = (context, node) => context.getSourceCode().text.substr(node.range[0], node.range[1] - node.range[0]);
278
164
  /**
279
165
  * Checks if the specified package is imported or not
280
166
  * using the package name or any alias
@@ -351,6 +237,7 @@
351
237
  const API_ACTIONS_ARGUMENT_LIMIT = 2;
352
238
  const FIXABLE_PARAMETER_TYPES = [TYPES.IDENTIFIER, TYPES.ASSIGNMENT_PATTERN];
353
239
 
240
+ const addInJsconfig = aliasName => `Add path for "${aliasName}" in jsconfig.json. Refer https://github.com/bigbinary/eslint-plugin-neeto/blob/master/docs/src/rules/webpack-aliases-and-jsconfig-paths-should-be-in-sync.md for more details.`;
354
241
  const canBeReplacedWith = fix => `Can be replaced with: ${fix}`;
355
242
  const disallowUse = packageName => `Do not use the package ${packageName}`;
356
243
  const moveToConstantsJs = constantName => `Move the constant ${constantName} to a constants.js file.`;
@@ -370,9 +257,20 @@
370
257
  const NO_EXCESS_API_ACTION_ARGUMENTS_MESSAGE = `API actions should use named parameters for more than ${API_ACTIONS_ARGUMENT_LIMIT} parameters`;
371
258
  const NO_EXCESS_FUNCTION_ARGUMENTS_MESSAGE = `Functions should use named parameters for more than ${ARGUMENT_LIMIT} parameters`;
372
259
  const NO_GLOBALPROPS_REASSIGNMENT_MESSAGE = "globalProps or its nested properties should not be re-assigned.";
260
+ const NO_ROUTES_ASSIGNMENT = "`routes` or its nested properties should not be assigned to other variables.";
261
+ const NO_ROUTES_REASSIGNMENT = "`routes` or its nested properties should not be re-assigned.";
373
262
  const NO_NAMESPACE_SPECIFIER_IMPORT_FOR_RAMDA_MESSAGE = 'Use `import { methodsToImport } from "ramda"` instead';
374
263
  const PREFIX_NEETO_UI_IMPORT_ALIAS_MESSAGE = "Aliases for anything imported from neetoUI or neetoIcons should start with `NeetoUI`";
264
+ const ROUTE_SHOULD_BE_A_STRING = "Route should be a plain string literal.";
265
+ const ROUTE_SHOULD_MATCH_OBJECT_PATH = "Route string should match with its object path.";
266
+ const ROUTES_HAS_ALREADY_BEEN_DECLARED = "Identifier `routes` has already been declared";
267
+ const ROUTES_CONSTANT_SHOULD_BE_DECLARED = "A constant object named `routes` should be declared inside routes.js";
268
+ const ROUTES_SHOULD_USE_DEFAULT_EXPORT = "`routes` should be exported using the default export method.";
375
269
  const USE_AXIOS_CONFIG_FOR_PASSING_QUERY_PARAMETERS_MESSAGE = "Use 'params attribute in axios configuration' to pass query parameters.";
270
+ const USE_LINK_FOR_NAVIGATION = "Use Link or NavLink component from 'react-router-dom' for click-navigation instead of onClick handler";
271
+ const USE_ANCHOR_FOR_NAVIGATION = "Use Anchor element (<a>) for click-navigation instead of onClick handler";
272
+ const USE_BUTTON_TO_FOR_LINK_NAVIGATION = "Use `to` prop of Button (<Button to='path'>) for click-navigation instead of onClick handler";
273
+ const USE_BUTTON_TO_FOR_ANCHOR_NAVIGATION = "Use `href` prop of Button (<Button href='path'>) for click-navigation instead of onClick handler";
376
274
 
377
275
  const toCamelCase = text => {
378
276
  const capitalized = text.split(/[^0-9a-z]/i).map(part => `${part[0].toUpperCase()}${part.slice(1)}`).join("");
@@ -468,14 +366,14 @@
468
366
  * @param {Object} pattern the pattern to match partially
469
367
  */
470
368
 
471
- const patternExistsDeep = (obj, pattern) => {
369
+ const patternExistsDeep = ramda.curry((pattern, obj) => {
472
370
  if (typeof obj !== "object" || ramda.isNil(obj)) return false;
473
371
  if (matches(pattern, obj)) return true;
474
372
  return Object.entries(obj).some(([key, value]) => {
475
373
  if (key === "parent") return false;
476
- return patternExistsDeep(value, pattern);
374
+ return patternExistsDeep(pattern, value);
477
375
  });
478
- };
376
+ });
479
377
 
480
378
  const AWAIT_DECLARATION_PATTERN = {
481
379
  type: TYPES.VARIABLE_DECLARATOR,
@@ -483,7 +381,7 @@
483
381
  type: TYPES.AWAIT_EXPRESSION
484
382
  }
485
383
  };
486
- const IDENTIFIER_PATTERN = {
384
+ const IDENTIFIER_PATTERN$1 = {
487
385
  type: TYPES.IDENTIFIER
488
386
  };
489
387
 
@@ -493,8 +391,8 @@
493
391
  return ((_node$parent = node.parent) === null || _node$parent === void 0 ? void 0 : (_node$parent$right = _node$parent.right) === null || _node$parent$right === void 0 ? void 0 : _node$parent$right.name) === node.name;
494
392
  };
495
393
 
496
- const getChildCode = (context, nodes, pathArray) => nodes.map(node => getCode(context, ramda.path(pathArray, node))).join(", ");
497
- const getIdentifiersInNode = node => findMatchingNodesDeep(node, IDENTIFIER_PATTERN).filter(node => !(isKeyNode(node) || isDefaultValueNode(node) || node.parent.computed)); // id from const { id : orgID } = ...
394
+ const getChildCode = (context, nodes, pathArray) => nodes.map(node => getCode$1(context, ramda.path(pathArray, node))).join(", ");
395
+ const getIdentifiersInNode = node => findMatchingNodesDeep(node, IDENTIFIER_PATTERN$1).filter(node => !(isKeyNode(node) || isDefaultValueNode(node) || node.parent.computed)); // id from const { id : orgID } = ...
498
396
 
499
397
  const isKeyNode = node => {
500
398
  var _node$parent2;
@@ -513,7 +411,7 @@
513
411
  return isPrevLineSameAsLastAwait || areBothAwaitsInSameLine;
514
412
  };
515
413
 
516
- const identifiersUsed = (node, identifiers) => identifiers.length > 0 && identifiers.some(identifier => findMatchingNodesDeep(node, { ...IDENTIFIER_PATTERN,
414
+ const identifiersUsed = (node, identifiers) => identifiers.length > 0 && identifiers.some(identifier => findMatchingNodesDeep(node, { ...IDENTIFIER_PATTERN$1,
517
415
  name: identifier.name
518
416
  }).some(node => !isKeyNode(node)));
519
417
 
@@ -596,8 +494,530 @@
596
494
  })
597
495
  };
598
496
 
497
+ const SPECIAL_KEYS = ["home", "index", "show"]; // import routes from "common/routes", import routes from "routes"
498
+
499
+ const ROUTES_IMPORT_PATTERN = {
500
+ type: TYPES.IMPORT_DECLARATION,
501
+ source: {
502
+ type: TYPES.LITERAL,
503
+ value: matchesAny(["routes", "common/routes"])
504
+ },
505
+ specifiers: [{
506
+ type: TYPES.IMPORT_DEFAULT_SPECIFIER,
507
+ local: {
508
+ type: TYPES.IDENTIFIER,
509
+ name: "routes"
510
+ }
511
+ }]
512
+ }; // const routes = { home: "/" }, const routes = "/", const routes = ["/"] etc
513
+
514
+ const DECALARATION_PATTERN = {
515
+ type: TYPES.VARIABLE_DECLARATION,
516
+ declarations: [{
517
+ type: TYPES.VARIABLE_DECLARATOR,
518
+ id: {
519
+ type: TYPES.IDENTIFIER,
520
+ name: "routes"
521
+ }
522
+ }]
523
+ }; // const routes = { home: "/" }
524
+
525
+ const OBJECT_DECALARATION_PATTERN = ramda.assocPath(["declarations", 0, "init"], {
526
+ type: TYPES.OBJECT_EXPRESSION
527
+ }, DECALARATION_PATTERN); // export const routes = { home: "/" }
528
+
529
+ const EXPORT_NAMED_DECLARATION_PATTERN = {
530
+ type: TYPES.EXPORT_NAMED_DECLARATION,
531
+ declaration: matches(OBJECT_DECALARATION_PATTERN)
532
+ }; // export default routes, export default deepFreezeObject(routes)
533
+
534
+ const DEFAULT_EXPORT_PATTERN = {
535
+ type: TYPES.EXPORT_DEFAULT_DECLARATION,
536
+ declaration: matchesAny([{
537
+ type: TYPES.IDENTIFIER,
538
+ name: "routes"
539
+ }, {
540
+ type: TYPES.CALL_EXPRESSION,
541
+ callee: {
542
+ name: "deepFreezeObject"
543
+ },
544
+ arguments: [{
545
+ name: "routes"
546
+ }]
547
+ }])
548
+ };
549
+ const IDENTIFIER_PATTERN = {
550
+ type: TYPES.IDENTIFIER,
551
+ name: "routes"
552
+ };
553
+ const MEMBER_EXPRESSION_PATTERN = {
554
+ type: TYPES.MEMBER_EXPRESSION,
555
+ object: {
556
+ type: TYPES.IDENTIFIER,
557
+ name: "routes"
558
+ }
559
+ }; // const { home } = routes, const homeRoute = routes.home
560
+
561
+ const ASSIGNMENT_PATTERN = {
562
+ id: {
563
+ type: matchesAny([TYPES.IDENTIFIER, TYPES.OBJECT_PATTERN])
564
+ },
565
+ init: matchesAny([IDENTIFIER_PATTERN, MEMBER_EXPRESSION_PATTERN])
566
+ }; // const routes = { home: "/" }, const routes.home = "/"
567
+
568
+ const REASSIGNMENT_PATTERN = {
569
+ left: matchesAny([IDENTIFIER_PATTERN, MEMBER_EXPRESSION_PATTERN])
570
+ }; // path={string | template literals}, to={string | template literals}
571
+
572
+ const PATH_OR_TO_PROP_PATTERN = {
573
+ type: TYPES.JSX_ATTRIBUTE,
574
+ name: {
575
+ type: TYPES.JSX_IDENTIFIER,
576
+ name: ramda.includes(ramda.__, ["to", "path"])
577
+ },
578
+ value: matchesAny([{
579
+ type: TYPES.JSX_EXPRESSION_CONTAINER,
580
+ expression: {
581
+ type: ramda.includes(ramda.__, [TYPES.LITERAL, TYPES.TEMPLATE_LITERAL]),
582
+ value: notEquals("*")
583
+ }
584
+ }, {
585
+ type: TYPES.LITERAL,
586
+ value: notEquals("*")
587
+ }])
588
+ };
589
+ /*
590
+ <Route path={string | template literals} />, <Redirect to={string | template literals} />
591
+ <Link to={string | template literals} />, <NavLink to={string | template literals} />
592
+ */
593
+
594
+ const NAVIGATION_COMPONENTS_PATTERN = {
595
+ name: {
596
+ type: TYPES.JSX_IDENTIFIER,
597
+ name: ramda.includes(ramda.__, ["Route", "Redirect", "Link", "NavLink"])
598
+ },
599
+ attributes: ramda.any(matches(PATH_OR_TO_PROP_PATTERN))
600
+ }; // history.push(string| | template literal), history.replace(string | template literal)
601
+
602
+ const HISTORY_PATTERN = {
603
+ callee: {
604
+ type: TYPES.MEMBER_EXPRESSION,
605
+ object: {
606
+ name: "history"
607
+ },
608
+ property: {
609
+ name: ramda.includes(ramda.__, ["push", "replace"])
610
+ }
611
+ },
612
+ arguments: [{
613
+ type: ramda.includes(ramda.__, [TYPES.LITERAL, TYPES.TEMPLATE_LITERAL])
614
+ }]
615
+ };
616
+ /*
617
+ `${routes.home.projects}/find?id=123`, `home/${routes.projects}/find?id=322`
618
+ `${routes.projects}/{routes.user.show}?name=john&&age=21`
619
+ */
620
+
621
+ const QUERY_PARAMS_PATTERN = {
622
+ type: TYPES.TEMPLATE_LITERAL,
623
+ expressions: ramda.any(patternExistsDeep({
624
+ type: TYPES.MEMBER_EXPRESSION,
625
+ object: {
626
+ type: TYPES.IDENTIFIER,
627
+ name: "routes"
628
+ }
629
+ })),
630
+ quasis: ramda.any(({
631
+ value
632
+ }) => value.cooked.includes("?"))
633
+ };
634
+
635
+ const isRoutesImportPresent = context => context.getAncestors()[0].body.some(matches(ROUTES_IMPORT_PATTERN));
636
+
637
+ var noRoutesDestructureAndReassignment = {
638
+ meta: {
639
+ type: "problem",
640
+ docs: {
641
+ description: "Disallow destructuring and re-assignment of routes"
642
+ }
643
+ },
644
+ create: context => ({
645
+ VariableDeclaration(node) {
646
+ if (matches(DECALARATION_PATTERN, node) && isRoutesImportPresent(context)) {
647
+ context.report(node, ROUTES_HAS_ALREADY_BEEN_DECLARED);
648
+ }
649
+ },
650
+
651
+ VariableDeclarator(node) {
652
+ if (matches(ASSIGNMENT_PATTERN, node) && isRoutesImportPresent(context)) {
653
+ context.report(node, NO_ROUTES_ASSIGNMENT);
654
+ }
655
+ },
656
+
657
+ AssignmentExpression(node) {
658
+ if (matches(REASSIGNMENT_PATTERN, node) && isRoutesImportPresent(context)) {
659
+ context.report(node, NO_ROUTES_REASSIGNMENT);
660
+ }
661
+ }
662
+
663
+ })
664
+ };
665
+
666
+ const isInsideRoutesFile$1 = context => {
667
+ const filename = context.getFilename();
668
+ return filename.endsWith(ROUTES_FILE_PATH);
669
+ };
670
+
671
+ var routesShouldBeDeclaredAndDefaultExported = {
672
+ meta: {
673
+ type: "problem",
674
+ docs: {
675
+ description: "common/routes.js file should declare a constant named routes and should be exported using default export"
676
+ }
677
+ },
678
+ create: context => ({
679
+ Program(node) {
680
+ if (!isInsideRoutesFile$1(context)) return;
681
+ const isDeclarationPresent = node.body.some(matchesAny([OBJECT_DECALARATION_PATTERN, EXPORT_NAMED_DECLARATION_PATTERN]));
682
+
683
+ if (isDeclarationPresent) {
684
+ const isDefaultExportPresent = node.body.some(matches(DEFAULT_EXPORT_PATTERN));
685
+ if (isDefaultExportPresent) return;
686
+ context.report(node, ROUTES_SHOULD_USE_DEFAULT_EXPORT);
687
+ } else {
688
+ context.report(node, ROUTES_CONSTANT_SHOULD_BE_DECLARED);
689
+ }
690
+ }
691
+
692
+ })
693
+ };
694
+
695
+ const camelize = text => {
696
+ text = text.replace(/[-_\s.]+(.)?/g, (_, c) => c ? c.toUpperCase() : "");
697
+ return text.substring(0, 1).toLowerCase() + text.substring(1);
698
+ };
699
+
700
+ const isInsideRoutesFile = context => {
701
+ const filename = context.getFilename();
702
+ return filename.endsWith(ROUTES_FILE_PATH);
703
+ };
704
+
705
+ const isParam = e => e === null || e === void 0 ? void 0 : e.startsWith(":");
706
+
707
+ const isNotParam = e => !isParam(e);
708
+
709
+ const checkObjectPath = (context, object, objectPath = []) => {
710
+ if (object.type === TYPES.OBJECT_EXPRESSION) {
711
+ object.properties.forEach(property => checkObjectPath(context, property, objectPath));
712
+ } else if (object.type === TYPES.PROPERTY) {
713
+ if (object.value.type === TYPES.LITERAL) {
714
+ const routeKeys = object.value.value.split("/").filter(isNotEmpty$1);
715
+ const routeNonParams = routeKeys.filter(isNotParam).map(camelize);
716
+ if (isNotEmpty$1(routeKeys) && ramda.isEmpty(routeNonParams)) return;
717
+ let updatedObjectPath = SPECIAL_KEYS.includes(object.key.name) ? objectPath : [...objectPath, object.key.name];
718
+
719
+ if (isParam(routeKeys[0])) {
720
+ updatedObjectPath = ramda.remove(0, 1, updatedObjectPath);
721
+ }
722
+
723
+ const lastNonParamIndex = ramda.findLastIndex(isNotParam, routeKeys);
724
+ const consecutiveParamsCount = routeKeys.length - (lastNonParamIndex + 1);
725
+ const keyDiff = updatedObjectPath.length - routeNonParams.length;
726
+
727
+ if (matches(routeNonParams, updatedObjectPath) && [0, consecutiveParamsCount, consecutiveParamsCount - 1].includes(keyDiff)) {
728
+ return;
729
+ }
730
+
731
+ context.report({
732
+ node: object.value,
733
+ message: ROUTE_SHOULD_MATCH_OBJECT_PATH
734
+ });
735
+ } else if (object.value.type === TYPES.OBJECT_EXPRESSION) {
736
+ const updatedObjectPath = [...objectPath, object.key.name];
737
+ checkObjectPath(context, object.value, updatedObjectPath);
738
+ } else {
739
+ context.report({
740
+ node: object.value,
741
+ message: ROUTE_SHOULD_BE_A_STRING
742
+ });
743
+ }
744
+ }
745
+ };
746
+
747
+ var routesShouldMatchObjectPath = {
748
+ meta: {
749
+ type: "problem",
750
+ docs: {
751
+ description: "Each route string defined in the `routes` object should match with its object path."
752
+ }
753
+ },
754
+ create: context => ({
755
+ VariableDeclaration(node) {
756
+ if (isInsideRoutesFile(context) && matches(DECALARATION_PATTERN, node)) {
757
+ const object = node.declarations[0].init;
758
+ checkObjectPath(context, object);
759
+ }
760
+ }
761
+
762
+ })
763
+ };
764
+
765
+ const getCode = (context, start, length) => context.getSourceCode().text.substr(start, length);
766
+
767
+ const replacer$2 = nodeReplacerWithImports(ramda.assocPath(["source", "value"], "neetocommons/utils", IMPORT_DECLARATION));
768
+ var useCommonRoutes = {
769
+ meta: {
770
+ type: "problem",
771
+ fixable: "code",
772
+ docs: {
773
+ description: "Prevent usage of strings and template literals in Route, Redirect, Link, NavLink, history.push() and history.replace()"
774
+ }
775
+ },
776
+ create: context => ({
777
+ JSXOpeningElement(node) {
778
+ if (!matches(NAVIGATION_COMPONENTS_PATTERN, node)) return;
779
+ const componentName = node.name.name;
780
+ const valueNode = findMatchingNodesDeep(node, PATH_OR_TO_PROP_PATTERN)[0].value;
781
+ if (matches(QUERY_PARAMS_PATTERN, valueNode)) return;
782
+ const message = componentName === "Route" ? canBeReplacedWith("routes.*") : canBeReplacedWith("routes.* or buildUrl(routes.*)");
783
+ context.report(valueNode, message);
784
+ },
785
+
786
+ CallExpression(node) {
787
+ if (!matches(HISTORY_PATTERN, node)) return;
788
+ const argumentNode = node.arguments[0];
789
+ if (matches(QUERY_PARAMS_PATTERN, argumentNode)) return;
790
+ const message = canBeReplacedWith("routes.* or buildUrl(routes.*)");
791
+ context.report(argumentNode, message);
792
+ },
793
+
794
+ TemplateLiteral(node) {
795
+ if (!matches(QUERY_PARAMS_PATTERN, node)) return;
796
+ const quasiWithQuestionMark = node.quasis.find(({
797
+ value
798
+ }) => value.cooked.includes("?"));
799
+ const questionMarkIndex = quasiWithQuestionMark.range[0] + quasiWithQuestionMark.value.cooked.indexOf("?") + 1;
800
+ const textBeforeQuestionMark = getCode(context, node.range[0] + 1, questionMarkIndex - (node.range[0] + 1));
801
+ const textAfterQuestionMark = getCode(context, questionMarkIndex + 1, node.range[1] - (questionMarkIndex + 2));
802
+ if (ramda.isEmpty(textAfterQuestionMark)) return;
803
+
804
+ if (textAfterQuestionMark.indexOf("${") > -1) {
805
+ context.report({
806
+ node,
807
+ message: canBeReplacedWith("buildUrl()")
808
+ });
809
+ } else {
810
+ const queryParams = JSON.stringify(Object.fromEntries(new URLSearchParams(textAfterQuestionMark)));
811
+ const fix = `buildUrl(\`${textBeforeQuestionMark}\`, ${queryParams})`;
812
+ context.report({
813
+ node,
814
+ message: canBeReplacedWith(fix),
815
+ fix: replacer$2.replaceNode({
816
+ context,
817
+ node,
818
+ fix,
819
+ functions: "buildUrl"
820
+ })
821
+ });
822
+ }
823
+ }
824
+
825
+ })
826
+ };
827
+
828
+ // () => { <expression> }
829
+
830
+ const wrappedInBlockPattern = expression => ({
831
+ type: "BlockStatement",
832
+ body: body => body.length === 1 && matches({
833
+ type: "ExpressionStatement",
834
+ expression
835
+ }, body[0])
836
+ }); // everything except this style
837
+ // history.push({ state: something, path: somethingElse })
838
+
839
+
840
+ const HISTORY_PUSH_PATTERN = {
841
+ type: TYPES.CALL_EXPRESSION,
842
+ callee: {
843
+ type: TYPES.MEMBER_EXPRESSION,
844
+ object: {
845
+ type: TYPES.IDENTIFIER,
846
+ name: "history"
847
+ },
848
+ property: {
849
+ type: TYPES.IDENTIFIER,
850
+ name: "push"
851
+ }
852
+ },
853
+ arguments: args => args.length === 1 && matches([{
854
+ type: notEquals(TYPES.OBJECT_EXPRESSION)
855
+ }], args)
856
+ };
857
+ const WINDOW_LOCATION_PATTERNS = [// window.location.href = "/path"
858
+ {
859
+ type: "AssignmentExpression",
860
+ operator: "=",
861
+ left: {
862
+ type: TYPES.MEMBER_EXPRESSION,
863
+ object: {
864
+ type: TYPES.MEMBER_EXPRESSION,
865
+ object: {
866
+ type: TYPES.IDENTIFIER,
867
+ name: "window"
868
+ },
869
+ property: {
870
+ type: TYPES.IDENTIFIER,
871
+ name: "location"
872
+ },
873
+ computed: false,
874
+ optional: false
875
+ },
876
+ property: {
877
+ type: TYPES.IDENTIFIER,
878
+ name: "href"
879
+ },
880
+ computed: false,
881
+ optional: false
882
+ }
883
+ }, // window.location.assign("/path")
884
+ {
885
+ type: TYPES.CALL_EXPRESSION,
886
+ callee: {
887
+ type: TYPES.MEMBER_EXPRESSION,
888
+ object: {
889
+ type: TYPES.MEMBER_EXPRESSION,
890
+ object: {
891
+ type: TYPES.IDENTIFIER,
892
+ name: "window"
893
+ },
894
+ property: {
895
+ type: TYPES.IDENTIFIER,
896
+ name: "location"
897
+ },
898
+ computed: false,
899
+ optional: false
900
+ },
901
+ property: {
902
+ type: TYPES.IDENTIFIER,
903
+ name: "assign"
904
+ },
905
+ computed: false,
906
+ optional: false
907
+ },
908
+ optional: false
909
+ }]; // onClick={() => <expression>}
910
+
911
+ const ONCLICK_ATTRIBUTE_PATTERN = {
912
+ type: TYPES.JSX_ATTRIBUTE,
913
+ name: {
914
+ type: TYPES.JSX_IDENTIFIER,
915
+ name: "onClick"
916
+ },
917
+ value: {
918
+ type: TYPES.JSX_EXPRESSION_CONTAINER,
919
+ expression: {
920
+ type: TYPES.ARROW_FUNCTION_EXPRESSION,
921
+ params: ramda.isEmpty,
922
+ generator: false,
923
+ async: false
924
+ }
925
+ }
926
+ }; // <div onClick={() => history.push("/path")} />
927
+
928
+ const LINK_REPLACEABLE_PATTERN = {
929
+ name: {
930
+ type: TYPES.JSX_IDENTIFIER,
931
+ name: ramda.includes(ramda.__, ["Button", "button", "span", "div", "a"])
932
+ },
933
+ attributes: ramda.any(matches(ramda.assocPath(["value", "expression", "body"], matchesAny([HISTORY_PUSH_PATTERN, wrappedInBlockPattern(HISTORY_PUSH_PATTERN)]), ONCLICK_ATTRIBUTE_PATTERN)))
934
+ }; // <div onClick={() => window.location.href = "/path"} />
935
+
936
+ const ANCHOR_REPLACEABLE_PATTERN = {
937
+ name: {
938
+ type: TYPES.JSX_IDENTIFIER,
939
+ name: ramda.includes(ramda.__, ["Button", "button", "span", "div"])
940
+ },
941
+ attributes: ramda.any(matches(ramda.assocPath(["value", "expression", "body"], matchesAny([WINDOW_LOCATION_PATTERNS, WINDOW_LOCATION_PATTERNS.map(wrappedInBlockPattern)].flat()), ONCLICK_ATTRIBUTE_PATTERN)))
942
+ };
943
+
944
+ const getFixer = ({
945
+ context,
946
+ node,
947
+ attribute,
948
+ findPathNode
949
+ }) => fixer => {
950
+ const onClickAttributeNode = node.attributes.find(matches(ONCLICK_ATTRIBUTE_PATTERN));
951
+ const pathNode = findPathNode(onClickAttributeNode);
952
+ const navigateAttr = pathNode.type === TYPES.LITERAL ? `${attribute}="${pathNode.value}"` : `${attribute}={${getCode$1(context, pathNode)}}`;
953
+ return fixer.replaceText(onClickAttributeNode, navigateAttr);
954
+ };
955
+
956
+ const findLinkPathNode = node => findMatchingNodesDeep(node, HISTORY_PUSH_PATTERN)[0].arguments[0];
957
+
958
+ const findAnchorPathNode = node => {
959
+ let expressionNode = findMatchingNodesDeep(node, WINDOW_LOCATION_PATTERNS[0]);
960
+ if (!ramda.isEmpty(expressionNode)) return expressionNode[0].right;
961
+ expressionNode = findMatchingNodesDeep(node, WINDOW_LOCATION_PATTERNS[1]);
962
+ return expressionNode[0].arguments[0];
963
+ };
964
+
965
+ var consistentButtonLinkAnchor = {
966
+ meta: {
967
+ type: "suggestion",
968
+ docs: {
969
+ description: "Warns when button, div, span etc. are used with an onClick handler " + "instead of link or anchor tag to navigate to other pages."
970
+ },
971
+ fixable: "code"
972
+ },
973
+ create: context => ({
974
+ JSXOpeningElement(node) {
975
+ let message, fix;
976
+
977
+ if (matches(LINK_REPLACEABLE_PATTERN, node)) {
978
+ if (node.name.name === "Button") {
979
+ message = USE_BUTTON_TO_FOR_LINK_NAVIGATION;
980
+ const findPathNode = findLinkPathNode;
981
+ fix = getFixer({
982
+ context,
983
+ node,
984
+ attribute: "to",
985
+ findPathNode
986
+ });
987
+ } else message = USE_LINK_FOR_NAVIGATION;
988
+ } else if (matches(ANCHOR_REPLACEABLE_PATTERN, node)) {
989
+ if (node.name.name === "Button") {
990
+ message = USE_BUTTON_TO_FOR_ANCHOR_NAVIGATION;
991
+ const findPathNode = findAnchorPathNode;
992
+ fix = getFixer({
993
+ context,
994
+ node,
995
+ attribute: "href",
996
+ findPathNode
997
+ });
998
+ } else message = USE_ANCHOR_FOR_NAVIGATION;
999
+ } else return;
1000
+
1001
+ context.report({
1002
+ node,
1003
+ message,
1004
+ fix
1005
+ });
1006
+ }
1007
+
1008
+ })
1009
+ };
1010
+
599
1011
  const CONSTANT_CASE_PATTERN = /^[A-Z0-9_]*$/;
600
- const CONSTANTS_FILES = [/.*constants.js$/i, /app\/javascript\/src\/common\/urls.js$/];
1012
+ const CONSTANTS_FILES = [/.*constants.(js|jsx)$/i, /app\/javascript\/src\/common\/urls.js$/];
1013
+ const CONSTANT_DECALARATION_PATTERN = {
1014
+ kind: "const",
1015
+ declarations: [{
1016
+ id: {
1017
+ name: name => CONSTANT_CASE_PATTERN.test(name)
1018
+ }
1019
+ }]
1020
+ };
601
1021
 
602
1022
  const isConstantsFile = context => CONSTANTS_FILES.some(filePath => filePath.test(path__default["default"].resolve(context.getFilename())));
603
1023
 
@@ -605,16 +1025,13 @@
605
1025
  meta: {
606
1026
  type: "suggestion",
607
1027
  docs: {
608
- description: "Indicates that any constant outside a constant.js file should be moved to a constant.js file."
1028
+ description: "Indicates that any constant outside a constant.[js|jsx] file should be moved to a constant.[js|jsx] file."
609
1029
  }
610
1030
  },
611
1031
  create: context => ({
612
1032
  VariableDeclaration(node) {
613
- if (node.kind !== "const") return;
614
- if (isConstantsFile(context)) return;
615
- const constantName = node.declarations[0].id.name;
616
-
617
- if (CONSTANT_CASE_PATTERN.test(constantName)) {
1033
+ if (!isConstantsFile(context) && matches(CONSTANT_DECALARATION_PATTERN, node)) {
1034
+ const constantName = node.declarations[0].id.name;
618
1035
  context.report(node, moveToConstantsJs(constantName));
619
1036
  }
620
1037
  }
@@ -676,7 +1093,7 @@
676
1093
  node,
677
1094
  message: useConstantCaseFor(name),
678
1095
  suggest: [{
679
- desc: `Replace ${getCode(context, node)} with ${toConstantCase(name)}`,
1096
+ desc: `Replace ${getCode$1(context, node)} with ${toConstantCase(name)}`,
680
1097
  fix: fixer => fixer.replaceText(node, toConstantCase(name))
681
1098
  }]
682
1099
  });
@@ -694,7 +1111,7 @@
694
1111
  node,
695
1112
  message: useConstantCaseFor(exported.name),
696
1113
  suggest: [{
697
- desc: `Replace ${getCode(context, node)} with ${toConstantCase(exported.name)}`,
1114
+ desc: `Replace ${getCode$1(context, node)} with ${toConstantCase(exported.name)}`,
698
1115
  fix: fixer => {
699
1116
  const newName = toConstantCase(exported.name);
700
1117
 
@@ -773,26 +1190,26 @@
773
1190
 
774
1191
  const ASSIGNMENT_PATTERNS = [{
775
1192
  id: {
776
- type: "ObjectPattern"
1193
+ type: TYPES.OBJECT_PATTERN
777
1194
  }
778
1195
  }, {
779
1196
  id: {
780
- type: "Identifier"
1197
+ type: TYPES.IDENTIFIER
781
1198
  }
782
1199
  }];
783
1200
  const IDENTIFIER_INITIALIZATION_PATTERN = {
784
- type: "Identifier",
1201
+ type: TYPES.IDENTIFIER,
785
1202
  name: "globalProps"
786
1203
  };
787
1204
  const MEMBER_EXPRESSION_INITIALIZATION_PATTERN = {
788
- type: "MemberExpression",
1205
+ type: TYPES.MEMBER_EXPRESSION,
789
1206
  object: {
790
- type: "Identifier",
1207
+ type: TYPES.IDENTIFIER,
791
1208
  name: "globalProps"
792
1209
  }
793
1210
  };
794
1211
  const GLOBALPROPS_REASSIGNMENT_PATTERN = {
795
- type: "Identifier",
1212
+ type: TYPES.IDENTIFIER,
796
1213
  name: "globalProps"
797
1214
  };
798
1215
 
@@ -830,7 +1247,7 @@
830
1247
  },
831
1248
  create: context => ({
832
1249
  AssignmentExpression(node) {
833
- if (patternExistsDeep(node.left, GLOBALPROPS_REASSIGNMENT_PATTERN)) {
1250
+ if (patternExistsDeep(GLOBALPROPS_REASSIGNMENT_PATTERN, node.left)) {
834
1251
  context.report(node, NO_GLOBALPROPS_REASSIGNMENT_MESSAGE);
835
1252
  }
836
1253
  }
@@ -928,6 +1345,13 @@
928
1345
  }
929
1346
  }
930
1347
  };
1348
+ const ARRAY_LENGTH_PATTERN$1 = {
1349
+ type: TYPES.MEMBER_EXPRESSION,
1350
+ property: {
1351
+ type: TYPES.IDENTIFIER,
1352
+ name: "length"
1353
+ }
1354
+ };
931
1355
  const OBJ_KEY_LENGTH_PATTERN$1 = {
932
1356
  type: TYPES.MEMBER_EXPRESSION,
933
1357
  object: {
@@ -937,11 +1361,11 @@
937
1361
  object: {
938
1362
  type: TYPES.IDENTIFIER,
939
1363
  name: "Object"
1364
+ },
1365
+ property: {
1366
+ type: TYPES.IDENTIFIER,
1367
+ name: "keys"
940
1368
  }
941
- },
942
- property: {
943
- type: TYPES.IDENTIFIER,
944
- name: "keys"
945
1369
  }
946
1370
  },
947
1371
  property: {
@@ -949,13 +1373,6 @@
949
1373
  name: "length"
950
1374
  }
951
1375
  };
952
- const ARRAY_LENGTH_PATTERN$1 = {
953
- type: TYPES.MEMBER_EXPRESSION,
954
- property: {
955
- type: TYPES.IDENTIFIER,
956
- name: "length"
957
- }
958
- };
959
1376
  const NOOP_PATTERN = {
960
1377
  generator: false,
961
1378
  async: false,
@@ -967,35 +1384,35 @@
967
1384
  };
968
1385
  const LABEL_AND_VALUE_PATTERN = {
969
1386
  properties: props => props.length === 2 && props.every(matches({
970
- type: "Property",
1387
+ type: TYPES.PROPERTY,
971
1388
  computed: false,
972
1389
  key: {
973
- type: "Identifier"
1390
+ type: TYPES.IDENTIFIER
974
1391
  },
975
1392
  value: {
976
- type: ramda.includes(ramda.__, ["Literal", "Identifier", "MemberExpression"])
1393
+ type: ramda.includes(ramda.__, [TYPES.LITERAL, TYPES.IDENTIFIER, TYPES.MEMBER_EXPRESSION])
977
1394
  }
978
1395
  })) && ramda.equals(["label", "value"], props.map(ramda.path(["key", "name"])).sort())
979
1396
  };
980
1397
  const DYNAMIC_ARRAY_PATTERN = {
981
1398
  callee: {
982
- type: "MemberExpression",
1399
+ type: TYPES.MEMBER_EXPRESSION,
983
1400
  object: {
984
- type: "Identifier",
1401
+ type: TYPES.IDENTIFIER,
985
1402
  name: "Array"
986
1403
  },
987
1404
  property: {
988
- type: "Identifier",
1405
+ type: TYPES.IDENTIFIER,
989
1406
  name: "from"
990
1407
  }
991
1408
  },
992
1409
  arguments: args => args.length === 2 && matches({
993
1410
  0: {
994
- type: "ObjectExpression",
1411
+ type: TYPES.OBJECT_EXPRESSION,
995
1412
  properties: props => props.length === 1 && props[0].key.name === "length"
996
1413
  },
997
1414
  1: {
998
- type: "ArrowFunctionExpression"
1415
+ type: TYPES.ARROW_FUNCTION_EXPRESSION
999
1416
  }
1000
1417
  }, args)
1001
1418
  };
@@ -1004,7 +1421,11 @@
1004
1421
  type: TYPES.LOGICAL_EXPRESSION,
1005
1422
  operator: "&&",
1006
1423
  left: NOT_NIL_BINARY_EXP_PATTERN,
1007
- right: NOT_NIL_BINARY_EXP_PATTERN
1424
+ right: (right, root) => {
1425
+ var _right$left, _root$left, _root$left$left;
1426
+
1427
+ return matches(NOT_NIL_BINARY_EXP_PATTERN, right) && ((_right$left = right.left) === null || _right$left === void 0 ? void 0 : _right$left.name) === ((_root$left = root.left) === null || _root$left === void 0 ? void 0 : (_root$left$left = _root$left.left) === null || _root$left$left === void 0 ? void 0 : _root$left$left.name);
1428
+ }
1008
1429
  }, // x => !isNil(x)
1009
1430
  {
1010
1431
  type: TYPES.ARROW_FUNCTION_EXPRESSION,
@@ -1019,7 +1440,14 @@
1019
1440
  type: TYPES.IDENTIFIER,
1020
1441
  name: "isNil"
1021
1442
  },
1022
- arguments: args => args.length === 1 && args[0].type === TYPES.IDENTIFIER
1443
+ arguments: (args, root) => args.length === 1 && matches([{
1444
+ type: TYPES.IDENTIFIER,
1445
+ name: name => {
1446
+ var _root$params$;
1447
+
1448
+ return name === ((_root$params$ = root.params[0]) === null || _root$params$ === void 0 ? void 0 : _root$params$.name);
1449
+ }
1450
+ }], args)
1023
1451
  }
1024
1452
  }
1025
1453
  }];
@@ -1063,18 +1491,18 @@
1063
1491
  };
1064
1492
  const REPLACE_BY_PATTERN = {
1065
1493
  callee: {
1066
- type: "MemberExpression",
1494
+ type: TYPES.MEMBER_EXPRESSION,
1067
1495
  property: {
1068
- type: "Identifier",
1496
+ type: TYPES.IDENTIFIER,
1069
1497
  name: "map"
1070
1498
  }
1071
1499
  },
1072
1500
  arguments: [{
1073
- type: "ArrowFunctionExpression",
1501
+ type: TYPES.ARROW_FUNCTION_EXPRESSION,
1074
1502
  generator: false,
1075
1503
  async: false,
1076
1504
  expression: true,
1077
- params: params => params.length === 1 && params[0].type === "Identifier",
1505
+ params: params => params.length === 1 && params[0].type === TYPES.IDENTIFIER,
1078
1506
  body: {
1079
1507
  type: "ConditionalExpression",
1080
1508
  test: (test, root) => areAllChecksWithParams(test, arrayFuncParam(root), {
@@ -1082,7 +1510,7 @@
1082
1510
  join: "&&"
1083
1511
  }),
1084
1512
  alternate: {
1085
- type: "Identifier",
1513
+ type: TYPES.IDENTIFIER,
1086
1514
  name: (name, root) => name === arrayFuncParam(root).name
1087
1515
  }
1088
1516
  }
@@ -1095,35 +1523,35 @@
1095
1523
  join: "||"
1096
1524
  }),
1097
1525
  consequent: {
1098
- type: "Identifier",
1526
+ type: TYPES.IDENTIFIER,
1099
1527
  name: (name, root) => name === arrayFuncParam(root).name
1100
1528
  }
1101
1529
  }, REPLACE_BY_PATTERN);
1102
1530
  const COPY_KEYS_PATTERN = {
1103
1531
  properties: props => props.length > 1 && matches({
1104
- type: "SpreadElement",
1532
+ type: TYPES.SPREAD_ELEMENT,
1105
1533
  argument: {
1106
- type: "Identifier"
1534
+ type: TYPES.IDENTIFIER
1107
1535
  }
1108
1536
  }, props[0]) && props.slice(1).every(matches({
1109
- type: "Property",
1537
+ type: TYPES.PROPERTY,
1110
1538
  kind: "init",
1111
1539
  method: false,
1112
1540
  computed: false,
1113
1541
  shorthand: false,
1114
1542
  key: {
1115
- type: "Identifier"
1543
+ type: TYPES.IDENTIFIER
1116
1544
  },
1117
1545
  value: {
1118
- type: "MemberExpression",
1546
+ type: TYPES.MEMBER_EXPRESSION,
1119
1547
  computed: false,
1120
1548
  optional: false,
1121
1549
  object: {
1122
- type: "Identifier",
1550
+ type: TYPES.IDENTIFIER,
1123
1551
  name: props[0].argument.name
1124
1552
  },
1125
1553
  property: {
1126
- type: "Identifier"
1554
+ type: TYPES.IDENTIFIER
1127
1555
  }
1128
1556
  }
1129
1557
  }))
@@ -1197,7 +1625,11 @@
1197
1625
  type: TYPES.MEMBER_EXPRESSION,
1198
1626
  object: {
1199
1627
  type: TYPES.IDENTIFIER,
1200
- name: (name, root) => name === root.right.callee.object.name
1628
+ name: (name, root) => {
1629
+ var _root$right$callee, _root$right$callee$ob;
1630
+
1631
+ return name === ((_root$right$callee = root.right.callee) === null || _root$right$callee === void 0 ? void 0 : (_root$right$callee$ob = _root$right$callee.object) === null || _root$right$callee$ob === void 0 ? void 0 : _root$right$callee$ob.name);
1632
+ }
1201
1633
  },
1202
1634
  property: {
1203
1635
  type: TYPES.IDENTIFIER,
@@ -1234,11 +1666,11 @@
1234
1666
  }
1235
1667
  };
1236
1668
  const COUNT_BY_PATTERN = {
1237
- type: "MemberExpression",
1669
+ type: TYPES.MEMBER_EXPRESSION,
1238
1670
  computed: false,
1239
1671
  optional: false,
1240
1672
  property: {
1241
- type: "Identifier",
1673
+ type: TYPES.IDENTIFIER,
1242
1674
  name: "length"
1243
1675
  }
1244
1676
  };
@@ -1276,9 +1708,9 @@
1276
1708
  let array = null;
1277
1709
 
1278
1710
  if (matches(genericPatterns.VANILLA, node)) {
1279
- array = getCode(context, node.callee.object);
1711
+ array = getCode$1(context, node.callee.object);
1280
1712
  } else if (matches(genericPatterns.RAMDA, node)) {
1281
- array = getCode(context, node.arguments[1]);
1713
+ array = getCode$1(context, node.arguments[1]);
1282
1714
  } else return; // arr.filter(x => x.prop === something).length
1283
1715
 
1284
1716
 
@@ -1302,7 +1734,7 @@
1302
1734
  }
1303
1735
 
1304
1736
  if (idUsagePattern) {
1305
- const id = matches(idUsagePattern, predicate.body.left) ? getCode(context, predicate.body.right) : getCode(context, predicate.body.left);
1737
+ const id = matches(idUsagePattern, predicate.body.left) ? getCode$1(context, predicate.body.right) : getCode$1(context, predicate.body.left);
1306
1738
  const fix = `${functions.byId}(${id}, ${array})`;
1307
1739
  reportSuggestion({
1308
1740
  context,
@@ -1454,7 +1886,7 @@
1454
1886
  operateOn: TYPES.BINARY_EXPRESSION,
1455
1887
  check: (context, node) => {
1456
1888
  if (!matches(CAPITALIZE_PATTERN, node)) return;
1457
- const fix = `capitalize(${getCode(context, node.right.callee.object)})`;
1889
+ const fix = `capitalize(${getCode$1(context, node.right.callee.object)})`;
1458
1890
  reportSuggestion({
1459
1891
  context,
1460
1892
  node,
@@ -1470,7 +1902,7 @@
1470
1902
  if (!matches(COPY_KEYS_PATTERN, node)) return;
1471
1903
  const keyValuePairs = node.properties.slice(1).map(prop => [prop.value.property.name, prop.key.name]);
1472
1904
  const keyMap = JSON.stringify(ramda.fromPairs(keyValuePairs));
1473
- const sourceObject = getCode(context, node.properties[0].argument);
1905
+ const sourceObject = getCode$1(context, node.properties[0].argument);
1474
1906
  const fix = `copyKeys(${keyMap}, ${sourceObject})`;
1475
1907
  reportSuggestion({
1476
1908
  context,
@@ -1482,14 +1914,14 @@
1482
1914
  };
1483
1915
 
1484
1916
  var dynamicArray = {
1485
- operateOn: "CallExpression",
1917
+ operateOn: TYPES.CALL_EXPRESSION,
1486
1918
  check: (context, node) => {
1487
1919
  var _node$arguments$1$par;
1488
1920
 
1489
1921
  if (!matches(DYNAMIC_ARRAY_PATTERN, node)) return;
1490
- const length = getCode(context, node.arguments[0].properties[0].value);
1922
+ const length = getCode$1(context, node.arguments[0].properties[0].value);
1491
1923
  const elementGenIndexParam = (_node$arguments$1$par = node.arguments[1].params[1]) === null || _node$arguments$1$par === void 0 ? void 0 : _node$arguments$1$par.name;
1492
- const elementGenBody = getCode(context, node.arguments[1].body);
1924
+ const elementGenBody = getCode$1(context, node.arguments[1].body);
1493
1925
  const fix = `dynamicArray(${length}, ${elementGenIndexParam || "()"} => ${elementGenBody})`;
1494
1926
  reportSuggestion({
1495
1927
  context,
@@ -1505,13 +1937,11 @@
1505
1937
  check: (context, node) => {
1506
1938
  let fix = null;
1507
1939
 
1508
- if (node.type === TYPES.UNARY_EXPRESSION) {
1509
- if (!matches(NOT_EQUALS_PATTERNS.UNARY_EXP, node)) return;
1510
- const first = getCode(context, node.argument.arguments[0]);
1511
- const second = getCode(context, node.argument.arguments[1]);
1940
+ if (matches(NOT_EQUALS_PATTERNS.UNARY_EXP, node)) {
1941
+ const first = getCode$1(context, node.argument.arguments[0]);
1942
+ const second = getCode$1(context, node.argument.arguments[1]);
1512
1943
  fix = `isNot(${first}, ${second})`;
1513
- } else {
1514
- if (!matches(NOT_EQUALS_PATTERNS.ARROW_FUNC, node)) return;
1944
+ } else if (matches(NOT_EQUALS_PATTERNS.ARROW_FUNC, node)) {
1515
1945
  const argumentName = node.params[0].name;
1516
1946
  let externalValueNode = null;
1517
1947
 
@@ -1521,9 +1951,9 @@
1521
1951
  externalValueNode = node.body.left;
1522
1952
  } else return;
1523
1953
 
1524
- const externalValue = getCode(context, externalValueNode);
1954
+ const externalValue = getCode$1(context, externalValueNode);
1525
1955
  fix = `isNot(${externalValue})`;
1526
- }
1956
+ } else return;
1527
1957
 
1528
1958
  reportSuggestion({
1529
1959
  context,
@@ -1537,32 +1967,27 @@
1537
1967
  var isNotEmpty = {
1538
1968
  operateOn: [TYPES.IF_STATEMENT, TYPES.WHILE_STATEMENT, TYPES.BINARY_EXPRESSION, TYPES.ARROW_FUNCTION_EXPRESSION],
1539
1969
  check: (context, node) => {
1540
- const isBinaryExp = node.type === TYPES.BINARY_EXPRESSION;
1541
- const isArrowFunc = node.type === TYPES.ARROW_FUNCTION_EXPRESSION;
1542
- const isCondStat = node.type === TYPES.IF_STATEMENT || node.type === TYPES.WHILE_STATEMENT;
1543
- if (isBinaryExp && !matches(NOT_EMPTY_PATTERNS.BINARY_EXP, node)) return;
1544
- if (isArrowFunc && !matches(NOT_EMPTY_PATTERNS.ARROW_FUNC, node)) return;
1545
- if (isCondStat && !matches(NOT_EMPTY_PATTERNS.CONDT_STAT, node)) return;
1546
1970
  let fix = null;
1547
1971
  let message = "";
1548
1972
  let nodeToBeReplaced = node;
1549
1973
 
1550
- if (isArrowFunc) {
1974
+ if (matches(NOT_EMPTY_PATTERNS.ARROW_FUNC, node)) {
1551
1975
  fix = "isNotEmpty";
1552
- } else {
1553
- const object = isBinaryExp ? node.left.object : node.test.object;
1554
- const isObjLenCheck = object.type === TYPES.CALL_EXPRESSION;
1976
+ } else if (matchesAny([NOT_EMPTY_PATTERNS.BINARY_EXP, NOT_EMPTY_PATTERNS.CONDT_STAT], node)) {
1977
+ const isBinaryExp = node.type === TYPES.BINARY_EXPRESSION;
1978
+ const expNode = isBinaryExp ? node.left : node.test;
1979
+ const isObjLenCheck = matches(OBJ_KEY_LENGTH_PATTERN$1, expNode);
1555
1980
  nodeToBeReplaced = isBinaryExp ? node : node.test;
1556
1981
 
1557
1982
  if (isObjLenCheck) {
1558
- const objName = getCode(context, object.arguments[0]);
1983
+ const objName = getCode$1(context, expNode.object.arguments[0]);
1559
1984
  fix = `isNotEmpty(${objName})`;
1560
1985
  } else {
1561
- const arrName = getCode(context, object);
1986
+ const arrName = getCode$1(context, expNode.object);
1562
1987
  fix = `isNotEmpty(${arrName})`;
1563
1988
  message = `Can be replaced with: ${fix}, if '${arrName}' is an array or string.`;
1564
1989
  }
1565
- }
1990
+ } else return;
1566
1991
 
1567
1992
  reportSuggestion({
1568
1993
  context,
@@ -1579,13 +2004,11 @@
1579
2004
  check: (context, node) => {
1580
2005
  let fix = null;
1581
2006
 
1582
- if (node.type === TYPES.BINARY_EXPRESSION) {
1583
- if (!matches(NOT_EQUALS_DEEP_PATTERNS.BINARY_EXP, node)) return;
1584
- const first = getCode(context, node.left.arguments[0]);
1585
- const second = getCode(context, node.right.arguments[0]);
2007
+ if (matches(NOT_EQUALS_DEEP_PATTERNS.BINARY_EXP, node)) {
2008
+ const first = getCode$1(context, node.left.arguments[0]);
2009
+ const second = getCode$1(context, node.right.arguments[0]);
1586
2010
  fix = `isNotEqualDeep(${first}, ${second})`;
1587
- } else {
1588
- if (!matches(NOT_EQUALS_DEEP_PATTERNS.ARROW_FUNC, node)) return;
2011
+ } else if (matches(NOT_EQUALS_DEEP_PATTERNS.ARROW_FUNC, node)) {
1589
2012
  const argumentName = node.params[0].name;
1590
2013
  let externalValueNode = null;
1591
2014
 
@@ -1595,9 +2018,9 @@
1595
2018
  externalValueNode = node.body.left.arguments[0];
1596
2019
  } else return;
1597
2020
 
1598
- const externalValue = getCode(context, externalValueNode);
2021
+ const externalValue = getCode$1(context, externalValueNode);
1599
2022
  fix = `isNotEqualDeep(${externalValue})`;
1600
- }
2023
+ } else return;
1601
2024
 
1602
2025
  reportSuggestion({
1603
2026
  context,
@@ -1615,10 +2038,8 @@
1615
2038
  let fix = null;
1616
2039
 
1617
2040
  if (node.type === TYPES.ARROW_FUNCTION_EXPRESSION) {
1618
- if (node.params[0].name !== node.body.argument.arguments[0].name) return;
1619
2041
  fix = `isNotNil`;
1620
2042
  } else if (node.type === TYPES.LOGICAL_EXPRESSION) {
1621
- if (node.left.left.name !== node.right.left.name) return;
1622
2043
  fix = `isNotNil(${node.left.left.name})`;
1623
2044
  }
1624
2045
 
@@ -1632,7 +2053,7 @@
1632
2053
  };
1633
2054
 
1634
2055
  var noop = {
1635
- operateOn: ["FunctionExpression", "ArrowFunctionExpression", "FunctionDeclaration"],
2056
+ operateOn: [TYPES.FUNCTION_EXPRESSION, TYPES.ARROW_FUNCTION_EXPRESSION, TYPES.FUNCTION_DECLARATION],
1636
2057
  check: (context, node) => {
1637
2058
  if (matches(NOOP_PATTERN, node)) {
1638
2059
  reportSuggestion({
@@ -1705,9 +2126,9 @@
1705
2126
  externalId = comparison.left;
1706
2127
  } else return;
1707
2128
 
1708
- const id = getCode(context, externalId);
1709
- const replacement = getCode(context, replaceNode);
1710
- const array = getCode(context, node.callee.object);
2129
+ const id = getCode$1(context, externalId);
2130
+ const replacement = getCode$1(context, replaceNode);
2131
+ const array = getCode$1(context, node.callee.object);
1711
2132
  const fix = `replaceById(${id}, ${replacement}, ${array})`;
1712
2133
  reportSuggestion({
1713
2134
  context,
@@ -1719,10 +2140,10 @@
1719
2140
  };
1720
2141
 
1721
2142
  var toLabelAndValue = {
1722
- operateOn: "ObjectExpression",
2143
+ operateOn: TYPES.OBJECT_EXPRESSION,
1723
2144
  check: (context, node) => {
1724
2145
  if (!matches(LABEL_AND_VALUE_PATTERN, node)) return;
1725
- const [label, value] = ramda.pluck("value", node.properties).map(node => getCode(context, node)); // order doesn't matter here
2146
+ const [label, value] = ramda.pluck("value", node.properties).map(node => getCode$1(context, node)); // order doesn't matter here
1726
2147
 
1727
2148
  if (label !== value) return;
1728
2149
  const fix = `toLabelAndValue(${label})`;
@@ -1805,7 +2226,7 @@
1805
2226
  return isApiAction ? noOfParams > API_ACTIONS_ARGUMENT_LIMIT : noOfParams > ARGUMENT_LIMIT;
1806
2227
  };
1807
2228
 
1808
- const getParamList = (context, params) => params.map(param => getCode(context, param)).join(", ");
2229
+ const getParamList = (context, params) => params.map(param => getCode$1(context, param)).join(", ");
1809
2230
 
1810
2231
  const checkAndReport$2 = (context, params) => {
1811
2232
  const isApiAction = isInsideApisDirectory(context);
@@ -1857,22 +2278,22 @@
1857
2278
 
1858
2279
  const LOCALIZATION_CALL_PATTERNS = [{
1859
2280
  callee: {
1860
- type: "Identifier",
2281
+ type: TYPES.IDENTIFIER,
1861
2282
  name: "t"
1862
2283
  },
1863
2284
  arguments: [{
1864
- type: "Literal"
2285
+ type: TYPES.LITERAL
1865
2286
  }]
1866
2287
  }, {
1867
2288
  callee: {
1868
- type: "MemberExpression",
2289
+ type: TYPES.MEMBER_EXPRESSION,
1869
2290
  property: {
1870
- type: "Identifier",
2291
+ type: TYPES.IDENTIFIER,
1871
2292
  name: "t"
1872
2293
  }
1873
2294
  },
1874
2295
  arguments: [{
1875
- type: "Literal"
2296
+ type: TYPES.LITERAL
1876
2297
  }]
1877
2298
  }];
1878
2299
 
@@ -2010,20 +2431,20 @@
2010
2431
  specifiers: [{
2011
2432
  type: "ImportNamespaceSpecifier",
2012
2433
  local: {
2013
- type: "Identifier"
2434
+ type: TYPES.IDENTIFIER
2014
2435
  }
2015
2436
  }],
2016
2437
  source: {
2017
- type: "Literal",
2438
+ type: TYPES.LITERAL,
2018
2439
  value: "ramda"
2019
2440
  }
2020
2441
  };
2021
2442
 
2022
2443
  const ramdaUsage = identifierName => ({
2023
- type: "MemberExpression",
2444
+ type: TYPES.MEMBER_EXPRESSION,
2024
2445
  object: {
2025
2446
  name: identifierName,
2026
- type: "Identifier"
2447
+ type: TYPES.IDENTIFIER
2027
2448
  }
2028
2449
  });
2029
2450
 
@@ -2065,8 +2486,12 @@
2065
2486
  };
2066
2487
 
2067
2488
  const SPECIAL_CHARS_PATTERN = /^ +| +$|[\r\n\t\f\v&<>{}"'`\\]+/;
2068
-
2069
- const isReplaceableLiteral = node => node.expression.type === TYPES.LITERAL && typeof node.expression.value === "string" && !SPECIAL_CHARS_PATTERN.test(node.expression.value);
2489
+ const REPLACEABLE_LITERAL_PATTERN = {
2490
+ expression: {
2491
+ type: TYPES.LITERAL,
2492
+ value: value => typeof value === "string" && !SPECIAL_CHARS_PATTERN.test(value)
2493
+ }
2494
+ };
2070
2495
 
2071
2496
  var noRedundantJsxExpression = {
2072
2497
  meta: {
@@ -2078,7 +2503,7 @@
2078
2503
  },
2079
2504
  create: context => ({
2080
2505
  JSXExpressionContainer(node) {
2081
- if (!isReplaceableLiteral(node)) return;
2506
+ if (!matches(REPLACEABLE_LITERAL_PATTERN, node)) return;
2082
2507
  let fix = "";
2083
2508
 
2084
2509
  if (node.parent.type === TYPES.JSX_ELEMENT) {
@@ -2103,26 +2528,58 @@
2103
2528
  const ENDS_WITH_SPACES_PATTERN = / +$/;
2104
2529
  const STARTS_OR_ENDS_WITH_SPACES_PATTERN = /^ +| +$/;
2105
2530
  const STARTS_WITH_SPACES_PATTERN = /^ +/;
2531
+ const QUASI_WITH_SPECIAL_CHARACTERS_PATTERN = {
2532
+ value: {
2533
+ cooked: cooked => cooked.match(CHARACTERS_TO_IGNORE_PATTERN)
2534
+ }
2535
+ };
2536
+ const EXPRESSION_ONLY_PATTERN = {
2537
+ expressions: exps => exps.length === 1,
2538
+ quasis: quasis => quasis.length === 2 && matches([{
2539
+ value: {
2540
+ cooked: ""
2541
+ }
2542
+ }, {
2543
+ value: {
2544
+ cooked: ""
2545
+ }
2546
+ }], quasis)
2547
+ };
2548
+ const INSIDE_JSX_ELEMENT_PATTERN = {
2549
+ parent: {
2550
+ type: TYPES.JSX_EXPRESSION_CONTAINER,
2551
+ parent: {
2552
+ type: TYPES.JSX_ELEMENT
2553
+ }
2554
+ },
2555
+ quasis: quasis => {
2556
+ var _quasis$, _last;
2557
+
2558
+ return !((_quasis$ = quasis[0]) !== null && _quasis$ !== void 0 && _quasis$.value.cooked.match(STARTS_WITH_SPACES_PATTERN)) && !((_last = ramda.last(quasis)) !== null && _last !== void 0 && _last.value.cooked.match(ENDS_WITH_SPACES_PATTERN));
2559
+ }
2560
+ };
2561
+ const REPLACEABLE_QUASIS_ONLY_PATTERN = {
2562
+ expressions: exps => exps.length === 0,
2563
+ quasis: quasis => quasis.length === 1 && matches([{
2564
+ value: {
2565
+ cooked: cooked => !cooked.match(STARTS_OR_ENDS_WITH_SPACES_PATTERN)
2566
+ }
2567
+ }], quasis)
2568
+ };
2106
2569
 
2107
2570
  const doQusisHaveSpecialCharacters = ({
2108
2571
  quasis
2109
- }) => quasis.some(quasi => quasi.value.cooked.match(CHARACTERS_TO_IGNORE_PATTERN));
2572
+ }) => quasis.some(matches(QUASI_WITH_SPECIAL_CHARACTERS_PATTERN));
2110
2573
 
2111
2574
  const getTemplateContent = (context, {
2112
2575
  expressions,
2113
2576
  quasis
2114
2577
  }) => expressions.reduce((acc, expression, index) => {
2115
- const expressionCode = getCode(context, expression);
2578
+ const expressionCode = getCode$1(context, expression);
2116
2579
  const quasi = quasis[index + 1].value.cooked;
2117
2580
  return `${acc}{${expressionCode}}${quasi}`;
2118
2581
  }, `${quasis[0].value.cooked}`);
2119
2582
 
2120
- const isExpressionOnly = node => node.expressions.length === 1 && node.quasis.length === 2 && node.quasis[0].value.cooked === "" && node.quasis[1].value.cooked === "";
2121
-
2122
- const isInsideJSXElement = node => node.parent.type === TYPES.JSX_EXPRESSION_CONTAINER && node.parent.parent.type === TYPES.JSX_ELEMENT && !node.quasis[0].value.cooked.match(STARTS_WITH_SPACES_PATTERN) && !ramda.last(node.quasis).value.cooked.match(ENDS_WITH_SPACES_PATTERN);
2123
-
2124
- const isReplaceableQuasiOnly = node => node.expressions.length === 0 && node.quasis.length === 1 && !node.quasis[0].value.cooked.match(STARTS_OR_ENDS_WITH_SPACES_PATTERN);
2125
-
2126
2583
  var noRedundantStringTemplates = {
2127
2584
  meta: {
2128
2585
  type: "problem",
@@ -2135,21 +2592,21 @@
2135
2592
  TemplateLiteral(node) {
2136
2593
  if (doQusisHaveSpecialCharacters(node)) return;
2137
2594
 
2138
- if (isExpressionOnly(node)) {
2139
- const fix = getCode(context, node.expressions[0]);
2595
+ if (matches(EXPRESSION_ONLY_PATTERN, node)) {
2596
+ const fix = getCode$1(context, node.expressions[0]);
2140
2597
  context.report({
2141
2598
  node,
2142
2599
  message: canBeReplacedWith(fix),
2143
2600
  fix: fixer => fixer.replaceText(node, fix)
2144
2601
  });
2145
- } else if (isInsideJSXElement(node)) {
2602
+ } else if (matches(INSIDE_JSX_ELEMENT_PATTERN, node)) {
2146
2603
  const fix = getTemplateContent(context, node);
2147
2604
  context.report({
2148
2605
  node,
2149
2606
  message: canBeReplacedWith(fix),
2150
2607
  fix: fixer => fixer.replaceText(node.parent, fix)
2151
2608
  });
2152
- } else if (isReplaceableQuasiOnly(node)) {
2609
+ } else if (matches(REPLACEABLE_QUASIS_ONLY_PATTERN, node)) {
2153
2610
  const fix = `"${node.quasis[0].value.cooked}"`;
2154
2611
  context.report({
2155
2612
  node,
@@ -2207,6 +2664,19 @@
2207
2664
  name: "undefined"
2208
2665
  }])
2209
2666
  };
2667
+ const ARRAY_LENGTH_PATTERN = {
2668
+ type: TYPES.MEMBER_EXPRESSION,
2669
+ property: {
2670
+ type: TYPES.IDENTIFIER,
2671
+ name: "length"
2672
+ }
2673
+ };
2674
+ const ARRAY_PATTERN = {
2675
+ body: {
2676
+ type: TYPES.ARRAY_EXPRESSION,
2677
+ elements: elmts => elmts.length === 2
2678
+ }
2679
+ };
2210
2680
  const OBJ_KEY_LENGTH_PATTERN = {
2211
2681
  type: TYPES.MEMBER_EXPRESSION,
2212
2682
  object: {
@@ -2216,11 +2686,11 @@
2216
2686
  object: {
2217
2687
  type: TYPES.IDENTIFIER,
2218
2688
  name: "Object"
2689
+ },
2690
+ property: {
2691
+ type: TYPES.IDENTIFIER,
2692
+ name: "keys"
2219
2693
  }
2220
- },
2221
- property: {
2222
- type: TYPES.IDENTIFIER,
2223
- name: "keys"
2224
2694
  }
2225
2695
  },
2226
2696
  property: {
@@ -2228,107 +2698,122 @@
2228
2698
  name: "length"
2229
2699
  }
2230
2700
  };
2231
- const ARRAY_LENGTH_PATTERN = {
2232
- type: TYPES.MEMBER_EXPRESSION,
2233
- property: {
2234
- type: TYPES.IDENTIFIER,
2235
- name: "length"
2236
- }
2237
- };
2238
2701
  const ARRAY_INSERTS_PATTERN = {
2239
2702
  APPEND: {
2240
2703
  // x => [...x, y]
2241
2704
  params: [{
2242
- type: "Identifier"
2705
+ type: TYPES.IDENTIFIER
2243
2706
  }],
2244
2707
  body: {
2245
- type: "ArrayExpression",
2708
+ type: TYPES.ARRAY_EXPRESSION,
2246
2709
  elements: [{
2247
- type: "SpreadElement",
2710
+ type: TYPES.SPREAD_ELEMENT,
2248
2711
  argument: {
2249
- type: "Identifier"
2712
+ type: TYPES.IDENTIFIER,
2713
+ name: (name, root) => {
2714
+ var _root$params$;
2715
+
2716
+ return name === ((_root$params$ = root.params[0]) === null || _root$params$ === void 0 ? void 0 : _root$params$.name);
2717
+ }
2250
2718
  }
2719
+ }, {
2720
+ type: notEquals(TYPES.SPREAD_ELEMENT)
2251
2721
  }]
2252
2722
  }
2253
2723
  },
2254
2724
  PREPEND: {
2255
2725
  // x => [y, ...x]
2256
2726
  params: [{
2257
- type: "Identifier"
2727
+ type: TYPES.IDENTIFIER
2258
2728
  }],
2259
2729
  body: {
2260
- type: "ArrayExpression",
2261
- elements: {
2262
- 1: {
2263
- type: "SpreadElement",
2264
- argument: {
2265
- type: "Identifier"
2730
+ type: TYPES.ARRAY_EXPRESSION,
2731
+ elements: [{
2732
+ type: notEquals(TYPES.SPREAD_ELEMENT)
2733
+ }, {
2734
+ type: TYPES.SPREAD_ELEMENT,
2735
+ argument: {
2736
+ type: TYPES.IDENTIFIER,
2737
+ name: (name, root) => {
2738
+ var _root$params$2;
2739
+
2740
+ return name === ((_root$params$2 = root.params[0]) === null || _root$params$2 === void 0 ? void 0 : _root$params$2.name);
2266
2741
  }
2267
2742
  }
2268
- }
2743
+ }]
2269
2744
  }
2270
2745
  },
2271
2746
  CONCAT: {
2272
2747
  // x => [...x, ...y]
2273
2748
  params: [{
2274
- type: "Identifier"
2749
+ type: TYPES.IDENTIFIER
2275
2750
  }],
2276
2751
  body: {
2277
- type: "ArrayExpression",
2752
+ type: TYPES.ARRAY_EXPRESSION,
2278
2753
  elements: [{
2279
- type: "SpreadElement",
2754
+ type: TYPES.SPREAD_ELEMENT,
2280
2755
  argument: {
2281
- type: "Identifier"
2756
+ type: TYPES.IDENTIFIER,
2757
+ name: (name, root) => {
2758
+ var _root$params$3;
2759
+
2760
+ return name === ((_root$params$3 = root.params[0]) === null || _root$params$3 === void 0 ? void 0 : _root$params$3.name);
2761
+ }
2282
2762
  }
2283
2763
  }, {
2284
- type: "SpreadElement"
2764
+ type: TYPES.SPREAD_ELEMENT
2285
2765
  }]
2286
2766
  }
2287
2767
  }
2288
2768
  };
2289
2769
  const ASSOC_PATTERN = {
2290
2770
  // x => ({ ...x, y: 10 })
2291
- params: [{
2292
- type: "Identifier"
2293
- }],
2771
+ params: params => params.length === 1 && matches([{
2772
+ type: TYPES.IDENTIFIER
2773
+ }], params),
2294
2774
  body: {
2295
- type: "ObjectExpression",
2296
- properties: [{
2297
- type: "SpreadElement",
2298
- argument: {
2299
- type: "Identifier"
2300
- }
2301
- }, {
2302
- type: "Property"
2303
- }]
2775
+ type: TYPES.OBJECT_EXPRESSION,
2776
+ properties: (props, root) => {
2777
+ var _root$params$4;
2778
+
2779
+ return props.length === 2 && matches([{
2780
+ type: TYPES.SPREAD_ELEMENT,
2781
+ argument: {
2782
+ type: TYPES.IDENTIFIER,
2783
+ name: (_root$params$4 = root.params[0]) === null || _root$params$4 === void 0 ? void 0 : _root$params$4.name
2784
+ }
2785
+ }, {
2786
+ type: TYPES.PROPERTY
2787
+ }], props);
2788
+ }
2304
2789
  }
2305
2790
  };
2306
2791
  const JSON_EQUALS_PATTERN = {
2307
2792
  // JSON.stringify(x) === JSON.stringify(y)
2308
2793
  left: {
2309
- type: "CallExpression",
2794
+ type: TYPES.CALL_EXPRESSION,
2310
2795
  callee: {
2311
- type: "MemberExpression",
2796
+ type: TYPES.MEMBER_EXPRESSION,
2312
2797
  object: {
2313
- type: "Identifier",
2798
+ type: TYPES.IDENTIFIER,
2314
2799
  name: "JSON"
2315
2800
  },
2316
2801
  property: {
2317
- type: "Identifier",
2802
+ type: TYPES.IDENTIFIER,
2318
2803
  name: "stringify"
2319
2804
  }
2320
2805
  }
2321
2806
  },
2322
2807
  right: {
2323
- type: "CallExpression",
2808
+ type: TYPES.CALL_EXPRESSION,
2324
2809
  callee: {
2325
- type: "MemberExpression",
2810
+ type: TYPES.MEMBER_EXPRESSION,
2326
2811
  object: {
2327
- type: "Identifier",
2812
+ type: TYPES.IDENTIFIER,
2328
2813
  name: "JSON"
2329
2814
  },
2330
2815
  property: {
2331
- type: "Identifier",
2816
+ type: TYPES.IDENTIFIER,
2332
2817
  name: "stringify"
2333
2818
  }
2334
2819
  }
@@ -2341,108 +2826,138 @@
2341
2826
  async: false,
2342
2827
  expression: true,
2343
2828
  params: [{
2344
- type: "Identifier"
2829
+ type: TYPES.IDENTIFIER
2345
2830
  }],
2346
2831
  body: {
2347
- type: "Identifier"
2832
+ type: TYPES.IDENTIFIER,
2833
+ name: (name, root) => {
2834
+ var _root$params$5;
2835
+
2836
+ return name === ((_root$params$5 = root.params[0]) === null || _root$params$5 === void 0 ? void 0 : _root$params$5.name);
2837
+ }
2348
2838
  }
2349
2839
  };
2350
2840
  const MERGE_PATTERNS = [{
2351
2841
  // x => ({ ...x, y: 10, z: 11 })
2352
- params: [{
2353
- type: "Identifier"
2354
- }],
2842
+ params: params => params.length === 1 && matches([{
2843
+ type: TYPES.IDENTIFIER
2844
+ }], params),
2355
2845
  body: {
2356
- type: "ObjectExpression",
2846
+ type: TYPES.OBJECT_EXPRESSION,
2357
2847
  properties: [{
2358
- type: "SpreadElement",
2848
+ type: TYPES.SPREAD_ELEMENT,
2359
2849
  argument: {
2360
- type: "Identifier"
2850
+ type: TYPES.IDENTIFIER,
2851
+ name: (name, root) => {
2852
+ var _root$params$6;
2853
+
2854
+ return name === ((_root$params$6 = root.params[0]) === null || _root$params$6 === void 0 ? void 0 : _root$params$6.name);
2855
+ }
2361
2856
  }
2362
2857
  }, {
2363
- type: "Property"
2858
+ type: TYPES.PROPERTY
2364
2859
  }, {
2365
- type: "Property"
2860
+ type: TYPES.PROPERTY
2366
2861
  }]
2367
2862
  }
2368
2863
  }, {
2369
2864
  // x => ({ ...x, y: 10, ...z })
2370
- params: [{
2371
- type: "Identifier"
2372
- }],
2865
+ params: params => params.length === 1 && matches([{
2866
+ type: TYPES.IDENTIFIER
2867
+ }], params),
2373
2868
  body: {
2374
- type: "ObjectExpression",
2869
+ type: TYPES.OBJECT_EXPRESSION,
2375
2870
  properties: [{
2376
- type: "SpreadElement",
2871
+ type: TYPES.SPREAD_ELEMENT,
2377
2872
  argument: {
2378
- type: "Identifier"
2873
+ type: TYPES.IDENTIFIER,
2874
+ name: (name, root) => {
2875
+ var _root$params$7;
2876
+
2877
+ return name === ((_root$params$7 = root.params[0]) === null || _root$params$7 === void 0 ? void 0 : _root$params$7.name);
2878
+ }
2379
2879
  }
2380
2880
  }, {
2381
- type: "Property"
2881
+ type: TYPES.PROPERTY
2382
2882
  }, {
2383
- type: "SpreadElement"
2883
+ type: TYPES.SPREAD_ELEMENT
2384
2884
  }]
2385
2885
  }
2386
2886
  }, {
2387
2887
  // x => ({ ...x, ...y })
2388
- params: [{
2389
- type: "Identifier"
2390
- }],
2888
+ params: params => params.length === 1 && matches([{
2889
+ type: TYPES.IDENTIFIER
2890
+ }], params),
2391
2891
  body: {
2392
- type: "ObjectExpression",
2892
+ type: TYPES.OBJECT_EXPRESSION,
2393
2893
  properties: [{
2394
- type: "SpreadElement",
2894
+ type: TYPES.SPREAD_ELEMENT,
2395
2895
  argument: {
2396
- type: "Identifier"
2896
+ type: TYPES.IDENTIFIER,
2897
+ name: (name, root) => {
2898
+ var _root$params$8;
2899
+
2900
+ return name === ((_root$params$8 = root.params[0]) === null || _root$params$8 === void 0 ? void 0 : _root$params$8.name);
2901
+ }
2397
2902
  }
2398
2903
  }, {
2399
- type: "SpreadElement"
2904
+ type: TYPES.SPREAD_ELEMENT
2400
2905
  }]
2401
2906
  }
2402
2907
  }];
2403
2908
  const PLUCK_PATTERNS = {
2404
2909
  OBJECT_ARROW_FUNCTION: {
2405
2910
  callee: {
2406
- type: "MemberExpression",
2911
+ type: TYPES.MEMBER_EXPRESSION,
2407
2912
  property: {
2408
2913
  name: "map"
2409
2914
  }
2410
2915
  },
2411
2916
  arguments: [{
2412
- type: "ArrowFunctionExpression"
2917
+ type: TYPES.ARROW_FUNCTION_EXPRESSION
2413
2918
  }]
2414
2919
  },
2415
2920
  IDENTIFIER: {
2416
2921
  // abc.map(x => x.a);
2417
2922
  params: [{
2418
- type: "Identifier"
2923
+ type: TYPES.IDENTIFIER
2419
2924
  }],
2420
2925
  body: {
2421
- type: "MemberExpression",
2926
+ type: TYPES.MEMBER_EXPRESSION,
2422
2927
  object: {
2423
- type: "Identifier"
2928
+ type: TYPES.IDENTIFIER,
2929
+ name: (name, root) => {
2930
+ var _root$params$9;
2931
+
2932
+ return name === ((_root$params$9 = root.params[0]) === null || _root$params$9 === void 0 ? void 0 : _root$params$9.name);
2933
+ }
2424
2934
  },
2425
2935
  property: {
2426
- type: "Identifier"
2936
+ type: TYPES.IDENTIFIER
2427
2937
  }
2428
2938
  }
2429
2939
  },
2430
2940
  DESTRUCTURE: {
2431
2941
  // abc.map(({ a }) => a);
2432
2942
  params: [{
2433
- type: "ObjectPattern",
2943
+ type: TYPES.OBJECT_PATTERN,
2434
2944
  properties: [{
2435
- type: "Property",
2945
+ type: TYPES.PROPERTY,
2436
2946
  key: {
2437
- type: "Identifier"
2947
+ type: TYPES.IDENTIFIER
2438
2948
  },
2439
2949
  value: {
2440
- type: "Identifier"
2950
+ type: TYPES.IDENTIFIER,
2951
+ name: (name, root) => {
2952
+ var _root$body;
2953
+
2954
+ return name === ((_root$body = root.body) === null || _root$body === void 0 ? void 0 : _root$body.name);
2955
+ }
2441
2956
  }
2442
2957
  }]
2443
2958
  }],
2444
2959
  body: {
2445
- type: "Identifier"
2960
+ type: TYPES.IDENTIFIER
2446
2961
  }
2447
2962
  }
2448
2963
  };
@@ -2453,16 +2968,21 @@
2453
2968
  async: false,
2454
2969
  expression: true,
2455
2970
  params: [{
2456
- type: "ObjectPattern",
2971
+ type: TYPES.OBJECT_PATTERN,
2457
2972
  properties: [{
2458
- type: "Property",
2973
+ type: TYPES.PROPERTY,
2459
2974
  value: {
2460
- type: "Identifier"
2975
+ type: TYPES.IDENTIFIER,
2976
+ name: (name, root) => {
2977
+ var _root$body2;
2978
+
2979
+ return name === ((_root$body2 = root.body) === null || _root$body2 === void 0 ? void 0 : _root$body2.name);
2980
+ }
2461
2981
  }
2462
2982
  }]
2463
2983
  }],
2464
2984
  body: {
2465
- type: "Identifier"
2985
+ type: TYPES.IDENTIFIER
2466
2986
  }
2467
2987
  },
2468
2988
  IDENTITY: {
@@ -2471,62 +2991,67 @@
2471
2991
  async: false,
2472
2992
  expression: true,
2473
2993
  params: [{
2474
- type: "Identifier"
2994
+ type: TYPES.IDENTIFIER
2475
2995
  }],
2476
2996
  body: {
2477
- type: "MemberExpression",
2997
+ type: TYPES.MEMBER_EXPRESSION,
2478
2998
  object: {
2479
- type: "Identifier"
2999
+ type: TYPES.IDENTIFIER,
3000
+ name: (name, root) => {
3001
+ var _root$params$10;
3002
+
3003
+ return name === ((_root$params$10 = root.params[0]) === null || _root$params$10 === void 0 ? void 0 : _root$params$10.name);
3004
+ }
2480
3005
  },
2481
3006
  property: {
2482
- type: "Identifier"
3007
+ type: TYPES.IDENTIFIER
2483
3008
  }
2484
3009
  }
2485
3010
  }
2486
3011
  };
2487
3012
  const SUM_PATTERN = {
2488
3013
  callee: {
2489
- type: "MemberExpression",
3014
+ type: TYPES.MEMBER_EXPRESSION,
2490
3015
  property: {
2491
- type: "Identifier",
3016
+ type: TYPES.IDENTIFIER,
2492
3017
  name: "reduce"
2493
3018
  }
2494
3019
  },
2495
3020
  arguments: [{
2496
- type: "ArrowFunctionExpression",
3021
+ type: TYPES.ARROW_FUNCTION_EXPRESSION,
2497
3022
  generator: false,
2498
3023
  async: false,
2499
3024
  expression: true,
2500
3025
  body: {
2501
- type: "BinaryExpression",
3026
+ type: TYPES.BINARY_EXPRESSION,
2502
3027
  operator: "+"
2503
3028
  },
2504
3029
  params: [{
2505
- type: "Identifier"
3030
+ type: TYPES.IDENTIFIER
2506
3031
  }]
2507
3032
  }, {
2508
- type: "Literal",
3033
+ type: TYPES.LITERAL,
2509
3034
  value: 0
2510
3035
  }]
2511
3036
  };
2512
3037
  const RAMDA_IMPORT_PATTERN = {
2513
- type: "ImportDeclaration",
3038
+ type: TYPES.IMPORT_DECLARATION,
2514
3039
  source: {
2515
- type: "Literal",
3040
+ type: TYPES.LITERAL,
2516
3041
  value: "ramda"
2517
3042
  },
2518
3043
  specifiers: [{
2519
- type: "ImportSpecifier"
3044
+ type: TYPES.IMPORT_SPECIFIER
2520
3045
  }]
2521
3046
  }; // when ({ name }) => name is used inside something.map(...), ignore it.
2522
3047
  // It will be picked up by pluck function
2523
3048
 
2524
3049
  const PROP_IGNORE_PATTERN = {
2525
- type: "CallExpression",
3050
+ type: TYPES.CALL_EXPRESSION,
2526
3051
  callee: {
2527
- type: "MemberExpression",
3052
+ type: TYPES.MEMBER_EXPRESSION,
2528
3053
  property: {
2529
- type: "Identifier",
3054
+ type: TYPES.IDENTIFIER,
2530
3055
  name: "map"
2531
3056
  }
2532
3057
  }
@@ -2535,7 +3060,11 @@
2535
3060
  {
2536
3061
  operator: "||",
2537
3062
  left: NIL_BINARY_EXP_PATTERN,
2538
- right: NIL_BINARY_EXP_PATTERN
3063
+ right: (right, root) => {
3064
+ var _right$left, _root$left, _root$left$left;
3065
+
3066
+ return matches(NIL_BINARY_EXP_PATTERN, right) && ((_right$left = right.left) === null || _right$left === void 0 ? void 0 : _right$left.name) === ((_root$left = root.left) === null || _root$left === void 0 ? void 0 : (_root$left$left = _root$left.left) === null || _root$left$left === void 0 ? void 0 : _root$left$left.name);
3067
+ }
2539
3068
  };
2540
3069
  const EMPTY_PATTERN = [// array.length === 0, Object.keys(obj).length === 0, if(array.length === 0), while(array.length === 0)
2541
3070
  {
@@ -2556,28 +3085,18 @@
2556
3085
  const replacer = nodeReplacerWithImports(RAMDA_IMPORT_PATTERN);
2557
3086
 
2558
3087
  var arrayInsertsLintCheck = {
2559
- operateOn: "ArrowFunctionExpression",
3088
+ operateOn: TYPES.ARROW_FUNCTION_EXPRESSION,
2560
3089
  check: (context, node) => {
2561
- const {
2562
- params,
2563
- body
2564
- } = node;
2565
-
2566
- if (body.type !== TYPES.ARRAY_EXPRESSION || body.elements.length !== 2) {
2567
- return;
2568
- }
2569
-
2570
- const {
2571
- elements
2572
- } = body;
3090
+ if (!matches(ARRAY_PATTERN, node)) return;
3091
+ const elements = node.body.elements;
2573
3092
  let fix = null;
2574
3093
 
2575
- if (matches(ARRAY_INSERTS_PATTERN.APPEND, node) && elements[1].type !== TYPES.SPREAD_ELEMENT && params[0].name === elements[0].argument.name) {
2576
- fix = `append(${getCode(context, elements[1])})`;
2577
- } else if (matches(ARRAY_INSERTS_PATTERN.PREPEND, node) && elements[0].type !== TYPES.SPREAD_ELEMENT && params[0].name === elements[1].argument.name) {
2578
- fix = `prepend(${getCode(context, elements[0])})`;
2579
- } else if (matches(ARRAY_INSERTS_PATTERN.CONCAT, node) && params[0].name === elements[0].argument.name) {
2580
- fix = `concat(${getCode(context, elements[1].argument)})`;
3094
+ if (matches(ARRAY_INSERTS_PATTERN.APPEND, node)) {
3095
+ fix = `append(${getCode$1(context, elements[1])})`;
3096
+ } else if (matches(ARRAY_INSERTS_PATTERN.PREPEND, node)) {
3097
+ fix = `prepend(${getCode$1(context, elements[0])})`;
3098
+ } else if (matches(ARRAY_INSERTS_PATTERN.CONCAT, node)) {
3099
+ fix = `concat(${getCode$1(context, elements[1].argument)})`;
2581
3100
  }
2582
3101
 
2583
3102
  if (!fix) return;
@@ -2600,23 +3119,20 @@
2600
3119
 
2601
3120
  const convertToAssoc = (context, node) => {
2602
3121
  const property = node.body.properties[1];
2603
- const key = property.computed ? getCode(context, property.key) : `"${property.key.name}"`;
2604
- return `assoc(${key}, ${getCode(context, property.value)})`;
3122
+ const key = property.computed ? getCode$1(context, property.key) : `"${property.key.name}"`;
3123
+ return `assoc(${key}, ${getCode$1(context, property.value)})`;
2605
3124
  };
2606
3125
 
2607
3126
  var assocLintCheck = {
2608
- operateOn: "ArrowFunctionExpression",
3127
+ operateOn: TYPES.ARROW_FUNCTION_EXPRESSION,
2609
3128
  check: (context, node) => {
2610
- if (node.params.length !== 1) return;
2611
3129
  if (!matches(ASSOC_PATTERN, node)) return;
2612
- if (node.body.properties.length !== 2) return;
2613
- if (node.body.properties[0].argument.name !== node.params[0].name) return;
2614
3130
  const property = node.body.properties[1];
2615
3131
  const selfReferencingPattern = {
2616
- type: "Identifier",
3132
+ type: TYPES.IDENTIFIER,
2617
3133
  name: node.params[0].name
2618
3134
  };
2619
- if (patternExistsDeep(property.value, selfReferencingPattern)) return;
3135
+ if (patternExistsDeep(selfReferencingPattern, property.value)) return;
2620
3136
  const fix = convertToAssoc(context, node);
2621
3137
  context.report({
2622
3138
  node,
@@ -2635,10 +3151,10 @@
2635
3151
  };
2636
3152
 
2637
3153
  var equalsLintCheck = {
2638
- operateOn: "BinaryExpression",
3154
+ operateOn: TYPES.BINARY_EXPRESSION,
2639
3155
  check: (context, node) => {
2640
3156
  if (matches(JSON_EQUALS_PATTERN, node)) {
2641
- const fix = `equals(${getCode(context, node.left.arguments[0])}, ${getCode(context, node.right.arguments[0])})`;
3157
+ const fix = `equals(${getCode$1(context, node.left.arguments[0])}, ${getCode$1(context, node.right.arguments[0])})`;
2642
3158
  context.report({
2643
3159
  node,
2644
3160
  message: canBeReplacedWith(fix),
@@ -2657,9 +3173,9 @@
2657
3173
  };
2658
3174
 
2659
3175
  var identityLintCheck = {
2660
- operateOn: "ArrowFunctionExpression",
3176
+ operateOn: TYPES.ARROW_FUNCTION_EXPRESSION,
2661
3177
  check: (context, node) => {
2662
- if (matches(IDENTITY_PATTERN, node) && node.params[0].name === node.body.name) {
3178
+ if (matches(IDENTITY_PATTERN, node)) {
2663
3179
  context.report({
2664
3180
  node,
2665
3181
  message: canBeReplacedWith("identity"),
@@ -2684,14 +3200,14 @@
2684
3200
  let fix = null;
2685
3201
  let message = "";
2686
3202
  const isBinaryExp = node.type === TYPES.BINARY_EXPRESSION;
2687
- const object = isBinaryExp ? node.left.object : node.argument.object;
2688
- const isObjLenCheck = object.type === TYPES.CALL_EXPRESSION;
3203
+ const expNode = isBinaryExp ? node.left : node.argument;
3204
+ const isObjLenCheck = matches(OBJ_KEY_LENGTH_PATTERN, expNode);
2689
3205
 
2690
3206
  if (isObjLenCheck) {
2691
- const objName = getCode(context, object.arguments[0]);
3207
+ const objName = getCode$1(context, expNode.object.arguments[0]);
2692
3208
  fix = `isEmpty(${objName})`;
2693
3209
  } else {
2694
- const arrName = getCode(context, object);
3210
+ const arrName = getCode$1(context, expNode.object);
2695
3211
  fix = `isEmpty(${arrName})`;
2696
3212
  message = `Can be replaced with: ${fix}, if '${arrName}' is an array or string.`;
2697
3213
  }
@@ -2710,7 +3226,6 @@
2710
3226
  operateOn: TYPES.LOGICAL_EXPRESSION,
2711
3227
  check: (context, node) => {
2712
3228
  if (!matches(NIL_PATTERN, node)) return;
2713
- if (node.left.left.name !== node.right.left.name) return;
2714
3229
  const fix = `isNil(${node.left.left.name})`;
2715
3230
  reportSuggestion({
2716
3231
  context,
@@ -2726,24 +3241,22 @@
2726
3241
  const convertToMergeLeft = (context, node) => {
2727
3242
  const properties = node.body.properties.slice(1);
2728
3243
  const matchIndex = findPatternIndex(node);
2729
- return matchIndex === 2 && properties.length === 1 ? `mergeLeft(${getCode(context, properties[0].argument)})` : `mergeLeft({ ${getCode(context, {
3244
+ return matchIndex === 2 && properties.length === 1 ? `mergeLeft(${getCode$1(context, properties[0].argument)})` : `mergeLeft({ ${getCode$1(context, {
2730
3245
  range: [properties[0].range[0], ramda.last(properties).range[1]]
2731
3246
  })} })`;
2732
3247
  };
2733
3248
 
2734
3249
  var mergeLintCheck = {
2735
- operateOn: "ArrowFunctionExpression",
3250
+ operateOn: TYPES.ARROW_FUNCTION_EXPRESSION,
2736
3251
  check: (context, node) => {
2737
- if (node.params.length !== 1) return;
2738
3252
  const matchIndex = findPatternIndex(node);
2739
3253
  if (matchIndex < 0) return;
2740
- if (node.body.properties[0].argument.name !== node.params[0].name) return;
2741
3254
  const selfReferencingPattern = {
2742
- type: "Identifier",
3255
+ type: TYPES.IDENTIFIER,
2743
3256
  name: node.params[0].name
2744
3257
  };
2745
3258
  const properties = node.body.properties.slice(1);
2746
- if (patternExistsDeep(properties, selfReferencingPattern)) return;
3259
+ if (patternExistsDeep(selfReferencingPattern, properties)) return;
2747
3260
  const fix = convertToMergeLeft(context, node);
2748
3261
  context.report({
2749
3262
  node,
@@ -2776,19 +3289,17 @@
2776
3289
  });
2777
3290
 
2778
3291
  var pluckLintCheck = {
2779
- operateOn: "CallExpression",
3292
+ operateOn: TYPES.CALL_EXPRESSION,
2780
3293
  check: (context, node) => {
2781
3294
  if (matches(PLUCK_PATTERNS.OBJECT_ARROW_FUNCTION, node)) {
2782
- const code = getCode(context, node.callee.object);
3295
+ const code = getCode$1(context, node.callee.object);
2783
3296
  const func = node.arguments[0];
2784
3297
 
2785
3298
  if (matches(PLUCK_PATTERNS.IDENTIFIER, func)) {
2786
- if (func.params[0].name !== func.body.object.name) return;
2787
3299
  const fix = `pluck("${func.body.property.name}", ${code})`;
2788
3300
  reportViolation(context, node, fix);
2789
3301
  } else if (matches(PLUCK_PATTERNS.DESTRUCTURE, func)) {
2790
3302
  const propertyName = func.params[0].properties[0].value.name;
2791
- if (propertyName !== func.body.name) return;
2792
3303
  const fix = `pluck("${propertyName}", ${code})`;
2793
3304
  reportViolation(context, node, fix);
2794
3305
  }
@@ -2797,14 +3308,14 @@
2797
3308
  };
2798
3309
 
2799
3310
  var propLintCheck = {
2800
- operateOn: "ArrowFunctionExpression",
3311
+ operateOn: TYPES.ARROW_FUNCTION_EXPRESSION,
2801
3312
  check: (context, node) => {
2802
3313
  let fix = null;
2803
3314
  if (matches(PROP_IGNORE_PATTERN, node.parent)) return;
2804
3315
 
2805
- if (matches(PROP_PATTERNS.OBJECT, node) && node.params[0].properties[0].value.name === node.body.name) {
3316
+ if (matches(PROP_PATTERNS.OBJECT, node)) {
2806
3317
  fix = `prop("${node.body.name}")`;
2807
- } else if (matches(PROP_PATTERNS.IDENTITY, node) && node.params[0].name === node.body.object.name) {
3318
+ } else if (matches(PROP_PATTERNS.IDENTITY, node)) {
2808
3319
  fix = `prop("${node.body.property.name}")`;
2809
3320
  } else {
2810
3321
  return;
@@ -2847,7 +3358,7 @@
2847
3358
  };
2848
3359
 
2849
3360
  var sumLintCheck = {
2850
- operateOn: "CallExpression",
3361
+ operateOn: TYPES.CALL_EXPRESSION,
2851
3362
  check: (context, node) => {
2852
3363
  if (!matches(SUM_PATTERN, node)) return;
2853
3364
  const reduceFunc = node.arguments[0];
@@ -2860,12 +3371,12 @@
2860
3371
  let functions = ["sum"];
2861
3372
 
2862
3373
  if (reduceFunc.params[1].type === TYPES.IDENTIFIER && delta.type === TYPES.IDENTIFIER && delta.name === reduceFunc.params[1].name) {
2863
- fix = `sum(${getCode(context, node.callee.object)})`;
3374
+ fix = `sum(${getCode$1(context, node.callee.object)})`;
2864
3375
  } else if (reduceFunc.params[1].type === TYPES.OBJECT_PATTERN && delta.type === TYPES.IDENTIFIER && delta.name === reduceFunc.params[1].properties[0].value.name) {
2865
- fix = `sum(pluck("${delta.name}", ${getCode(context, node.callee.object)}))`;
3376
+ fix = `sum(pluck("${delta.name}", ${getCode$1(context, node.callee.object)}))`;
2866
3377
  functions = ["sum", "pluck"];
2867
3378
  } else if (reduceFunc.params[1].type === TYPES.IDENTIFIER && delta.type === TYPES.MEMBER_EXPRESSION && delta.property.type === TYPES.IDENTIFIER && delta.object.name === reduceFunc.params[1].name) {
2868
- fix = `sum(pluck("${delta.property.name}", ${getCode(context, node.callee.object)}))`;
3379
+ fix = `sum(pluck("${delta.property.name}", ${getCode$1(context, node.callee.object)}))`;
2869
3380
  functions = ["sum", "pluck"];
2870
3381
  } else {
2871
3382
  return;
@@ -2896,11 +3407,19 @@
2896
3407
  }
2897
3408
  },
2898
3409
  create: context => {
2899
- const operations = ramda.groupBy(ramda.prop("operateOn"), [arrayInsertsLintCheck, assocLintCheck, equalsLintCheck, identityLintCheck, mergeLintCheck, pluckLintCheck, propLintCheck, sumLintCheck, isNilCheck, isEmptyCheck]);
3410
+ const operationsArr = [arrayInsertsLintCheck, assocLintCheck, equalsLintCheck, identityLintCheck, mergeLintCheck, pluckLintCheck, propLintCheck, sumLintCheck, isNilCheck, isEmptyCheck].map(op => {
3411
+ if (!Array.isArray(op.operateOn)) return op;
3412
+ return op.operateOn.map(operateOn => ({
3413
+ operateOn,
3414
+ check: op.check
3415
+ }));
3416
+ }).flat();
3417
+ const operations = ramda.groupBy(ramda.prop("operateOn"), operationsArr);
2900
3418
  return ramda.fromPairs(ramda.toPairs(operations).map(([operateOn, operations]) => [operateOn, node => operations.forEach(op => op.check(context, node))]));
2901
3419
  }
2902
3420
  };
2903
3421
 
3422
+ const VALID_URL_TYPES = [TYPES.LITERAL, TYPES.TEMPLATE_LITERAL];
2904
3423
  const AXIOS_GET_PATTERN = {
2905
3424
  callee: {
2906
3425
  type: TYPES.MEMBER_EXPRESSION,
@@ -2912,15 +3431,20 @@
2912
3431
  name: "get",
2913
3432
  type: TYPES.IDENTIFIER
2914
3433
  }
2915
- }
3434
+ },
3435
+ arguments: [{
3436
+ type: ramda.includes(ramda.__, VALID_URL_TYPES)
3437
+ }]
2916
3438
  };
2917
3439
  const AXIOS_PATTERN = {
2918
3440
  callee: {
2919
3441
  name: "axios",
2920
3442
  type: TYPES.IDENTIFIER
2921
- }
3443
+ },
3444
+ arguments: [{
3445
+ type: ramda.includes(ramda.__, [TYPES.LITERAL, TYPES.TEMPLATE_LITERAL, TYPES.OBJECT_EXPRESSION])
3446
+ }]
2922
3447
  };
2923
- const VALID_URL_TYPES = [TYPES.LITERAL, TYPES.TEMPLATE_LITERAL];
2924
3448
 
2925
3449
  const getPropValue = (configNode, name) => {
2926
3450
  var _configNode$propertie;
@@ -2930,26 +3454,25 @@
2930
3454
  }) => key.name === name)) === null || _configNode$propertie === void 0 ? void 0 : _configNode$propertie.value;
2931
3455
  };
2932
3456
 
2933
- const containsQuestionMark = ({
2934
- quasis
2935
- }) => quasis.some(({
2936
- value
2937
- }) => value.cooked.includes("?"));
2938
-
2939
3457
  const checkAndReport = ({
2940
3458
  context,
2941
3459
  node,
2942
3460
  urlNode
2943
3461
  }) => {
2944
- const addsQueryThroughTemplate = urlNode.type === TYPES.TEMPLATE_LITERAL && containsQuestionMark(urlNode);
2945
- const urlStringContainsQuery = urlNode.type === TYPES.LITERAL && urlNode.value.includes("?");
3462
+ let isQuestionMarkPresent = false;
2946
3463
 
2947
- if (addsQueryThroughTemplate || urlStringContainsQuery) {
2948
- context.report({
2949
- node,
2950
- message: USE_AXIOS_CONFIG_FOR_PASSING_QUERY_PARAMETERS_MESSAGE
2951
- });
3464
+ if (urlNode.type === TYPES.LITERAL) {
3465
+ isQuestionMarkPresent = urlNode.value.includes("?");
3466
+ } else {
3467
+ isQuestionMarkPresent = urlNode.quasis.some(({
3468
+ value
3469
+ }) => value.cooked.includes("?"));
2952
3470
  }
3471
+
3472
+ if (isQuestionMarkPresent) context.report({
3473
+ node,
3474
+ message: USE_AXIOS_CONFIG_FOR_PASSING_QUERY_PARAMETERS_MESSAGE
3475
+ });
2953
3476
  };
2954
3477
 
2955
3478
  var useAxiosConfigForPassingQueryParameters = {
@@ -2962,7 +3485,6 @@
2962
3485
  create: context => ({
2963
3486
  CallExpression(node) {
2964
3487
  if (matches(AXIOS_GET_PATTERN, node)) {
2965
- if (!VALID_URL_TYPES.includes(node.arguments[0].type)) return;
2966
3488
  checkAndReport({
2967
3489
  context,
2968
3490
  node,
@@ -2974,21 +3496,16 @@
2974
3496
  let methodNode = null;
2975
3497
  let urlNode = null;
2976
3498
 
2977
- if (VALID_URL_TYPES.includes(node.arguments[0].type)) {
2978
- urlNode = node.arguments[0];
2979
- methodNode = getPropValue(node.arguments[1], "method");
2980
- } else if (node.arguments[0].type === TYPES.OBJECT_EXPRESSION) {
3499
+ if (node.arguments[0].type === TYPES.OBJECT_EXPRESSION) {
2981
3500
  urlNode = getPropValue(node.arguments[0], "url");
2982
3501
  methodNode = getPropValue(node.arguments[0], "method");
3502
+ if (!VALID_URL_TYPES.includes(urlNode.type)) return;
3503
+ } else {
3504
+ urlNode = node.arguments[0];
3505
+ methodNode = getPropValue(node.arguments[1], "method");
2983
3506
  }
2984
3507
 
2985
- if (!urlNode) return;
2986
-
2987
- if (((_methodNode = methodNode) === null || _methodNode === void 0 ? void 0 : _methodNode.value.toLowerCase()) !== "get") {
2988
- return;
2989
- }
2990
-
2991
- if (!VALID_URL_TYPES.includes(urlNode.type)) return;
3508
+ if (((_methodNode = methodNode) === null || _methodNode === void 0 ? void 0 : _methodNode.value.toLowerCase()) !== "get") return;
2992
3509
  checkAndReport({
2993
3510
  context,
2994
3511
  node,
@@ -3049,21 +3566,66 @@
3049
3566
  const aliases = context.options[0];
3050
3567
  if (ramda.isEmpty(aliases) || ramda.isNil(aliases)) return;
3051
3568
  const importPath = node.source.value;
3052
- if (!importPath.startsWith("../")) return;
3053
- const filename = context.getFilename();
3054
- const importFullPath = path__default["default"].resolve(filename, "../", importPath);
3055
- const matches = Object.entries(aliases).filter(([, path]) => importFullPath.startsWith(path)).sort((first, second) => first.length - second.length);
3056
- if (matches.length === 0) return;
3057
- const aliasMatch = matches[matches.length - 1];
3058
- const importPathWithAlias = importFullPath.replace(aliasMatch[1], aliasMatch[0]);
3059
-
3060
- if (importPath.length > importPathWithAlias.length) {
3061
- context.report({
3062
- node,
3063
- message: canBeReplacedWith(importPathWithAlias),
3064
- fix: fixer => fixer.replaceText(node.source, `"${importPathWithAlias}"`)
3065
- });
3569
+ let importPathWithAlias = "";
3570
+
3571
+ if (importPath.startsWith("../")) {
3572
+ const filename = context.getFilename();
3573
+ const importFullPath = path__default["default"].resolve(filename, "../", importPath);
3574
+ const matches = Object.entries(aliases).filter(([, path]) => importFullPath.startsWith(path)).sort((first, second) => first.length - second.length);
3575
+ if (matches.length === 0) return;
3576
+ const aliasMatch = matches[matches.length - 1];
3577
+ importPathWithAlias = importFullPath.replace(aliasMatch[1], aliasMatch[0]);
3578
+ if (importPath.length < importPathWithAlias.length) return;
3579
+ } else {
3580
+ const aliasMatch = Object.entries(aliases).find(([, path]) => importPath.startsWith(path));
3581
+ if (!aliasMatch) return;
3582
+ importPathWithAlias = importPath.replace(aliasMatch[1], aliasMatch[0]);
3066
3583
  }
3584
+
3585
+ context.report({
3586
+ node,
3587
+ message: canBeReplacedWith(importPathWithAlias),
3588
+ fix: fixer => fixer.replaceText(node.source, `"${importPathWithAlias}"`)
3589
+ });
3590
+ }
3591
+
3592
+ })
3593
+ };
3594
+
3595
+ const removeComments = json => JSON.parse(json.replace(/\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g, (m, g) => g ? "" : m));
3596
+
3597
+ const parseJsconfigData = ramda.memoizeWith(ramda.identity, jsconfigFilePath => {
3598
+ if (!jsconfigFilePath) return null;
3599
+ const jsonData = fs__default["default"].readFileSync(jsconfigFilePath).toString();
3600
+ const parsedJsonData = removeComments(jsonData);
3601
+ return parsedJsonData;
3602
+ });
3603
+ var webpackAliasesAndJsconfigPathsShouldBeInSync = {
3604
+ meta: {
3605
+ type: "problem",
3606
+ docs: {
3607
+ description: "Ensures that webpack alias and jsconfig are in sync"
3608
+ }
3609
+ },
3610
+ create: context => ({
3611
+ Property(node) {
3612
+ var _node$parent, _node$parent$parent, _node$parent$parent$k, _jsconfigData$compile;
3613
+
3614
+ const filename = context.getFilename();
3615
+ const isInsideAliasFile = filename.endsWith(WEBPACK_ALIAS_PATH);
3616
+ if (!isInsideAliasFile) return;
3617
+ const isInsideAliasProperty = ((_node$parent = node.parent) === null || _node$parent === void 0 ? void 0 : (_node$parent$parent = _node$parent.parent) === null || _node$parent$parent === void 0 ? void 0 : (_node$parent$parent$k = _node$parent$parent.key) === null || _node$parent$parent$k === void 0 ? void 0 : _node$parent$parent$k.name) === "alias";
3618
+ if (!isInsideAliasProperty) return;
3619
+ const jsconfigData = parseJsconfigData(context.options[0]);
3620
+ const jsconfigPaths = jsconfigData === null || jsconfigData === void 0 ? void 0 : (_jsconfigData$compile = jsconfigData.compilerOptions) === null || _jsconfigData$compile === void 0 ? void 0 : _jsconfigData$compile.paths;
3621
+ if (!jsconfigPaths) return;
3622
+ const aliasName = node.key.name;
3623
+ const matches = Object.keys(jsconfigPaths).some(ramda.includes(ramda.__, [aliasName, `${aliasName}/*`]));
3624
+ if (matches) return;
3625
+ context.report({
3626
+ node,
3627
+ message: addInJsconfig(aliasName)
3628
+ });
3067
3629
  }
3068
3630
 
3069
3631
  })
@@ -3071,10 +3633,11 @@
3071
3633
 
3072
3634
  var index = {
3073
3635
  rules: {
3074
- "neetocommons-tips": neetocommonsTips,
3075
3636
  "api-connector-name-should-match-filename": apiConnectorNameShouldMatchFilename,
3076
3637
  "api-connector-should-use-default-export": apiConnectorShouldUseDefaultExport,
3077
3638
  "combine-multiple-independent-awaits": combineMultipleIndependentAwaits,
3639
+ "consistent-button-link-anchor": consistentButtonLinkAnchor,
3640
+ "neetocommons-tips": neetocommonsTips,
3078
3641
  "no-blacklisted-imports": noBlacklistedImports,
3079
3642
  "no-axios-import-outside-apis": noAxiosImportOutsideApis,
3080
3643
  "no-excess-function-arguments": noExcessFunctionArguments,
@@ -3086,12 +3649,17 @@
3086
3649
  "no-namespace-specifier-import-for-ramda": noNamespaceSpecifierImportForRamda,
3087
3650
  "no-redundant-jsx-expression": noRedundantJsxExpression,
3088
3651
  "no-redundant-string-templates": noRedundantStringTemplates,
3652
+ "no-routes-destructure-and-reassignment": noRoutesDestructureAndReassignment,
3089
3653
  "prefix-neeto-ui-import-alias": prefixNeetoUIImportAlias,
3090
3654
  "ramda-tips": ramdaTips,
3655
+ "routes-should-be-declared-and-default-exported": routesShouldBeDeclaredAndDefaultExported,
3656
+ "routes-should-match-object-path": routesShouldMatchObjectPath,
3091
3657
  "use-axios-config-for-passing-query-parameters": useAxiosConfigForPassingQueryParameters,
3092
3658
  "use-constant-case": useConstantCase,
3659
+ "use-common-routes": useCommonRoutes,
3093
3660
  "use-snake-case-for-api-connector-filename": useSnakeCaseForApiConnectorFilename,
3094
- "use-webpack-alias": useWebpackAlias
3661
+ "use-webpack-alias": useWebpackAlias,
3662
+ "webpack-aliases-and-jsconfig-paths-should-be-in-sync": webpackAliasesAndJsconfigPathsShouldBeInSync
3095
3663
  }
3096
3664
  };
3097
3665