Skip to content

Commit c0f418e

Browse files
Chore: Remove lodash (#14287)
* Chore: Update table to ^6.0.9 * Chore: Remove lodash.last lodash.last(array) -> array[array.length - 1] * Chore: Remove lodash.get v = lodash.get(a, "b.c") -> if (a && a.b && a.b.c) v = a.b.c * Chore: Remove lodash.noop lodash.noop -> () => {} * Chore: Remove lodash.union https://exploringjs.com/impatient-js/ch_sets.html#union-a-b * Chore: Remove lodash.intersection https://exploringjs.com/impatient-js/ch_sets.html#intersection-a-b * Chore: Remove lodash.findLast lodash.findLast(array) -> [...array].reverse().find(_ =>_) * Chore: Remove lodash.overSome * Chore: Remove lodash.isPlainObject * Chore: Remove lodash.isString lodash.isString(str) -> typeof str === "string"; * Chore: Remove lodash.range * Chore: Remove lodash.sortedLastIndex https://www.30secondsofcode.org/js/s/sorted-last-index * Chore: Remove lodash.sortedIndexBy https://www.30secondsofcode.org/js/s/sorted-index-by * Chore: Remove lodash.sample https://www.30secondsofcode.org/js/s/sample * Chore: Remove lodash.flatMap * Chore: Remove lodash.flatten * Chore: Remove lodash.template * Chore: Remove lodash.escapeRegExp Add the escape-string-regexp package * Chore: Remove lodash.isEqual Add the fast-deep-equal package * Chore: Remove lodash.merge Add the deep-extend package * Chore: Remove lodash.cloneDeep Add the clone package * Chore: Remove lodash.omit Add the omit package * Chore: Remove lodash.upperFirst Add the upper-case-first package * Chore: Remove lodash.memoize Add the fast-memoize package * Chore: Remove lodash.mapValues Add the map-values package * Chore: Remove lodash.flatten * Chore: Remove lodash * Chore: Replace arrays.flat() * Chore: Replace clone with rfdc * Chore: Add comment about map-values * Chore: Remove omit dependency * Chore: Remove rfdc dependency * Chore: Remove upper-case-first dependency * Chore: Remove fast-memoize dependency * Chore: Apply suggestions in lib/linter/node-event-generator.js Co-authored-by: Milos Djermanovic <[email protected]> * Chore: Add tests for upperCaseFirst * Chore: Remove map-values dependency * Chore: Apply review suggestions * Chore: Upgrade deep-extend to ^0.6.0 * Chore: Replace deep-extend dependency with lodash.merge * Chore: Apply review suggestion * Chore: Simplify search code * Chore: Apply review suggestions Co-authored-by: Milos Djermanovic <[email protected]>
1 parent 52655dd commit c0f418e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+526
-220
lines changed

bin/eslint.js

+2-12
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,8 @@ function readStdin() {
6666
*/
6767
function getErrorMessage(error) {
6868

69-
// Lazy loading because those are used only if error happened.
70-
const fs = require("fs");
71-
const path = require("path");
69+
// Lazy loading because this is used only if an error happened.
7270
const util = require("util");
73-
const lodash = require("lodash");
7471

7572
// Foolproof -- thirdparty module might throw non-object.
7673
if (typeof error !== "object" || error === null) {
@@ -80,14 +77,7 @@ function getErrorMessage(error) {
8077
// Use templates if `error.messageTemplate` is present.
8178
if (typeof error.messageTemplate === "string") {
8279
try {
83-
const templateFilePath = path.resolve(
84-
__dirname,
85-
`../messages/${error.messageTemplate}.txt`
86-
);
87-
88-
// Use sync API because Node.js should exit at this tick.
89-
const templateText = fs.readFileSync(templateFilePath, "utf-8");
90-
const template = lodash.template(templateText);
80+
const template = require(`../messages/${error.messageTemplate}.js`);
9181

9282
return template(error.messageData || {});
9383
} catch {

docs/developer-guide/code-path-analysis.md

+5-7
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,6 @@ bar();
195195
### To check whether or not this is reachable
196196

197197
```js
198-
var last = require("lodash").last;
199-
200198
function isReachable(segment) {
201199
return segment.reachable;
202200
}
@@ -215,7 +213,7 @@ module.exports = function(context) {
215213

216214
// Checks reachable or not.
217215
"ExpressionStatement": function(node) {
218-
var codePath = last(codePathStack);
216+
var codePath = codePathStack[codePathStack.length - 1];
219217

220218
// Checks the current code path segments.
221219
if (!codePath.currentSegments.some(isReachable)) {
@@ -239,8 +237,6 @@ So a rule must not modify those instances.
239237
Please use a map of information instead.
240238

241239
```js
242-
var last = require("lodash").last;
243-
244240
function hasCb(node, context) {
245241
if (node.type.indexOf("Function") !== -1) {
246242
return context.getDeclaredVariables(node).some(function(v) {
@@ -285,8 +281,10 @@ module.exports = function(context) {
285281

286282
// Manages state of code paths.
287283
"onCodePathSegmentStart": function(segment) {
284+
var funcInfo = funcInfoStack[funcInfoStack.length - 1];
285+
288286
// Ignores if `cb` doesn't exist.
289-
if (!last(funcInfoStack).hasCb) {
287+
if (!funcInfo.hasCb) {
290288
return;
291289
}
292290

@@ -304,7 +302,7 @@ module.exports = function(context) {
304302

305303
// Checks reachable or not.
306304
"CallExpression": function(node) {
307-
var funcInfo = last(funcInfoStack);
305+
var funcInfo = funcInfoStack[funcInfoStack.length - 1];
308306

309307
// Ignores if `cb` doesn't exist.
310308
if (!funcInfo.hasCb) {

docs/developer-guide/code-path-analysis/README.md

+5-7
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,6 @@ bar();
195195
### To check whether or not this is reachable
196196

197197
```js
198-
var last = require("lodash").last;
199-
200198
function isReachable(segment) {
201199
return segment.reachable;
202200
}
@@ -215,7 +213,7 @@ module.exports = function(context) {
215213

216214
// Checks reachable or not.
217215
"ExpressionStatement": function(node) {
218-
var codePath = last(codePathStack);
216+
var codePath = codePathStack[codePathStack.length - 1];
219217

220218
// Checks the current code path segments.
221219
if (!codePath.currentSegments.some(isReachable)) {
@@ -239,8 +237,6 @@ So a rule must not modify those instances.
239237
Please use a map of information instead.
240238

241239
```js
242-
var last = require("lodash").last;
243-
244240
function hasCb(node, context) {
245241
if (node.type.indexOf("Function") !== -1) {
246242
return context.getDeclaredVariables(node).some(function(v) {
@@ -285,8 +281,10 @@ module.exports = function(context) {
285281

286282
// Manages state of code paths.
287283
"onCodePathSegmentStart": function(segment) {
284+
var funcInfo = funcInfoStack[funcInfoStack - 1];
285+
288286
// Ignores if `cb` doesn't exist.
289-
if (!last(funcInfoStack).hasCb) {
287+
if (!funcInfo.hasCb) {
290288
return;
291289
}
292290

@@ -304,7 +302,7 @@ module.exports = function(context) {
304302

305303
// Checks reachable or not.
306304
"CallExpression": function(node) {
307-
var funcInfo = last(funcInfoStack);
305+
var funcInfo = funcInfoStack[funcInfoStack - 1];
308306

309307
// Ignores if `cb` doesn't exist.
310308
if (!funcInfo.hasCb) {

lib/cli-engine/file-enumerator.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const fs = require("fs");
3838
const path = require("path");
3939
const getGlobParent = require("glob-parent");
4040
const isGlob = require("is-glob");
41-
const { escapeRegExp } = require("lodash");
41+
const escapeRegExp = require("escape-string-regexp");
4242
const { Minimatch } = require("minimatch");
4343

4444
const {

lib/cli-engine/formatters/html-template-message.html

-8
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"use strict";
2+
3+
module.exports = function(it, encodeHTML) {
4+
const {
5+
parentIndex,
6+
lineNumber,
7+
columnNumber,
8+
severityNumber,
9+
severityName,
10+
message,
11+
ruleUrl,
12+
ruleId
13+
} = it;
14+
15+
return `
16+
<tr style="display:none" class="f-${parentIndex}">
17+
<td>${lineNumber}:${columnNumber}</td>
18+
<td class="clr-${severityNumber}">${severityName}</td>
19+
<td>${encodeHTML(message)}</td>
20+
<td>
21+
<a href="${ruleUrl ? ruleUrl : ""}" target="_blank" rel="noopener noreferrer">${ruleId ? ruleId : ""}</a>
22+
</td>
23+
</tr>
24+
`.trimLeft();
25+
};

lib/cli-engine/formatters/html-template-page.html renamed to lib/cli-engine/formatters/html-template-page.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
"use strict";
2+
3+
module.exports = function(it) {
4+
const { reportColor, reportSummary, date, results } = it;
5+
6+
return `
17
<!DOCTYPE html>
28
<html>
39
<head>
@@ -88,15 +94,15 @@
8894
</style>
8995
</head>
9096
<body>
91-
<div id="overview" class="bg-<%= reportColor %>">
97+
<div id="overview" class="bg-${reportColor}">
9298
<h1>ESLint Report</h1>
9399
<div>
94-
<span><%= reportSummary %></span> - Generated on <%= date %>
100+
<span>${reportSummary}</span> - Generated on ${date}
95101
</div>
96102
</div>
97103
<table>
98104
<tbody>
99-
<%= results %>
105+
${results}
100106
</tbody>
101107
</table>
102108
<script type="text/javascript">
@@ -113,3 +119,5 @@ <h1>ESLint Report</h1>
113119
</script>
114120
</body>
115121
</html>
122+
`.trimLeft();
123+
};

lib/cli-engine/formatters/html-template-result.html

-6
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"use strict";
2+
3+
module.exports = function(it, encodeHTML) {
4+
const { color, index, filePath, summary } = it;
5+
6+
return `
7+
<tr class="bg-${color}" data-group="f-${index}">
8+
<th colspan="4">
9+
[+] ${encodeHTML(filePath)}
10+
<span>${encodeHTML(summary)}</span>
11+
</th>
12+
</tr>
13+
`.trimLeft();
14+
};

lib/cli-engine/formatters/html.js

+25-11
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,30 @@
44
*/
55
"use strict";
66

7-
const lodash = require("lodash");
8-
const fs = require("fs");
9-
const path = require("path");
10-
117
//------------------------------------------------------------------------------
128
// Helpers
139
//------------------------------------------------------------------------------
1410

15-
const pageTemplate = lodash.template(fs.readFileSync(path.join(__dirname, "html-template-page.html"), "utf-8"));
16-
const messageTemplate = lodash.template(fs.readFileSync(path.join(__dirname, "html-template-message.html"), "utf-8"));
17-
const resultTemplate = lodash.template(fs.readFileSync(path.join(__dirname, "html-template-result.html"), "utf-8"));
11+
const encodeHTML = (function() {
12+
const encodeHTMLRules = {
13+
"&": "&#38;",
14+
"<": "&#60;",
15+
">": "&#62;",
16+
'"': "&#34;",
17+
"'": "&#39;"
18+
};
19+
const matchHTML = /[&<>"']/ug;
20+
21+
return function(code) {
22+
return code
23+
? code.toString().replace(matchHTML, m => encodeHTMLRules[m] || m)
24+
: "";
25+
};
26+
}());
27+
28+
const pageTemplate = require("./html-template-page.js");
29+
const messageTemplate = require("./html-template-message.js");
30+
const resultTemplate = require("./html-template-result.js");
1831

1932
/**
2033
* Given a word and a count, append an s if count is not one.
@@ -80,7 +93,9 @@ function renderMessages(messages, parentIndex, rulesMeta) {
8093
if (rulesMeta) {
8194
const meta = rulesMeta[message.ruleId];
8295

83-
ruleUrl = lodash.get(meta, "docs.url", null);
96+
if (meta && meta.docs && meta.docs.url) {
97+
ruleUrl = meta.docs.url;
98+
}
8499
}
85100

86101
return messageTemplate({
@@ -92,7 +107,7 @@ function renderMessages(messages, parentIndex, rulesMeta) {
92107
message: message.message,
93108
ruleId: message.ruleId,
94109
ruleUrl
95-
});
110+
}, encodeHTML);
96111
}).join("\n");
97112
}
98113

@@ -108,8 +123,7 @@ function renderResults(results, rulesMeta) {
108123
color: renderColor(result.errorCount, result.warningCount),
109124
filePath: result.filePath,
110125
summary: renderSummary(result.errorCount, result.warningCount)
111-
112-
}) + renderMessages(result.messages, index, rulesMeta)).join("\n");
126+
}, encodeHTML) + renderMessages(result.messages, index, rulesMeta)).join("\n");
113127
}
114128

115129
//------------------------------------------------------------------------------

lib/init/autoconfig.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// Requirements
1010
//------------------------------------------------------------------------------
1111

12-
const lodash = require("lodash"),
12+
const equal = require("fast-deep-equal"),
1313
recConfig = require("../../conf/eslint-recommended"),
1414
ConfigOps = require("@eslint/eslintrc/lib/shared/config-ops"),
1515
{ Linter } = require("../linter"),
@@ -329,7 +329,7 @@ function extendFromRecommended(config) {
329329
const recRules = Object.keys(recConfig.rules).filter(ruleId => ConfigOps.isErrorSeverity(recConfig.rules[ruleId]));
330330

331331
recRules.forEach(ruleId => {
332-
if (lodash.isEqual(recConfig.rules[ruleId], newConfig.rules[ruleId])) {
332+
if (equal(recConfig.rules[ruleId], newConfig.rules[ruleId])) {
333333
delete newConfig.rules[ruleId];
334334
}
335335
});

lib/linter/apply-disable-directives.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
"use strict";
77

8-
const lodash = require("lodash");
9-
108
/**
119
* Compares the locations of two objects in a source file
1210
* @param {{line: number, column: number}} itemA The first object
@@ -124,7 +122,21 @@ module.exports = ({ directives, problems, reportUnusedDisableDirectives = "off"
124122
.map(directive => Object.assign({}, directive, { unprocessedDirective: directive }))
125123
.sort(compareLocations);
126124

127-
const lineDirectives = lodash.flatMap(directives, directive => {
125+
/**
126+
* Returns a new array formed by applying a given callback function to each element of the array, and then flattening the result by one level.
127+
* TODO(stephenwade): Replace this with array.flatMap when we drop support for Node v10
128+
* @param {any[]} array The array to process
129+
* @param {Function} fn The function to use
130+
* @returns {any[]} The result array
131+
*/
132+
function flatMap(array, fn) {
133+
const mapped = array.map(fn);
134+
const flattened = [].concat(...mapped);
135+
136+
return flattened;
137+
}
138+
139+
const lineDirectives = flatMap(directives, directive => {
128140
switch (directive.type) {
129141
case "disable":
130142
case "enable":

lib/linter/linter.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const
1515
eslintScope = require("eslint-scope"),
1616
evk = require("eslint-visitor-keys"),
1717
espree = require("espree"),
18-
lodash = require("lodash"),
18+
merge = require("lodash.merge"),
1919
BuiltInEnvironments = require("@eslint/eslintrc/conf/environments"),
2020
pkg = require("../../package.json"),
2121
astUtils = require("../shared/ast-utils"),
@@ -529,8 +529,8 @@ function normalizeVerifyOptions(providedOptions, config) {
529529
function resolveParserOptions(parserName, providedOptions, enabledEnvironments) {
530530
const parserOptionsFromEnv = enabledEnvironments
531531
.filter(env => env.parserOptions)
532-
.reduce((parserOptions, env) => lodash.merge(parserOptions, env.parserOptions), {});
533-
const mergedParserOptions = lodash.merge(parserOptionsFromEnv, providedOptions || {});
532+
.reduce((parserOptions, env) => merge(parserOptions, env.parserOptions), {});
533+
const mergedParserOptions = merge(parserOptionsFromEnv, providedOptions || {});
534534
const isModule = mergedParserOptions.sourceType === "module";
535535

536536
if (isModule) {
@@ -1286,7 +1286,9 @@ class Linter {
12861286
const filenameToExpose = normalizeFilename(filename);
12871287
const text = ensureText(textOrSourceCode);
12881288
const preprocess = options.preprocess || (rawText => [rawText]);
1289-
const postprocess = options.postprocess || lodash.flatten;
1289+
1290+
// TODO(stephenwade): Replace this with array.flat() when we drop support for Node v10
1291+
const postprocess = options.postprocess || (array => [].concat(...array));
12901292
const filterCodeBlock =
12911293
options.filterCodeBlock ||
12921294
(blockFilename => blockFilename.endsWith(".js"));

0 commit comments

Comments
 (0)