Skip to content

Commit

Permalink
New: Allow options to be passed to prettier.getFileInfo (#187)
Browse files Browse the repository at this point in the history
New option `fileInfoOptions` to allow configuration of `getFileInfo`, notably to to unignore `node_modules` folders.
  • Loading branch information
sebjambor authored and BPScott committed May 11, 2019
1 parent bb597e1 commit 21fa69a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
10 changes: 10 additions & 0 deletions README.md
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
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
@@ -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
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

0 comments on commit 21fa69a

Please sign in to comment.