Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to skip loading prettierrc #83

Merged
merged 2 commits into from
Feb 2, 2018
Merged
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
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