diff --git a/src/cli-util.js b/src/cli-util.js index 1ce44e54d..8a79e1088 100644 --- a/src/cli-util.js +++ b/src/cli-util.js @@ -226,11 +226,18 @@ function applyConfigPrecedence(argv, options) { } function formatStdin(argv) { - thirdParty.getStream(process.stdin).then(input => { - const filepath = argv["stdin-filepath"] - ? path.resolve(process.cwd(), argv["stdin-filepath"]) - : process.cwd(); + const filepath = argv["stdin-filepath"] + ? path.resolve(process.cwd(), argv["stdin-filepath"]) + : process.cwd(); + const ignorer = createIgnorer(argv); + const relativeFilepath = path.relative(process.cwd(), filepath); + + if (relativeFilepath && ignorer.filter([relativeFilepath]).length === 0) { + return; + } + + thirdParty.getStream(process.stdin).then(input => { const options = getOptionsForFile(argv, filepath); if (listDifferent(argv, input, options, "(stdin)")) { @@ -245,10 +252,7 @@ function formatStdin(argv) { }); } -function eachFilename(argv, patterns, callback) { - const ignoreNodeModules = argv["with-node-modules"] === false; - // The ignorer will be used to filter file paths after the glob is checked, - // before any files are actually read +function createIgnorer(argv) { const ignoreFilePath = path.resolve(argv["ignore-path"]); let ignoreText = ""; @@ -261,7 +265,14 @@ function eachFilename(argv, patterns, callback) { } } - const ignorer = ignore().add(ignoreText); + return ignore().add(ignoreText); +} + +function eachFilename(argv, patterns, callback) { + const ignoreNodeModules = argv["with-node-modules"] === false; + // The ignorer will be used to filter file paths after the glob is checked, + // before any files are actually read + const ignorer = createIgnorer(argv); if (ignoreNodeModules) { patterns = patterns.concat(["!**/node_modules/**", "!./node_modules/**"]); diff --git a/tests_integration/__tests__/__snapshots__/stdin-filepath.js.snap b/tests_integration/__tests__/__snapshots__/stdin-filepath.js.snap index f94c00c36..fe293fe93 100644 --- a/tests_integration/__tests__/__snapshots__/stdin-filepath.js.snap +++ b/tests_integration/__tests__/__snapshots__/stdin-filepath.js.snap @@ -11,6 +11,10 @@ exports[`format correctly if stdin content compatible with stdin-filepath (stdou exports[`format correctly if stdin content compatible with stdin-filepath (write) 1`] = `Array []`; +exports[`output nothing if stdin-filepath matched patterns in ignore-path (stderr) 1`] = `""`; + +exports[`output nothing if stdin-filepath matched patterns in ignore-path (write) 1`] = `Array []`; + exports[`throw error if stdin content incompatible with stdin-filepath (stderr) 1`] = ` "[error] stdin: SyntaxError: Unexpected token (1:1) [error] > 1 | .name { display: none; } diff --git a/tests_integration/__tests__/stdin-filepath.js b/tests_integration/__tests__/stdin-filepath.js index 3e6708634..19037daa1 100644 --- a/tests_integration/__tests__/stdin-filepath.js +++ b/tests_integration/__tests__/stdin-filepath.js @@ -21,3 +21,12 @@ describe("throw error if stdin content incompatible with stdin-filepath", () => status: "non-zero" }); }); + +describe("output nothing if stdin-filepath matched patterns in ignore-path", () => { + runPrettier("cli/stdin-ignore", ["--stdin-filepath", "ignore/example.js"], { + input: "hello_world();" + }).test({ + stdout: "", + status: 0 + }); +}); diff --git a/tests_integration/cli/stdin-ignore/.prettierignore b/tests_integration/cli/stdin-ignore/.prettierignore new file mode 100644 index 000000000..9973a57b8 --- /dev/null +++ b/tests_integration/cli/stdin-ignore/.prettierignore @@ -0,0 +1 @@ +ignore/