Skip to content

Commit

Permalink
Update: Add option to skip loading prettierrc (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliperelman authored and not-an-aardvark committed Feb 2, 2018
1 parent e5b5fa7 commit 9e0fb48
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 7 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,24 @@ You can then set Prettier's own options inside a `.prettierrc` file.
```

_This option is useful if you're migrating a large codebase and already use pragmas like `@flow`._

- An object with the following options

- `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 `@`.

```json
"prettier/prettier": ["error", null, {
"pragma": "@prettier"
}]
```

- `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.

```json
"prettier/prettier": ["error", null, {
"usePrettierrc": false
}]
```

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

Expand Down
46 changes: 39 additions & 7 deletions eslint-plugin-prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,25 @@ function reportReplace(context, offset, deleteText, insertText) {
});
}

/**
* Get the pragma from the ESLint rule context.
* @param {RuleContext} context - The ESLint rule context.
* @returns {string|null}
*/
function getPragma(context) {
const pluginOptions = context.options[1];

if (!pluginOptions) {
return null;
}

const pragmaRef =
typeof pluginOptions === 'string' ? pluginOptions : pluginOptions.pragma;

// Remove leading @
return pragmaRef ? pragmaRef.slice(1) : null;
}

// ------------------------------------------------------------------------------
// Module Definition
// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -313,15 +332,26 @@ module.exports = {
{ type: 'object', properties: {}, additionalProperties: true }
]
},
// Pragma:
{ type: 'string', pattern: '^@\\w+$' }
{
anyOf: [
// Pragma:
{ type: 'string', pattern: '^@\\w+$' },
{
type: 'object',
properties: {
pragma: { type: 'string', pattern: '^@\\w+$' },
usePrettierrc: { type: 'boolean' }
},
additionalProperties: true
}
]
}
]
},
create(context) {
const pragma = context.options[1]
? context.options[1].slice(1) // Remove leading @
: null;

const pragma = getPragma(context);
const usePrettierrc =
!context.options[1] || context.options[1].usePrettierrc !== false;
const sourceCode = context.getSourceCode();
const source = sourceCode.text;

Expand Down Expand Up @@ -365,7 +395,9 @@ module.exports = {
? FB_PRETTIER_OPTIONS
: context.options[0];
const prettierRcOptions =
prettier.resolveConfig && prettier.resolveConfig.sync
usePrettierrc &&
prettier.resolveConfig &&
prettier.resolveConfig.sync
? prettier.resolveConfig.sync(context.getFilename())
: null;
const prettierOptions = Object.assign(
Expand Down
11 changes: 11 additions & 0 deletions test/prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ ruleTester.run('prettier', rule, {
{ code: `/** @format */\n('');\n`, options: ['fb', '@format'] },
// Shebang with pragma.
{ code: `#!/bin/node\n/** @format */\n"";\n`, options: [null, '@format'] },
// Shebang with pragma from options.
{
code: `#!/bin/node\n/** @format */\n"";\n`,
options: [null, { pragma: '@format' }]
},
// Single quote from .prettierrc.
{ code: `'';\n`, filename: getPrettierRcJsFilename('single-quote') },
// Override .prettierrc from object option.
Expand All @@ -54,6 +59,12 @@ ruleTester.run('prettier', rule, {
code: `('');\n`,
filename: getPrettierRcJsFilename('double-quote'),
options: ['fb']
},
// Only use options from plugin, skipping .prettierrc
{
code: `var foo = {bar: 0};\n`,
filename: getPrettierRcJsFilename('bracket-spacing'),
options: [{ bracketSpacing: false }, { usePrettierrc: false }]
}
],
invalid: [
Expand Down

0 comments on commit 9e0fb48

Please sign in to comment.