Skip to content
Closed
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ For the list of every available exclusion rule set, please see the [readme of es
}]
```

- `withNodeModules`: Prettier ignores files located in `node_modules` directories. To opt-out from this behavior set `true`, (default: `false`). May be useful if you are using multiple `node_modules` directories, example to avoid many `../..` sequences.

```json
"prettier/prettier": ["error", {}, {
"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
8 changes: 6 additions & 2 deletions eslint-plugin-prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ module.exports = {
{
type: 'object',
properties: {
usePrettierrc: { type: 'boolean' }
usePrettierrc: { type: 'boolean' },
withNodeModules: { type: 'boolean' }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this an object named fileInfoOptions

},
additionalProperties: true
}
Expand All @@ -140,6 +141,8 @@ module.exports = {
create(context) {
const usePrettierrc =
!context.options[1] || context.options[1].usePrettierrc !== false;
const withNodeModules =
context.options[1] && context.options[1].withNodeModules !== false;
const sourceCode = context.getSourceCode();
const filepath = context.getFilename();
const source = sourceCode.text;
Expand All @@ -164,7 +167,8 @@ module.exports = {
: null;

const prettierFileInfo = prettier.getFileInfo.sync(filepath, {
ignorePath: '.prettierignore'
ignorePath: '.prettierignore',
withNodeModules
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than pass in a single value within the options here, I think it would be more future-compatible to pass in an object that is merged with some defaults. That means that if prettier decides to add additional options to getFileInfo() then we won't need to make additional changes to accommodate them.

Something like this (written but not tested):

const fileInfoOptions = Object.assign(
    {},
    { ignorePath: '.prettierignore' }
    eslintFileInfoOptions
)

const prettierFileInfo = prettier.getFileInfo.sync(filepath, fileInfoOptions);

Where eslintFileInfoOptions is defined above as something like this: (written but not tested)

const eslintFileInfoOptions = context.options[1] && context.options[1].fileInfoOptions || {}

});

// Skip if file is ignored using a .prettierignore file
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:
[{}, { withNodeModules: true }]

ERRORS:
[
{
message: 'Delete `;;;`',
line: 1, column: 5, endLine: 1, endColumn: 8,
},
]

FILENAME:
node_modules/dummy.js
17 changes: 16 additions & 1 deletion test/prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ 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'
},
// Should check files from node_modules too
{
code: 'a();\n',
filename: 'node_modules/dummy.js',
options: [{}, { withNodeModules: true }]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated usage would be

options: [{}, { fileInfoOptions: { withNodeModules: true }}]

}
],
invalid: [
Expand All @@ -81,7 +92,8 @@ ruleTester.run('prettier', rule, {
'14',
'15',
'16',
'17'
'17',
'18'
].map(loadInvalidFixture)
});

Expand Down Expand Up @@ -127,6 +139,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