Skip to content

Commit

Permalink
Chore: Remove lodash (#14287)
Browse files Browse the repository at this point in the history
* 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 <milos.djermanovic@gmail.com>

* 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 <milos.djermanovic@gmail.com>
  • Loading branch information
stephenwade and mdjermanovic committed May 21, 2021
1 parent 52655dd commit c0f418e
Show file tree
Hide file tree
Showing 58 changed files with 526 additions and 220 deletions.
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 ? ruleUrl : ""}" target="_blank" rel="noopener noreferrer">${ruleId ? ruleId : ""}</a>
</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();
};
36 changes: 25 additions & 11 deletions lib/cli-engine/formatters/html.js
Expand Up @@ -4,17 +4,30 @@
*/
"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;"
};
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 +93,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 +107,7 @@ function renderMessages(messages, parentIndex, rulesMeta) {
message: message.message,
ruleId: message.ruleId,
ruleUrl
});
}, encodeHTML);
}).join("\n");
}

Expand All @@ -108,8 +123,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
* @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"),
merge = require("lodash.merge"),
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) => merge(parserOptions, env.parserOptions), {});
const mergedParserOptions = merge(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

0 comments on commit c0f418e

Please sign in to comment.