Skip to content

Commit 90f3750

Browse files
committed
Add usePrettierRc: false option to skip loading prettierrc
1 parent e5b5fa7 commit 90f3750

File tree

2 files changed

+56
-7
lines changed

2 files changed

+56
-7
lines changed

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,24 @@ You can then set Prettier's own options inside a `.prettierrc` file.
141141
```
142142

143143
_This option is useful if you're migrating a large codebase and already use pragmas like `@flow`._
144+
145+
- An object with the following options
146+
147+
- `pragma`: Also sets the aforementioned `pragma`: a string with a pragma that triggers this rule. By default, this rule applies to all files. However, if you set a pragma (this option), only files with that pragma in the heading docblock will be checked. All pragmas must start with `@`.
148+
149+
```json
150+
"prettier/prettier": ["error", null, {
151+
"pragma": "@prettier"
152+
}]
153+
```
154+
155+
- `usePrettierRc`: Enables loading of the Prettier configuration file, set to `false` to disable. May be useful if you are using multiple tools that conflict with each other, or do not wish to mix your ESLint settings with your Prettier configuration.
156+
157+
```json
158+
"prettier/prettier": ["error", null, {
159+
"usePrettierRc": false
160+
}]
161+
```
144162

145163
* The rule is autofixable -- if you run `eslint` with the `--fix` flag, your code will be formatted according to `prettier` style.
146164

eslint-plugin-prettier.js

+38-7
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,24 @@ function reportReplace(context, offset, deleteText, insertText) {
282282
});
283283
}
284284

285+
/**
286+
* Get the pragma from the ESLint rule context.
287+
* @param {RuleContext} context - The ESLint rule context.
288+
* @returns {string|null}
289+
*/
290+
function getPragma(context) {
291+
const pluginOptions = context.options[1];
292+
293+
if (!pluginOptions) {
294+
return null;
295+
}
296+
297+
const pragmaRef =
298+
typeof pluginOptions === 'string' ? pluginOptions : pluginOptions.pragma;
299+
300+
return pragmaRef.slice(1); // Remove leading @
301+
}
302+
285303
// ------------------------------------------------------------------------------
286304
// Module Definition
287305
// ------------------------------------------------------------------------------
@@ -313,15 +331,26 @@ module.exports = {
313331
{ type: 'object', properties: {}, additionalProperties: true }
314332
]
315333
},
316-
// Pragma:
317-
{ type: 'string', pattern: '^@\\w+$' }
334+
{
335+
anyOf: [
336+
// Pragma:
337+
{ type: 'string', pattern: '^@\\w+$' },
338+
{
339+
type: 'object',
340+
properties: {
341+
pragma: { type: 'string', pattern: '^@\\w+$' },
342+
usePrettierRc: { type: 'boolean' }
343+
},
344+
additionalProperties: true
345+
}
346+
]
347+
}
318348
]
319349
},
320350
create(context) {
321-
const pragma = context.options[1]
322-
? context.options[1].slice(1) // Remove leading @
323-
: null;
324-
351+
const pragma = getPragma(context);
352+
const usePrettierRc =
353+
!context.options[1] || context.options[1].usePrettierRc !== false;
325354
const sourceCode = context.getSourceCode();
326355
const source = sourceCode.text;
327356

@@ -365,7 +394,9 @@ module.exports = {
365394
? FB_PRETTIER_OPTIONS
366395
: context.options[0];
367396
const prettierRcOptions =
368-
prettier.resolveConfig && prettier.resolveConfig.sync
397+
usePrettierRc &&
398+
prettier.resolveConfig &&
399+
prettier.resolveConfig.sync
369400
? prettier.resolveConfig.sync(context.getFilename())
370401
: null;
371402
const prettierOptions = Object.assign(

0 commit comments

Comments
 (0)