Skip to content

Commit

Permalink
Fix: Support prettier v1.6.0 config (prettier#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
kombucha committed Sep 17, 2017
1 parent 95f0808 commit 2aa0cd3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ Then, in your `.eslintrc.json`:
## Options

* The first option:

- Objects are passed directly to Prettier as [options](https://github.com/prettier/prettier#api). Example:
- Objects are passed directly to Prettier as [options](https://github.com/prettier/prettier#options). Example:

```json
"prettier/prettier": ["error", {"singleQuote": true, "parser": "flow"}]
Expand All @@ -78,6 +77,7 @@ Then, in your `.eslintrc.json`:
"parser": "flow"
}]
```
NB: This option will merge and override any config set with `.prettierrc` files (for Prettier < 1.7.0, [config files are ignored](https://github.com/prettier/eslint-plugin-prettier/issues/46))

* The second option:

Expand Down
24 changes: 19 additions & 5 deletions eslint-plugin-prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,6 @@ module.exports = {
]
},
create(context) {
const prettierOptions =
context.options[0] === 'fb'
? FB_PRETTIER_OPTIONS
: context.options[0];

const pragma = context.options[1]
? context.options[1].slice(1) // Remove leading @
: null;
Expand Down Expand Up @@ -342,12 +337,31 @@ module.exports = {
}
}

if (prettier) {
prettier.clearConfigCache();
}

return {
Program() {
if (!prettier) {
// Prettier is expensive to load, so only load it if needed.
prettier = require('prettier');
}

const eslintPrettierOptions =
context.options[0] === 'fb'
? FB_PRETTIER_OPTIONS
: context.options[0];
const prettierRcOptions =
prettier.resolveConfig && prettier.resolveConfig.sync
? prettier.resolveConfig.sync(context.getFilename())
: null;
const prettierOptions = Object.assign(
{},
prettierRcOptions,
eslintPrettierOptions
);

const prettierSource = prettier.format(source, prettierOptions);
if (source !== prettierSource) {
const differences = generateDifferences(source, prettierSource);
Expand Down

0 comments on commit 2aa0cd3

Please sign in to comment.