Skip to content

Commit

Permalink
Ported test suite: print-code
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
43081j authored and fabiospampinato committed Feb 23, 2025
1 parent b3e6bc8 commit 8ad8648
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 6 deletions.
11 changes: 7 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ async function runGlobs(options: Options, pluginsDefaultOptions: PluginsOptions,

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

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

Expand All @@ -112,8 +113,10 @@ async function runGlobs(options: Options, pluginsDefaultOptions: PluginsOptions,
filesPathsTargets.map(async (filePath) => {
const isIgnored = () => (ignoreManual ? ignoreManual(filePath) : getIgnoreResolved(filePath, ignoreNames));
const isCacheable = () => cache?.has(filePath, isIgnored);
const ignored = cache ? !(await isCacheable()) : await isIgnored();
if (ignored) return;
const isExplicitlyIncluded = () => filesExplicitPathsSet.has(filePath);
const isForceIncluded = options.dump && isExplicitlyIncluded();
const isExcluded = cache ? !(await isCacheable()) : await isIgnored();
if (!isForceIncluded && isExcluded) return;
const getFormatOptions = async (): Promise<FormatOptions> => {
const editorConfig = options.editorConfig ? getEditorConfigFormatOptions(await getEditorConfigResolved(filePath, editorConfigNames)) : {};
const prettierConfig = prettierManualConfig || (options.config ? await getPrettierConfigResolved(filePath, prettierConfigNames) : {});
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type Options = {
globs: string[];
/* OUTPUT OPTIONS */
check: boolean;
dump: boolean;
list: boolean;
write: boolean;
/* CONFIG OPTIONS */
Expand Down
7 changes: 5 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ async function getTargetsPaths(
rootPath: string,
globs: string[],
withNodeModules: boolean,
): Promise<[string[], string[], Record<string, string[]>, string[], string[]]> {
): Promise<[string[], string[], Record<string, string[]>, string[], string[], string[]]> {
const targetFiles: string[] = [];
const targetFilesNames: string[] = [];
const targetFilesNamesToPaths: Record<string, string[]> = {};
Expand Down Expand Up @@ -225,9 +225,10 @@ async function getTargetsPaths(
filesNamesToPaths[fileName] = uniq(next);
}

const filesExplicitPaths = targetFiles;
const filesFoundPaths = result.filesFound;
const foldersFoundPaths = [rootPath, ...result.directoriesFound];
return [filesPaths, filesNames, filesNamesToPaths, filesFoundPaths, foldersFoundPaths];
return [filesPaths, filesNames, filesNamesToPaths, filesExplicitPaths, filesFoundPaths, foldersFoundPaths];
}

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

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

Expand Down Expand Up @@ -339,6 +341,7 @@ async function normalizeOptions(options: unknown, targets: unknown[]): Promise<O
return {
globs,
check,
dump,
list,
write,
config,
Expand Down
1 change: 1 addition & 0 deletions test/__fixtures__/print-code/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ignored.js
2 changes: 2 additions & 0 deletions test/__fixtures__/print-code/ignored.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
foo(
)
2 changes: 2 additions & 0 deletions test/__fixtures__/print-code/not-ignored.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
foo(
)
5 changes: 5 additions & 0 deletions test/__tests__/__snapshots__/print-code.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Line breaking after filepath with errors (stdout) 1`] = `"foo();"`;

exports[`Line breaking after filepath with errors (stdout) 2`] = `"foo();"`;
23 changes: 23 additions & 0 deletions test/__tests__/print-code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { runCli } from "../utils";

describe("Line breaking after filepath with errors", () => {
runCli("print-code", [
"./ignored.js"
], {
stdoutIsTTY: true,
}).test({
status: 0,
write: [],
stderr: ""
});

runCli("print-code", [
"./not-ignored.js"
], {
stdoutIsTTY: true,
}).test({
status: 0,
write: [],
stderr: ""
});
});

0 comments on commit 8ad8648

Please sign in to comment.