Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jun 12, 2020
1 parent 1fde8cc commit 5ac6b81
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"html-element-attributes": "2.2.1",
"html-styles": "1.0.0",
"html-tag-names": "1.1.5",
"ignore": "5.1.6",
"ignore": "5.1.8",
"jest-docblock": "26.0.0",
"json-stable-stringify": "1.0.1",
"leven": "3.1.0",
Expand Down
10 changes: 7 additions & 3 deletions src/cli/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const path = require("path");
const camelCase = require("camelcase");
const dashify = require("dashify");
const fs = require("fs");

const { isPathValid } = require("ignore");
const chalk = require("chalk");
const readline = require("readline");
const stringify = require("json-stable-stringify");
Expand Down Expand Up @@ -379,7 +379,7 @@ function formatStdin(context) {
.then((input) => {
if (
relativeFilepath &&
ignorer.ignores(fixWindowsSlashes(relativeFilepath))
isIgnored(ignorer, fixWindowsSlashes(relativeFilepath))
) {
writeOutput(context, { formatted: input });
return;
Expand Down Expand Up @@ -410,6 +410,10 @@ function createIgnorerFromContextOrDie(context) {
}
}

function isIgnored(ignorer, file) {
return isPathValid(file) && ignorer.ignores(file);
}

function formatFiles(context) {
// The ignorer will be used to filter file paths after the glob is checked,
// before any files are actually written
Expand All @@ -436,7 +440,7 @@ function formatFiles(context) {
? path.relative(path.dirname(context.argv["ignore-path"]), filename)
: filename;

const fileIgnored = ignorer.ignores(fixWindowsSlashes(ignoreFilename));
const fileIgnored = isIgnored(ignorer, fixWindowsSlashes(ignoreFilename));
if (
fileIgnored &&
(context.argv["debug-check"] ||
Expand Down
25 changes: 12 additions & 13 deletions src/common/get-file-info.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use strict";

const ignore = require("ignore");
const createIgnorer = require("./create-ignorer");
const options = require("../main/options");
const config = require("../config/resolve-config");
Expand Down Expand Up @@ -29,9 +30,10 @@ async function getFileInfo(filePath, opts) {
const ignorer = await createIgnorer(opts.ignorePath, opts.withNodeModules);
return _getFileInfo({
ignorer,
filePath: normalizeFilePath(filePath, opts.ignorePath),
filePath,
plugins: opts.plugins,
resolveConfig: opts.resolveConfig,
ignorePath: opts.ignorePath,
sync: false,
});
}
Expand All @@ -51,9 +53,10 @@ getFileInfo.sync = function (filePath, opts) {
const ignorer = createIgnorer.sync(opts.ignorePath, opts.withNodeModules);
return _getFileInfo({
ignorer,
filePath: normalizeFilePath(filePath, opts.ignorePath),
filePath,
plugins: opts.plugins,
resolveConfig: opts.resolveConfig,
ignorePath: opts.ignorePath,
sync: true,
});
};
Expand All @@ -63,17 +66,12 @@ function _getFileInfo({
filePath,
plugins,
resolveConfig = false,
ignorePath,
sync = false,
}) {
let ignored = true;
try {
ignored = ignorer.ignores(path.relative(process.cwd(), filePath));
} catch (_) {
// Invalid path
}

const relativeFilePath = normalizeFilePath(filePath, ignorePath)
const fileInfo = {
ignored,
ignored: ignore.isPathValid(relativeFilePath) && ignorer.ignores(relativeFilePath),
inferredParser: options.inferParser(filePath, plugins) || null,
};

Expand All @@ -98,9 +96,10 @@ function _getFileInfo({
}

function normalizeFilePath(filePath, ignorePath) {
return ignorePath
? path.relative(path.dirname(ignorePath), filePath)
: filePath;
return path.relative(
ignorePath ? path.dirname(ignorePath) : process.cwd(),
filePath
);
}

module.exports = getFileInfo;
10 changes: 10 additions & 0 deletions tests_integration/__tests__/__snapshots__/file-info.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Should not ignore files in parent dir (stderr) 1`] = `""`;

exports[`Should not ignore files in parent dir (stdout) 1`] = `
"{ \\"ignored\\": false, \\"inferredParser\\": \\"babel\\" }
"
`;

exports[`Should not ignore files in parent dir (write) 1`] = `Array []`;

exports[`extracts file-info for a file in not_node_modules (stderr) 1`] = `""`;

exports[`extracts file-info for a file in not_node_modules (stdout) 1`] = `
Expand Down
10 changes: 10 additions & 0 deletions tests_integration/__tests__/file-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ describe("extracts file-info with ignored=true for a file in .prettierignore", (
});

describe("extracts file-info with ignored=true for a file in a hand-picked .prettierignore", () => {
runPrettier("cli/ignore-path/", [
"--file-info",
"regular-module.js",
"--ignore-path=.prettierignore",
]).test({
status: 0,
});
});

describe("Should not ignore files in parent dir", () => {
runPrettier("cli/", [
"--file-info",
"regular-module.js",
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4133,10 +4133,10 @@ ignore-walk@3.0.3:
dependencies:
minimatch "^3.0.4"

ignore@5.1.6:
version "5.1.6"
resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.1.6.tgz#643194ad4bf2712f37852e386b6998eff0db2106"
integrity sha512-cgXgkypZBcCnOgSihyeqbo6gjIaIyDqPQB7Ra4vhE9m6kigdGoQDMHjviFhRZo3IMlRy6yElosoviMs5YxZXUA==
ignore@5.1.8:
version "5.1.8"
resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.1.8.tgz#f150a8b50a34289b33e22f5889abd4d8016f0e57"
integrity sha512-BMpfD7PpiETpBl/A6S498BaIJ6Y/ABT93ETbby2fP00v4EbvPBXWEoaR1UBPKs3iR53pJY7EtZk5KACI57i1Uw==

ignore@^4.0.6:
version "4.0.6"
Expand Down

0 comments on commit 5ac6b81

Please sign in to comment.