Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

---
Expand Down
20 changes: 16 additions & 4 deletions eslint-plugin-prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,12 @@ module.exports = {
{
type: 'object',
properties: {
usePrettierrc: { type: 'boolean' }
usePrettierrc: { type: 'boolean' },
fileInfoOptions: {
type: 'object',
properties: {},
additionalProperties: true
}
},
additionalProperties: true
}
Expand All @@ -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;
Expand All @@ -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) {
Expand Down
19 changes: 19 additions & 0 deletions test/invalid/18.txt
Original file line number Diff line number Diff line change
@@ -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
11 changes: 10 additions & 1 deletion test/prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -81,7 +86,8 @@ ruleTester.run('prettier', rule, {
'14',
'15',
'16',
'17'
'17',
'18'
].map(loadInvalidFixture)
});

Expand Down Expand Up @@ -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;
}

Expand Down