Respect prettierignore when executed in subdirectories (#7588)

* Respect prettierignore when executed in subdirectories

* Add ignore-in-subdirectories tests

* Add changelog entry
This commit is contained in:
Corinne Hoener authored and GitHub committed 2020-02-18 00:40:26 +02:00
1 parent f40c5e3336
commit 50e0ea91ee
9 files changed
+136 -2

No files matched your search

+4
View File
@@ -0,0 +1,4 @@
#### Respect `--ignore-path` when prettier executes from a subdirectory ([#7588](https://github.com/prettier/prettier/pull/7588) by [@heylookltsme](https://github.com/heylookltsme))
Changes the filename used when filtering ignored files to be relative to the
`--ignore-path`, if present, rather than the current working directory.
+11 -2
View File
@@ -362,7 +362,11 @@ function formatStdin(context) {
: process.cwd();
const ignorer = createIgnorerFromContextOrDie(context);
const relativeFilepath = path.relative(process.cwd(), filepath);
// If there's an ignore-path set, the filename must be relative to the
// ignore path, not the current working directory.
const relativeFilepath = context.argv["ignore-path"]
? path.relative(path.dirname(context.argv["ignore-path"]), filepath)
: path.relative(process.cwd(), filepath);
thirdParty
.getStream(process.stdin)
@@ -439,7 +443,12 @@ function formatFiles(context) {
}
eachFilename(context, context.filePatterns, filename => {
const fileIgnored = ignorer.filter([filename]).length === 0;
// If there's an ignore-path set, the filename must be relative to the
// ignore path, not the current working directory.
const ignoreFilename = context.argv["ignore-path"]
? path.relative(path.dirname(context.argv["ignore-path"]), filename)
: filename;
const fileIgnored = ignorer.filter([ignoreFilename]).length === 0;
if (
fileIgnored &&
(context.argv["debug-check"] ||
@@ -0,0 +1,39 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`formats files when executing in a subdirectory (stderr) 1`] = `""`;
exports[`formats files when executing in a subdirectory (stderr) 2`] = `""`;
exports[`formats files when executing in a subdirectory (stdout) 1`] = `
"should-not-ignore.js
"
`;
exports[`formats files when executing in a subdirectory (stdout) 2`] = `
"should-not-ignore.js
"
`;
exports[`formats files when executing in a subdirectory (write) 1`] = `Array []`;
exports[`formats files when executing in a subdirectory (write) 2`] = `Array []`;
exports[`formats files when executing in a subdirectory and using stdin (stderr) 1`] = `""`;
exports[`formats files when executing in a subdirectory and using stdin (write) 1`] = `Array []`;
exports[`ignore files when executing in a subdirectory and using stdin (stderr) 1`] = `""`;
exports[`ignore files when executing in a subdirectory and using stdin (write) 1`] = `Array []`;
exports[`ignores files when executing in a subdirectory (stderr) 1`] = `""`;
exports[`ignores files when executing in a subdirectory (stderr) 2`] = `""`;
exports[`ignores files when executing in a subdirectory (stdout) 1`] = `""`;
exports[`ignores files when executing in a subdirectory (stdout) 2`] = `""`;
exports[`ignores files when executing in a subdirectory (write) 1`] = `Array []`;
exports[`ignores files when executing in a subdirectory (write) 2`] = `Array []`;
@@ -0,0 +1,77 @@
"use strict";
const runPrettier = require("../runPrettier");
expect.addSnapshotSerializer(require("../path-serializer"));
describe("ignores files when executing in a subdirectory", () => {
runPrettier("cli/ignore-in-subdirectories/web1", [
"ignore-me/should-ignore.js",
"--ignore-path",
"../.prettierignore",
"-l"
]).test({
status: 0
});
runPrettier("cli/ignore-in-subdirectories/web1", [
"ignore-me/subdirectory/should-ignore.js",
"--ignore-path",
"../.prettierignore",
"-l"
]).test({
status: 0
});
});
describe("formats files when executing in a subdirectory", () => {
runPrettier("cli/ignore-in-subdirectories/web1", [
"should-not-ignore.js",
"--ignore-path",
"../.prettierignore",
"-l"
]).test({
status: 1
});
runPrettier("cli/ignore-in-subdirectories/web2", [
"should-not-ignore.js",
"--ignore-path",
"../.prettierignore",
"-l"
]).test({
status: 1
});
});
describe("ignore files when executing in a subdirectory and using stdin", () => {
runPrettier(
"cli/ignore-in-subdirectories/web1",
[
"--ignore-path",
"../.prettierignore",
"--stdin-filepath",
"ignore-me/example.js"
],
{
input: "hello_world( );"
}
).test({
stdout: "hello_world( );",
status: 0
});
});
describe("formats files when executing in a subdirectory and using stdin", () => {
runPrettier(
"cli/ignore-in-subdirectories/web1",
["--ignore-path", "../.prettierignore", "--stdin-filepath", "example.js"],
{
input: "hello_world( );"
}
).test({
stdout: `hello_world();
`,
status: 0
});
});
@@ -0,0 +1 @@
web1/ignore-me
@@ -0,0 +1 @@
var x = 'this should not be formatted';
@@ -0,0 +1 @@
var x = 'this should not be formatted';
@@ -0,0 +1 @@
var x = 'this should be formatted';
@@ -0,0 +1 @@
var x = 'this should be formatted';