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

fix: update prettier to respect stderr and stdout concurrently #722

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 54 additions & 7 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 40 additions & 7 deletions src/linters/prettier.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
const { sep } = require("path");

const { run } = require("../utils/action");
const commandExists = require("../utils/command-exists");
const { initLintResult } = require("../utils/lint-result");
const { getNpmBinCommand } = require("../utils/npm/get-npm-bin-command");
const { removeANSIColorCodes } = require("../utils/string");

/** @typedef {import('../utils/lint-result').LintResult} LintResult */

const PARSE_REGEX = /^\[(warning|error)] ([^:]*): (.*) \(([0-9]+):([0-9]+)\)$/gm;

/**
* https://prettier.io
*/
Expand Down Expand Up @@ -68,13 +73,41 @@ class Prettier {
}

const paths = output.stdout.split(/\r?\n/);
lintResult.error = paths.map((path) => ({
path,
firstLine: 1,
lastLine: 1,
message:
"There are issues with this file's formatting, please run Prettier to fix the errors",
}));
lintResult.error = paths
.filter((path) => !!path)
.map((path) => ({
path,
firstLine: 1,
lastLine: 1,
message:
"There are issues with this file's formatting, please run Prettier to fix the errors",
}));

// Fall back to stderr if stdout is empty
if (output.stderr) {
// -no-color not fully respected
const matches = removeANSIColorCodes(output.stderr).matchAll(PARSE_REGEX);
for (const match of matches) {
const [_, level, pathFull, text, line] = match;
const leadingSep = `.${sep}`;
let path = pathFull;
if (path.startsWith(leadingSep)) {
path = path.substring(2); // Remove "./" or ".\" from start of path
}
const lineNr = parseInt(line, 10);
const result = {
path,
firstLine: lineNr,
lastLine: lineNr,
message: text,
};
if (level === "error") {
lintResult.error.push(result);
} else if (level === "warning") {
lintResult.warning.push(result);
}
}
}

return lintResult;
}
Expand Down
14 changes: 14 additions & 0 deletions src/utils/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,21 @@ function removeTrailingPeriod(str) {
return str[str.length - 1] === "." ? str.substring(0, str.length - 1) : str;
}

/**
* Strips ansi escape codes
* @param {string} str - Console output to escape
* @returns {string} - Pure ansi output
*/
function removeANSIColorCodes(str) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had trouble even with -no-colors with unit tests locally. Perhaps some limitation of the command line tool?

return str.replace(
// eslint-disable-next-line no-control-regex
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
"",
);
}

module.exports = {
capitalizeFirstLetter,
removeTrailingPeriod,
removeANSIColorCodes,
};
7 changes: 5 additions & 2 deletions test/linters/linters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { join } = require("path");

const { copy, remove } = require("fs-extra");

const { removeANSIColorCodes } = require("../../src/utils/string");
const { normalizeDates, normalizePaths, createTmpDir } = require("../test-utils");
const autopep8Params = require("./params/autopep8");
const blackParams = require("./params/black");
Expand All @@ -17,6 +18,7 @@ const golintParams = require("./params/golint");
const mypyParams = require("./params/mypy");
const phpCodeSnifferParams = require("./params/php-codesniffer");
const prettierParams = require("./params/prettier");
const prettierInvalidParams = require("./params/prettier-invalid");
const pylintParams = require("./params/pylint");
const ruboCopParams = require("./params/rubocop");
const rustfmtParams = require("./params/rustfmt");
Expand All @@ -42,6 +44,7 @@ const linterParams = [
mypyParams,
phpCodeSnifferParams,
prettierParams,
prettierInvalidParams,
pylintParams,
ruboCopParams,
rustfmtParams,
Expand Down Expand Up @@ -109,10 +112,10 @@ describe.each(linterParams)(
stderr = normalizePaths(stderr, tmpDir);
if ("stderrParts" in expected.cmdOutput) {
expected.cmdOutput.stderrParts.forEach((stderrParts) =>
expect(stderr).toEqual(expect.stringContaining(stderrParts)),
expect(removeANSIColorCodes(stderr)).toEqual(expect.stringContaining(stderrParts)),
);
} else if ("stderr" in expected.cmdOutput) {
expect(stderr).toEqual(expected.cmdOutput.stderr);
expect(removeANSIColorCodes(stderr)).toEqual(expected.cmdOutput.stderr);
}
});

Expand Down
63 changes: 63 additions & 0 deletions test/linters/params/prettier-invalid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const Prettier = require("../../../src/linters/prettier");

const testName = "prettier-invalid";
const linter = Prettier;
const args = "";
const commandPrefix = "";
const extensions = [
"css",
"html",
"js",
"json",
"jsx",
"md",
"sass",
"scss",
"ts",
"tsx",
"vue",
"yaml",
"yml",
];

// Linting without auto-fixing
function getLintParams(dir) {
const stdoutFile2 = `file2.css`;
return {
// Expected output of the linting function
cmdOutput: {
status: 2,
stdoutParts: [stdoutFile2],
stdout: `${stdoutFile2}`,
stderr:
"[error] file1.ts: SyntaxError: ')' expected. (8:38)\n[error] 6 |\n[error] 7 | let userName: string = 'John';\n[error] > 8 | let greeting: string = greet(userName; // Syntax error\n[error] | ^\n[error] 9 |\n[error] 10 | console.log(greeting);\n[error] 11 |",
},
// Expected output of the parsing function
lintResult: {
isSuccess: false,
warning: [],
error: [
{
path: "file2.css",
firstLine: 1,
lastLine: 1,
message:
"There are issues with this file's formatting, please run Prettier to fix the errors",
},
{
path: "file1.ts",
firstLine: 8,
lastLine: 8,
message: "SyntaxError: ')' expected.",
},
],
},
};
}

// Linting with auto-fixing
function getFixParams(dir) {
return getLintParams(dir);
}

module.exports = [testName, linter, commandPrefix, extensions, args, getLintParams, getFixParams];
1 change: 1 addition & 0 deletions test/linters/projects/prettier-invalid/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
5 changes: 5 additions & 0 deletions test/linters/projects/prettier-invalid/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"semi": true,
"singleQuote": true,
"useTabs": true
}
10 changes: 10 additions & 0 deletions test/linters/projects/prettier-invalid/file1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// example.ts

function greet(name: string): string {
return `Hello, ${name}!`
}

let userName: string = 'John';
let greeting: string = greet(userName; // Syntax error

console.log(greeting);
1 change: 1 addition & 0 deletions test/linters/projects/prettier-invalid/file2.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
body {color: red;} /* Line break errors */
8 changes: 8 additions & 0 deletions test/linters/projects/prettier-invalid/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "test-prettier-invalid",
"main": "index.js",
"private": true,
"devDependencies": {
"prettier": "^2.8.1"
}
}
8 changes: 8 additions & 0 deletions test/linters/projects/prettier-invalid/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


prettier@^2.8.1:
version "2.8.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.8.1.tgz#4e1fd11c34e2421bc1da9aea9bd8127cd0a35efc"
integrity sha512-lqGoSJBQNJidqCHE80vqZJHWHRFoNYsSpP9AjFhlhi9ODCJA541svILes/+/1GM3VaL/abZi7cpFzOpdR9UPKg==
Loading