Skip to content

Commit

Permalink
fix(api): normalize file path for getFileInfo (#5570)
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatyang committed Nov 29, 2018
1 parent 28b938d commit 3fcf69a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/common/get-file-info.js
Expand Up @@ -2,6 +2,7 @@

const createIgnorer = require("./create-ignorer");
const options = require("../main/options");
const path = require("path");

/**
* @typedef {{ ignorePath?: string, withNodeModules?: boolean, plugins: object }} FileInfoOptions
Expand All @@ -19,7 +20,11 @@ const options = require("../main/options");
*/
function getFileInfo(filePath, opts) {
return createIgnorer(opts.ignorePath, opts.withNodeModules).then(ignorer =>
_getFileInfo(ignorer, filePath, opts.plugins)
_getFileInfo(
ignorer,
normalizeFilePath(filePath, opts.ignorePath),
opts.plugins
)
);
}

Expand All @@ -30,7 +35,11 @@ function getFileInfo(filePath, opts) {
*/
getFileInfo.sync = function(filePath, opts) {
const ignorer = createIgnorer.sync(opts.ignorePath, opts.withNodeModules);
return _getFileInfo(ignorer, filePath, opts.plugins);
return _getFileInfo(
ignorer,
normalizeFilePath(filePath, opts.ignorePath),
opts.plugins
);
};

function _getFileInfo(ignorer, filePath, plugins) {
Expand All @@ -43,4 +52,10 @@ function _getFileInfo(ignorer, filePath, plugins) {
};
}

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

module.exports = getFileInfo;
36 changes: 36 additions & 0 deletions tests_integration/__tests__/file-info.js
@@ -1,6 +1,8 @@
"use strict";

const path = require("path");
const tempy = require("tempy");
const fs = require("fs");

const runPrettier = require("../runPrettier");
const prettier = require("prettier/local");
Expand Down Expand Up @@ -176,6 +178,40 @@ test("API getFileInfo.sync with ignorePath", () => {
});
});

describe("API getFileInfo.sync with ignorePath", () => {
let cwd;
let filePath;
let options;
beforeAll(() => {
cwd = process.cwd();
const tempDir = tempy.directory();
process.chdir(tempDir);
const fileDir = "src";
filePath = `${fileDir}/should-be-ignored.js`;
const ignorePath = path.join(tempDir, ".prettierignore");
fs.writeFileSync(ignorePath, filePath, "utf8");
options = { ignorePath };
});
afterAll(() => {
process.chdir(cwd);
});
test("with relative filePath", () => {
expect(
prettier.getFileInfo.sync(filePath, options).ignored
).toMatchInlineSnapshot(`true`);
});
test("with relative filePath starts with dot", () => {
expect(
prettier.getFileInfo.sync(`./${filePath}`, options).ignored
).toMatchInlineSnapshot(`true`);
});
test("with absolute filePath", () => {
expect(
prettier.getFileInfo.sync(path.resolve(filePath), options).ignored
).toMatchInlineSnapshot(`true`);
});
});

test("API getFileInfo with withNodeModules", () => {
const file = path.resolve(
path.join(__dirname, "../cli/with-node-modules/node_modules/file.js")
Expand Down

0 comments on commit 3fcf69a

Please sign in to comment.