eslint-plugin-remeda 1.2.0 → 1.3.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.
- package/dist/index.cjs +267 -245
- package/dist/index.js +278 -256
- package/package.json +8 -4
package/dist/index.js
CHANGED
@@ -19,12 +19,14 @@ var package_default = {
|
|
19
19
|
scripts: {
|
20
20
|
build: "tsup",
|
21
21
|
typecheck: "tsc",
|
22
|
+
lint: "eslint",
|
22
23
|
knip: "knip",
|
23
24
|
publint: "publint",
|
24
25
|
"test:watch": "vitest --ui",
|
25
26
|
test: "vitest run",
|
26
27
|
attw: "attw --pack .",
|
27
28
|
qa: "pnpm typecheck && pnpm test && pnpm knip && pnpm publint && attw",
|
29
|
+
nuke: "rm -rf node_modules pnpm-lock.yaml",
|
28
30
|
"semantic-release": "pnpm build && semantic-release"
|
29
31
|
},
|
30
32
|
files: [
|
@@ -54,8 +56,10 @@ var package_default = {
|
|
54
56
|
"@types/node": "^20.14.9",
|
55
57
|
"@vitest/coverage-v8": "^2.0.3",
|
56
58
|
"@vitest/ui": "^2.0.3",
|
57
|
-
eslint: "
|
58
|
-
"eslint-
|
59
|
+
eslint: "9.10.0",
|
60
|
+
"eslint-config-sheriff": "^21.2.0",
|
61
|
+
"eslint-define-config": "^2.1.0",
|
62
|
+
"eslint-plugin-eslint-plugin": "^6.2.0",
|
59
63
|
"eslint-vitest-rule-tester": "^0.3.3",
|
60
64
|
knip: "^5.29.1",
|
61
65
|
prettier: "^3.3.2",
|
@@ -68,7 +72,7 @@ var package_default = {
|
|
68
72
|
engines: {
|
69
73
|
node: ">=20"
|
70
74
|
},
|
71
|
-
packageManager: "pnpm@9.
|
75
|
+
packageManager: "pnpm@9.9.0",
|
72
76
|
publishConfig: {
|
73
77
|
access: "public"
|
74
78
|
},
|
@@ -87,17 +91,231 @@ var package_default = {
|
|
87
91
|
}
|
88
92
|
};
|
89
93
|
|
94
|
+
// src/rules/collection-method-value.ts
|
95
|
+
import includes5 from "lodash/includes";
|
96
|
+
|
97
|
+
// src/util/astUtil.ts
|
98
|
+
import {
|
99
|
+
cond,
|
100
|
+
flatMap,
|
101
|
+
get,
|
102
|
+
includes,
|
103
|
+
isEqualWith,
|
104
|
+
isMatch,
|
105
|
+
matches,
|
106
|
+
matchesProperty,
|
107
|
+
overEvery,
|
108
|
+
overSome,
|
109
|
+
property
|
110
|
+
} from "lodash";
|
111
|
+
var getCaller = property(["callee", "object"]);
|
112
|
+
var getMethodName = property(["callee", "property", "name"]);
|
113
|
+
var isMethodCall = matches({
|
114
|
+
type: "CallExpression",
|
115
|
+
callee: { type: "MemberExpression" }
|
116
|
+
});
|
117
|
+
var isFunctionExpression = overSome(
|
118
|
+
matchesProperty("type", "FunctionExpression"),
|
119
|
+
matchesProperty("type", "FunctionDeclaration")
|
120
|
+
);
|
121
|
+
var isFunctionDefinitionWithBlock = overSome(
|
122
|
+
isFunctionExpression,
|
123
|
+
matches({
|
124
|
+
type: "ArrowFunctionExpression",
|
125
|
+
body: { type: "BlockStatement" }
|
126
|
+
})
|
127
|
+
);
|
128
|
+
var getFirstFunctionLine = cond([
|
129
|
+
[isFunctionDefinitionWithBlock, property(["body", "body", 0])],
|
130
|
+
[matches({ type: "ArrowFunctionExpression" }), property("body")]
|
131
|
+
]);
|
132
|
+
var isPropAccess = overSome(
|
133
|
+
matches({ computed: false }),
|
134
|
+
matchesProperty(["property", "type"], "Literal")
|
135
|
+
);
|
136
|
+
function isMemberExpOf(node, objectName, { maxLength = Number.MAX_VALUE, allowComputed } = {}) {
|
137
|
+
if (objectName) {
|
138
|
+
let curr = node;
|
139
|
+
let depth = maxLength;
|
140
|
+
while (curr && depth) {
|
141
|
+
if (allowComputed || isPropAccess(curr)) {
|
142
|
+
if (curr.type === "MemberExpression" && curr.object.name === objectName) {
|
143
|
+
return true;
|
144
|
+
}
|
145
|
+
curr = curr.object;
|
146
|
+
depth--;
|
147
|
+
} else {
|
148
|
+
return false;
|
149
|
+
}
|
150
|
+
}
|
151
|
+
}
|
152
|
+
}
|
153
|
+
var getFirstParamName = property(["params", 0, "name"]);
|
154
|
+
var isReturnStatement = matchesProperty("type", "ReturnStatement");
|
155
|
+
function hasOnlyOneStatement(func) {
|
156
|
+
if (isFunctionDefinitionWithBlock(func)) {
|
157
|
+
return get(func, "body.body.length") === 1;
|
158
|
+
}
|
159
|
+
if (func.type === "ArrowFunctionExpression") {
|
160
|
+
return !get(func, "body.body");
|
161
|
+
}
|
162
|
+
}
|
163
|
+
function isObjectOfMethodCall(node) {
|
164
|
+
return get(node, "parent.object") === node && get(node, "parent.parent.type") === "CallExpression";
|
165
|
+
}
|
166
|
+
function isLiteral(node) {
|
167
|
+
return node.type === "Literal";
|
168
|
+
}
|
169
|
+
function isBinaryExpWithMemberOf(operator, exp, objectName, {
|
170
|
+
maxLength,
|
171
|
+
allowComputed,
|
172
|
+
onlyLiterals
|
173
|
+
} = {}) {
|
174
|
+
if (!isMatch(exp, { type: "BinaryExpression", operator })) {
|
175
|
+
return false;
|
176
|
+
}
|
177
|
+
const [left, right] = [exp.left, exp.right].map(
|
178
|
+
(side) => isMemberExpOf(side, objectName, { maxLength, allowComputed })
|
179
|
+
);
|
180
|
+
return left === !right && (!onlyLiterals || isLiteral(exp.left) || isLiteral(exp.right));
|
181
|
+
}
|
182
|
+
var isNegationExpression = matches({
|
183
|
+
type: "UnaryExpression",
|
184
|
+
operator: "!"
|
185
|
+
});
|
186
|
+
function isNegationOfMemberOf(exp, objectName, { maxLength } = {}) {
|
187
|
+
return isNegationExpression(exp) && isMemberExpOf(exp.argument, objectName, { maxLength, allowComputed: false });
|
188
|
+
}
|
189
|
+
function isIdentifierWithName(exp, paramName) {
|
190
|
+
return exp && paramName && exp.type === "Identifier" && exp.name === paramName;
|
191
|
+
}
|
192
|
+
function getValueReturnedInFirstStatement(func) {
|
193
|
+
const firstLine = getFirstFunctionLine(func);
|
194
|
+
if (func) {
|
195
|
+
if (isFunctionDefinitionWithBlock(func)) {
|
196
|
+
return isReturnStatement(firstLine) ? firstLine.argument : void 0;
|
197
|
+
}
|
198
|
+
if (func.type === "ArrowFunctionExpression") {
|
199
|
+
return firstLine;
|
200
|
+
}
|
201
|
+
}
|
202
|
+
}
|
203
|
+
function isCallFromObject(node, objName) {
|
204
|
+
return node && objName && node.type === "CallExpression" && get(node, "callee.object.name") === objName;
|
205
|
+
}
|
206
|
+
function isComputed(node) {
|
207
|
+
return get(node, "computed") && node.property.type !== "Literal";
|
208
|
+
}
|
209
|
+
function isEquivalentMemberExp(a, b) {
|
210
|
+
return isEqualWith(a, b, (left, right, key) => {
|
211
|
+
if (includes(["loc", "range", "computed", "start", "end", "parent"], key)) {
|
212
|
+
return true;
|
213
|
+
}
|
214
|
+
if (isComputed(left) || isComputed(right)) {
|
215
|
+
return false;
|
216
|
+
}
|
217
|
+
if (key === "property") {
|
218
|
+
const leftValue = left.name || left.value;
|
219
|
+
const rightValue = right.name || right.value;
|
220
|
+
return leftValue === rightValue;
|
221
|
+
}
|
222
|
+
});
|
223
|
+
}
|
224
|
+
var isEqEqEq = matches({ type: "BinaryExpression", operator: "===" });
|
225
|
+
var isMinus = (node) => node.type === "UnaryExpression" && node.operator === "-";
|
226
|
+
var comparisonType = {
|
227
|
+
exact: 0,
|
228
|
+
over: 1,
|
229
|
+
under: 2,
|
230
|
+
any: 3
|
231
|
+
};
|
232
|
+
var comparisonOperators = ["==", "!=", "===", "!=="];
|
233
|
+
function getIsValue(value2) {
|
234
|
+
return value2 < 0 ? overEvery(isMinus, matches({ argument: { value: -value2 } })) : matches({ value: value2 });
|
235
|
+
}
|
236
|
+
function getExpressionComparedToInt(node, value2, checkOver) {
|
237
|
+
const isValue = getIsValue(value2);
|
238
|
+
if (includes(comparisonOperators, node.operator)) {
|
239
|
+
if (isValue(node.right)) {
|
240
|
+
return node.left;
|
241
|
+
}
|
242
|
+
if (isValue(node.left)) {
|
243
|
+
return node.right;
|
244
|
+
}
|
245
|
+
}
|
246
|
+
if (checkOver) {
|
247
|
+
if (node.operator === ">" && isValue(node.right)) {
|
248
|
+
return node.left;
|
249
|
+
}
|
250
|
+
if (node.operator === "<" && isValue(node.left)) {
|
251
|
+
return node.right;
|
252
|
+
}
|
253
|
+
const isNext = getIsValue(value2 + 1);
|
254
|
+
if ((node.operator === ">=" || node.operator === "<") && isNext(node.right)) {
|
255
|
+
return node.left;
|
256
|
+
}
|
257
|
+
if ((node.operator === "<=" || node.operator === ">") && isNext(node.left)) {
|
258
|
+
return node.right;
|
259
|
+
}
|
260
|
+
}
|
261
|
+
}
|
262
|
+
var isIndexOfCall = (node) => isMethodCall(node) && getMethodName(node) === "indexOf";
|
263
|
+
var isFindIndexCall = (node) => isMethodCall(node) && getMethodName(node) === "findIndex";
|
264
|
+
function collectParameterValues(node) {
|
265
|
+
switch (node && node.type) {
|
266
|
+
case "Identifier": {
|
267
|
+
return [node.name];
|
268
|
+
}
|
269
|
+
case "ObjectPattern": {
|
270
|
+
return flatMap(
|
271
|
+
node.properties,
|
272
|
+
(prop) => collectParameterValues(prop.value)
|
273
|
+
);
|
274
|
+
}
|
275
|
+
case "ArrayPattern": {
|
276
|
+
return flatMap(node.elements, collectParameterValues);
|
277
|
+
}
|
278
|
+
default: {
|
279
|
+
return [];
|
280
|
+
}
|
281
|
+
}
|
282
|
+
}
|
283
|
+
var astUtil_default = {
|
284
|
+
getCaller,
|
285
|
+
getMethodName,
|
286
|
+
isMethodCall,
|
287
|
+
getFirstFunctionLine,
|
288
|
+
isMemberExpOf,
|
289
|
+
getFirstParamName,
|
290
|
+
hasOnlyOneStatement,
|
291
|
+
isObjectOfMethodCall,
|
292
|
+
isEqEqEqToMemberOf: isBinaryExpWithMemberOf.bind(null, "==="),
|
293
|
+
isNotEqEqToMemberOf: isBinaryExpWithMemberOf.bind(null, "!=="),
|
294
|
+
isNegationOfMemberOf,
|
295
|
+
isIdentifierWithName,
|
296
|
+
isNegationExpression,
|
297
|
+
getValueReturnedInFirstStatement,
|
298
|
+
isCallFromObject,
|
299
|
+
isComputed,
|
300
|
+
isEquivalentMemberExp,
|
301
|
+
isEqEqEq,
|
302
|
+
comparisonType,
|
303
|
+
getExpressionComparedToInt,
|
304
|
+
isIndexOfCall,
|
305
|
+
isFindIndexCall,
|
306
|
+
isFunctionExpression,
|
307
|
+
isFunctionDefinitionWithBlock,
|
308
|
+
collectParameterValues
|
309
|
+
};
|
310
|
+
|
90
311
|
// src/util/getDocsUrl.ts
|
91
312
|
var REPO_URL = "https://github.com/AndreaPontrandolfo/eslint-plugin-remeda";
|
92
313
|
function getDocsUrl(ruleName) {
|
93
314
|
return `${REPO_URL}/blob/v${version}/docs/rules/${ruleName}.md`;
|
94
315
|
}
|
95
316
|
|
96
|
-
// src/util/remedaUtil.ts
|
97
|
-
import { includes as includes4, capitalize as capitalize2 } from "lodash";
|
98
|
-
|
99
317
|
// src/util/methodDataUtil.ts
|
100
|
-
import {
|
318
|
+
import { get as get3, has as has2, includes as includes3, isObject as isObject2 } from "lodash";
|
101
319
|
|
102
320
|
// src/util/methodData.ts
|
103
321
|
var methodData_exports = {};
|
@@ -128,7 +346,7 @@ __export(methodData_exports, {
|
|
128
346
|
cloneWith: () => cloneWith,
|
129
347
|
commit: () => commit,
|
130
348
|
concat: () => concat,
|
131
|
-
cond: () =>
|
349
|
+
cond: () => cond2,
|
132
350
|
conforms: () => conforms,
|
133
351
|
conformsTo: () => conformsTo,
|
134
352
|
constant: () => constant,
|
@@ -165,7 +383,7 @@ __export(methodData_exports, {
|
|
165
383
|
findLastIndex: () => findLastIndex,
|
166
384
|
findLastKey: () => findLastKey,
|
167
385
|
first: () => first,
|
168
|
-
flatMap: () =>
|
386
|
+
flatMap: () => flatMap2,
|
169
387
|
flatMapDeep: () => flatMapDeep,
|
170
388
|
flatMapDepth: () => flatMapDepth,
|
171
389
|
flatten: () => flatten,
|
@@ -184,7 +402,7 @@ __export(methodData_exports, {
|
|
184
402
|
fromPairs: () => fromPairs,
|
185
403
|
functions: () => functions,
|
186
404
|
functionsIn: () => functionsIn,
|
187
|
-
get: () =>
|
405
|
+
get: () => get2,
|
188
406
|
groupBy: () => groupBy,
|
189
407
|
gt: () => gt,
|
190
408
|
gte: () => gte,
|
@@ -193,7 +411,7 @@ __export(methodData_exports, {
|
|
193
411
|
head: () => head,
|
194
412
|
identity: () => identity,
|
195
413
|
inRange: () => inRange,
|
196
|
-
includes: () =>
|
414
|
+
includes: () => includes2,
|
197
415
|
indexBy: () => indexBy,
|
198
416
|
indexOf: () => indexOf,
|
199
417
|
initial: () => initial,
|
@@ -215,14 +433,14 @@ __export(methodData_exports, {
|
|
215
433
|
isElement: () => isElement,
|
216
434
|
isEmpty: () => isEmpty,
|
217
435
|
isEqual: () => isEqual,
|
218
|
-
isEqualWith: () =>
|
436
|
+
isEqualWith: () => isEqualWith2,
|
219
437
|
isError: () => isError,
|
220
438
|
isFinite: () => isFinite,
|
221
439
|
isFunction: () => isFunction,
|
222
440
|
isInteger: () => isInteger,
|
223
441
|
isLength: () => isLength,
|
224
442
|
isMap: () => isMap,
|
225
|
-
isMatch: () =>
|
443
|
+
isMatch: () => isMatch2,
|
226
444
|
isMatchWith: () => isMatchWith,
|
227
445
|
isNaN: () => isNaN,
|
228
446
|
isNative: () => isNative,
|
@@ -256,8 +474,8 @@ __export(methodData_exports, {
|
|
256
474
|
map: () => map,
|
257
475
|
mapKeys: () => mapKeys,
|
258
476
|
mapValues: () => mapValues,
|
259
|
-
matches: () =>
|
260
|
-
matchesProperty: () =>
|
477
|
+
matches: () => matches2,
|
478
|
+
matchesProperty: () => matchesProperty2,
|
261
479
|
max: () => max,
|
262
480
|
maxBy: () => maxBy,
|
263
481
|
mean: () => mean,
|
@@ -284,8 +502,8 @@ __export(methodData_exports, {
|
|
284
502
|
orderBy: () => orderBy,
|
285
503
|
over: () => over,
|
286
504
|
overArgs: () => overArgs,
|
287
|
-
overEvery: () =>
|
288
|
-
overSome: () =>
|
505
|
+
overEvery: () => overEvery2,
|
506
|
+
overSome: () => overSome2,
|
289
507
|
pad: () => pad,
|
290
508
|
padEnd: () => padEnd,
|
291
509
|
padStart: () => padStart,
|
@@ -297,7 +515,7 @@ __export(methodData_exports, {
|
|
297
515
|
pickBy: () => pickBy,
|
298
516
|
plant: () => plant,
|
299
517
|
pop: () => pop,
|
300
|
-
property: () =>
|
518
|
+
property: () => property2,
|
301
519
|
propertyOf: () => propertyOf,
|
302
520
|
pull: () => pull,
|
303
521
|
pullAll: () => pullAll,
|
@@ -583,7 +801,7 @@ var concat = {
|
|
583
801
|
chainable: true,
|
584
802
|
iteratee: false
|
585
803
|
};
|
586
|
-
var
|
804
|
+
var cond2 = {
|
587
805
|
wrapper: false,
|
588
806
|
shorthand: false,
|
589
807
|
chainable: false,
|
@@ -828,7 +1046,7 @@ var findLastKey = {
|
|
828
1046
|
iteratee: true,
|
829
1047
|
args: 2
|
830
1048
|
};
|
831
|
-
var
|
1049
|
+
var flatMap2 = {
|
832
1050
|
wrapper: false,
|
833
1051
|
shorthand: true,
|
834
1052
|
chainable: true,
|
@@ -959,7 +1177,7 @@ var functionsIn = {
|
|
959
1177
|
iteratee: false,
|
960
1178
|
args: 1
|
961
1179
|
};
|
962
|
-
var
|
1180
|
+
var get2 = {
|
963
1181
|
wrapper: false,
|
964
1182
|
shorthand: false,
|
965
1183
|
chainable: false,
|
@@ -1029,7 +1247,7 @@ var inRange = {
|
|
1029
1247
|
iteratee: false,
|
1030
1248
|
args: 3
|
1031
1249
|
};
|
1032
|
-
var
|
1250
|
+
var includes2 = {
|
1033
1251
|
wrapper: false,
|
1034
1252
|
shorthand: false,
|
1035
1253
|
chainable: false,
|
@@ -1177,7 +1395,7 @@ var isEqual = {
|
|
1177
1395
|
iteratee: false,
|
1178
1396
|
args: 2
|
1179
1397
|
};
|
1180
|
-
var
|
1398
|
+
var isEqualWith2 = {
|
1181
1399
|
wrapper: false,
|
1182
1400
|
shorthand: false,
|
1183
1401
|
chainable: false,
|
@@ -1226,7 +1444,7 @@ var isMap = {
|
|
1226
1444
|
iteratee: false,
|
1227
1445
|
args: 1
|
1228
1446
|
};
|
1229
|
-
var
|
1447
|
+
var isMatch2 = {
|
1230
1448
|
wrapper: false,
|
1231
1449
|
shorthand: false,
|
1232
1450
|
chainable: false,
|
@@ -1463,14 +1681,14 @@ var mapValues = {
|
|
1463
1681
|
iteratee: true,
|
1464
1682
|
args: 2
|
1465
1683
|
};
|
1466
|
-
var
|
1684
|
+
var matches2 = {
|
1467
1685
|
wrapper: false,
|
1468
1686
|
shorthand: false,
|
1469
1687
|
chainable: true,
|
1470
1688
|
iteratee: false,
|
1471
1689
|
args: 1
|
1472
1690
|
};
|
1473
|
-
var
|
1691
|
+
var matchesProperty2 = {
|
1474
1692
|
wrapper: false,
|
1475
1693
|
shorthand: false,
|
1476
1694
|
chainable: true,
|
@@ -1651,13 +1869,13 @@ var overArgs = {
|
|
1651
1869
|
chainable: true,
|
1652
1870
|
iteratee: false
|
1653
1871
|
};
|
1654
|
-
var
|
1872
|
+
var overEvery2 = {
|
1655
1873
|
wrapper: false,
|
1656
1874
|
shorthand: true,
|
1657
1875
|
chainable: true,
|
1658
1876
|
iteratee: true
|
1659
1877
|
};
|
1660
|
-
var
|
1878
|
+
var overSome2 = {
|
1661
1879
|
wrapper: false,
|
1662
1880
|
shorthand: true,
|
1663
1881
|
chainable: true,
|
@@ -1734,7 +1952,7 @@ var pop = {
|
|
1734
1952
|
chainable: false,
|
1735
1953
|
iteratee: false
|
1736
1954
|
};
|
1737
|
-
var
|
1955
|
+
var property2 = {
|
1738
1956
|
wrapper: false,
|
1739
1957
|
shorthand: false,
|
1740
1958
|
chainable: true,
|
@@ -2521,10 +2739,10 @@ var zipWith = {
|
|
2521
2739
|
|
2522
2740
|
// src/util/methodDataUtil.ts
|
2523
2741
|
function isCollectionMethod(method2) {
|
2524
|
-
return methodSupportsShorthand(method2) ||
|
2742
|
+
return methodSupportsShorthand(method2) || includes3(["reduce", "reduceRight"], method2);
|
2525
2743
|
}
|
2526
2744
|
function methodSupportsShorthand(method2, shorthandType) {
|
2527
|
-
const methodShorthandData =
|
2745
|
+
const methodShorthandData = get3(methodData_exports, `${method2}.shorthand`);
|
2528
2746
|
return isObject2(methodShorthandData) ? Boolean(shorthandType && methodShorthandData[shorthandType]) : Boolean(methodShorthandData);
|
2529
2747
|
}
|
2530
2748
|
function getIterateeIndex(method2) {
|
@@ -2532,7 +2750,7 @@ function getIterateeIndex(method2) {
|
|
2532
2750
|
if (has2(methodData, "iterateeIndex")) {
|
2533
2751
|
return methodData.iterateeIndex;
|
2534
2752
|
}
|
2535
|
-
if (methodData
|
2753
|
+
if (methodData.iteratee) {
|
2536
2754
|
return 1;
|
2537
2755
|
}
|
2538
2756
|
return -1;
|
@@ -2549,215 +2767,8 @@ function getSideEffectIterationMethods() {
|
|
2549
2767
|
return sideEffectIterationMethods;
|
2550
2768
|
}
|
2551
2769
|
|
2552
|
-
// src/util/
|
2553
|
-
import {
|
2554
|
-
property as property2,
|
2555
|
-
matches as matches2,
|
2556
|
-
overSome as overSome2,
|
2557
|
-
matchesProperty as matchesProperty2,
|
2558
|
-
cond as cond2,
|
2559
|
-
get as get3,
|
2560
|
-
isMatch as isMatch2,
|
2561
|
-
isEqualWith as isEqualWith2,
|
2562
|
-
includes as includes3,
|
2563
|
-
overEvery as overEvery2,
|
2564
|
-
flatMap as flatMap2
|
2565
|
-
} from "lodash";
|
2566
|
-
var getCaller = property2(["callee", "object"]);
|
2567
|
-
var getMethodName = property2(["callee", "property", "name"]);
|
2568
|
-
var isMethodCall = matches2({
|
2569
|
-
type: "CallExpression",
|
2570
|
-
callee: { type: "MemberExpression" }
|
2571
|
-
});
|
2572
|
-
var isFunctionExpression = overSome2(
|
2573
|
-
matchesProperty2("type", "FunctionExpression"),
|
2574
|
-
matchesProperty2("type", "FunctionDeclaration")
|
2575
|
-
);
|
2576
|
-
var isFunctionDefinitionWithBlock = overSome2(
|
2577
|
-
isFunctionExpression,
|
2578
|
-
matches2({
|
2579
|
-
type: "ArrowFunctionExpression",
|
2580
|
-
body: { type: "BlockStatement" }
|
2581
|
-
})
|
2582
|
-
);
|
2583
|
-
var getFirstFunctionLine = cond2([
|
2584
|
-
[isFunctionDefinitionWithBlock, property2(["body", "body", 0])],
|
2585
|
-
[matches2({ type: "ArrowFunctionExpression" }), property2("body")]
|
2586
|
-
]);
|
2587
|
-
var isPropAccess = overSome2(
|
2588
|
-
matches2({ computed: false }),
|
2589
|
-
matchesProperty2(["property", "type"], "Literal")
|
2590
|
-
);
|
2591
|
-
function isMemberExpOf(node, objectName, { maxLength = Number.MAX_VALUE, allowComputed } = {}) {
|
2592
|
-
if (objectName) {
|
2593
|
-
let curr = node;
|
2594
|
-
let depth = maxLength;
|
2595
|
-
while (curr && depth) {
|
2596
|
-
if (allowComputed || isPropAccess(curr)) {
|
2597
|
-
if (curr.type === "MemberExpression" && curr.object.name === objectName) {
|
2598
|
-
return true;
|
2599
|
-
}
|
2600
|
-
curr = curr.object;
|
2601
|
-
depth--;
|
2602
|
-
} else {
|
2603
|
-
return false;
|
2604
|
-
}
|
2605
|
-
}
|
2606
|
-
}
|
2607
|
-
}
|
2608
|
-
var getFirstParamName = property2(["params", 0, "name"]);
|
2609
|
-
var isReturnStatement = matchesProperty2("type", "ReturnStatement");
|
2610
|
-
function hasOnlyOneStatement(func) {
|
2611
|
-
if (isFunctionDefinitionWithBlock(func)) {
|
2612
|
-
return get3(func, "body.body.length") === 1;
|
2613
|
-
}
|
2614
|
-
if (func.type === "ArrowFunctionExpression") {
|
2615
|
-
return !get3(func, "body.body");
|
2616
|
-
}
|
2617
|
-
}
|
2618
|
-
function isObjectOfMethodCall(node) {
|
2619
|
-
return get3(node, "parent.object") === node && get3(node, "parent.parent.type") === "CallExpression";
|
2620
|
-
}
|
2621
|
-
function isLiteral(node) {
|
2622
|
-
return node.type === "Literal";
|
2623
|
-
}
|
2624
|
-
function isBinaryExpWithMemberOf(operator, exp, objectName, {
|
2625
|
-
maxLength,
|
2626
|
-
allowComputed,
|
2627
|
-
onlyLiterals
|
2628
|
-
} = {}) {
|
2629
|
-
if (!isMatch2(exp, { type: "BinaryExpression", operator })) {
|
2630
|
-
return false;
|
2631
|
-
}
|
2632
|
-
const [left, right] = [exp.left, exp.right].map(
|
2633
|
-
(side) => isMemberExpOf(side, objectName, { maxLength, allowComputed })
|
2634
|
-
);
|
2635
|
-
return left === !right && (!onlyLiterals || isLiteral(exp.left) || isLiteral(exp.right));
|
2636
|
-
}
|
2637
|
-
var isNegationExpression = matches2({
|
2638
|
-
type: "UnaryExpression",
|
2639
|
-
operator: "!"
|
2640
|
-
});
|
2641
|
-
function isNegationOfMemberOf(exp, objectName, { maxLength } = {}) {
|
2642
|
-
return isNegationExpression(exp) && isMemberExpOf(exp.argument, objectName, { maxLength, allowComputed: false });
|
2643
|
-
}
|
2644
|
-
function isIdentifierWithName(exp, paramName) {
|
2645
|
-
return exp && paramName && exp.type === "Identifier" && exp.name === paramName;
|
2646
|
-
}
|
2647
|
-
function getValueReturnedInFirstStatement(func) {
|
2648
|
-
const firstLine = getFirstFunctionLine(func);
|
2649
|
-
if (func) {
|
2650
|
-
if (isFunctionDefinitionWithBlock(func)) {
|
2651
|
-
return isReturnStatement(firstLine) ? firstLine.argument : void 0;
|
2652
|
-
}
|
2653
|
-
if (func.type === "ArrowFunctionExpression") {
|
2654
|
-
return firstLine;
|
2655
|
-
}
|
2656
|
-
}
|
2657
|
-
}
|
2658
|
-
function isCallFromObject(node, objName) {
|
2659
|
-
return node && objName && node.type === "CallExpression" && get3(node, "callee.object.name") === objName;
|
2660
|
-
}
|
2661
|
-
function isComputed(node) {
|
2662
|
-
return get3(node, "computed") && node.property.type !== "Literal";
|
2663
|
-
}
|
2664
|
-
function isEquivalentMemberExp(a, b) {
|
2665
|
-
return isEqualWith2(a, b, (left, right, key) => {
|
2666
|
-
if (includes3(["loc", "range", "computed", "start", "end", "parent"], key)) {
|
2667
|
-
return true;
|
2668
|
-
}
|
2669
|
-
if (isComputed(left) || isComputed(right)) {
|
2670
|
-
return false;
|
2671
|
-
}
|
2672
|
-
if (key === "property") {
|
2673
|
-
const leftValue = left.name || left.value;
|
2674
|
-
const rightValue = right.name || right.value;
|
2675
|
-
return leftValue === rightValue;
|
2676
|
-
}
|
2677
|
-
});
|
2678
|
-
}
|
2679
|
-
var isEqEqEq = matches2({ type: "BinaryExpression", operator: "===" });
|
2680
|
-
var isMinus = (node) => node.type === "UnaryExpression" && node.operator === "-";
|
2681
|
-
var comparisonType = {
|
2682
|
-
exact: 0,
|
2683
|
-
over: 1,
|
2684
|
-
under: 2,
|
2685
|
-
any: 3
|
2686
|
-
};
|
2687
|
-
var comparisonOperators = ["==", "!=", "===", "!=="];
|
2688
|
-
function getIsValue(value2) {
|
2689
|
-
return value2 < 0 ? overEvery2(isMinus, matches2({ argument: { value: -value2 } })) : matches2({ value: value2 });
|
2690
|
-
}
|
2691
|
-
function getExpressionComparedToInt(node, value2, checkOver) {
|
2692
|
-
const isValue = getIsValue(value2);
|
2693
|
-
if (includes3(comparisonOperators, node.operator)) {
|
2694
|
-
if (isValue(node.right)) {
|
2695
|
-
return node.left;
|
2696
|
-
}
|
2697
|
-
if (isValue(node.left)) {
|
2698
|
-
return node.right;
|
2699
|
-
}
|
2700
|
-
}
|
2701
|
-
if (checkOver) {
|
2702
|
-
if (node.operator === ">" && isValue(node.right)) {
|
2703
|
-
return node.left;
|
2704
|
-
}
|
2705
|
-
if (node.operator === "<" && isValue(node.left)) {
|
2706
|
-
return node.right;
|
2707
|
-
}
|
2708
|
-
const isNext = getIsValue(value2 + 1);
|
2709
|
-
if ((node.operator === ">=" || node.operator === "<") && isNext(node.right)) {
|
2710
|
-
return node.left;
|
2711
|
-
}
|
2712
|
-
if ((node.operator === "<=" || node.operator === ">") && isNext(node.left)) {
|
2713
|
-
return node.right;
|
2714
|
-
}
|
2715
|
-
}
|
2716
|
-
}
|
2717
|
-
var isIndexOfCall = (node) => isMethodCall(node) && getMethodName(node) === "indexOf";
|
2718
|
-
var isFindIndexCall = (node) => isMethodCall(node) && getMethodName(node) === "findIndex";
|
2719
|
-
function collectParameterValues(node) {
|
2720
|
-
switch (node && node.type) {
|
2721
|
-
case "Identifier":
|
2722
|
-
return [node.name];
|
2723
|
-
case "ObjectPattern":
|
2724
|
-
return flatMap2(
|
2725
|
-
node.properties,
|
2726
|
-
(prop) => collectParameterValues(prop.value)
|
2727
|
-
);
|
2728
|
-
case "ArrayPattern":
|
2729
|
-
return flatMap2(node.elements, collectParameterValues);
|
2730
|
-
default:
|
2731
|
-
return [];
|
2732
|
-
}
|
2733
|
-
}
|
2734
|
-
var astUtil_default = {
|
2735
|
-
getCaller,
|
2736
|
-
getMethodName,
|
2737
|
-
isMethodCall,
|
2738
|
-
getFirstFunctionLine,
|
2739
|
-
isMemberExpOf,
|
2740
|
-
getFirstParamName,
|
2741
|
-
hasOnlyOneStatement,
|
2742
|
-
isObjectOfMethodCall,
|
2743
|
-
isEqEqEqToMemberOf: isBinaryExpWithMemberOf.bind(null, "==="),
|
2744
|
-
isNotEqEqToMemberOf: isBinaryExpWithMemberOf.bind(null, "!=="),
|
2745
|
-
isNegationOfMemberOf,
|
2746
|
-
isIdentifierWithName,
|
2747
|
-
isNegationExpression,
|
2748
|
-
getValueReturnedInFirstStatement,
|
2749
|
-
isCallFromObject,
|
2750
|
-
isComputed,
|
2751
|
-
isEquivalentMemberExp,
|
2752
|
-
isEqEqEq,
|
2753
|
-
comparisonType,
|
2754
|
-
getExpressionComparedToInt,
|
2755
|
-
isIndexOfCall,
|
2756
|
-
isFindIndexCall,
|
2757
|
-
isFunctionExpression,
|
2758
|
-
isFunctionDefinitionWithBlock,
|
2759
|
-
collectParameterValues
|
2760
|
-
};
|
2770
|
+
// src/util/remedaUtil.ts
|
2771
|
+
import { capitalize as capitalize2, includes as includes4 } from "lodash";
|
2761
2772
|
|
2762
2773
|
// src/util/settingsUtil.ts
|
2763
2774
|
import { chain as chain2, get as get4 } from "lodash";
|
@@ -2789,7 +2800,8 @@ var RemedaContext_default = class {
|
|
2789
2800
|
_pragma;
|
2790
2801
|
/**
|
2791
2802
|
* Create a Remeda context wrapper from a file's RuleContext
|
2792
|
-
*
|
2803
|
+
*
|
2804
|
+
* @param context
|
2793
2805
|
*/
|
2794
2806
|
constructor(context) {
|
2795
2807
|
this.context = context;
|
@@ -2798,7 +2810,8 @@ var RemedaContext_default = class {
|
|
2798
2810
|
}
|
2799
2811
|
/**
|
2800
2812
|
* Gets visitors to collect Remeda declarations in the context
|
2801
|
-
*
|
2813
|
+
*
|
2814
|
+
* @returns visitors for everywhere Remeda can be declared
|
2802
2815
|
*/
|
2803
2816
|
getImportVisitors() {
|
2804
2817
|
const self = this;
|
@@ -2808,15 +2821,17 @@ var RemedaContext_default = class {
|
|
2808
2821
|
specifiers.forEach((spec) => {
|
2809
2822
|
switch (spec.type) {
|
2810
2823
|
case "ImportNamespaceSpecifier":
|
2811
|
-
case "ImportDefaultSpecifier":
|
2824
|
+
case "ImportDefaultSpecifier": {
|
2812
2825
|
self.general[spec.local.name] = true;
|
2813
2826
|
break;
|
2814
|
-
|
2827
|
+
}
|
2828
|
+
case "ImportSpecifier": {
|
2815
2829
|
self.methods[spec.local.name] = spec.imported.name;
|
2816
2830
|
if (spec.imported.name === "chain") {
|
2817
2831
|
self.general[spec.local.name] = true;
|
2818
2832
|
}
|
2819
2833
|
break;
|
2834
|
+
}
|
2820
2835
|
}
|
2821
2836
|
});
|
2822
2837
|
} else {
|
@@ -2850,8 +2865,9 @@ var RemedaContext_default = class {
|
|
2850
2865
|
}
|
2851
2866
|
/**
|
2852
2867
|
* Returns whether the node is an imported Remeda in this context
|
2868
|
+
*
|
2853
2869
|
* @param node
|
2854
|
-
* @returns
|
2870
|
+
* @returns
|
2855
2871
|
*/
|
2856
2872
|
isImportedRemeda(node) {
|
2857
2873
|
if (node && node.type === "Identifier") {
|
@@ -2860,8 +2876,9 @@ var RemedaContext_default = class {
|
|
2860
2876
|
}
|
2861
2877
|
/**
|
2862
2878
|
* Returns the name of the Remeda method for this node, if any
|
2879
|
+
*
|
2863
2880
|
* @param node
|
2864
|
-
* @returns
|
2881
|
+
* @returns
|
2865
2882
|
*/
|
2866
2883
|
getImportedRemedaMethod(node) {
|
2867
2884
|
if (node && node.type === "CallExpression" && !isMethodCall2(node)) {
|
@@ -2870,15 +2887,16 @@ var RemedaContext_default = class {
|
|
2870
2887
|
}
|
2871
2888
|
/**
|
2872
2889
|
* Returns whether the node is a call from a Remeda object
|
2890
|
+
*
|
2873
2891
|
* @param node
|
2874
|
-
* @returns
|
2892
|
+
* @returns
|
2875
2893
|
*/
|
2876
2894
|
isRemedaCall(node) {
|
2877
2895
|
return this.pragma && isCallFromObject2(node, this.pragma) || this.isImportedRemeda(getCaller2(node));
|
2878
2896
|
}
|
2879
2897
|
/**
|
2880
2898
|
*
|
2881
|
-
* @returns
|
2899
|
+
* @returns the current Remeda pragma
|
2882
2900
|
*/
|
2883
2901
|
get pragma() {
|
2884
2902
|
if (!this._pragma) {
|
@@ -2956,7 +2974,6 @@ function getRemedaContext(context) {
|
|
2956
2974
|
}
|
2957
2975
|
|
2958
2976
|
// src/rules/collection-method-value.ts
|
2959
|
-
import includes5 from "lodash/includes";
|
2960
2977
|
var { getMethodName: getMethodName2 } = astUtil_default;
|
2961
2978
|
var meta = {
|
2962
2979
|
type: "problem",
|
@@ -3074,16 +3091,21 @@ function create4(context) {
|
|
3074
3091
|
const shouldCheckFunctionDeclarations = context.options[1] !== void 0 ? context.options[1] : false;
|
3075
3092
|
function isCompletelyLiteral(node) {
|
3076
3093
|
switch (node.type) {
|
3077
|
-
case "Literal":
|
3094
|
+
case "Literal": {
|
3078
3095
|
return true;
|
3079
|
-
|
3096
|
+
}
|
3097
|
+
case "BinaryExpression": {
|
3080
3098
|
return isCompletelyLiteral(node.left) && isCompletelyLiteral(node.right);
|
3081
|
-
|
3099
|
+
}
|
3100
|
+
case "UnaryExpression": {
|
3082
3101
|
return isCompletelyLiteral(node.argument);
|
3083
|
-
|
3102
|
+
}
|
3103
|
+
case "ConditionalExpression": {
|
3084
3104
|
return isCompletelyLiteral(node.test) && isCompletelyLiteral(node.consequent) && isCompletelyLiteral(node.alternate);
|
3085
|
-
|
3105
|
+
}
|
3106
|
+
default: {
|
3086
3107
|
return false;
|
3108
|
+
}
|
3087
3109
|
}
|
3088
3110
|
}
|
3089
3111
|
function reportIfLikeConstant(func, node) {
|
@@ -3380,7 +3402,7 @@ var RULE_NAME8 = "prefer-is-empty";
|
|
3380
3402
|
var prefer_is_empty_default = rule8;
|
3381
3403
|
|
3382
3404
|
// src/rules/prefer-is-nil.ts
|
3383
|
-
import _, {
|
3405
|
+
import _, { cond as cond3, matches as matches3, property as property3 } from "lodash";
|
3384
3406
|
var { isNegationExpression: isNegationExpression2, isEquivalentMemberExp: isEquivalentMemberExp2 } = astUtil_default;
|
3385
3407
|
var meta9 = {
|
3386
3408
|
type: "problem",
|