From 2aa0cd3211aa616b11fc670dd99fd411ef78fda6 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 | 4 ++-- eslint-plugin-prettier.js | 24 +++++++++++++++++++----- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b8fe9328..6e2d98ed 100644 --- a/README.md +++ b/README.md @@ -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"}] @@ -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: 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);