From 3fcf69a7d561a22314c9644b4e309907310db266 Mon Sep 17 00:00:00 2001 From: Ika Date: Thu, 29 Nov 2018 11:04:44 +0800 Subject: [PATCH] fix(api): normalize file path for `getFileInfo` (#5570) --- src/common/get-file-info.js | 19 +++++++++++-- tests_integration/__tests__/file-info.js | 36 ++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/common/get-file-info.js b/src/common/get-file-info.js index 88bade3767df..a587b31ee5e9 100644 --- a/src/common/get-file-info.js +++ b/src/common/get-file-info.js @@ -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; diff --git a/tests_integration/__tests__/file-info.js b/tests_integration/__tests__/file-info.js index 085db27c4eba..3d2710492233 100644 --- a/tests_integration/__tests__/file-info.js +++ b/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"); @@ -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")