Skip to content

Commit 9e0fb48

Browse files
eliperelmannot-an-aardvark
authored andcommitted
Update: Add option to skip loading prettierrc (#83)
1 parent e5b5fa7 commit 9e0fb48

File tree

3 files changed

+68
-7
lines changed

3 files changed

+68
-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, (default: `true`). 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

+39-7
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,25 @@ 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+
// Remove leading @
301+
return pragmaRef ? pragmaRef.slice(1) : null;
302+
}
303+
285304
// ------------------------------------------------------------------------------
286305
// Module Definition
287306
// ------------------------------------------------------------------------------
@@ -313,15 +332,26 @@ module.exports = {
313332
{ type: 'object', properties: {}, additionalProperties: true }
314333
]
315334
},
316-
// Pragma:
317-
{ type: 'string', pattern: '^@\\w+$' }
335+
{
336+
anyOf: [
337+
// Pragma:
338+
{ type: 'string', pattern: '^@\\w+$' },
339+
{
340+
type: 'object',
341+
properties: {
342+
pragma: { type: 'string', pattern: '^@\\w+$' },
343+
usePrettierrc: { type: 'boolean' }
344+
},
345+
additionalProperties: true
346+
}
347+
]
348+
}
318349
]
319350
},
320351
create(context) {
321-
const pragma = context.options[1]
322-
? context.options[1].slice(1) // Remove leading @
323-
: null;
324-
352+
const pragma = getPragma(context);
353+
const usePrettierrc =
354+
!context.options[1] || context.options[1].usePrettierrc !== false;
325355
const sourceCode = context.getSourceCode();
326356
const source = sourceCode.text;
327357

@@ -365,7 +395,9 @@ module.exports = {
365395
? FB_PRETTIER_OPTIONS
366396
: context.options[0];
367397
const prettierRcOptions =
368-
prettier.resolveConfig && prettier.resolveConfig.sync
398+
usePrettierrc &&
399+
prettier.resolveConfig &&
400+
prettier.resolveConfig.sync
369401
? prettier.resolveConfig.sync(context.getFilename())
370402
: null;
371403
const prettierOptions = Object.assign(

test/prettier.js

+11
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ ruleTester.run('prettier', rule, {
4141
{ code: `/** @format */\n('');\n`, options: ['fb', '@format'] },
4242
// Shebang with pragma.
4343
{ code: `#!/bin/node\n/** @format */\n"";\n`, options: [null, '@format'] },
44+
// Shebang with pragma from options.
45+
{
46+
code: `#!/bin/node\n/** @format */\n"";\n`,
47+
options: [null, { pragma: '@format' }]
48+
},
4449
// Single quote from .prettierrc.
4550
{ code: `'';\n`, filename: getPrettierRcJsFilename('single-quote') },
4651
// Override .prettierrc from object option.
@@ -54,6 +59,12 @@ ruleTester.run('prettier', rule, {
5459
code: `('');\n`,
5560
filename: getPrettierRcJsFilename('double-quote'),
5661
options: ['fb']
62+
},
63+
// Only use options from plugin, skipping .prettierrc
64+
{
65+
code: `var foo = {bar: 0};\n`,
66+
filename: getPrettierRcJsFilename('bracket-spacing'),
67+
options: [{ bracketSpacing: false }, { usePrettierrc: false }]
5768
}
5869
],
5970
invalid: [

0 commit comments

Comments
 (0)