Skip to content

Commit 8ad8648

Browse files
43081jfabiospampinato
authored andcommitted
Ported test suite: print-code
- Force-including explicitly included files in dump mode, to match the old behavior - Updated snapshots to match prettier v3 output, which for some reason the snapshot didn't seem to reflect - Treating explicitly included files like this the same way, previously if one of these files was ignore its content was dumped to the console, which seems weird and useless
1 parent b3e6bc8 commit 8ad8648

8 files changed

Lines changed: 46 additions & 6 deletions

File tree

src/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ async function runGlobs(options: Options, pluginsDefaultOptions: PluginsOptions,
5656

5757
const rootPath = process.cwd();
5858
const projectPath = getProjectPath(rootPath);
59-
const [filesPaths, filesNames, filesNamesToPaths, filesFoundPaths, foldersFoundPaths] = await getTargetsPaths(rootPath, options.globs, options.withNodeModules); // prettier-ignore
59+
const [filesPaths, filesNames, filesNamesToPaths, filesExplicitPaths, filesFoundPaths, foldersFoundPaths] = await getTargetsPaths(rootPath, options.globs, options.withNodeModules); // prettier-ignore
60+
const filesExplicitPathsSet = new Set(filesExplicitPaths);
6061
const filesPathsTargets = filesPaths.filter(negate(isBinaryPath)).sort();
6162
const [foldersPathsTargets, foldersExtraPaths] = getExpandedFoldersPaths(foldersFoundPaths, projectPath);
6263
const filesExtraPaths = await getFoldersChildrenPaths([rootPath, ...foldersExtraPaths]);
@@ -103,7 +104,7 @@ async function runGlobs(options: Options, pluginsDefaultOptions: PluginsOptions,
103104
const cliFormatConfig = options.formatOptions;
104105
const cacheVersion = stringify({ prettierVersion, cliVersion, pluginsNames, pluginsVersions, editorConfigs, ignoreContents, prettierConfigs, ignoreManualFilesPaths, ignoreManualFilesContents, prettierManualFilesPaths, prettierManualFilesContents, cliContextConfig, cliFormatConfig, pluginsDefaultOptions, pluginsCustomOptions }); // prettier-ignore
105106

106-
const shouldCache = options.cache && !pluginsVersionsMissing.length && isUndefined(cliContextConfig.cursorOffset);
107+
const shouldCache = options.cache && !options.dump && !pluginsVersionsMissing.length && isUndefined(cliContextConfig.cursorOffset);
107108
const cache = shouldCache ? new Cache(cacheVersion, projectPath, options, stdout) : undefined;
108109
const prettier = await makePrettier(options, cache);
109110

@@ -112,8 +113,10 @@ async function runGlobs(options: Options, pluginsDefaultOptions: PluginsOptions,
112113
filesPathsTargets.map(async (filePath) => {
113114
const isIgnored = () => (ignoreManual ? ignoreManual(filePath) : getIgnoreResolved(filePath, ignoreNames));
114115
const isCacheable = () => cache?.has(filePath, isIgnored);
115-
const ignored = cache ? !(await isCacheable()) : await isIgnored();
116-
if (ignored) return;
116+
const isExplicitlyIncluded = () => filesExplicitPathsSet.has(filePath);
117+
const isForceIncluded = options.dump && isExplicitlyIncluded();
118+
const isExcluded = cache ? !(await isCacheable()) : await isIgnored();
119+
if (!isForceIncluded && isExcluded) return;
117120
const getFormatOptions = async (): Promise<FormatOptions> => {
118121
const editorConfig = options.editorConfig ? getEditorConfigFormatOptions(await getEditorConfigResolved(filePath, editorConfigNames)) : {};
119122
const prettierConfig = prettierManualConfig || (options.config ? await getPrettierConfigResolved(filePath, prettierConfigNames) : {});

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type Options = {
4848
globs: string[];
4949
/* OUTPUT OPTIONS */
5050
check: boolean;
51+
dump: boolean;
5152
list: boolean;
5253
write: boolean;
5354
/* CONFIG OPTIONS */

src/utils.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ async function getTargetsPaths(
192192
rootPath: string,
193193
globs: string[],
194194
withNodeModules: boolean,
195-
): Promise<[string[], string[], Record<string, string[]>, string[], string[]]> {
195+
): Promise<[string[], string[], Record<string, string[]>, string[], string[], string[]]> {
196196
const targetFiles: string[] = [];
197197
const targetFilesNames: string[] = [];
198198
const targetFilesNamesToPaths: Record<string, string[]> = {};
@@ -225,9 +225,10 @@ async function getTargetsPaths(
225225
filesNamesToPaths[fileName] = uniq(next);
226226
}
227227

228+
const filesExplicitPaths = targetFiles;
228229
const filesFoundPaths = result.filesFound;
229230
const foldersFoundPaths = [rootPath, ...result.directoriesFound];
230-
return [filesPaths, filesNames, filesNamesToPaths, filesFoundPaths, foldersFoundPaths];
231+
return [filesPaths, filesNames, filesNamesToPaths, filesExplicitPaths, filesFoundPaths, foldersFoundPaths];
231232
}
232233

233234
function isArray(value: unknown): value is unknown[] {
@@ -309,6 +310,7 @@ async function normalizeOptions(options: unknown, targets: unknown[]): Promise<O
309310
const check = "check" in options && !!options.check;
310311
const list = "listDifferent" in options && !!options.listDifferent;
311312
const write = "write" in options && !!options.write;
313+
const dump = !check && !list && !write;
312314

313315
if (check && list) exit('The "--check" and "--list-different" flags cannot be used together');
314316

@@ -339,6 +341,7 @@ async function normalizeOptions(options: unknown, targets: unknown[]): Promise<O
339341
return {
340342
globs,
341343
check,
344+
dump,
342345
list,
343346
write,
344347
config,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ignored.js
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
foo(
2+
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
foo(
2+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Line breaking after filepath with errors (stdout) 1`] = `"foo();"`;
4+
5+
exports[`Line breaking after filepath with errors (stdout) 2`] = `"foo();"`;

test/__tests__/print-code.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { runCli } from "../utils";
2+
3+
describe("Line breaking after filepath with errors", () => {
4+
runCli("print-code", [
5+
"./ignored.js"
6+
], {
7+
stdoutIsTTY: true,
8+
}).test({
9+
status: 0,
10+
write: [],
11+
stderr: ""
12+
});
13+
14+
runCli("print-code", [
15+
"./not-ignored.js"
16+
], {
17+
stdoutIsTTY: true,
18+
}).test({
19+
status: 0,
20+
write: [],
21+
stderr: ""
22+
});
23+
});

0 commit comments

Comments
 (0)