Repository: prettier/prettier-eslint
Pull request #98 (2017-06-23 19:01:32)

Use fallbacks for disabled ESLint rules
Fixes https://github.com/prettier/prettier-eslint/issues/97

kentcdodds: Thanks for this! Could you add some tests for your changes?

Patch:
diff --git a/.all-contributorsrc b/.all-contributorsrc
index 2b23a0d..8740c87 100644
--- a/.all-contributorsrc
+++ b/.all-contributorsrc
@@ -202,6 +202,13 @@
         "bug",
         "test"
       ]
+    },
+    {
+      "login": "zimme",
+      "name": "Simon Fridlund",
+      "avatar_url": "https://avatars0.githubusercontent.com/u/1215414?v=3",
+      "profile": "https://github.com/zimme",
+      "contributions": []
     }
   ]
 }
diff --git a/README.md b/README.md
index fce2951..2279b82 100644
--- a/README.md
+++ b/README.md
@@ -2,9 +2,6 @@
 
 Formats your JavaScript using [`prettier`][prettier] followed by [`eslint --fix`][eslint]
 
-For files with an extension of `.css`, `.less`, or `.scss`,
-this only runs `prettier` since `eslint` cannot process those.
-
 [![Build Status][build-badge]][build]
 [![Code Coverage][coverage-badge]][coverage]
 [![Dependencies][dependencyci-badge]][dependencyci]
@@ -12,7 +9,7 @@ this only runs `prettier` since `eslint` cannot process those.
 [![downloads][downloads-badge]][npm-stat]
 [![MIT License][license-badge]][LICENSE]
 
-[![All Contributors](https://img.shields.io/badge/all_contributors-20-orange.svg?style=flat-square)](#contributors)
+[![All Contributors](https://img.shields.io/badge/all_contributors-21-orange.svg?style=flat-square)](#contributors)
 [![PRs Welcome][prs-badge]][prs]
 [![Donate][donate-badge]][donate]
 [![Code of Conduct][coc-badge]][coc]
@@ -36,6 +33,9 @@ This formats your code via `prettier`, and then passes the result of that to `es
 benefits of `prettier`'s superior formatting capabilities, but also benefit from the configuration capabilities of
 `eslint`.
 
+> For files with an extension of `.css`, `.less`, `.scss`, or `.json`
+> this only runs `prettier` since `eslint` cannot process those.
+
 ## Installation
 
 This module is distributed via [npm][npm] which is bundled with [node][node] and should be installed as one of your
@@ -234,10 +234,10 @@ None that I'm aware of. Feel free to file a PR if you know of any other solution
 Thanks goes to these people ([emoji key][emojis]):
 
 <!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
-| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub>Kent C. Dodds</sub>](https://kentcdodds.com)<br />[💻](https://github.com/prettier/prettier-eslint/commits?author=kentcdodds) [📖](https://github.com/prettier/prettier-eslint/commits?author=kentcdodds) 🚇 [⚠️](https://github.com/prettier/prettier-eslint/commits?author=kentcdodds) | [<img src="https://avatars.githubusercontent.com/u/5554486?v=3" width="100px;"/><br /><sub>Gyandeep Singh</sub>](http://gyandeeps.com)<br />👀 | [<img src="https://avatars.githubusercontent.com/u/682584?v=3" width="100px;"/><br /><sub>Igor Pnev</sub>](https://github.com/exdeniz)<br />[🐛](https://github.com/prettier/prettier-eslint/issues?q=author%3Aexdeniz) | [<img src="https://avatars.githubusercontent.com/u/813865?v=3" width="100px;"/><br /><sub>Benjamin Tan</sub>](https://demoneaux.github.io/)<br />💬 👀 | [<img src="https://avatars.githubusercontent.com/u/622118?v=3" width="100px;"/><br /><sub>Eric McCormick</sub>](https://ericmccormick.io)<br />[💻](https://github.com/prettier/prettier-eslint/commits?author=edm00se) [📖](https://github.com/prettier/prettier-eslint/commits?author=edm00se) [⚠️](https://github.com/prettier/prettier-eslint/commits?author=edm00se) | [<img src="https://avatars.githubusercontent.com/u/2142817?v=3" width="100px;"/><br /><sub>Simon Lydell</sub>](https://github.com/lydell)<br />[📖](https://github.com/prettier/prettier-eslint/commits?author=lydell) | [<img src="https://avatars0.githubusercontent.com/u/981957?v=3" width="100px;"/><br /><sub>Tom McKearney</sub>](https://github.com/tommck)<br />[📖](https://github.com/prettier/prettier-eslint/commits?author=tommck) 💡 |
+| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub>Kent C. Dodds</sub>](https://kentcdodds.com)<br />[💻](https://github.com/prettier/prettier-eslint/commits?author=kentcdodds "Code") [📖](https://github.com/prettier/prettier-eslint/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/prettier/prettier-eslint/commits?author=kentcdodds "Tests") | [<img src="https://avatars.githubusercontent.com/u/5554486?v=3" width="100px;"/><br /><sub>Gyandeep Singh</sub>](http://gyandeeps.com)<br />[👀](#review-gyandeeps "Reviewed Pull Requests") | [<img src="https://avatars.githubusercontent.com/u/682584?v=3" width="100px;"/><br /><sub>Igor Pnev</sub>](https://github.com/exdeniz)<br />[🐛](https://github.com/prettier/prettier-eslint/issues?q=author%3Aexdeniz "Bug reports") | [<img src="https://avatars.githubusercontent.com/u/813865?v=3" width="100px;"/><br /><sub>Benjamin Tan</sub>](https://demoneaux.github.io/)<br />[💬](#question-demoneaux "Answering Questions") [👀](#review-demoneaux "Reviewed Pull Requests") | [<img src="https://avatars.githubusercontent.com/u/622118?v=3" width="100px;"/><br /><sub>Eric McCormick</sub>](https://ericmccormick.io)<br />[💻](https://github.com/prettier/prettier-eslint/commits?author=edm00se "Code") [📖](https://github.com/prettier/prettier-eslint/commits?author=edm00se "Documentation") [⚠️](https://github.com/prettier/prettier-eslint/commits?author=edm00se "Tests") | [<img src="https://avatars.githubusercontent.com/u/2142817?v=3" width="100px;"/><br /><sub>Simon Lydell</sub>](https://github.com/lydell)<br />[📖](https://github.com/prettier/prettier-eslint/commits?author=lydell "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/981957?v=3" width="100px;"/><br /><sub>Tom McKearney</sub>](https://github.com/tommck)<br />[📖](https://github.com/prettier/prettier-eslint/commits?author=tommck "Documentation") [💡](#example-tommck "Examples") |
 | :---: | :---: | :---: | :---: | :---: | :---: | :---: |
-| [<img src="https://avatars.githubusercontent.com/u/463105?v=3" width="100px;"/><br /><sub>Patrik Åkerstrand</sub>](https://github.com/PAkerstrand)<br />[💻](https://github.com/prettier/prettier-eslint/commits?author=PAkerstrand) | [<img src="https://avatars.githubusercontent.com/u/1560301?v=3" width="100px;"/><br /><sub>Lochlan Bunn</sub>](https://twitter.com/loklaan)<br />[💻](https://github.com/prettier/prettier-eslint/commits?author=loklaan) | [<img src="https://avatars.githubusercontent.com/u/25886902?v=3" width="100px;"/><br /><sub>Daniël Terwiel</sub>](https://github.com/danielterwiel)<br />🔌 🔧 | [<img src="https://avatars1.githubusercontent.com/u/1834413?v=3" width="100px;"/><br /><sub>Robin Malfait</sub>](https://robinmalfait.com)<br />🔧 | [<img src="https://avatars0.githubusercontent.com/u/8161781?v=3" width="100px;"/><br /><sub>Michael McDermott</sub>](http://mgmcdermott.com)<br />[💻](https://github.com/prettier/prettier-eslint/commits?author=mgmcdermott) | [<img src="https://avatars3.githubusercontent.com/u/292365?v=3" width="100px;"/><br /><sub>Adam Stankiewicz</sub>](http://sheerun.net)<br />[💻](https://github.com/prettier/prettier-eslint/commits?author=sheerun) | [<img src="https://avatars3.githubusercontent.com/u/487068?v=3" width="100px;"/><br /><sub>Stephen John Sorensen</sub>](http://www.stephenjohnsorensen.com/)<br />[💻](https://github.com/prettier/prettier-eslint/commits?author=spudly) |
-| [<img src="https://avatars2.githubusercontent.com/u/1597820?v=3" width="100px;"/><br /><sub>Brian Di Palma</sub>](https://github.com/briandipalma)<br />[🐛](https://github.com/prettier/prettier-eslint/issues?q=author%3Abriandipalma) [💻](https://github.com/prettier/prettier-eslint/commits?author=briandipalma) | [<img src="https://avatars0.githubusercontent.com/u/6173488?v=3" width="100px;"/><br /><sub>Rob Wise</sub>](https://robwise.github.io)<br />[📖](https://github.com/prettier/prettier-eslint/commits?author=robwise) [💻](https://github.com/prettier/prettier-eslint/commits?author=robwise) | [<img src="https://avatars0.githubusercontent.com/u/4818642?v=3" width="100px;"/><br /><sub>Patryk Peas</sub>](https://github.com/Belir)<br />[🐛](https://github.com/prettier/prettier-eslint/issues?q=author%3ABelir) [💻](https://github.com/prettier/prettier-eslint/commits?author=Belir) [⚠️](https://github.com/prettier/prettier-eslint/commits?author=Belir) | [<img src="https://avatars2.githubusercontent.com/u/1193520?v=3" width="100px;"/><br /><sub>Thijs Koerselman</sub>](http://vauxlab.com)<br />[🐛](https://github.com/prettier/prettier-eslint/issues?q=author%3A0x80) [💻](https://github.com/prettier/prettier-eslint/commits?author=0x80) [⚠️](https://github.com/prettier/prettier-eslint/commits?author=0x80) | [<img src="https://avatars3.githubusercontent.com/u/7918284?v=3" width="100px;"/><br /><sub>Enrique Caballero</sub>](https://github.com/enriquecaballero)<br />[🐛](https://github.com/prettier/prettier-eslint/issues?q=author%3Aenriquecaballero) [💻](https://github.com/prettier/prettier-eslint/commits?author=enriquecaballero) | [<img src="https://avatars2.githubusercontent.com/u/1408542?v=3" width="100px;"/><br /><sub>Łukasz Moroz</sub>](https://github.com/lukaszmoroz)<br />[🐛](https://github.com/prettier/prettier-eslint/issues?q=author%3Alukaszmoroz) [⚠️](https://github.com/prettier/prettier-eslint/commits?author=lukaszmoroz) |
+| [<img src="https://avatars.githubusercontent.com/u/463105?v=3" width="100px;"/><br /><sub>Patrik Åkerstrand</sub>](https://github.com/PAkerstrand)<br />[💻](https://github.com/prettier/prettier-eslint/commits?author=PAkerstrand "Code") | [<img src="https://avatars.githubusercontent.com/u/1560301?v=3" width="100px;"/><br /><sub>Lochlan Bunn</sub>](https://twitter.com/loklaan)<br />[💻](https://github.com/prettier/prettier-eslint/commits?author=loklaan "Code") | [<img src="https://avatars.githubusercontent.com/u/25886902?v=3" width="100px;"/><br /><sub>Daniël Terwiel</sub>](https://github.com/danielterwiel)<br />[🔌](#plugin-danielterwiel "Plugin/utility libraries") [🔧](#tool-danielterwiel "Tools") | [<img src="https://avatars1.githubusercontent.com/u/1834413?v=3" width="100px;"/><br /><sub>Robin Malfait</sub>](https://robinmalfait.com)<br />[🔧](#tool-RobinMalfait "Tools") | [<img src="https://avatars0.githubusercontent.com/u/8161781?v=3" width="100px;"/><br /><sub>Michael McDermott</sub>](http://mgmcdermott.com)<br />[💻](https://github.com/prettier/prettier-eslint/commits?author=mgmcdermott "Code") | [<img src="https://avatars3.githubusercontent.com/u/292365?v=3" width="100px;"/><br /><sub>Adam Stankiewicz</sub>](http://sheerun.net)<br />[💻](https://github.com/prettier/prettier-eslint/commits?author=sheerun "Code") | [<img src="https://avatars3.githubusercontent.com/u/487068?v=3" width="100px;"/><br /><sub>Stephen John Sorensen</sub>](http://www.stephenjohnsorensen.com/)<br />[💻](https://github.com/prettier/prettier-eslint/commits?author=spudly "Code") |
+| [<img src="https://avatars2.githubusercontent.com/u/1597820?v=3" width="100px;"/><br /><sub>Brian Di Palma</sub>](https://github.com/briandipalma)<br />[🐛](https://github.com/prettier/prettier-eslint/issues?q=author%3Abriandipalma "Bug reports") [💻](https://github.com/prettier/prettier-eslint/commits?author=briandipalma "Code") | [<img src="https://avatars0.githubusercontent.com/u/6173488?v=3" width="100px;"/><br /><sub>Rob Wise</sub>](https://robwise.github.io)<br />[📖](https://github.com/prettier/prettier-eslint/commits?author=robwise "Documentation") [💻](https://github.com/prettier/prettier-eslint/commits?author=robwise "Code") | [<img src="https://avatars0.githubusercontent.com/u/4818642?v=3" width="100px;"/><br /><sub>Patryk Peas</sub>](https://github.com/Belir)<br />[🐛](https://github.com/prettier/prettier-eslint/issues?q=author%3ABelir "Bug reports") [💻](https://github.com/prettier/prettier-eslint/commits?author=Belir "Code") [⚠️](https://github.com/prettier/prettier-eslint/commits?author=Belir "Tests") | [<img src="https://avatars2.githubusercontent.com/u/1193520?v=3" width="100px;"/><br /><sub>Thijs Koerselman</sub>](http://vauxlab.com)<br />[🐛](https://github.com/prettier/prettier-eslint/issues?q=author%3A0x80 "Bug reports") [💻](https://github.com/prettier/prettier-eslint/commits?author=0x80 "Code") [⚠️](https://github.com/prettier/prettier-eslint/commits?author=0x80 "Tests") | [<img src="https://avatars3.githubusercontent.com/u/7918284?v=3" width="100px;"/><br /><sub>Enrique Caballero</sub>](https://github.com/enriquecaballero)<br />[🐛](https://github.com/prettier/prettier-eslint/issues?q=author%3Aenriquecaballero "Bug reports") [💻](https://github.com/prettier/prettier-eslint/commits?author=enriquecaballero "Code") | [<img src="https://avatars2.githubusercontent.com/u/1408542?v=3" width="100px;"/><br /><sub>Łukasz Moroz</sub>](https://github.com/lukaszmoroz)<br />[🐛](https://github.com/prettier/prettier-eslint/issues?q=author%3Alukaszmoroz "Bug reports") [⚠️](https://github.com/prettier/prettier-eslint/commits?author=lukaszmoroz "Tests") | [<img src="https://avatars0.githubusercontent.com/u/1215414?v=3" width="100px;"/><br /><sub>Simon Fridlund</sub>](https://github.com/zimme)<br /> |
 <!-- ALL-CONTRIBUTORS-LIST:END -->
 
 This project follows the [all-contributors][all-contributors] specification. Contributions of any kind welcome!
diff --git a/package.json b/package.json
index 43adac4..1374d6d 100644
--- a/package.json
+++ b/package.json
@@ -23,7 +23,7 @@
     "indent-string": "^3.1.0",
     "lodash.merge": "^4.6.0",
     "loglevel-colored-level-prefix": "^1.0.0",
-    "prettier": "^1.4.2",
+    "prettier": "^1.5.0",
     "pretty-format": "^20.0.3",
     "require-relative": "^0.8.7"
   },
diff --git a/src/__mocks__/eslint.js b/src/__mocks__/eslint.js
index 66cac05..6844615 100644
--- a/src/__mocks__/eslint.js
+++ b/src/__mocks__/eslint.js
@@ -45,7 +45,11 @@ function mockGetConfigForFile(filePath) {
         semi: [2, 'never'],
         'max-len': [2, 120, 2],
         indent: [2, 2, {SwitchCase: 1}],
-        quotes: [2, 'single', {avoidEscape: true, allowTemplateLiterals: true}],
+        quotes: [
+          2,
+          'single',
+          {avoidEscape: true, allowTemplateLiterals: true},
+        ],
         'comma-dangle': [
           2,
           {
diff --git a/src/index.js b/src/index.js
index c55f701..cb6149c 100644
--- a/src/index.js
+++ b/src/index.js
@@ -1,4 +1,5 @@
 /* eslint no-console:0, global-require:0, import/no-dynamic-require:0 */
+/* eslint complexity: [1, 6] */
 import fs from 'fs'
 import path from 'path'
 import requireRelative from 'require-relative'
@@ -24,7 +25,7 @@ module.exports = format
  *   Will default to require.resolve('eslint')
  * @param {String} options.prettierPath - the path to the prettier module.
  *   Will default to require.resovlve('prettierPath')
- * @param {String} options.eslintConfig - the config to use for formatting
+ * @param {Object} options.eslintConfig - the config to use for formatting
  *  with ESLint.
  * @param {Object} options.prettierOptions - the options to pass for
  *  formatting with `prettier`. If not provided, prettier-eslint will attempt
@@ -78,15 +79,23 @@ function format(options) {
   )
 
   const isCss = /\.(css|less|scss)$/.test(filePath)
+  const isJson = /\.json$/.test(filePath)
+
   if (isCss) {
     formattingOptions.prettier.parser = 'postcss'
+  } else if (isJson) {
+    formattingOptions.prettier.parser = 'json'
+    formattingOptions.prettier.trailingComma = 'none'
   }
+
   const prettify = createPrettify(formattingOptions.prettier, prettierPath)
-  const eslintFix = createEslintFix(formattingOptions.eslint, eslintPath)
 
-  if (isCss) {
+  if (isCss || isJson) {
     return prettify(text, filePath)
   }
+
+  const eslintFix = createEslintFix(formattingOptions.eslint, eslintPath)
+
   if (prettierLast) {
     return prettify(eslintFix(text, filePath))
   }
diff --git a/src/utils.js b/src/utils.js
index 30866dc..9a78f74 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -148,17 +148,11 @@ function configureOptions(
     const {ruleValue, ruleValueToPrettierOption} = OPTION_GETTERS[key]
     const eslintRuleValue = ruleValue(rules)
 
-    if (eslintRuleValue !== RULE_DISABLED) {
-      const prettierOptionValue = ruleValueToPrettierOption(
-        eslintRuleValue,
-        fallbackPrettierOptions,
-        rules,
-      )
-
-      if (prettierOptionValue !== RULE_DISABLED) {
-        options[key] = prettierOptionValue
-      }
-    }
+    options[key] = ruleValueToPrettierOption(
+      eslintRuleValue,
+      fallbackPrettierOptions,
+      rules,
+    )
   }
 
   return options
@@ -335,6 +329,7 @@ function makePrettierOption(
 ) {
   if (
     prettierRuleValue !== RULE_NOT_CONFIGURED &&
+    prettierRuleValue !== RULE_DISABLED &&
     typeof prettierRuleValue !== 'undefined'
   ) {
     return prettierRuleValue


Test patch:
diff --git a/src/index.test.js b/src/index.test.js
index 13f0dec..5b09585 100644
--- a/src/index.test.js
+++ b/src/index.test.js
@@ -162,6 +162,14 @@ const tests = [
     },
     output: '.stop {\n  color: red;\n}',
   },
+  {
+    title: 'JSON example',
+    input: {
+      text: '{  "foo": "bar"}',
+      filePath: path.resolve('./test.json'),
+    },
+    output: '{ "foo": "bar" }',
+  },
 ]
 
 beforeEach(() => {
@@ -305,7 +313,9 @@ test('logs error if it cannot read the file from the filePath', () => {
   fsMock.readFileSync = jest.fn(() => {
     throw new Error('some error')
   })
-  expect(() => format({filePath: '/some-path.js'})).toThrowError(/some error/)
+  expect(() => format({filePath: '/some-path.js'})).toThrowError(
+    /some error/,
+  )
   expect(logger.error).toHaveBeenCalledTimes(1)
   fsMock.readFileSync = originalMock
 })
diff --git a/src/utils.test.js b/src/utils.test.js
index 0fb9176..31c086e 100644
--- a/src/utils.test.js
+++ b/src/utils.test.js
@@ -12,7 +12,11 @@ const defaultEslintConfigTests = [
       },
       rules: {
         indent: [2, 2, {SwitchCase: 1}],
-        quotes: [2, 'single', {avoidEscape: true, allowTemplateLiterals: true}],
+        quotes: [
+          2,
+          'single',
+          {avoidEscape: true, allowTemplateLiterals: true},
+        ],
       },
       options: {
         printWidth: 120,
@@ -53,7 +57,11 @@ const defaultEslintConfigTests = [
       rules: {
         'max-len': [2, 120, 2],
         indent: [2, 2, {SwitchCase: 1}],
-        quotes: [2, 'single', {avoidEscape: true, allowTemplateLiterals: true}],
+        quotes: [
+          2,
+          'single',
+          {avoidEscape: true, allowTemplateLiterals: true},
+        ],
       },
       options: {
         parser: 'babylon',
@@ -120,8 +128,14 @@ const getPrettierOptionsFromESLintRulesTests = [
     options: {bracketSpacing: false},
   },
   {rules: {'max-len': 2}, options: {printWidth: 80}},
-  {rules: {'comma-dangle': [2, 'never']}, options: {trailingComma: 'none'}},
-  {rules: {'comma-dangle': [2, 'always']}, options: {trailingComma: 'es5'}},
+  {
+    rules: {'comma-dangle': [2, 'never']},
+    options: {trailingComma: 'none'},
+  },
+  {
+    rules: {'comma-dangle': [2, 'always']},
+    options: {trailingComma: 'es5'},
+  },
   {
     rules: {
       'comma-dangle': [
@@ -167,7 +181,10 @@ const getPrettierOptionsFromESLintRulesTests = [
     },
     options: {trailingComma: 'none'},
   },
-  {rules: {'max-len': ['error', {code: 120}]}, options: {printWidth: 120}},
+  {
+    rules: {'max-len': ['error', {code: 120}]},
+    options: {printWidth: 120},
+  },
   {rules: {quotes: [2, 'double']}, options: {singleQuote: false}},
   {rules: {quotes: [2, 'backtick']}, options: {singleQuote: false}},
   {
@@ -232,6 +249,22 @@ test('if prettierOptions are provided, those are preferred', () => {
   expect(prettier).toMatchObject({singleQuote: false})
 })
 
+// eslint-disable-next-line max-len
+test(`if fallbacks are provided, those are preferred over disabled eslint rules`, () => {
+  const {prettier} = getOptionsForFormatting(
+    {
+      rules: {
+        quotes: [0],
+      },
+    },
+    {},
+    {
+      singleQuote: true,
+    },
+  )
+  expect(prettier).toMatchObject({singleQuote: true})
+})
+
 test('if fallbacks are provided, those are used if not found in eslint', () => {
   const {prettier} = getOptionsForFormatting({rules: {}}, undefined, {
     singleQuote: false,