Ignore .gitignored files (#14731)

This commit is contained in:
fisker Cheung authored and GitHub committed 2023-05-04 17:20:05 +08:00
1 parent dcc081db0d
commit b86e42cc84
20 files changed
+228 -121

No files matched your search

+8
View File
@@ -0,0 +1,8 @@
#### [Breaking] Ignore `.gitignore`d files by default (#14731 by @fisker)
Prettier ignores files ignored by `.gitignore` by default.
If you want the old behavior(only ignore files ignored by `.prettierignore`), use
```console
prettier . --write --ignore-path=.prettierignore
```
+1
View File
@@ -62,6 +62,7 @@
"concat", "concat",
"concating", "concating",
"cond", "cond",
"customignore",
"daleroy", "daleroy",
"danez", "danez",
"Dara", "Dara",
@@ -31,6 +31,18 @@ module.exports = {
return; return;
} }
// wrapped
// `const runCliWithoutGitignore = () => runCli()`
// `const runCliWithoutGitignore = () => { return runCli() }`
if (
(parent.type === "ArrowFunctionExpression" &&
parent.body === callExpression) ||
(parent.type === "ReturnStatement" &&
parent.argument === callExpression)
) {
return;
}
if ( if (
parent.type === "MemberExpression" && parent.type === "MemberExpression" &&
parent.object === callExpression && parent.object === callExpression &&
+1 -1
View File
@@ -195,7 +195,7 @@ const options = {
ignorePath: { ignorePath: {
array: true, array: true,
category: optionCategories.CATEGORY_CONFIG, category: optionCategories.CATEGORY_CONFIG,
default: [{ value: [".prettierignore"] }], default: [{ value: [".gitignore", ".prettierignore"] }],
description: outdent` description: outdent`
Path to a file with patterns describing files to ignore. Path to a file with patterns describing files to ignore.
Multiple values are accepted. Multiple values are accepted.
@@ -112,7 +112,7 @@ Config options:
Find and print the path to a configuration file for the given input file. Find and print the path to a configuration file for the given input file.
--ignore-path <path> Path to a file with patterns describing files to ignore. --ignore-path <path> Path to a file with patterns describing files to ignore.
Multiple values are accepted. Multiple values are accepted.
Defaults to [.prettierignore]. Defaults to [.gitignore, .prettierignore].
--plugin <path> Add a plugin. Multiple plugins can be passed as separate \`--plugin\`s. --plugin <path> Add a plugin. Multiple plugins can be passed as separate \`--plugin\`s.
Defaults to []. Defaults to [].
--plugin-search-dir <path> --plugin-search-dir <path>
@@ -283,7 +283,7 @@ Config options:
Find and print the path to a configuration file for the given input file. Find and print the path to a configuration file for the given input file.
--ignore-path <path> Path to a file with patterns describing files to ignore. --ignore-path <path> Path to a file with patterns describing files to ignore.
Multiple values are accepted. Multiple values are accepted.
Defaults to [.prettierignore]. Defaults to [.gitignore, .prettierignore].
--plugin <path> Add a plugin. Multiple plugins can be passed as separate \`--plugin\`s. --plugin <path> Add a plugin. Multiple plugins can be passed as separate \`--plugin\`s.
Defaults to []. Defaults to [].
--plugin-search-dir <path> --plugin-search-dir <path>
@@ -54,14 +54,14 @@ exports[`extracts file-info with ignored=true for a file in .prettierignore (std
exports[`extracts file-info with ignored=true for a file in .prettierignore (write) 1`] = `[]`; exports[`extracts file-info with ignored=true for a file in .prettierignore (write) 1`] = `[]`;
exports[`extracts file-info with ignored=true for a file in a hand-picked .prettierignore (stderr) 1`] = `""`; exports[`extracts file-info with ignored=true for a file in a hand-picked ignore file (stderr) 1`] = `""`;
exports[`extracts file-info with ignored=true for a file in a hand-picked .prettierignore (stdout) 1`] = ` exports[`extracts file-info with ignored=true for a file in a hand-picked ignore file (stdout) 1`] = `
"{ "ignored": true, "inferredParser": null } "{ "ignored": true, "inferredParser": null }
" "
`; `;
exports[`extracts file-info with ignored=true for a file in a hand-picked .prettierignore (write) 1`] = `[]`; exports[`extracts file-info with ignored=true for a file in a hand-picked ignore file (write) 1`] = `[]`;
exports[`extracts file-info with inferredParser=foo when a plugin is hand-picked (stderr) 1`] = `""`; exports[`extracts file-info with inferredParser=foo when a plugin is hand-picked (stderr) 1`] = `""`;
@@ -251,7 +251,7 @@ exports[`show detailed usage with --help ignore-path (stdout) 1`] = `
Path to a file with patterns describing files to ignore. Path to a file with patterns describing files to ignore.
Multiple values are accepted. Multiple values are accepted.
Default: [.prettierignore]" Default: [.gitignore, .prettierignore]"
`; `;
exports[`show detailed usage with --help ignore-path (write) 1`] = `[]`; exports[`show detailed usage with --help ignore-path (write) 1`] = `[]`;
@@ -1,9 +0,0 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`ignore file when using --debug-check (stdout) 1`] = `"other-regular-modules.js"`;
exports[`ignore path (stdout) 1`] = `"regular-module.js"`;
exports[`outputs files as-is if no --write (stdout) 1`] = `"'use strict';"`;
exports[`support .prettierignore (stdout) 1`] = `"other-regular-modules.js"`;
+120 -45
View File
@@ -6,6 +6,9 @@ function resolveDir(dir) {
return fileURLToPath(new URL(`../${dir}/`, import.meta.url)); return fileURLToPath(new URL(`../${dir}/`, import.meta.url));
} }
const runCliWithoutGitignore = (dir, args, options) =>
runCli(dir, [...args, "--ignore-path", ".prettierignore"], options);
describe("--cache option", () => { describe("--cache option", () => {
const dir = resolveDir("cli/cache"); const dir = resolveDir("cli/cache");
const defaultCacheFile = path.join( const defaultCacheFile = path.join(
@@ -55,7 +58,7 @@ describe("--cache option", () => {
afterAll(clean); afterAll(clean);
it("throw error when cache-strategy is invalid", async () => { it("throw error when cache-strategy is invalid", async () => {
const { stderr } = await runCli(dir, [ const { stderr } = await runCliWithoutGitignore(dir, [
"--cache", "--cache",
"--cache-strategy", "--cache-strategy",
"invalid", "invalid",
@@ -67,7 +70,7 @@ describe("--cache option", () => {
}); });
it("throws error when use with --stdin-filepath", async () => { it("throws error when use with --stdin-filepath", async () => {
const { stderr } = await runCli( const { stderr } = await runCliWithoutGitignore(
dir, dir,
["--cache", "--stdin-filepath", "foo.js"], ["--cache", "--stdin-filepath", "foo.js"],
{ input: "const a = a;" } { input: "const a = a;" }
@@ -76,7 +79,7 @@ describe("--cache option", () => {
}); });
it("throws error when use `--cache-strategy` without `--cache`.", async () => { it("throws error when use `--cache-strategy` without `--cache`.", async () => {
const { stderr } = await runCli( const { stderr } = await runCliWithoutGitignore(
dir, dir,
["foo.js", "--cache-strategy", "content"], ["foo.js", "--cache-strategy", "content"],
{ {
@@ -89,7 +92,7 @@ describe("--cache option", () => {
}); });
it("throws error when `--cache-location` is a directory.", async () => { it("throws error when `--cache-location` is a directory.", async () => {
const { stderr } = await runCli(dir, [ const { stderr } = await runCliWithoutGitignore(dir, [
"foo.js", "foo.js",
"--cache", "--cache",
"--cache-location", "--cache-location",
@@ -108,12 +111,17 @@ describe("--cache option", () => {
"code", "code",
"ENOENT" "ENOENT"
); );
await runCli(dir, ["--cache", "--cache-strategy", "metadata", "*.js"]); await runCliWithoutGitignore(dir, [
"--cache",
"--cache-strategy",
"metadata",
"*.js",
]);
await expect(fs.stat(defaultCacheFile)).resolves.not.toThrowError(); await expect(fs.stat(defaultCacheFile)).resolves.not.toThrowError();
}); });
it("doesn't format when cache is available", async () => { it("doesn't format when cache is available", async () => {
const { stdout: firstStdout } = await runCli(dir, [ const { stdout: firstStdout } = await runCliWithoutGitignore(dir, [
"--cache", "--cache",
"--write", "--write",
"--cache-strategy", "--cache-strategy",
@@ -127,7 +135,7 @@ describe("--cache option", () => {
]) ])
); );
const { stdout: secondStdout } = await runCli(dir, [ const { stdout: secondStdout } = await runCliWithoutGitignore(dir, [
"--cache", "--cache",
"--write", "--write",
"--cache-strategy", "--cache-strategy",
@@ -150,7 +158,10 @@ describe("--cache option", () => {
"metadata", "metadata",
"*.js", "*.js",
]; ];
const { stdout: firstStdout } = await runCli(dir, cliArguments); const { stdout: firstStdout } = await runCliWithoutGitignore(
dir,
cliArguments
);
expect(firstStdout.split("\n")).toEqual( expect(firstStdout.split("\n")).toEqual(
expect.arrayContaining([ expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/), expect.stringMatching(/^a\.js .+ms$/),
@@ -161,7 +172,10 @@ describe("--cache option", () => {
// Update `a.js` // Update `a.js`
await fs.writeFile(path.join(dir, "a.js"), "const a = `a`;"); await fs.writeFile(path.join(dir, "a.js"), "const a = `a`;");
const { stdout: secondStdout } = await runCli(dir, cliArguments); const { stdout: secondStdout } = await runCliWithoutGitignore(
dir,
cliArguments
);
expect(secondStdout.split("\n")).toEqual( expect(secondStdout.split("\n")).toEqual(
// the cache of `b.js` is only available. // the cache of `b.js` is only available.
expect.arrayContaining([ expect.arrayContaining([
@@ -179,7 +193,10 @@ describe("--cache option", () => {
"metadata", "metadata",
"*.js", "*.js",
]; ];
const { stdout: firstStdout } = await runCli(dir, cliArguments); const { stdout: firstStdout } = await runCliWithoutGitignore(
dir,
cliArguments
);
expect(firstStdout.split("\n")).toEqual( expect(firstStdout.split("\n")).toEqual(
expect.arrayContaining([ expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/), expect.stringMatching(/^a\.js .+ms$/),
@@ -191,7 +208,10 @@ describe("--cache option", () => {
const time = new Date(); const time = new Date();
await fs.utimes(path.join(dir, "a.js"), time, time); await fs.utimes(path.join(dir, "a.js"), time, time);
const { stdout: secondStdout } = await runCli(dir, cliArguments); const { stdout: secondStdout } = await runCliWithoutGitignore(
dir,
cliArguments
);
expect(secondStdout.split("\n")).toEqual( expect(secondStdout.split("\n")).toEqual(
// the cache of `b.js` is only available. // the cache of `b.js` is only available.
expect.arrayContaining([ expect.arrayContaining([
@@ -202,7 +222,7 @@ describe("--cache option", () => {
}); });
it("re-formats when options has been updated.", async () => { it("re-formats when options has been updated.", async () => {
const { stdout: firstStdout } = await runCli(dir, [ const { stdout: firstStdout } = await runCliWithoutGitignore(dir, [
"--cache", "--cache",
"--write", "--write",
"--cache-strategy", "--cache-strategy",
@@ -216,7 +236,7 @@ describe("--cache option", () => {
]) ])
); );
const { stdout: secondStdout } = await runCli(dir, [ const { stdout: secondStdout } = await runCliWithoutGitignore(dir, [
"--cache", "--cache",
"--cache-strategy", "--cache-strategy",
"metadata", "metadata",
@@ -234,9 +254,14 @@ describe("--cache option", () => {
}); });
it("re-formats after execution without write.", async () => { it("re-formats after execution without write.", async () => {
await runCli(dir, ["--cache", "--cache-strategy", "metadata", "*.js"]); await runCliWithoutGitignore(dir, [
"--cache",
"--cache-strategy",
"metadata",
"*.js",
]);
const { stdout: secondStdout } = await runCli(dir, [ const { stdout: secondStdout } = await runCliWithoutGitignore(dir, [
"--write", "--write",
"--cache", "--cache",
"--cache-strategy", "--cache-strategy",
@@ -259,7 +284,7 @@ describe("--cache option", () => {
"metadata", "metadata",
"*.js", "*.js",
]; ];
await runCli(dir, cliArguments); await runCliWithoutGitignore(dir, cliArguments);
// Update `a.js` to unformatted // Update `a.js` to unformatted
await fs.writeFile(path.join(dir, "a.js"), "const a = `a`; "); await fs.writeFile(path.join(dir, "a.js"), "const a = `a`; ");
@@ -268,9 +293,17 @@ describe("--cache option", () => {
const time = new Date(); const time = new Date();
await fs.utimes(path.join(dir, "b.js"), time, time); await fs.utimes(path.join(dir, "b.js"), time, time);
await runCli(dir, ["--cache", "--cache-strategy", "metadata", "*.js"]); await runCliWithoutGitignore(dir, [
"--cache",
"--cache-strategy",
"metadata",
"*.js",
]);
const { stdout: thirdStdout } = await runCli(dir, cliArguments); const { stdout: thirdStdout } = await runCliWithoutGitignore(
dir,
cliArguments
);
expect(thirdStdout.split("\n")).toEqual( expect(thirdStdout.split("\n")).toEqual(
expect.arrayContaining([ expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/), expect.stringMatching(/^a\.js .+ms$/),
@@ -284,7 +317,7 @@ describe("--cache option", () => {
stdout: firstStdout, stdout: firstStdout,
stderr: firstStderr, stderr: firstStderr,
status: firstStatus, status: firstStatus,
} = await runCli( } = await runCliWithoutGitignore(
dir, dir,
["--write", "--cache", "--cache-strategy", "metadata", "*.js"], ["--write", "--cache", "--cache-strategy", "metadata", "*.js"],
{ {
@@ -305,7 +338,7 @@ describe("--cache option", () => {
]) ])
); );
const { stdout: secondStdout } = await runCli(dir, [ const { stdout: secondStdout } = await runCliWithoutGitignore(dir, [
"--list-different", "--list-different",
"--cache", "--cache",
"--cache-strategy", "--cache-strategy",
@@ -316,7 +349,7 @@ describe("--cache option", () => {
}); });
it("removes cache file when run Prettier without `--cache` option", async () => { it("removes cache file when run Prettier without `--cache` option", async () => {
await runCli(dir, [ await runCliWithoutGitignore(dir, [
"--cache", "--cache",
"--write", "--write",
"--cache-strategy", "--cache-strategy",
@@ -324,7 +357,7 @@ describe("--cache option", () => {
"*.js", "*.js",
]); ]);
await expect(fs.stat(defaultCacheFile)).resolves.not.toThrowError(); await expect(fs.stat(defaultCacheFile)).resolves.not.toThrowError();
await runCli(dir, ["--write", "*.js"]); await runCliWithoutGitignore(dir, ["--write", "*.js"]);
await expect(fs.stat(defaultCacheFile)).rejects.toThrowError(); await expect(fs.stat(defaultCacheFile)).rejects.toThrowError();
}); });
}); });
@@ -335,7 +368,12 @@ describe("--cache option", () => {
"code", "code",
"ENOENT" "ENOENT"
); );
await runCli(dir, ["--cache", "--cache-strategy", "content", "*.js"]); await runCliWithoutGitignore(dir, [
"--cache",
"--cache-strategy",
"content",
"*.js",
]);
await expect(fs.stat(defaultCacheFile)).resolves.not.toThrowError(); await expect(fs.stat(defaultCacheFile)).resolves.not.toThrowError();
}); });
@@ -347,7 +385,10 @@ describe("--cache option", () => {
"--write", "--write",
"*.js", "*.js",
]; ];
const { stdout: firstStdout } = await runCli(dir, cliArguments); const { stdout: firstStdout } = await runCliWithoutGitignore(
dir,
cliArguments
);
expect(firstStdout.split("\n")).toEqual( expect(firstStdout.split("\n")).toEqual(
expect.arrayContaining([ expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/), expect.stringMatching(/^a\.js .+ms$/),
@@ -355,7 +396,10 @@ describe("--cache option", () => {
]) ])
); );
const { stdout: secondStdout } = await runCli(dir, cliArguments); const { stdout: secondStdout } = await runCliWithoutGitignore(
dir,
cliArguments
);
expect(secondStdout.split("\n")).toEqual( expect(secondStdout.split("\n")).toEqual(
expect.arrayContaining([ expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms \(cached\)$/), expect.stringMatching(/^a\.js .+ms \(cached\)$/),
@@ -372,7 +416,10 @@ describe("--cache option", () => {
"--write", "--write",
"*.js", "*.js",
]; ];
const { stdout: firstStdout } = await runCli(dir, cliArguments); const { stdout: firstStdout } = await runCliWithoutGitignore(
dir,
cliArguments
);
expect(firstStdout.split("\n")).toEqual( expect(firstStdout.split("\n")).toEqual(
expect.arrayContaining([ expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/), expect.stringMatching(/^a\.js .+ms$/),
@@ -383,7 +430,10 @@ describe("--cache option", () => {
// Update `a.js` // Update `a.js`
await fs.writeFile(path.join(dir, "a.js"), "const a = `a`;"); await fs.writeFile(path.join(dir, "a.js"), "const a = `a`;");
const { stdout: secondStdout } = await runCli(dir, cliArguments); const { stdout: secondStdout } = await runCliWithoutGitignore(
dir,
cliArguments
);
expect(secondStdout.split("\n")).toEqual( expect(secondStdout.split("\n")).toEqual(
// the cache of `b.js` is only available. // the cache of `b.js` is only available.
expect.arrayContaining([ expect.arrayContaining([
@@ -401,7 +451,10 @@ describe("--cache option", () => {
"--write", "--write",
"*.js", "*.js",
]; ];
const { stdout: firstStdout } = await runCli(dir, cliArguments); const { stdout: firstStdout } = await runCliWithoutGitignore(
dir,
cliArguments
);
expect(firstStdout.split("\n")).toEqual( expect(firstStdout.split("\n")).toEqual(
expect.arrayContaining([ expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/), expect.stringMatching(/^a\.js .+ms$/),
@@ -413,7 +466,10 @@ describe("--cache option", () => {
const time = new Date(); const time = new Date();
await fs.utimes(path.join(dir, "a.js"), time, time); await fs.utimes(path.join(dir, "a.js"), time, time);
const { stdout: secondStdout } = await runCli(dir, cliArguments); const { stdout: secondStdout } = await runCliWithoutGitignore(
dir,
cliArguments
);
expect(secondStdout.split("\n")).toEqual( expect(secondStdout.split("\n")).toEqual(
expect.arrayContaining([ expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms \(cached\)$/), expect.stringMatching(/^a\.js .+ms \(cached\)$/),
@@ -423,7 +479,7 @@ describe("--cache option", () => {
}); });
it("re-formats when options has been updated.", async () => { it("re-formats when options has been updated.", async () => {
const { stdout: firstStdout } = await runCli(dir, [ const { stdout: firstStdout } = await runCliWithoutGitignore(dir, [
"--cache", "--cache",
"--cache-strategy", "--cache-strategy",
"content", "content",
@@ -437,7 +493,7 @@ describe("--cache option", () => {
]) ])
); );
const { stdout: secondStdout } = await runCli(dir, [ const { stdout: secondStdout } = await runCliWithoutGitignore(dir, [
"--cache", "--cache",
"--write", "--write",
"--cache-strategy", "--cache-strategy",
@@ -455,9 +511,14 @@ describe("--cache option", () => {
}); });
it("re-formats after execution without write.", async () => { it("re-formats after execution without write.", async () => {
await runCli(dir, ["--cache", "--cache-strategy", "content", "*.js"]); await runCliWithoutGitignore(dir, [
"--cache",
"--cache-strategy",
"content",
"*.js",
]);
const { stdout: secondStdout } = await runCli(dir, [ const { stdout: secondStdout } = await runCliWithoutGitignore(dir, [
"--write", "--write",
"--cache", "--cache",
"--cache-strategy", "--cache-strategy",
@@ -480,7 +541,7 @@ describe("--cache option", () => {
"content", "content",
"*.js", "*.js",
]; ];
await runCli(dir, cliArguments); await runCliWithoutGitignore(dir, cliArguments);
// Update `a.js` to unformatted // Update `a.js` to unformatted
await fs.writeFile(path.join(dir, "a.js"), "const a = `a`; "); await fs.writeFile(path.join(dir, "a.js"), "const a = `a`; ");
@@ -489,9 +550,17 @@ describe("--cache option", () => {
const time = new Date(); const time = new Date();
await fs.utimes(path.join(dir, "b.js"), time, time); await fs.utimes(path.join(dir, "b.js"), time, time);
await runCli(dir, ["--cache", "--cache-strategy", "content", "*.js"]); await runCliWithoutGitignore(dir, [
"--cache",
"--cache-strategy",
"content",
"*.js",
]);
const { stdout: thirdStdout } = await runCli(dir, cliArguments); const { stdout: thirdStdout } = await runCliWithoutGitignore(
dir,
cliArguments
);
expect(thirdStdout.split("\n")).toEqual( expect(thirdStdout.split("\n")).toEqual(
expect.arrayContaining([ expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/), expect.stringMatching(/^a\.js .+ms$/),
@@ -505,7 +574,7 @@ describe("--cache option", () => {
stdout: firstStdout, stdout: firstStdout,
stderr: firstStderr, stderr: firstStderr,
status: firstStatus, status: firstStatus,
} = await runCli( } = await runCliWithoutGitignore(
dir, dir,
["--write", "--cache", "--cache-strategy", "content", "*.js"], ["--write", "--cache", "--cache-strategy", "content", "*.js"],
{ {
@@ -526,7 +595,7 @@ describe("--cache option", () => {
]) ])
); );
const { stdout: secondStdout } = await runCli(dir, [ const { stdout: secondStdout } = await runCliWithoutGitignore(dir, [
"--list-different", "--list-different",
"--cache", "--cache",
"--cache-strategy", "--cache-strategy",
@@ -537,9 +606,9 @@ describe("--cache option", () => {
}); });
it("removes cache file when run Prettier without `--cache` option", async () => { it("removes cache file when run Prettier without `--cache` option", async () => {
await runCli(dir, ["--cache", "--write", "*.js"]); await runCliWithoutGitignore(dir, ["--cache", "--write", "*.js"]);
await expect(fs.stat(defaultCacheFile)).resolves.not.toThrowError(); await expect(fs.stat(defaultCacheFile)).resolves.not.toThrowError();
await runCli(dir, ["--write", "*.js"]); await runCliWithoutGitignore(dir, ["--write", "*.js"]);
await expect(fs.stat(defaultCacheFile)).rejects.toThrowError(); await expect(fs.stat(defaultCacheFile)).rejects.toThrowError();
}); });
}); });
@@ -550,7 +619,7 @@ describe("--cache option", () => {
"code", "code",
"ENOENT" "ENOENT"
); );
await runCli(dir, [ await runCliWithoutGitignore(dir, [
"--cache", "--cache",
"--cache-location", "--cache-location",
nonDefaultCacheFileName, nonDefaultCacheFileName,
@@ -563,7 +632,7 @@ describe("--cache option", () => {
}); });
it("throws error for invalid JSON file", async () => { it("throws error for invalid JSON file", async () => {
const { stderr } = await runCli(dir, [ const { stderr } = await runCliWithoutGitignore(dir, [
"--cache", "--cache",
"--cache-location", "--cache-location",
"a.js", "a.js",
@@ -580,7 +649,7 @@ describe("--cache option", () => {
"code", "code",
"ENOENT" "ENOENT"
); );
await runCli(dir, [ await runCliWithoutGitignore(dir, [
"--cache", "--cache",
"--cache-location", "--cache-location",
nonDefaultCacheFileName, nonDefaultCacheFileName,
@@ -599,7 +668,10 @@ describe("--cache option", () => {
nonDefaultCacheFileName, nonDefaultCacheFileName,
"*.js", "*.js",
]; ];
const { stdout: firstStdout } = await runCli(dir, cliArguments); const { stdout: firstStdout } = await runCliWithoutGitignore(
dir,
cliArguments
);
expect(firstStdout.split("\n")).toEqual( expect(firstStdout.split("\n")).toEqual(
expect.arrayContaining([ expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/), expect.stringMatching(/^a\.js .+ms$/),
@@ -607,7 +679,10 @@ describe("--cache option", () => {
]) ])
); );
const { stdout: secondStdout } = await runCli(dir, cliArguments); const { stdout: secondStdout } = await runCliWithoutGitignore(
dir,
cliArguments
);
expect(secondStdout.split("\n")).toEqual( expect(secondStdout.split("\n")).toEqual(
expect.arrayContaining([ expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms \(cached\)$/), expect.stringMatching(/^a\.js .+ms \(cached\)$/),
+13 -7
View File
@@ -25,7 +25,10 @@ describe("extracts file-info for a known markdown file with no extension", () =>
}); });
describe("extracts file-info with ignored=true for a file in .prettierignore", () => { describe("extracts file-info with ignored=true for a file in .prettierignore", () => {
runCli("cli/ignore-path/", ["--file-info", "regular-module.js"]).test({ runCli("cli/ignore-path/file-info-test/", [
"--file-info",
"ignored-by-prettierignore.js",
]).test({
status: 0, status: 0,
}); });
}); });
@@ -48,11 +51,11 @@ describe("file-info should not try resolve config with --no-config", () => {
}); });
}); });
describe("extracts file-info with ignored=true for a file in a hand-picked .prettierignore", () => { describe("extracts file-info with ignored=true for a file in a hand-picked ignore file", () => {
runCli("cli/", [ runCli("cli/", [
"--file-info", "--file-info",
"regular-module.js", "ignored-by-customignore.js",
"--ignore-path=ignore-path/.prettierignore", "--ignore-path=ignore-path/file-info-test/.customignore",
]).test({ ]).test({
status: 0, status: 0,
}); });
@@ -62,7 +65,7 @@ describe("non-exists ignore path", () => {
runCli("cli/", [ runCli("cli/", [
"--file-info", "--file-info",
"regular-module.js", "regular-module.js",
"--ignore-path=ignore-path/non-exists/.prettierignore", "--ignore-path=ignore-path/file-info-test/.non-exists-ignore-file",
]).test({ ]).test({
status: 0, status: 0,
}); });
@@ -233,10 +236,13 @@ describe("API getFileInfo resolveConfig when no config is present", () => {
test("API getFileInfo with ignorePath", async () => { test("API getFileInfo with ignorePath", async () => {
const file = path.resolve( const file = path.resolve(
path.join(__dirname, "../cli/ignore-path/regular-module.js") path.join(
__dirname,
"../cli/ignore-path/file-info-test/ignored-by-customignore.js"
)
); );
const ignorePath = path.resolve( const ignorePath = path.resolve(
path.join(__dirname, "../cli/ignore-path/.prettierignore") path.join(__dirname, "../cli/ignore-path/file-info-test/.customignore")
); );
await expect(prettier.getFileInfo(file)).resolves.toMatchObject({ await expect(prettier.getFileInfo(file)).resolves.toMatchObject({
+56 -47
View File
@@ -1,67 +1,76 @@
import path from "node:path"; import fs from "node:fs/promises";
import fs from "node:fs";
import createEsmUtils from "esm-utils";
const { __dirname } = createEsmUtils(import.meta);
fs.writeFileSync( // `.js` files are ignored in `.gitignore`
// This file is in `.gitignore`, just copy from `regular-module.js` const files = [
path.join(__dirname, "../cli/ignore-path/other-regular-modules.js"), "ignored-by-customignore.js",
fs.readFileSync(path.join(__dirname, "../cli/ignore-path/regular-module.js")) "ignored-by-gitignore.js",
"ignored-by-prettierignore.js",
].map(
(file) =>
new URL(`../cli/ignore-path/ignore-path-test/${file}`, import.meta.url)
); );
const clean = () =>
Promise.all(files.map((file) => fs.rm(file, { force: true })));
const setup = () =>
Promise.all(files.map((file) => fs.writeFile(file, " a+ b")));
describe("ignore path", () => { beforeAll(async () => {
runCli("cli/ignore-path", [ await clean();
await setup();
});
afterAll(clean);
const getUnformattedFiles = async (args) => {
const { stdout } = await runCli("cli/ignore-path/ignore-path-test/", [
"**/*.js", "**/*.js",
"--ignore-path",
".gitignore",
"-l", "-l",
]).test({ ...args,
status: 1, ]);
stderr: "", return stdout ? stdout.split("\n").sort() : [];
write: [], };
});
test("custom ignore path", async () => {
expect(await getUnformattedFiles(["--ignore-path", ".customignore"])).toEqual(
["ignored-by-gitignore.js", "ignored-by-prettierignore.js"]
);
}); });
describe("support .prettierignore", () => { test("ignore files by .prettierignore and .gitignore by default", async () => {
runCli("cli/ignore-path", ["**/*.js", "-l"]).test({ expect(
status: 1, await getUnformattedFiles(["--ignore-path", ".non-exists-ignore-file"])
stderr: "", ).toEqual([
write: [], "ignored-by-customignore.js",
}); "ignored-by-gitignore.js",
"ignored-by-prettierignore.js",
]);
expect(await getUnformattedFiles([])).toEqual([]);
}); });
describe("ignore file when using --debug-check", () => { describe("ignore file when using --debug-check", () => {
runCli("cli/ignore-path", ["**/*.js", "--debug-check"]).test({ runCli("cli/ignore-path/ignore-path-test/", [
status: 0,
stderr: "",
write: [],
});
});
describe("outputs files as-is if no --write", () => {
runCli("cli/ignore-path", ["regular-module.js"], {
ignoreLineEndings: true,
}).test({
status: 0,
stderr: "",
write: [],
});
});
describe("multiple `--ignore-path`", () => {
runCli("cli/ignore-path", [
"**/*.js", "**/*.js",
"--debug-check", "--debug-check",
"--ignore-path", "--ignore-path",
".gitignore",
"--ignore-path",
".prettierignore", ".prettierignore",
"--ignore-path",
".non-exists-ignore-file",
]).test({ ]).test({
status: 0, status: 0,
stdout: "",
stderr: "", stderr: "",
stdout: ["ignored-by-customignore.js", "ignored-by-gitignore.js"].join(
"\n"
),
write: [], write: [],
}); });
}); });
test("multiple `--ignore-path`", async () => {
expect(
await getUnformattedFiles([
"--ignore-path",
".customignore",
"--ignore-path",
".prettierignore",
"--ignore-path",
".non-exists-ignore-file",
])
).toEqual(["ignored-by-gitignore.js"]);
});
+6 -3
View File
@@ -8,6 +8,9 @@ const { __dirname } = createEsmUtils(import.meta);
expect.addSnapshotSerializer(jestPathSerializer); expect.addSnapshotSerializer(jestPathSerializer);
const runCliWithoutGitignore = (dir, args, options) =>
runCli(dir, [...args, "--ignore-path", ".prettierignore"], options);
// ESLint-like behavior // ESLint-like behavior
// https://github.com/prettier/prettier/pull/6639#issuecomment-548949954 // https://github.com/prettier/prettier/pull/6639#issuecomment-548949954
// //
@@ -97,7 +100,7 @@ const uppercaseRocksPlugin = path.join(
"tests/config/prettier-plugins/prettier-plugin-uppercase-rocks/index.js" "tests/config/prettier-plugins/prettier-plugin-uppercase-rocks/index.js"
); );
describe("plugins `.`", () => { describe("plugins `.`", () => {
runCli("cli/dirs/plugins", [ runCliWithoutGitignore("cli/dirs/plugins", [
".", ".",
"-l", "-l",
"--plugin", "--plugin",
@@ -109,7 +112,7 @@ describe("plugins `.`", () => {
}); });
}); });
describe("plugins `*`", () => { describe("plugins `*`", () => {
runCli("cli/dirs/plugins", [ runCliWithoutGitignore("cli/dirs/plugins", [
"*", "*",
"-l", "-l",
"--plugin", "--plugin",
@@ -331,7 +334,7 @@ function testPatterns(
.join(" "); .join(" ");
describe(testName, () => { describe(testName, () => {
runCli(cwd, [...cliArgs, "-l"]).test({ runCliWithoutGitignore(cwd, [...cliArgs, "-l"]).test({
write: [], write: [],
...(!("status" in expected) && { stderr: "", status: 1 }), ...(!("status" in expected) && { stderr: "", status: 1 }),
...expected, ...expected,
@@ -1 +0,0 @@
other-regular-modules.js
@@ -1 +0,0 @@
regular-module.js
@@ -0,0 +1 @@
ignored-by-customignore.js
@@ -0,0 +1 @@
ignored-by-prettierignore.js
@@ -0,0 +1 @@
ignored-by-customignore.js
@@ -0,0 +1 @@
*.js
@@ -0,0 +1 @@
ignored-by-prettierignore.js
@@ -1 +0,0 @@
'use strict';