From 77799d5f5d56eb8aceadbe218d7c66e31ea3fe8a Mon Sep 17 00:00:00 2001 From: Vincent Lemeunier Date: Sun, 17 Sep 2017 12:48:33 +0200 Subject: [PATCH] Fix: Support prettier v1.6.0 config (#46) --- README.md | 2 +- eslint-plugin-prettier.js | 24 +++++++++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b8fe9328..b2f5b94b 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ Then, in your `.eslintrc.json`: * 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#api). This plugin will merge and override any config set with a `.prettierrc` file (only for prettier >= 0.17). Example: ```json "prettier/prettier": ["error", {"singleQuote": true, "parser": "flow"}] diff --git a/eslint-plugin-prettier.js b/eslint-plugin-prettier.js index 8d4a2a65..a5c0b757 100644 --- a/eslint-plugin-prettier.js +++ b/eslint-plugin-prettier.js @@ -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; @@ -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);