fix(cli): respect `--ignore-path` when using `--stdin-filepath` (#3309)

master
Ika 2017-11-23 18:54:04 +08:00 committed by Lucas Azzola
parent c0fa47d2d2
commit a1d878acc7
4 changed files with 34 additions and 9 deletions

View File

@ -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/**"]);

View File

@ -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; }

View File

@ -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
});
});

View File

@ -0,0 +1 @@
ignore/