diff --git a/changelog_unreleased/cli/14333.md b/changelog_unreleased/cli/14333.md new file mode 100644 index 000000000..75133bfa4 --- /dev/null +++ b/changelog_unreleased/cli/14333.md @@ -0,0 +1,18 @@ +#### Display posix style paths on Windows (#14333 by @fisker) + +Align with other tools like ESLint and Stylelint. + + +```jsx +// Prettier stable +Checking formatting... +[warn] src\utils\create-get-visitor-keys.js +[warn] src\utils\unexpected-node-error.js +[warn] Code style issues found in 2 files. Forgot to run Prettier? + +// Prettier main +Checking formatting... +[warn] src/utils/create-get-visitor-keys.js +[warn] src/utils/unexpected-node-error.js +[warn] Code style issues found in 2 files. Forgot to run Prettier? +``` diff --git a/cspell.json b/cspell.json index b8415b26f..de02b4c99 100644 --- a/cspell.json +++ b/cspell.json @@ -250,6 +250,7 @@ "Pieter", "pnpapi", "pnpm", + "posix", "postprocessor", "prebuilds", "precache", diff --git a/src/cli/expand-patterns.js b/src/cli/expand-patterns.js index ab32993dd..14994ed46 100644 --- a/src/cli/expand-patterns.js +++ b/src/cli/expand-patterns.js @@ -1,5 +1,5 @@ import path from "node:path"; -import { statSafe } from "./utils.js"; +import { statSafe, normalizeToPosix } from "./utils.js"; import { fastGlob } from "./prettier-internal.js"; /** @typedef {import('./context').Context} Context */ @@ -186,17 +186,12 @@ function escapePathForGlob(path) { .replaceAll("\0", "@(\\\\)"); // Workaround for fast-glob#262 (part 2) } -const isWindows = path.sep === "\\"; - /** * Using backslashes in globs is probably not okay, but not accepting * backslashes as path separators on Windows is even more not okay. * https://github.com/prettier/prettier/pull/6776#discussion_r380723717 * https://github.com/mrmlnc/fast-glob#how-to-write-patterns-on-windows - * @param {string} pattern */ -function fixWindowsSlashes(pattern) { - return isWindows ? pattern.replaceAll("\\", "/") : pattern; -} +const fixWindowsSlashes = normalizeToPosix; export { expandPatterns }; diff --git a/src/cli/format.js b/src/cli/format.js index 535611c79..8d718c0c9 100644 --- a/src/cli/format.js +++ b/src/cli/format.js @@ -10,7 +10,7 @@ import getOptionsForFile from "./options/get-options-for-file.js"; import isTTY from "./is-tty.js"; import findCacheFile from "./find-cache-file.js"; import FormatResultsCache from "./format-results-cache.js"; -import { statSafe } from "./utils.js"; +import { statSafe, normalizeToPosix } from "./utils.js"; const { getStdin } = thirdParty; @@ -346,7 +346,7 @@ async function formatFiles(context) { let printedFilename; if (isTTY()) { - printedFilename = context.logger.log(filename, { + printedFilename = context.logger.log(normalizeToPosix(filename), { newline: false, clearable: true, }); @@ -418,7 +418,9 @@ async function formatFiles(context) { // mtime based caches. if (isDifferent) { if (!context.argv.check && !context.argv.listDifferent) { - context.logger.log(`${filename} ${Date.now() - start}ms`); + context.logger.log( + `${normalizeToPosix(filename)} ${Date.now() - start}ms` + ); } try { @@ -435,7 +437,9 @@ async function formatFiles(context) { process.exitCode = 2; } } else if (!context.argv.check && !context.argv.listDifferent) { - const message = `${chalk.grey(filename)} ${Date.now() - start}ms`; + const message = `${chalk.grey(normalizeToPosix(filename))} ${ + Date.now() - start + }ms`; if (isCacheExists) { context.logger.log(`${message} (cached)`); } else { @@ -444,7 +448,7 @@ async function formatFiles(context) { } } else if (context.argv.debugCheck) { if (result.filepath) { - context.logger.log(result.filepath); + context.logger.log(normalizeToPosix(result.filepath)); } else { /* c8 ignore next */ process.exitCode = 2; @@ -461,9 +465,9 @@ async function formatFiles(context) { if (isDifferent) { if (context.argv.check) { - context.logger.warn(filename); + context.logger.warn(normalizeToPosix(filename)); } else if (context.argv.listDifferent) { - context.logger.log(filename); + context.logger.log(normalizeToPosix(filename)); } numberOfUnformattedFilesFound += 1; } diff --git a/src/cli/utils.js b/src/cli/utils.js index 5a9aeb88c..ba0203bd7 100644 --- a/src/cli/utils.js +++ b/src/cli/utils.js @@ -1,4 +1,5 @@ import fs from "node:fs/promises"; +import path from "node:path"; import sdbm from "sdbm"; import { __internal as sharedWithCli } from "../index.js"; @@ -79,6 +80,16 @@ function isJson(value) { } } +/** + * Replace `\` with `/` on Windows + * @param {string} filepath + * @returns {string} + */ +const normalizeToPosix = + path.sep === "\\" + ? (filepath) => filepath.replaceAll("\\", "/") + : (filepath) => filepath; + export { arrayify, isNonEmptyArray, @@ -89,4 +100,5 @@ export { createHash, statSafe, isJson, + normalizeToPosix, };