From 2b987b12555280716d02e3a6a716ab3b32808a7a Mon Sep 17 00:00:00 2001 From: Matthew King Date: Tue, 4 Jan 2022 13:26:48 -0800 Subject: [PATCH] Add fileInfoOptions to override options passed to prettier.getFileInfo --- README.md | 15 ++++++++++++ stylelint-prettier.js | 16 +++++++++---- test/prettierrc/file-options/.prettierignore | 1 + .../file-options/custom-ignore-me.css | 6 +++++ test/stylelint-prettier.test.js | 23 +++++++++++++++++++ 5 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 test/prettierrc/file-options/.prettierignore create mode 100644 test/prettierrc/file-options/custom-ignore-me.css diff --git a/README.md b/README.md index 2e8cacb..6d19c54 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,21 @@ Objects are passed directly to Prettier as [options](https://prettier.io/docs/en NB: This option will merge and override any config set with `.prettierrc` files (for Prettier < 1.7.0, config files are ignored) +### Additional Options + +- fileInfoOptions: Options that are passed to [prettier.getFileInfo](https://prettier.io/docs/en/api.html#prettiergetfileinfofilepath--options) in order to decide if a particular file needs to be formatted. As an example, this can be used to override the default ignorePath inferred from the current working directory. + +```json +{ + "rules": { + "prettier/prettier": [ + true, + {"fileInfoOptions": {"ignorePath": "../../.prettierignore"}} + ] + } +} +``` + --- ## Contributing diff --git a/stylelint-prettier.js b/stylelint-prettier.js index 0f6d942..2bd1ee9 100644 --- a/stylelint-prettier.js +++ b/stylelint-prettier.js @@ -51,10 +51,17 @@ module.exports = stylelint.createPlugin( editorconfig: true, }); - const prettierFileInfo = prettier.getFileInfo.sync(filepath, { - resolveConfig: true, - ignorePath: '.prettierignore', - }); + const stylelintFileInfoOptions = + (options && options.fileInfoOptions) || {}; + + const prettierFileInfo = prettier.getFileInfo.sync( + filepath, + Object.assign( + {}, + {resolveConfig: true, ignorePath: '.prettierignore'}, + stylelintFileInfoOptions + ) + ); // Skip if file is ignored using a .prettierignore file if (prettierFileInfo.ignored) { @@ -229,6 +236,7 @@ function omitStylelintSpecificOptions(options) { const prettierOptions = Object.assign({}, options); delete prettierOptions.message; delete prettierOptions.severity; + delete prettierOptions.fileInfoOptions; return prettierOptions; } diff --git a/test/prettierrc/file-options/.prettierignore b/test/prettierrc/file-options/.prettierignore new file mode 100644 index 0000000..0849bc2 --- /dev/null +++ b/test/prettierrc/file-options/.prettierignore @@ -0,0 +1 @@ +custom-ignore-me.css \ No newline at end of file diff --git a/test/prettierrc/file-options/custom-ignore-me.css b/test/prettierrc/file-options/custom-ignore-me.css new file mode 100644 index 0000000..f08a892 --- /dev/null +++ b/test/prettierrc/file-options/custom-ignore-me.css @@ -0,0 +1,6 @@ + .x { + + + color: red; +} + diff --git a/test/stylelint-prettier.test.js b/test/stylelint-prettier.test.js index 82e9978..a9d7c58 100644 --- a/test/stylelint-prettier.test.js +++ b/test/stylelint-prettier.test.js @@ -687,6 +687,29 @@ describe('stylelint configurations', () => { ); }); }); + + it('resolves prettier fileInfoOptions', () => { + const linted = stylelint.lint({ + files: [filename('file-options', 'custom-ignore-me.css')], + config: { + plugins: ['./'], + rules: { + 'prettier/prettier': [ + true, + { + fileInfoOptions: { + ignorePath: filename('file-options', '.prettierignore'), + }, + }, + ], + }, + }, + }); + + return linted.then(({errored}) => { + expect(errored).toBe(false); + }); + }); }); /**