eslint-plugin-remeda 1.0.0

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 (41) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +48 -0
  3. package/docs/rules/collection-method-value.md +27 -0
  4. package/docs/rules/collection-return.md +29 -0
  5. package/docs/rules/prefer-constant.md +53 -0
  6. package/docs/rules/prefer-do-nothing.md +29 -0
  7. package/docs/rules/prefer-filter.md +25 -0
  8. package/docs/rules/prefer-find.md +39 -0
  9. package/docs/rules/prefer-flat-map.md +25 -0
  10. package/docs/rules/prefer-is-empty.md +39 -0
  11. package/docs/rules/prefer-is-nil.md +27 -0
  12. package/docs/rules/prefer-map.md +29 -0
  13. package/docs/rules/prefer-nullish-coalescing.md +25 -0
  14. package/docs/rules/prefer-remeda-typecheck.md +36 -0
  15. package/docs/rules/prefer-some.md +29 -0
  16. package/docs/rules/prefer-times.md +36 -0
  17. package/package.json +62 -0
  18. package/src/index.js +35 -0
  19. package/src/rules/collection-method-value.js +82 -0
  20. package/src/rules/collection-return.js +71 -0
  21. package/src/rules/prefer-constant.js +87 -0
  22. package/src/rules/prefer-do-nothing.js +44 -0
  23. package/src/rules/prefer-filter.js +82 -0
  24. package/src/rules/prefer-find.js +68 -0
  25. package/src/rules/prefer-flat-map.js +50 -0
  26. package/src/rules/prefer-is-empty.js +134 -0
  27. package/src/rules/prefer-is-nil.js +97 -0
  28. package/src/rules/prefer-map.js +62 -0
  29. package/src/rules/prefer-nullish-coalescing.js +63 -0
  30. package/src/rules/prefer-remeda-typecheck.js +93 -0
  31. package/src/rules/prefer-some.js +43 -0
  32. package/src/rules/prefer-times.js +37 -0
  33. package/src/util/LodashContext.js +128 -0
  34. package/src/util/astUtil.js +353 -0
  35. package/src/util/getDocsUrl.js +17 -0
  36. package/src/util/importUtil.js +24 -0
  37. package/src/util/lodashUtil.js +123 -0
  38. package/src/util/methodData.js +2417 -0
  39. package/src/util/methodDataUtil.js +115 -0
  40. package/src/util/ruleUtil.js +13 -0
  41. package/src/util/settingsUtil.js +31 -0
@@ -0,0 +1,115 @@
1
+ "use strict";
2
+
3
+ const _ = require("lodash");
4
+
5
+ const getMethodData = _.memoize(() => require(`./methodData`));
6
+
7
+ /**
8
+ * Gets a method name and returns all its aliases including itself.
9
+ * @param {string} method
10
+ * @returns {string[]}
11
+ */
12
+ const expandAlias = (method) => {
13
+ const methodAliases = _.get(getMethodData(), [method, "aliases"], []);
14
+ return [method, ...methodAliases];
15
+ };
16
+
17
+ /**
18
+ * Gets a list of methods and returns a list of methods and all their aliases
19
+ * @param methods
20
+ * @returns {string[]}
21
+ */
22
+ function expandAliases(methods) {
23
+ return _.flatMap(methods, (method) => expandAlias(method));
24
+ }
25
+
26
+ /**
27
+ * Gets whether the method is a collection method
28
+ * @param {string} method
29
+ * @returns {Boolean}
30
+ */
31
+ function isCollectionMethod(method) {
32
+ return (
33
+ methodSupportsShorthand(method) ||
34
+ _.includes(expandAliases(["reduce", "reduceRight"]), method)
35
+ );
36
+ }
37
+
38
+ /**
39
+ * Returns whether the node's method call supports using shorthands.
40
+ * @param {string} method
41
+ * @returns {boolean}
42
+ */
43
+ function methodSupportsShorthand(method, shorthandType) {
44
+ const mainAlias = getMainAlias(method);
45
+ const methodShorthandData = _.get(getMethodData(), [mainAlias, "shorthand"]);
46
+ return _.isObject(methodShorthandData)
47
+ ? Boolean(shorthandType && methodShorthandData[shorthandType])
48
+ : Boolean(methodShorthandData);
49
+ }
50
+
51
+ /**
52
+ * Gets whether the suspect is an alias of the method
53
+ * @param {string} method
54
+ * @param {string} suspect
55
+ * @returns {boolean}
56
+ */
57
+ function isAliasOfMethod(method, suspect) {
58
+ return (
59
+ method === suspect ||
60
+ _.includes(_.get(getMethodData(), [method, "aliases"]), suspect)
61
+ );
62
+ }
63
+
64
+ /**
65
+ * Gets the index of the iteratee of a method when it isn't chained, or -1 if it doesn't have one.
66
+ * @param {string} method
67
+ * @returns {number}
68
+ */
69
+ function getIterateeIndex(method) {
70
+ const mainAlias = getMainAlias(method);
71
+ const methodData = getMethodData()[mainAlias];
72
+ if (_.has(methodData, "iterateeIndex")) {
73
+ return methodData.iterateeIndex;
74
+ }
75
+ if (methodData && methodData.iteratee) {
76
+ return 1;
77
+ }
78
+ return -1;
79
+ }
80
+
81
+ /**
82
+ * Returns the main alias for the method.
83
+ * @param {string} method
84
+ * @returns {string}
85
+ */
86
+ function getMainAlias(method) {
87
+ const data = getMethodData();
88
+ return data[method]
89
+ ? method
90
+ : _.findKey(data, (methodData) => _.includes(methodData.aliases, method));
91
+ }
92
+
93
+ const sideEffectIterationMethods = [
94
+ "forEach",
95
+ "forEachRight",
96
+ "forIn",
97
+ "forInRight",
98
+ "forOwn",
99
+ "forOwnRight",
100
+ ];
101
+
102
+ /**
103
+ * Gets a list of side effect iteration methods
104
+ * @returns {string[]}
105
+ */
106
+ function getSideEffectIterationMethods() {
107
+ return expandAliases(sideEffectIterationMethods);
108
+ }
109
+
110
+ module.exports = {
111
+ isAliasOfMethod,
112
+ isCollectionMethod,
113
+ getIterateeIndex,
114
+ getSideEffectIterationMethods,
115
+ };
@@ -0,0 +1,13 @@
1
+ 'use strict'
2
+ const assignWith = require('lodash/assignWith')
3
+ const mapValues = require('lodash/mapValues')
4
+ const over = require('lodash/over')
5
+
6
+ function combineVisitorObjects(...objects) {
7
+ const accumForAllVisitors = assignWith({}, ...objects, (objValue, sourceValue) => (objValue || []).concat(sourceValue))
8
+ return mapValues(accumForAllVisitors, over)
9
+ }
10
+
11
+ module.exports = {
12
+ combineVisitorObjects
13
+ }
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ const _ = require("lodash");
3
+
4
+ module.exports = {
5
+ /**
6
+ * Returns the lodash object settings, with default values if missing
7
+ * @param context
8
+ * @returns {LodashSettings}
9
+ */
10
+ getSettings(context) {
11
+ return _.chain(context)
12
+ .get(["settings", "remeda"])
13
+ .clone()
14
+ .defaults({
15
+ version: 4,
16
+ })
17
+ .value();
18
+ },
19
+
20
+ /**
21
+ * Gets whether the ecmaFeature specified is on for the context
22
+ * @param context
23
+ * @param {string} featureName
24
+ */
25
+ isEcmaFeatureOn(context, featureName) {
26
+ return (
27
+ _.get(context, ["ecmaFeatures", featureName]) ||
28
+ _.get(context, ["parserOptions", "ecmaVersion"], 0) > 5
29
+ );
30
+ },
31
+ };