Skip to content

Commit a45cef3

Browse files
zimmeKent C. Dodds
authored andcommitted
feat(json): Let prettier handle json files (#95)
* feat(json): Let prettier handle json files * docs(contributers): Add zimme to contributers * style(eslint): Increse complexity limit to 6 in src/index.js * test(json): Add basic test for json support * chore(dependencies): Upgrade prettier
1 parent da962ab commit a45cef3

File tree

6 files changed

+50
-23
lines changed

6 files changed

+50
-23
lines changed

.all-contributorsrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,13 @@
202202
"bug",
203203
"test"
204204
]
205+
},
206+
{
207+
"login": "zimme",
208+
"name": "Simon Fridlund",
209+
"avatar_url": "https://avatars0.githubusercontent.com/u/1215414?v=3",
210+
"profile": "https://github.com/zimme",
211+
"contributions": []
205212
}
206213
]
207214
}

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ this only runs `prettier` since `eslint` cannot process those.
1212
[![downloads][downloads-badge]][npm-stat]
1313
[![MIT License][license-badge]][LICENSE]
1414

15-
[![All Contributors](https://img.shields.io/badge/all_contributors-20-orange.svg?style=flat-square)](#contributors)
15+
[![All Contributors](https://img.shields.io/badge/all_contributors-21-orange.svg?style=flat-square)](#contributors)
1616
[![PRs Welcome][prs-badge]][prs]
1717
[![Donate][donate-badge]][donate]
1818
[![Code of Conduct][coc-badge]][coc]
@@ -234,10 +234,10 @@ None that I'm aware of. Feel free to file a PR if you know of any other solution
234234
Thanks goes to these people ([emoji key][emojis]):
235235

236236
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
237-
| [<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) 💡 |
237+
| [<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") |
238238
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
239-
| [<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) |
240-
| [<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) |
239+
| [<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") |
240+
| [<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 /> |
241241
<!-- ALL-CONTRIBUTORS-LIST:END -->
242242

243243
This project follows the [all-contributors][all-contributors] specification. Contributions of any kind welcome!

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"indent-string": "^3.1.0",
2424
"lodash.merge": "^4.6.0",
2525
"loglevel-colored-level-prefix": "^1.0.0",
26-
"prettier": "^1.4.2",
26+
"prettier": "^1.5.0",
2727
"pretty-format": "^20.0.3",
2828
"require-relative": "^0.8.7"
2929
},

src/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint no-console:0, global-require:0, import/no-dynamic-require:0 */
2+
/* eslint complexity: [1, 6] */
23
import fs from 'fs'
34
import path from 'path'
45
import requireRelative from 'require-relative'
@@ -78,15 +79,22 @@ function format(options) {
7879
)
7980

8081
const isCss = /\.(css|less|scss)$/.test(filePath)
82+
const isJson = /\.json$/.test(filePath)
83+
8184
if (isCss) {
8285
formattingOptions.prettier.parser = 'postcss'
86+
} else if (isJson) {
87+
formattingOptions.prettier.parser = 'json'
8388
}
89+
8490
const prettify = createPrettify(formattingOptions.prettier, prettierPath)
85-
const eslintFix = createEslintFix(formattingOptions.eslint, eslintPath)
8691

87-
if (isCss) {
92+
if (isCss || isJson) {
8893
return prettify(text, filePath)
8994
}
95+
96+
const eslintFix = createEslintFix(formattingOptions.eslint, eslintPath)
97+
9098
if (prettierLast) {
9199
return prettify(eslintFix(text, filePath))
92100
}

src/index.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,14 @@ const tests = [
162162
},
163163
output: '.stop {\n color: red;\n}',
164164
},
165+
{
166+
title: 'JSON example',
167+
input: {
168+
text: '{ "foo": "bar"}',
169+
filePath: path.resolve('./test.json'),
170+
},
171+
output: '{ "foo": "bar" }',
172+
},
165173
]
166174

167175
beforeEach(() => {

yarn.lock

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -948,11 +948,11 @@ babel-types@^6.18.0, babel-types@^6.19.0, babel-types@^6.22.0, babel-types@^6.23
948948
lodash "^4.2.0"
949949
to-fast-properties "^1.0.1"
950950

951-
babylon@^6.11.0, babylon@^6.13.0, babylon@^6.15.0:
951+
babylon@^6.11.0:
952952
version "6.15.0"
953953
resolved "https://registry.yarnpkg.com/babylon/-/babylon-6.15.0.tgz#ba65cfa1a80e1759b0e89fb562e27dccae70348e"
954954

955-
babylon@^6.17.0:
955+
babylon@^6.13.0, babylon@^6.15.0, babylon@^6.17.0:
956956
version "6.17.2"
957957
resolved "https://registry.yarnpkg.com/babylon/-/babylon-6.17.2.tgz#201d25ef5f892c41bae49488b08db0dd476e9f5c"
958958

@@ -1588,23 +1588,23 @@ dateformat@^1.0.11:
15881588
get-stdin "^4.0.1"
15891589
meow "^3.3.0"
15901590

1591-
debug@2, debug@^2.1.1, debug@^2.2.0:
1592-
version "2.6.1"
1593-
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.1.tgz#79855090ba2c4e3115cc7d8769491d58f0491351"
1591+
debug@2, debug@^2.6.3:
1592+
version "2.6.8"
1593+
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.8.tgz#e731531ca2ede27d188222427da17821d68ff4fc"
15941594
dependencies:
1595-
ms "0.7.2"
1595+
ms "2.0.0"
15961596

15971597
[email protected], debug@~2.2.0:
15981598
version "2.2.0"
15991599
resolved "https://registry.yarnpkg.com/debug/-/debug-2.2.0.tgz#f87057e995b1a1f6ae6a4960664137bc56f039da"
16001600
dependencies:
16011601
ms "0.7.1"
16021602

1603-
debug@^2.6.3:
1604-
version "2.6.8"
1605-
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.8.tgz#e731531ca2ede27d188222427da17821d68ff4fc"
1603+
debug@^2.1.1, debug@^2.2.0:
1604+
version "2.6.1"
1605+
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.1.tgz#79855090ba2c4e3115cc7d8769491d58f0491351"
16061606
dependencies:
1607-
ms "2.0.0"
1607+
ms "0.7.2"
16081608

16091609
decamelize@^1.0.0, decamelize@^1.1.1, decamelize@^1.1.2:
16101610
version "1.2.0"
@@ -2995,14 +2995,14 @@ istanbul-api@^1.1.1:
29952995
mkdirp "^0.5.1"
29962996
once "^1.4.0"
29972997

2998-
istanbul-lib-coverage@^1.0.0, istanbul-lib-coverage@^1.0.0-alpha.0, istanbul-lib-coverage@^1.0.1:
2999-
version "1.0.1"
3000-
resolved "https://registry.yarnpkg.com/istanbul-lib-coverage/-/istanbul-lib-coverage-1.0.1.tgz#f263efb519c051c5f1f3343034fc40e7b43ff212"
3001-
3002-
istanbul-lib-coverage@^1.1.1:
2998+
istanbul-lib-coverage@^1.0.0, istanbul-lib-coverage@^1.0.0-alpha.0, istanbul-lib-coverage@^1.1.1:
30032999
version "1.1.1"
30043000
resolved "https://registry.yarnpkg.com/istanbul-lib-coverage/-/istanbul-lib-coverage-1.1.1.tgz#73bfb998885299415c93d38a3e9adf784a77a9da"
30053001

3002+
istanbul-lib-coverage@^1.0.1:
3003+
version "1.0.1"
3004+
resolved "https://registry.yarnpkg.com/istanbul-lib-coverage/-/istanbul-lib-coverage-1.0.1.tgz#f263efb519c051c5f1f3343034fc40e7b43ff212"
3005+
30063006
istanbul-lib-hook@^1.0.7:
30073007
version "1.0.7"
30083008
resolved "https://registry.yarnpkg.com/istanbul-lib-hook/-/istanbul-lib-hook-1.0.7.tgz#dd6607f03076578fe7d6f2a630cf143b49bacddc"
@@ -4346,10 +4346,14 @@ prettier-eslint@^6.0.0:
43464346
pretty-format "^19.0.0"
43474347
require-relative "^0.8.7"
43484348

4349-
prettier@^1.3.0, prettier@^1.4.2:
4349+
prettier@^1.3.0:
43504350
version "1.4.2"
43514351
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.4.2.tgz#bcdd95ed1eca434ac7f98ca26ea4d25a2af6a2ac"
43524352

4353+
prettier@^1.5.0:
4354+
version "1.5.0"
4355+
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.5.0.tgz#f3476164f9a532a218a1337b1032638275d82614"
4356+
43534357
pretty-format@^19.0.0:
43544358
version "19.0.0"
43554359
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-19.0.0.tgz#56530d32acb98a3fa4851c4e2b9d37b420684c84"

0 commit comments

Comments
 (0)