Skip to content
Permalink
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 <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 c0f418e2476df98519bc156b81d20431984e8704
Show file tree
Hide file tree
Showing 58 changed files with 526 additions and 220 deletions.
@@ -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) {
@@ -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 {
@@ -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;
}
@@ -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)) {
@@ -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) {
@@ -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;
}
@@ -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) {
@@ -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;
}
@@ -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)) {
@@ -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) {
@@ -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;
}
@@ -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) {
@@ -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 {

This file was deleted.

@@ -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>
@@ -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">
@@ -113,3 +119,5 @@ <h1>ESLint Report</h1>
</script>
</body>
</html>
`.trimLeft();
};

This file was deleted.

@@ -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();
};
@@ -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.
@@ -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({
@@ -92,7 +107,7 @@ function renderMessages(messages, parentIndex, rulesMeta) {
message: message.message,
ruleId: message.ruleId,
ruleUrl
});
}, encodeHTML);
}).join("\n");
}

@@ -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");
}

//------------------------------------------------------------------------------
@@ -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"),
@@ -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];
}
});
@@ -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
@@ -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":
@@ -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"),
@@ -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) {
@@ -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"));

0 comments on commit c0f418e

Please sign in to comment.