Skip to content

Commit 21fa69a

Browse files
sebjamborBPScott
authored andcommitted
New: Allow options to be passed to prettier.getFileInfo (#187)
New option `fileInfoOptions` to allow configuration of `getFileInfo`, notably to to unignore `node_modules` folders.
1 parent bb597e1 commit 21fa69a

File tree

4 files changed

+55
-5
lines changed

4 files changed

+55
-5
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,16 @@ For the list of every available exclusion rule set, please see the [readme of es
121121
}]
122122
```
123123

124+
- `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.
125+
126+
```json
127+
"prettier/prettier": ["error", {}, {
128+
"fileInfoOptions": {
129+
"withNodeModules": true
130+
}
131+
}]
132+
```
133+
124134
- The rule is autofixable -- if you run `eslint` with the `--fix` flag, your code will be formatted according to `prettier` style.
125135

126136
---

eslint-plugin-prettier.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,12 @@ module.exports = {
131131
{
132132
type: 'object',
133133
properties: {
134-
usePrettierrc: { type: 'boolean' }
134+
usePrettierrc: { type: 'boolean' },
135+
fileInfoOptions: {
136+
type: 'object',
137+
properties: {},
138+
additionalProperties: true
139+
}
135140
},
136141
additionalProperties: true
137142
}
@@ -140,6 +145,8 @@ module.exports = {
140145
create(context) {
141146
const usePrettierrc =
142147
!context.options[1] || context.options[1].usePrettierrc !== false;
148+
const eslintFileInfoOptions =
149+
(context.options[1] && context.options[1].fileInfoOptions) || {};
143150
const sourceCode = context.getSourceCode();
144151
const filepath = context.getFilename();
145152
const source = sourceCode.text;
@@ -163,9 +170,14 @@ module.exports = {
163170
})
164171
: null;
165172

166-
const prettierFileInfo = prettier.getFileInfo.sync(filepath, {
167-
ignorePath: '.prettierignore'
168-
});
173+
const prettierFileInfo = prettier.getFileInfo.sync(
174+
filepath,
175+
Object.assign(
176+
{},
177+
{ ignorePath: '.prettierignore' },
178+
eslintFileInfoOptions
179+
)
180+
);
169181

170182
// Skip if file is ignored using a .prettierignore file
171183
if (prettierFileInfo.ignored) {

test/invalid/18.txt

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
CODE:
2+
a();;;;
3+
4+
OUTPUT:
5+
a();
6+
7+
OPTIONS:
8+
[{}, { fileInfoOptions: { withNodeModules: true } }]
9+
10+
ERRORS:
11+
[
12+
{
13+
message: 'Delete `;;;`',
14+
line: 1, column: 5, endLine: 1, endColumn: 8,
15+
},
16+
]
17+
18+
FILENAME:
19+
node_modules/dummy.js

test/prettier.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ ruleTester.run('prettier', rule, {
6161
{
6262
code: `('');\n`,
6363
filename: getPrettierRcJsFilename('single-quote', 'dummy.md')
64+
},
65+
// Should ignore files from node_modules
66+
{
67+
code: 'a();;;;;;\n',
68+
filename: 'node_modules/dummy.js'
6469
}
6570
],
6671
invalid: [
@@ -81,7 +86,8 @@ ruleTester.run('prettier', rule, {
8186
'14',
8287
'15',
8388
'16',
84-
'17'
89+
'17',
90+
'18'
8591
].map(loadInvalidFixture)
8692
});
8793

@@ -130,6 +136,9 @@ function loadInvalidFixture(name) {
130136
options: eval(sections[3]), // eslint-disable-line no-eval
131137
errors: eval(sections[4]) // eslint-disable-line no-eval
132138
};
139+
if (sections.length >= 6) {
140+
item.filename = sections[5];
141+
}
133142
return item;
134143
}
135144

0 commit comments

Comments
 (0)