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: Switch to native Array flat/flatMap #14614

Merged
merged 1 commit into from Aug 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 2 additions & 17 deletions lib/linter/apply-disable-directives.js
Expand Up @@ -96,20 +96,6 @@ function createCommentRemoval(directives, commentToken) {
};
}

/**
* 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;
}

/**
* Parses details from directives to create output Problems.
* @param {Directive[]} allDirectives Unused directives to be removed.
Expand All @@ -118,8 +104,7 @@ function flatMap(array, fn) {
function processUnusedDisableDirectives(allDirectives) {
const directiveGroups = groupByParentComment(allDirectives);

return flatMap(
directiveGroups,
return directiveGroups.flatMap(
directives => {
const { parentComment } = directives[0].unprocessedDirective;
const remainingRuleIds = new Set(parentComment.ruleIds);
Expand Down Expand Up @@ -247,7 +232,7 @@ module.exports = ({ directives, disableFixes, problems, reportUnusedDisableDirec
.map(directive => Object.assign({}, directive, { unprocessedDirective: directive }))
.sort(compareLocations);

const lineDirectives = flatMap(directives, directive => {
const lineDirectives = directives.flatMap(directive => {
switch (directive.type) {
case "disable":
case "enable":
Expand Down
3 changes: 1 addition & 2 deletions lib/linter/linter.js
Expand Up @@ -1306,8 +1306,7 @@ class Linter {
const text = ensureText(textOrSourceCode);
const preprocess = options.preprocess || (rawText => [rawText]);

// TODO(stephenwade): Replace this with array.flat() when we drop support for Node v10
const postprocess = options.postprocess || (array => [].concat(...array));
const postprocess = options.postprocess || (messagesList => messagesList.flat());
const filterCodeBlock =
options.filterCodeBlock ||
(blockFilename => blockFilename.endsWith(".js"));
Expand Down
4 changes: 1 addition & 3 deletions lib/linter/node-event-generator.js
Expand Up @@ -37,9 +37,7 @@ const esquery = require("esquery");
* @returns {any[]} The union of the input arrays
*/
function union(...arrays) {

// TODO(stephenwade): Replace this with arrays.flat() when we drop support for Node v10
return [...new Set([].concat(...arrays))];
return [...new Set(arrays.flat())];
}

/**
Expand Down
16 changes: 1 addition & 15 deletions lib/rules/max-lines.js
Expand Up @@ -137,20 +137,6 @@ module.exports = {
return [];
}

/**
* 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;
}

return {
"Program:exit"() {
let lines = sourceCode.lines.map((text, i) => ({
Expand All @@ -173,7 +159,7 @@ module.exports = {
if (skipComments) {
const comments = sourceCode.getAllComments();

const commentLines = flatMap(comments, comment => getLinesWithoutCode(comment));
const commentLines = comments.flatMap(getLinesWithoutCode);

lines = lines.filter(
l => !commentLines.includes(l.lineNumber)
Expand Down