fix(api): normalize file path for `getFileInfo` (#5570)
parent
28b938da97
commit
3fcf69a7d5
|
@ -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
|
||||
|
@ -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
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
|
|
|
@ -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");
|
||||
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue