Skip to content

Commit

Permalink
refactor: Drop dependency on text-table to improve performance
Browse files Browse the repository at this point in the history
Closes eslint#18709

Signed-off-by: Jon Koops <[email protected]>
  • Loading branch information
jonkoops committed Jul 26, 2024
1 parent 8e1a627 commit 88f64da
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 16 deletions.
70 changes: 56 additions & 14 deletions lib/cli-engine/formatters/stylish.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"use strict";

const chalk = require("chalk"),
stripAnsi = require("strip-ansi"),
table = require("text-table");
stripAnsi = require("strip-ansi");

//------------------------------------------------------------------------------
// Helpers
Expand All @@ -22,6 +21,51 @@ function pluralize(word, count) {
return (count === 1 ? word : `${word}s`);
}

/**
* Formats a two-dimensional array of strings into a table.
* @param {string[][]} rows The rows of the table, each row being an array of strings representing the columns.
* @returns {string} The formatted table.
*/
function table(rows) {
const columnSizes = new Array(rows[0].length).fill(0);

for (const row of rows) {
for (let columnIndex = 0; columnIndex < row.length; columnIndex++) {
const length = stripAnsi(row[columnIndex]).length;

if (length > columnSizes[columnIndex]) {
columnSizes[columnIndex] = length;
}
}
}

const hsep = " ";
let result = "";

for (let rowIndex = 0; rowIndex < rows.length; rowIndex++) {
const row = rows[rowIndex];

for (let columnIndex = 0; columnIndex < row.length; columnIndex++) {
const column = row[columnIndex];
const spacing = " ".repeat(columnSizes[columnIndex] - stripAnsi(column).length);

result += hsep;

if (columnIndex === 0) {
result += spacing + column;
} else {
result += column + (columnIndex === row.length - 1 ? "" : spacing);
}
}

if (rowIndex < rows.length - 1) {
result += "\n";
}
}

return result;
}

//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -60,21 +104,19 @@ module.exports = function(results) {
messageType = chalk.yellow("warning");
}
return [
"",
message.line || 0,
message.column || 0,
const row = [
(message.line || 0).toString(),
(message.column || 0).toString(),
messageType,
message.message.replace(/([^ ])\.$/u, "$1"),
chalk.dim(message.ruleId || "")
message.message.replace(/([^ ])\.$/u, "$1")
];
}),
{
align: ["", "r", "l"],
stringLength(str) {
return stripAnsi(str).length;
if (message.ruleId) {
row.push(chalk.dim(message.ruleId));
}
}
return row;
})
).split("\n").map(el => el.replace(/(\d+)\s+(\d+)/u, (m, p1, p2) => chalk.dim(`${p1}:${p2}`))).join("\n")}\n\n`;
});

Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@
"minimatch": "^3.1.2",
"natural-compare": "^1.4.0",
"optionator": "^0.9.3",
"strip-ansi": "^6.0.1",
"text-table": "^0.2.0"
"strip-ansi": "^6.0.1"
},
"devDependencies": {
"@babel/core": "^7.4.3",
Expand Down
28 changes: 28 additions & 0 deletions tests/lib/cli-engine/formatters/stylish.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,4 +419,32 @@ describe("formatter:stylish", () => {
assert.include(result, " 9 errors and 3 warnings potentially fixable with the `--fix` option.\n");
});
});

describe("when passed mixed length messages", () => {
const code = [{
filePath: "foo.js",
errorCount: 1,
warningCount: 1,
messages: [{
message: "Unexpected short.",
severity: 2,
line: 5,
column: 10,
ruleId: "short"
}, {
message: "Unexpected long named message.",
severity: 1,
line: 106,
column: 11,
ruleId: "aVeryLongNameForARule"
}]
}];

it("should return a string properly aligned as a table", () => {
const result = formatter(code);

assert.strictEqual(result, "\nfoo.js\n 5:10 error Unexpected short short\n 106:11 warning Unexpected long named message aVeryLongNameForARule\n\n\u2716 2 problems (1 error, 1 warning)\n");
});
});

});

0 comments on commit 88f64da

Please sign in to comment.