Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog_unreleased/cli/pr-7588.md
Original file line number Diff line number Diff line change
@@ -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.
13 changes: 11 additions & 2 deletions src/cli/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,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)
Expand Down Expand Up @@ -443,7 +447,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"] ||
Expand Down
Original file line number Diff line number Diff line change
@@ -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 []`;
77 changes: 77 additions & 0 deletions tests_integration/__tests__/ignore-in-subdirectories.js
Original file line number Diff line number Diff line change
@@ -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
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web1/ignore-me
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var x = 'this should not be formatted';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var x = 'this should not be formatted';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var x = 'this should be formatted';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var x = 'this should be formatted';