From 34114f492670c8a99bbb740905a6c5b29ebcb4cb 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 +++++++++++++++----- test/prettier.js | 25 ++++++++++++++++++++- test/prettierrc/bracket-spacing/.prettierrc | 3 +++ test/prettierrc/double-quote/.prettierrc | 3 +++ test/prettierrc/single-quote/.prettierrc | 3 +++ yarn.lock | 10 ++++----- 7 files changed, 59 insertions(+), 13 deletions(-) create mode 100644 test/prettierrc/bracket-spacing/.prettierrc create mode 100644 test/prettierrc/double-quote/.prettierrc create mode 100644 test/prettierrc/single-quote/.prettierrc 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); diff --git a/test/prettier.js b/test/prettier.js index 14b264e7..3d28c349 100644 --- a/test/prettier.js +++ b/test/prettier.js @@ -40,7 +40,21 @@ ruleTester.run('prettier', rule, { // Facebook style with pragma. { code: `/** @format */\n('');\n`, options: ['fb', '@format'] }, // Shebang with pragma. - { code: `#!/bin/node\n/** @format */\n"";\n`, options: [null, '@format'] } + { code: `#!/bin/node\n/** @format */\n"";\n`, options: [null, '@format'] }, + // Single quote from .prettierrc. + { code: `'';\n`, filename: getPrettierRcJsFilename('single-quote') }, + // Override .prettierrc from object option. + { + code: `var foo = {bar: 0};\n`, + filename: getPrettierRcJsFilename('bracket-spacing'), + options: [{ bracketSpacing: false }] + }, + // Override .prettierrc from facebook option. + { + code: `('');\n`, + filename: getPrettierRcJsFilename('double-quote'), + options: ['fb'] + } ], invalid: [ '01', @@ -120,3 +134,12 @@ function loadInvalidFixture(name) { }; return item; } + +/** + * Builds a dummy javascript file path to trick prettier into resolving a specific .prettierrc file. + * @param {string} name - Prettierrc fixture basename. + * @returns {string} A javascript filename relative to the .prettierrc config. + */ +function getPrettierRcJsFilename(name) { + return path.resolve(__dirname, `./prettierrc/${name}/dummy.js`); +} diff --git a/test/prettierrc/bracket-spacing/.prettierrc b/test/prettierrc/bracket-spacing/.prettierrc new file mode 100644 index 00000000..42ad37d8 --- /dev/null +++ b/test/prettierrc/bracket-spacing/.prettierrc @@ -0,0 +1,3 @@ +{ + "bracketSpacing": true +} diff --git a/test/prettierrc/double-quote/.prettierrc b/test/prettierrc/double-quote/.prettierrc new file mode 100644 index 00000000..1ca87ab7 --- /dev/null +++ b/test/prettierrc/double-quote/.prettierrc @@ -0,0 +1,3 @@ +{ + "singleQuote": false +} diff --git a/test/prettierrc/single-quote/.prettierrc b/test/prettierrc/single-quote/.prettierrc new file mode 100644 index 00000000..544138be --- /dev/null +++ b/test/prettierrc/single-quote/.prettierrc @@ -0,0 +1,3 @@ +{ + "singleQuote": true +} diff --git a/yarn.lock b/yarn.lock index 62995f14..ffb88b94 100644 --- a/yarn.lock +++ b/yarn.lock @@ -556,9 +556,9 @@ isarray@^1.0.0, isarray@~1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11" -jest-docblock@^20.0.1: - version "20.0.3" - resolved "https://registry.yarnpkg.com/jest-docblock/-/jest-docblock-20.0.3.tgz#17bea984342cc33d83c50fbe1545ea0efaa44712" +jest-docblock@^21.0.0: + version "21.1.0" + resolved "https://registry.yarnpkg.com/jest-docblock/-/jest-docblock-21.1.0.tgz#43154be2441fb91403e36bb35cb791a5017cea81" js-tokens@^3.0.0: version "3.0.1" @@ -763,8 +763,8 @@ prelude-ls@~1.1.2: resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54" prettier@^1.6.1: - version "1.6.1" - resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.6.1.tgz#850f411a3116226193e32ea5acfc21c0f9a76d7d" + version "1.7.0" + resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.7.0.tgz#47481588f41f7c90f63938feb202ac82554e7150" process-nextick-args@~1.0.6: version "1.0.7"