diff --git a/README.md b/README.md index cf002322..2ebde9bd 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,16 @@ For the list of every available exclusion rule set, please see the [readme of es }] ``` + - `fileInfoOptions`: Options that are passed to [prettier.getFileInfo](https://prettier.io/docs/en/api.html#prettiergetfileinfofilepath-options) to decide whether a file needs to be formatted. Can be used for example to opt-out from ignoring files located in `node_modules` directories. + + ```json + "prettier/prettier": ["error", {}, { + "fileInfoOptions": { + "withNodeModules": true + } + }] + ``` + - The rule is autofixable -- if you run `eslint` with the `--fix` flag, your code will be formatted according to `prettier` style. --- diff --git a/eslint-plugin-prettier.js b/eslint-plugin-prettier.js index 0c99b131..5d83e0e4 100644 --- a/eslint-plugin-prettier.js +++ b/eslint-plugin-prettier.js @@ -131,7 +131,12 @@ module.exports = { { type: 'object', properties: { - usePrettierrc: { type: 'boolean' } + usePrettierrc: { type: 'boolean' }, + fileInfoOptions: { + type: 'object', + properties: {}, + additionalProperties: true + } }, additionalProperties: true } @@ -140,6 +145,8 @@ module.exports = { create(context) { const usePrettierrc = !context.options[1] || context.options[1].usePrettierrc !== false; + const eslintFileInfoOptions = + (context.options[1] && context.options[1].fileInfoOptions) || {}; const sourceCode = context.getSourceCode(); const filepath = context.getFilename(); const source = sourceCode.text; @@ -163,9 +170,14 @@ module.exports = { }) : null; - const prettierFileInfo = prettier.getFileInfo.sync(filepath, { - ignorePath: '.prettierignore' - }); + const prettierFileInfo = prettier.getFileInfo.sync( + filepath, + Object.assign( + {}, + { ignorePath: '.prettierignore' }, + eslintFileInfoOptions + ) + ); // Skip if file is ignored using a .prettierignore file if (prettierFileInfo.ignored) { diff --git a/test/invalid/18.txt b/test/invalid/18.txt new file mode 100644 index 00000000..7d037f75 --- /dev/null +++ b/test/invalid/18.txt @@ -0,0 +1,19 @@ +CODE: +a();;;; + +OUTPUT: +a(); + +OPTIONS: +[{}, { fileInfoOptions: { withNodeModules: true } }] + +ERRORS: +[ + { + message: 'Delete `;;;`', + line: 1, column: 5, endLine: 1, endColumn: 8, + }, +] + +FILENAME: +node_modules/dummy.js diff --git a/test/prettier.js b/test/prettier.js index 0ef159d0..5eed05ca 100644 --- a/test/prettier.js +++ b/test/prettier.js @@ -61,6 +61,11 @@ ruleTester.run('prettier', rule, { { code: `('');\n`, filename: getPrettierRcJsFilename('single-quote', 'dummy.md') + }, + // Should ignore files from node_modules + { + code: 'a();;;;;;\n', + filename: 'node_modules/dummy.js' } ], invalid: [ @@ -81,7 +86,8 @@ ruleTester.run('prettier', rule, { '14', '15', '16', - '17' + '17', + '18' ].map(loadInvalidFixture) }); @@ -130,6 +136,9 @@ function loadInvalidFixture(name) { options: eval(sections[3]), // eslint-disable-line no-eval errors: eval(sections[4]) // eslint-disable-line no-eval }; + if (sections.length >= 6) { + item.filename = sections[5]; + } return item; }