Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Remove lodash #14287

Merged
merged 43 commits into from May 21, 2021
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
91c5b25
Chore: Update table to ^6.0.9
stephenwade Apr 1, 2021
17917e6
Chore: Remove lodash.last
stephenwade Apr 1, 2021
bbb79fc
Chore: Remove lodash.get
stephenwade Apr 1, 2021
052dde9
Chore: Remove lodash.noop
stephenwade Apr 1, 2021
62a3c09
Chore: Remove lodash.union
stephenwade Mar 4, 2021
8f836c1
Chore: Remove lodash.intersection
stephenwade Mar 4, 2021
93691e3
Chore: Remove lodash.findLast
stephenwade Mar 4, 2021
ae99066
Chore: Remove lodash.overSome
stephenwade Mar 4, 2021
b8e7710
Chore: Remove lodash.isPlainObject
stephenwade Mar 4, 2021
5d4adc4
Chore: Remove lodash.isString
stephenwade Mar 4, 2021
2cf5b34
Chore: Remove lodash.range
stephenwade Apr 1, 2021
cf8f628
Chore: Remove lodash.sortedLastIndex
stephenwade Mar 4, 2021
02510b2
Chore: Remove lodash.sortedIndexBy
stephenwade Mar 4, 2021
3975e6a
Chore: Remove lodash.sample
stephenwade Apr 1, 2021
03a20a0
Chore: Remove lodash.flatMap
stephenwade Apr 1, 2021
cc9659e
Chore: Remove lodash.flatten
stephenwade Apr 1, 2021
9e16719
Chore: Remove lodash.template
stephenwade Apr 1, 2021
9ab638c
Chore: Remove lodash.escapeRegExp
stephenwade Apr 1, 2021
cdecb6e
Chore: Remove lodash.isEqual
stephenwade Apr 1, 2021
0021d45
Chore: Remove lodash.merge
stephenwade Mar 3, 2021
70ecd65
Chore: Remove lodash.cloneDeep
stephenwade Mar 3, 2021
ae87d9b
Chore: Remove lodash.omit
stephenwade Apr 1, 2021
c720a5b
Chore: Remove lodash.upperFirst
stephenwade Apr 1, 2021
2dded3c
Chore: Remove lodash.memoize
stephenwade Mar 4, 2021
d00daec
Chore: Remove lodash.mapValues
stephenwade Apr 2, 2021
2cf7802
Chore: Remove lodash.flatten
stephenwade Apr 2, 2021
2c94559
Chore: Remove lodash
stephenwade Apr 2, 2021
1dc8a39
Chore: Replace arrays.flat()
stephenwade Apr 2, 2021
8354c69
Chore: Replace clone with rfdc
stephenwade Apr 8, 2021
a28cf2d
Chore: Add comment about map-values
stephenwade Apr 14, 2021
ada00d5
Chore: Remove omit dependency
stephenwade Apr 14, 2021
f9ac034
Chore: Remove rfdc dependency
stephenwade Apr 14, 2021
7dff256
Chore: Remove upper-case-first dependency
stephenwade Apr 15, 2021
6232136
Chore: Remove fast-memoize dependency
stephenwade Apr 24, 2021
4c70110
Chore: Apply suggestions in lib/linter/node-event-generator.js
stephenwade Apr 24, 2021
e46f471
Chore: Add tests for upperCaseFirst
stephenwade Apr 24, 2021
c20cce3
Chore: Remove map-values dependency
stephenwade May 6, 2021
a1bd47b
Chore: Apply review suggestions
stephenwade May 6, 2021
b3d6d9a
Chore: Upgrade deep-extend to ^0.6.0
stephenwade May 8, 2021
e0156d0
Chore: Replace deep-extend dependency with lodash.merge
stephenwade May 12, 2021
cba69f2
Chore: Apply review suggestion
stephenwade May 19, 2021
414766e
Chore: Simplify search code
stephenwade May 20, 2021
bf74251
Chore: Apply review suggestions
stephenwade May 20, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 2 additions & 12 deletions bin/eslint.js
Expand Up @@ -66,11 +66,8 @@ function readStdin() {
*/
function getErrorMessage(error) {

// Lazy loading because those are used only if error happened.
const fs = require("fs");
const path = require("path");
// Lazy loading because this is used only if an error happened.
const util = require("util");
const lodash = require("lodash");

// Foolproof -- thirdparty module might throw non-object.
if (typeof error !== "object" || error === null) {
Expand All @@ -80,14 +77,7 @@ function getErrorMessage(error) {
// Use templates if `error.messageTemplate` is present.
if (typeof error.messageTemplate === "string") {
try {
const templateFilePath = path.resolve(
__dirname,
`../messages/${error.messageTemplate}.txt`
);

// Use sync API because Node.js should exit at this tick.
const templateText = fs.readFileSync(templateFilePath, "utf-8");
const template = lodash.template(templateText);
const template = require(`../messages/${error.messageTemplate}.js`);

return template(error.messageData || {});
} catch {
Expand Down
12 changes: 5 additions & 7 deletions docs/developer-guide/code-path-analysis.md
Expand Up @@ -195,8 +195,6 @@ bar();
### To check whether or not this is reachable

```js
var last = require("lodash").last;

function isReachable(segment) {
return segment.reachable;
}
Expand All @@ -215,7 +213,7 @@ module.exports = function(context) {

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

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

```js
var last = require("lodash").last;

function hasCb(node, context) {
if (node.type.indexOf("Function") !== -1) {
return context.getDeclaredVariables(node).some(function(v) {
Expand Down Expand Up @@ -285,8 +281,10 @@ module.exports = function(context) {

// Manages state of code paths.
"onCodePathSegmentStart": function(segment) {
var funcInfo = funcInfoStack[funcInfoStack.length - 1];

// Ignores if `cb` doesn't exist.
if (!last(funcInfoStack).hasCb) {
if (!funcInfo.hasCb) {
return;
}

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

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

// Ignores if `cb` doesn't exist.
if (!funcInfo.hasCb) {
Expand Down
12 changes: 5 additions & 7 deletions docs/developer-guide/code-path-analysis/README.md
Expand Up @@ -195,8 +195,6 @@ bar();
### To check whether or not this is reachable

```js
var last = require("lodash").last;

function isReachable(segment) {
return segment.reachable;
}
Expand All @@ -215,7 +213,7 @@ module.exports = function(context) {

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

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

```js
var last = require("lodash").last;

function hasCb(node, context) {
if (node.type.indexOf("Function") !== -1) {
return context.getDeclaredVariables(node).some(function(v) {
Expand Down Expand Up @@ -285,8 +281,10 @@ module.exports = function(context) {

// Manages state of code paths.
"onCodePathSegmentStart": function(segment) {
var funcInfo = funcInfoStack[funcInfoStack - 1];

// Ignores if `cb` doesn't exist.
if (!last(funcInfoStack).hasCb) {
if (!funcInfo.hasCb) {
return;
}

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

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

// Ignores if `cb` doesn't exist.
if (!funcInfo.hasCb) {
Expand Down
2 changes: 1 addition & 1 deletion lib/cli-engine/file-enumerator.js
Expand Up @@ -38,7 +38,7 @@ const fs = require("fs");
const path = require("path");
const getGlobParent = require("glob-parent");
const isGlob = require("is-glob");
const { escapeRegExp } = require("lodash");
const escapeRegExp = require("escape-string-regexp");
const { Minimatch } = require("minimatch");

const {
Expand Down
8 changes: 0 additions & 8 deletions lib/cli-engine/formatters/html-template-message.html

This file was deleted.

25 changes: 25 additions & 0 deletions lib/cli-engine/formatters/html-template-message.js
@@ -0,0 +1,25 @@
"use strict";

module.exports = function(it, encodeHTML) {
const {
parentIndex,
lineNumber,
columnNumber,
severityNumber,
severityName,
message,
ruleUrl,
ruleId
} = it;

return `
<tr style="display:none" class="f-${parentIndex}">
<td>${lineNumber}:${columnNumber}</td>
<td class="clr-${severityNumber}">${severityName}</td>
<td>${encodeHTML(message)}</td>
<td>
<a href="${ruleUrl}" target="_blank" rel="noopener noreferrer">${ruleId ? ruleId : ""}</a>
stephenwade marked this conversation as resolved.
Show resolved Hide resolved
</td>
</tr>
`.trimLeft();
};
@@ -1,3 +1,9 @@
"use strict";

module.exports = function(it) {
const { reportColor, reportSummary, date, results } = it;

return `
<!DOCTYPE html>
<html>
<head>
Expand Down Expand Up @@ -88,15 +94,15 @@
</style>
</head>
<body>
<div id="overview" class="bg-<%= reportColor %>">
<div id="overview" class="bg-${reportColor}">
<h1>ESLint Report</h1>
<div>
<span><%= reportSummary %></span> - Generated on <%= date %>
<span>${reportSummary}</span> - Generated on ${date}
</div>
</div>
<table>
<tbody>
<%= results %>
${results}
</tbody>
</table>
<script type="text/javascript">
Expand All @@ -113,3 +119,5 @@ <h1>ESLint Report</h1>
</script>
</body>
</html>
`.trimLeft();
};
6 changes: 0 additions & 6 deletions lib/cli-engine/formatters/html-template-result.html

This file was deleted.

14 changes: 14 additions & 0 deletions lib/cli-engine/formatters/html-template-result.js
@@ -0,0 +1,14 @@
"use strict";

module.exports = function(it, encodeHTML) {
const { color, index, filePath, summary } = it;

return `
<tr class="bg-${color}" data-group="f-${index}">
<th colspan="4">
[+] ${encodeHTML(filePath)}
<span>${encodeHTML(summary)}</span>
</th>
</tr>
`.trimLeft();
};
37 changes: 26 additions & 11 deletions lib/cli-engine/formatters/html.js
Expand Up @@ -4,17 +4,31 @@
*/
"use strict";

const lodash = require("lodash");
const fs = require("fs");
const path = require("path");

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

const pageTemplate = lodash.template(fs.readFileSync(path.join(__dirname, "html-template-page.html"), "utf-8"));
const messageTemplate = lodash.template(fs.readFileSync(path.join(__dirname, "html-template-message.html"), "utf-8"));
const resultTemplate = lodash.template(fs.readFileSync(path.join(__dirname, "html-template-result.html"), "utf-8"));
const encodeHTML = (function() {
const encodeHTMLRules = {
"&": "&#38;",
"<": "&#60;",
">": "&#62;",
'"': "&#34;",
"'": "&#39;",
"/": "&#47;"
stephenwade marked this conversation as resolved.
Show resolved Hide resolved
};
const matchHTML = /[&<>"'/]/ug;

return function(code) {
return code
? code.toString().replace(matchHTML, m => encodeHTMLRules[m] || m)
: "";
};
}());

const pageTemplate = require("./html-template-page.js");
const messageTemplate = require("./html-template-message.js");
const resultTemplate = require("./html-template-result.js");

/**
* Given a word and a count, append an s if count is not one.
Expand Down Expand Up @@ -80,7 +94,9 @@ function renderMessages(messages, parentIndex, rulesMeta) {
if (rulesMeta) {
const meta = rulesMeta[message.ruleId];

ruleUrl = lodash.get(meta, "docs.url", null);
if (meta && meta.docs && meta.docs.url) {
ruleUrl = meta.docs.url;
}
}

return messageTemplate({
Expand All @@ -92,7 +108,7 @@ function renderMessages(messages, parentIndex, rulesMeta) {
message: message.message,
ruleId: message.ruleId,
ruleUrl
});
}, encodeHTML);
}).join("\n");
}

Expand All @@ -108,8 +124,7 @@ function renderResults(results, rulesMeta) {
color: renderColor(result.errorCount, result.warningCount),
filePath: result.filePath,
summary: renderSummary(result.errorCount, result.warningCount)

}) + renderMessages(result.messages, index, rulesMeta)).join("\n");
}, encodeHTML) + renderMessages(result.messages, index, rulesMeta)).join("\n");
}

//------------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions lib/init/autoconfig.js
Expand Up @@ -9,7 +9,7 @@
// Requirements
//------------------------------------------------------------------------------

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

recRules.forEach(ruleId => {
if (lodash.isEqual(recConfig.rules[ruleId], newConfig.rules[ruleId])) {
if (equal(recConfig.rules[ruleId], newConfig.rules[ruleId])) {
delete newConfig.rules[ruleId];
}
});
Expand Down
18 changes: 15 additions & 3 deletions lib/linter/apply-disable-directives.js
Expand Up @@ -5,8 +5,6 @@

"use strict";

const lodash = require("lodash");

/**
* Compares the locations of two objects in a source file
* @param {{line: number, column: number}} itemA The first object
Expand Down Expand Up @@ -124,7 +122,21 @@ module.exports = ({ directives, problems, reportUnusedDisableDirectives = "off"
.map(directive => Object.assign({}, directive, { unprocessedDirective: directive }))
.sort(compareLocations);

const lineDirectives = lodash.flatMap(directives, directive => {
/**
* 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.
* TODO(stephenwade): Replace this with array.flatMap when we drop support for Node v10
stephenwade marked this conversation as resolved.
Show resolved Hide resolved
* @param {any[]} array The array to process
* @param {Function} fn The function to use
* @returns {any[]} The result array
*/
function flatMap(array, fn) {
const mapped = array.map(fn);
const flattened = [].concat(...mapped);

return flattened;
}

const lineDirectives = flatMap(directives, directive => {
switch (directive.type) {
case "disable":
case "enable":
Expand Down
10 changes: 6 additions & 4 deletions lib/linter/linter.js
Expand Up @@ -15,7 +15,7 @@ const
eslintScope = require("eslint-scope"),
evk = require("eslint-visitor-keys"),
espree = require("espree"),
lodash = require("lodash"),
extend = require("deep-extend"),
BuiltInEnvironments = require("@eslint/eslintrc/conf/environments"),
pkg = require("../../package.json"),
astUtils = require("../shared/ast-utils"),
Expand Down Expand Up @@ -529,8 +529,8 @@ function normalizeVerifyOptions(providedOptions, config) {
function resolveParserOptions(parserName, providedOptions, enabledEnvironments) {
const parserOptionsFromEnv = enabledEnvironments
.filter(env => env.parserOptions)
.reduce((parserOptions, env) => lodash.merge(parserOptions, env.parserOptions), {});
const mergedParserOptions = lodash.merge(parserOptionsFromEnv, providedOptions || {});
.reduce((parserOptions, env) => extend(parserOptions, env.parserOptions), {});
const mergedParserOptions = extend(parserOptionsFromEnv, providedOptions || {});
const isModule = mergedParserOptions.sourceType === "module";

if (isModule) {
Expand Down Expand Up @@ -1286,7 +1286,9 @@ class Linter {
const filenameToExpose = normalizeFilename(filename);
const text = ensureText(textOrSourceCode);
const preprocess = options.preprocess || (rawText => [rawText]);
const postprocess = options.postprocess || lodash.flatten;

// TODO(stephenwade): Replace this with array.flat() when we drop support for Node v10
const postprocess = options.postprocess || (array => [].concat(...array));
const filterCodeBlock =
options.filterCodeBlock ||
(blockFilename => blockFilename.endsWith(".js"));
Expand Down