Skip to content

Commit 373bc15

Browse files
Refactored "normalizeToPosix" utility to fit with the rest of the utilities
1 parent 041e1e5 commit 373bc15

2 files changed

Lines changed: 31 additions & 11 deletions

File tree

src/index.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,18 @@ import Known from "./known.js";
1212
import Logger from "./logger.js";
1313
import { makePrettier } from "./prettier.js";
1414
import { castArray, getExpandedFoldersPaths, getFoldersChildrenPaths, getPluginsVersions, getProjectPath, getStdin, getTargetsPaths } from "./utils.js";
15-
import { fastRelativePath, isNull, isString, isUndefined, negate, pluralize, trimFinalNewline, uniq, without, normalizeToPosix } from "./utils.js";
15+
import {
16+
fastRelativePath,
17+
isNull,
18+
isString,
19+
isUndefined,
20+
negate,
21+
normalizePathSeparatorsToPosix,
22+
pluralize,
23+
trimFinalNewline,
24+
uniq,
25+
without,
26+
} from "./utils.js";
1627
import type { FormatOptions, Options, PluginsOptions } from "./types.js";
1728

1829
async function run(options: Options, pluginsDefaultOptions: PluginsOptions, pluginsCustomOptions: PluginsOptions): Promise<void> {
@@ -162,7 +173,7 @@ async function runGlobs(options: Options, pluginsDefaultOptions: PluginsOptions,
162173
} else {
163174
totalUnformatted += 1;
164175
const filePath = filesPathsTargets[i];
165-
const fileNameToDisplay = normalizeToPosix(fastRelativePath(rootPath, filePath));
176+
const fileNameToDisplay = normalizePathSeparatorsToPosix(fastRelativePath(rootPath, filePath));
166177
if (options.check) {
167178
stderr.prefixed.warn(fileNameToDisplay);
168179
} else if (options.list || options.write) {
@@ -180,7 +191,7 @@ async function runGlobs(options: Options, pluginsDefaultOptions: PluginsOptions,
180191
totalErrored += 1;
181192
pathsErrored.push(filesPathsTargets[i]);
182193
const filePath = filesPathsTargets[i];
183-
const fileNameToDisplay = normalizeToPosix(fastRelativePath(rootPath, filePath));
194+
const fileNameToDisplay = normalizePathSeparatorsToPosix(fastRelativePath(rootPath, filePath));
184195
//TODO: Make sure the error is syntax-highlighted when possible
185196
if (options.check || options.write || options.dump) {
186197
stderr.prefixed.error(`${fileNameToDisplay}: ${error}`);
@@ -197,9 +208,9 @@ async function runGlobs(options: Options, pluginsDefaultOptions: PluginsOptions,
197208
stdout.prefixed.debug(`Files formatted: ${totalFormatted}`);
198209
stdout.prefixed.debug(`Files unformatted: ${totalUnformatted}`);
199210
stdout.prefixed.debug(`Files unknown: ${totalUnknown}`);
200-
stdout.prefixed.debug(() => pathsUnknown.map((filePath) => normalizeToPosix(fastRelativePath(rootPath, filePath))).join("\n"));
211+
stdout.prefixed.debug(() => pathsUnknown.map((filePath) => normalizePathSeparatorsToPosix(fastRelativePath(rootPath, filePath))).join("\n"));
201212
stdout.prefixed.debug(`Files errored: ${totalErrored}`);
202-
stdout.prefixed.debug(() => pathsErrored.map((filePath) => normalizeToPosix(fastRelativePath(rootPath, filePath))).join("\n"));
213+
stdout.prefixed.debug(() => pathsErrored.map((filePath) => normalizePathSeparatorsToPosix(fastRelativePath(rootPath, filePath))).join("\n"));
203214

204215
if (!totalMatched && !totalIgnored) {
205216
if (options.errorOnUnmatchedPattern) {

src/utils.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ function getModulePath(name: string, rootPath: string): string {
123123
return modulePath;
124124
}
125125

126+
function identity<T>(value: T): T {
127+
return value;
128+
}
129+
126130
const getPlugin = memoize((name: string): Promise<PrettierPlugin> => {
127131
const pluginPath = getPluginPath(name);
128132
const plugin = getModule<PrettierPlugin>(pluginPath);
@@ -661,6 +665,16 @@ function normalizePrettierOptions(options: unknown, folderPath: string): Prettie
661665
return config;
662666
}
663667

668+
const normalizePathSeparatorsToPosix = (() => {
669+
if (path.sep === "\\") {
670+
return (filePath: string): string => {
671+
return filePath.replaceAll("\\", "/");
672+
};
673+
} else {
674+
return identity;
675+
}
676+
})();
677+
664678
function omit<T extends object, K extends keyof T>(object: T, keys: K[]): Omit<T, K> {
665679
const clone = { ...object };
666680

@@ -733,11 +747,6 @@ function zipObjectUnless<T extends Key, U>(keys: T[], values: U[], unless: (valu
733747
return map;
734748
}
735749

736-
/**
737-
* Replace `\` with `/` on Windows
738-
*/
739-
const normalizeToPosix = path.sep === "\\" ? (filepath: string): string => filepath.replaceAll("\\", "/") : (filepath: string): string => filepath;
740-
741750
export {
742751
castArray,
743752
fastJoinedPath,
@@ -781,7 +790,7 @@ export {
781790
normalizeFormatOptions,
782791
normalizePluginOptions,
783792
normalizePrettierOptions,
784-
normalizeToPosix,
793+
normalizePathSeparatorsToPosix,
785794
omit,
786795
once,
787796
pluralize,

0 commit comments

Comments
 (0)