jsfmt.spec.js.snap 154 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720
  1. // Jest Snapshot v1, https://goo.gl/fbAQLP
  2. exports[`real-world-case.md 1`] = `
  3. # Prettier
  4. [![Gitter](https://badges.gitter.im/gitterHQ/gitter.svg)](https://gitter.im/jlongster/prettier)
  5. [![Build Status](https://travis-ci.org/prettier/prettier.svg?branch=master)](https://travis-ci.org/prettier/prettier)
  6. [![Codecov](https://img.shields.io/codecov/c/github/prettier/prettier.svg)](https://codecov.io/gh/prettier/prettier)
  7. [![NPM version](https://img.shields.io/npm/v/prettier.svg)](https://www.npmjs.com/package/prettier)
  8. [![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](#badge)
  9. Prettier is an opinionated code formatter with support for:
  10. * JavaScript, including [ES2017](https://github.com/tc39/proposals/blob/master/finished-proposals.md)
  11. * [JSX](https://facebook.github.io/jsx/)
  12. * [Flow](https://flow.org/)
  13. * [TypeScript](https://www.typescriptlang.org/)
  14. * CSS, [Less](http://lesscss.org/), and [SCSS](http://sass-lang.com)
  15. * [JSON](http://json.org/)
  16. * [GraphQL](http://graphql.org/)
  17. It removes all original styling[\\*](#styling-footnote) and ensures that all outputted code
  18. conforms to a consistent style. (See this [blog post](http://jlongster.com/A-Prettier-Formatter))
  19. <details>
  20. <summary><strong>Table of Contents</strong></summary>
  21. <!-- Do not edit TOC, regenerate with \`yarn toc\` -->
  22. <!-- toc -->
  23. * [What does Prettier do?](#what-does-prettier-do)
  24. * [Why Prettier?](#why-prettier)
  25. + [Building and enforcing a style guide](#building-and-enforcing-a-style-guide)
  26. + [Helping Newcomers](#helping-newcomers)
  27. + [Writing code](#writing-code)
  28. + [Easy to adopt](#easy-to-adopt)
  29. + [Clean up an existing codebase](#clean-up-an-existing-codebase)
  30. + [Ride the hype train](#ride-the-hype-train)
  31. * [How does it compare to ESLint (or TSLint, stylelint...)?](#how-does-it-compare-to-eslint-or-tslint-stylelint)
  32. * [Usage](#usage)
  33. + [CLI](#cli)
  34. + [ESLint](#eslint)
  35. + [Pre-commit Hook](#pre-commit-hook)
  36. + [API](#api)
  37. + [Excluding code from formatting](#excluding-code-from-formatting)
  38. * [Options](#options)
  39. + [Print Width](#print-width)
  40. + [Tab Width](#tab-width)
  41. + [Tabs](#tabs)
  42. + [Semicolons](#semicolons)
  43. + [Quotes](#quotes)
  44. + [Trailing Commas](#trailing-commas)
  45. + [Bracket Spacing](#bracket-spacing)
  46. + [JSX Brackets](#jsx-brackets)
  47. + [Range](#range)
  48. + [Parser](#parser)
  49. + [Filepath](#filepath)
  50. * [Configuration File](#configuration-file)
  51. + [Basic Configuration](#basic-configuration)
  52. + [Configuration Overrides](#configuration-overrides)
  53. + [Configuration Schema](#configuration-schema)
  54. * [Editor Integration](#editor-integration)
  55. + [Atom](#atom)
  56. + [Emacs](#emacs)
  57. + [Vim](#vim)
  58. + [Visual Studio Code](#visual-studio-code)
  59. + [Visual Studio](#visual-studio)
  60. + [Sublime Text](#sublime-text)
  61. + [JetBrains WebStorm, PHPStorm, PyCharm...](#jetbrains-webstorm-phpstorm-pycharm)
  62. * [Language Support](#language-support)
  63. * [Related Projects](#related-projects)
  64. * [Technical Details](#technical-details)
  65. * [Badge](#badge)
  66. * [Contributing](#contributing)
  67. <!-- tocstop -->
  68. </details>
  69. --------------------------------------------------------------------------------
  70. ## What does Prettier do?
  71. Prettier takes your code and reprints it from scratch by taking the line length into account.
  72. For example, take the following code:
  73. \`\`\`js
  74. foo(arg1, arg2, arg3, arg4);
  75. \`\`\`
  76. It fits in a single line so it's going to stay as is. However, we've all run into this situation:
  77. <!-- prettier-ignore -->
  78. \`\`\`js
  79. foo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne());
  80. \`\`\`
  81. Suddenly our previous format for calling function breaks down because this is too long. Prettier is going to do the painstaking work of reprinting it like that for you:
  82. \`\`\`js
  83. foo(
  84. reallyLongArg(),
  85. omgSoManyParameters(),
  86. IShouldRefactorThis(),
  87. isThereSeriouslyAnotherOne()
  88. );
  89. \`\`\`
  90. Prettier enforces a consistent code **style** (i.e. code formatting that won't affect the AST) across your entire codebase because it disregards the original styling[\\*](#styling-footnote) by parsing it away and re-printing the parsed AST with its own rules that take the maximum line length
  91. into account, wrapping code when necessary.
  92. <a href="#styling-footnote" name="styling-footnote">\\*</a>_Well actually, some
  93. original styling is preserved when practical—see [empty lines] and [multi-line
  94. objects]._
  95. [empty lines]:Rationale.md#empty-lines
  96. [multi-line objects]:Rationale.md#multi-line-objects
  97. If you want to learn more, these two conference talks are great introductions:
  98. <a href="https://www.youtube.com/watch?v=hkfBvpEfWdA"><img width="298" src="https://cloud.githubusercontent.com/assets/197597/24886367/dda8a6f0-1e08-11e7-865b-22492450f10f.png"></a> <a href="https://www.youtube.com/watch?v=0Q4kUNx85_4"><img width="298" src="https://cloud.githubusercontent.com/assets/197597/24886368/ddacd6f8-1e08-11e7-806a-9febd23cbf47.png"></a>
  99. ## Why Prettier?
  100. ### Building and enforcing a style guide
  101. By far the biggest reason for adopting Prettier is to stop all the on-going debates over styles. It is generally accepted that having a common style guide is valuable for a project and team but getting there is a very painful and unrewarding process. People get very emotional around particular ways of writing code and nobody likes spending time writing and receiving nits.
  102. - “We want to free mental threads and end discussions around style. While sometimes fruitful, these discussions are for the most part wasteful.”
  103. - “Literally had an engineer go through a huge effort of cleaning up all of our code because we were debating ternary style for the longest time and were inconsistent about it. It was dumb, but it was a weird on-going "great debate" that wasted lots of little back and forth bits. It's far easier for us all to agree now: just run Prettier, and go with that style.”
  104. - “Getting tired telling people how to style their product code.”
  105. - “Our top reason was to stop wasting our time debating style nits.”
  106. - “Having a githook set up has reduced the amount of style issues in PRs that result in broken builds due to ESLint rules or things I have to nit-pick or clean up later.”
  107. - “I don't want anybody to nitpick any other person ever again.”
  108. - “It reminds me of how Steve Jobs used to wear the same clothes every day because he has a million decisions to make and he didn't want to be bothered to make trivial ones like picking out clothes. I think Prettier is like that.”
  109. ### Helping Newcomers
  110. Prettier is usually introduced by people with experience in the current codebase and JavaScript but the people that disproportionally benefit from it are newcomers to the codebase. One may think that it's only useful for people with very limited programming experience, but we've seen it quicken the ramp up time from experienced engineers joining the company, as they likely used a different coding style before, and developers coming from a different programming language.
  111. - “My motivations for using Prettier are: appearing that I know how to write JavaScript well.”
  112. - “I always put spaces in the wrong place, now I don't have to worry about it anymore.”
  113. - “When you're a beginner you're making a lot of mistakes caused by the syntax. Thanks to Prettier, you can reduce these mistakes and save a lot of time to focus on what really matters.”
  114. - “As a teacher, I will also tell to my students to install Prettier to help them to learn the JS syntax and have readable files.”
  115. ### Writing code
  116. What usually happens once people are using Prettier is that they realize that they actually spend a lot of time and mental energy formatting their code. With Prettier editor integration, you can just press that magic key binding and poof, the code is formatted. This is an eye opening experience if anything else.
  117. - “I want to write code. Not spend cycles on formatting.”
  118. - “It removed 5% that sucks in our daily life - aka formatting”
  119. - “We're in 2017 and it's still painful to break a call into multiple lines when you happen to add an argument that makes it go over the 80 columns limit :(“
  120. ### Easy to adopt
  121. We've worked very hard to use the least controversial coding styles, went through many rounds of fixing all the edge cases and polished the getting started experience. When you're ready to push Prettier into your codebase, not only should it be painless for you to do it technically but the newly formatted codebase should not generate major controversy and be accepted painlessly by your co-workers.
  122. - “It's low overhead. We were able to throw Prettier at very different kinds of repos without much work.”
  123. - “It's been mostly bug free. Had there been major styling issues during the course of implementation we would have been wary about throwing this at our JS codebase. I'm happy to say that's not the case.”
  124. - “Everyone runs it as part of their pre commit scripts, a couple of us use the editor on save extensions as well.”
  125. - “It's fast, against one of our larger JS codebases we were able to run Prettier in under 13 seconds.”
  126. - “The biggest benefit for Prettier for us was being able to format the entire code base at once.”
  127. ### Clean up an existing codebase
  128. Since coming up with a coding style and enforcing it is a big undertaking, it often slips through the cracks and you are left working on inconsistent codebases. Running Prettier in this case is a quick win, the codebase is now uniform and easier to read without spending hardly any time.
  129. - “Take a look at the code :) I just need to restore sanity.”
  130. - “We inherited a ~2000 module ES6 code base, developed by 20 different developers over 18 months, in a global team. Felt like such a win without much research.”
  131. ### Ride the hype train
  132. Purely technical aspects of the projects aren't the only thing people look into when choosing to adopt Prettier. Who built and uses it and how quickly it spreads through the community has a non-trivial impact.
  133. - “The amazing thing, for me, is: 1) Announced 2 months ago. 2) Already adopted by, it seems, every major JS project. 3) 7000 stars, 100,000 npm downloads/mo”
  134. - “Was built by the same people as React & React Native.”
  135. - “I like to be part of the hot new things.”
  136. - “Because soon enough people are gonna ask for it.”
  137. A few of the [many projects](https://www.npmjs.com/browse/depended/prettier) using Prettier:
  138. <table>
  139. <tr>
  140. <td><p align="center"><a href="https://facebook.github.io/react/"><img src="website/static/images/react-200x100.png" alt="React" width="200" height="100"><br>React</a></p></td>
  141. <td><p align="center"><a href="https://facebook.github.io/jest/"><img src="website/static/images/jest-200x100.png" alt="Jest" width="200" height="100"><br>Jest</a></p></td>
  142. <td><p align="center"><a href="https://yarnpkg.com"><img src="website/static/images/yarn-200x100.png" alt="Yarn" width="200" height="100"><br>Yarn</a></p></td>
  143. </tr>
  144. <tr>
  145. <td><p align="center"><a href="https://babeljs.io/"><img src="website/static/images/babel-200x100.png" alt="Babel" width="200" height="100"><br>Babel</a></p></td>
  146. <td><p align="center"><a href="https://zeit.co/"><img src="website/static/images/zeit-200x100.png" alt="Zeit" width="200" height="100"><br>Zeit</a></p></td>
  147. <td><p align="center"><a href="https://webpack.js.org/api/cli/"><img src="website/static/images/webpack-200x100.png" alt="Webpack-cli" width="200" height="100"><br>Webpack-cli</a></p></td>
  148. </tr>
  149. </table>
  150. ## How does it compare to ESLint (or TSLint, stylelint...)?
  151. Linters have two categories of rules:
  152. **Formatting rules**: eg: [max-len](http://eslint.org/docs/rules/max-len), [no-mixed-spaces-and-tabs](http://eslint.org/docs/rules/no-mixed-spaces-and-tabs), [keyword-spacing](http://eslint.org/docs/rules/keyword-spacing), [comma-style](http://eslint.org/docs/rules/comma-style)...
  153. Prettier alleviates the need for this whole category of rules! Prettier is going to reprint the entire program from scratch in a consistent way, so it's not possible for the programmer to make a mistake there anymore :)
  154. **Code-quality rules**: eg [no-unused-vars](http://eslint.org/docs/rules/no-unused-vars), [no-extra-bind](http://eslint.org/docs/rules/no-extra-bind), [no-implicit-globals](http://eslint.org/docs/rules/no-implicit-globals), [prefer-promise-reject-errors](http://eslint.org/docs/rules/prefer-promise-reject-errors)...
  155. Prettier does nothing to help with those kind of rules. They are also the most important ones provided by linters as they are likely to catch real bugs with your code!
  156. ## Usage
  157. Install:
  158. \`\`\`
  159. yarn add prettier --dev --exact
  160. \`\`\`
  161. You can install it globally if you like:
  162. \`\`\`
  163. yarn global add prettier
  164. \`\`\`
  165. *We're using \`yarn\` but you can use \`npm\` if you like:*
  166. \`\`\`
  167. npm install --save-dev --save-exact prettier
  168. # or globally
  169. npm install --global prettier
  170. \`\`\`
  171. > We recommend pinning an exact version of prettier in your \`package.json\`
  172. > as we introduce stylistic changes in patch releases.
  173. ### CLI
  174. Run Prettier through the CLI with this script. Run it without any
  175. arguments to see the [options](#options).
  176. To format a file in-place, use \`--write\`. You may want to consider
  177. committing your code before doing that, just in case.
  178. \`\`\`bash
  179. prettier [opts] [filename ...]
  180. \`\`\`
  181. In practice, this may look something like:
  182. \`\`\`bash
  183. prettier --single-quote --trailing-comma es5 --write "{app,__{tests,mocks}__}/**/*.js"
  184. \`\`\`
  185. Don't forget the quotes around the globs! The quotes make sure that Prettier
  186. expands the globs rather than your shell, for cross-platform usage.
  187. The [glob syntax from the glob module](https://github.com/isaacs/node-glob/blob/master/README.md#glob-primer)
  188. is used.
  189. #### \`--debug-check\`
  190. If you're worried that Prettier will change the correctness of your code, add \`--debug-check\` to the command.
  191. This will cause Prettier to print an error message if it detects that code correctness might have changed.
  192. Note that \`--write\` cannot be used with \`--debug-check\`.
  193. #### \`--find-config-path\` and \`--config\`
  194. If you are repeatedly formatting individual files with \`prettier\`, you will incur a small performance cost
  195. when prettier attempts to look up a [configuration file](#configuration-file). In order to skip this, you may
  196. ask prettier to find the config file once, and re-use it later on.
  197. \`\`\`bash
  198. prettier --find-config-path ./my/file.js
  199. ./my/.prettierrc
  200. \`\`\`
  201. This will provide you with a path to the configuration file, which you can pass to \`--config\`:
  202. \`\`\`bash
  203. prettier --config ./my/.prettierrc --write ./my/file.js
  204. \`\`\`
  205. You can also use \`--config\` if your configuration file lives somewhere where prettier cannot find it,
  206. such as a \`config/\` directory.
  207. If you don't have a configuration file, or want to ignore it if it does exist,
  208. you can pass \`--no-config\` instead.
  209. #### \`--ignore-path\`
  210. Path to a file containing patterns that describe files to ignore. By default, prettier looks for \`./.prettierignore\`.
  211. #### \`--require-pragma\`
  212. Require a special comment, called a pragma, to be present in the file's first docblock comment in order for prettier to format it.
  213. \`\`\`js
  214. /**
  215. * @prettier
  216. */
  217. \`\`\`
  218. Valid pragmas are \`@prettier\` and \`@format\`.
  219. #### \`--list-different\`
  220. Another useful flag is \`--list-different\` (or \`-l\`) which prints the filenames of files that are different from Prettier formatting. If there are differences the script errors out, which is useful in a CI scenario.
  221. \`\`\`bash
  222. prettier --single-quote --list-different "src/**/*.js"
  223. \`\`\`
  224. #### \`--no-config\`
  225. Do not look for a configuration file. The default settings will be used.
  226. #### \`--config-precedence\`
  227. Defines how config file should be evaluated in combination of CLI options.
  228. **cli-override (default)**
  229. CLI options take precedence over config file
  230. **file-override**
  231. Config file take precedence over CLI options
  232. **prefer-file**
  233. If a config file is found will evaluate it and ignore other CLI options. If no config file is found CLI options will evaluate as normal.
  234. This option adds support to editor integrations where users define their default configuration but want to respect project specific configuration.
  235. #### \`--with-node-modules\`
  236. Prettier CLI will ignore files located in \`node_modules\` directory. To opt-out from this behavior use \`--with-node-modules\` flag.
  237. #### \`--write\`
  238. This rewrites all processed files in place. This is comparable to the \`eslint --fix\` workflow.
  239. ### ESLint
  240. If you are using ESLint, integrating Prettier to your workflow is straightforward:
  241. Just add Prettier as an ESLint rule using [eslint-plugin-prettier](https://github.com/prettier/eslint-plugin-prettier).
  242. \`\`\`js
  243. yarn add --dev prettier eslint-plugin-prettier
  244. // .eslintrc.json
  245. {
  246. "plugins": [
  247. "prettier"
  248. ],
  249. "rules": {
  250. "prettier/prettier": "error"
  251. }
  252. }
  253. \`\`\`
  254. We also recommend that you use [eslint-config-prettier](https://github.com/prettier/eslint-config-prettier) to disable all the existing formatting rules. It's a one liner that can be added on-top of any existing ESLint configuration.
  255. \`\`\`
  256. $ yarn add --dev eslint-config-prettier
  257. \`\`\`
  258. .eslintrc.json:
  259. \`\`\`json
  260. {
  261. "extends": [
  262. "prettier"
  263. ]
  264. }
  265. \`\`\`
  266. ### Pre-commit Hook
  267. You can use Prettier with a pre-commit tool. This can re-format your files that are marked as "staged" via \`git add\` before you commit.
  268. ##### Option 1. [lint-staged](https://github.com/okonet/lint-staged)
  269. Install it along with [husky](https://github.com/typicode/husky):
  270. \`\`\`bash
  271. yarn add lint-staged husky --dev
  272. \`\`\`
  273. and add this config to your \`package.json\`:
  274. \`\`\`json
  275. {
  276. "scripts": {
  277. "precommit": "lint-staged"
  278. },
  279. "lint-staged": {
  280. "*.{js,json,css}": [
  281. "prettier --write",
  282. "git add"
  283. ]
  284. }
  285. }
  286. \`\`\`
  287. There is a limitation where if you stage specific lines this approach will stage the whole file after regardless. See this [issue](https://github.com/okonet/lint-staged/issues/62) for more info.
  288. See https://github.com/okonet/lint-staged#configuration for more details about how you can configure lint-staged.
  289. ##### Option 2. [pre-commit](https://github.com/pre-commit/pre-commit)
  290. Copy the following config into your \`.pre-commit-config.yaml\` file:
  291. \`\`\`yaml
  292. - repo: https://github.com/prettier/prettier
  293. sha: '' # Use the sha or tag you want to point at
  294. hooks:
  295. - id: prettier
  296. \`\`\`
  297. Find more info from [here](https://pre-commit.com).
  298. ##### Option 3. bash script
  299. Alternately you can save this script as \`.git/hooks/pre-commit\` and give it execute permission:
  300. \`\`\`bash
  301. #!/bin/sh
  302. jsfiles=$(git diff --cached --name-only --diff-filter=ACM | grep '\\.jsx\\?$' | tr '\\n' ' ')
  303. [ -z "$jsfiles" ] && exit 0
  304. # Prettify all staged .js files
  305. echo "$jsfiles" | xargs ./node_modules/.bin/prettier --write
  306. # Add back the modified/prettified files to staging
  307. echo "$jsfiles" | xargs git add
  308. exit 0
  309. \`\`\`
  310. ### API
  311. \`\`\`js
  312. const prettier = require("prettier");
  313. \`\`\`
  314. #### \`prettier.format(source [, options])\`
  315. \`format\` is used to format text using Prettier. [Options](#options) may be provided to override the defaults.
  316. \`\`\`js
  317. prettier.format("foo ( );", { semi: false });
  318. // -> "foo()"
  319. \`\`\`
  320. #### \`prettier.check(source [, options])\`
  321. \`check\` checks to see if the file has been formatted with Prettier given those options and returns a \`Boolean\`.
  322. This is similar to the \`--list-different\` parameter in the CLI and is useful for running Prettier in CI scenarios.
  323. #### \`prettier.formatWithCursor(source [, options])\`
  324. \`formatWithCursor\` both formats the code, and translates a cursor position from unformatted code to formatted code.
  325. This is useful for editor integrations, to prevent the cursor from moving when code is formatted.
  326. The \`cursorOffset\` option should be provided, to specify where the cursor is. This option cannot be used with \`rangeStart\` and \`rangeEnd\`.
  327. \`\`\`js
  328. prettier.formatWithCursor(" 1", { cursorOffset: 2 });
  329. // -> { formatted: '1;\\n', cursorOffset: 1 }
  330. \`\`\`
  331. #### \`prettier.resolveConfig([filePath [, options]])\`
  332. \`resolveConfig\` can be used to resolve configuration for a given source file.
  333. The function optionally accepts an input file path as an argument, which defaults to the current working directory.
  334. A promise is returned which will resolve to:
  335. * An options object, providing a [config file](#configuration-file) was found.
  336. * \`null\`, if no file was found.
  337. The promise will be rejected if there was an error parsing the configuration file.
  338. If \`options.useCache\` is \`false\`, all caching will be bypassed.
  339. \`\`\`js
  340. const text = fs.readFileSync(filePath, "utf8");
  341. prettier.resolveConfig(filePath).then(options => {
  342. const formatted = prettier.format(text, options);
  343. })
  344. \`\`\`
  345. Use \`prettier.resolveConfig.sync([filePath [, options]])\` if you'd like to use sync version.
  346. #### \`prettier.clearConfigCache()\`
  347. As you repeatedly call \`resolveConfig\`, the file system structure will be cached for performance.
  348. This function will clear the cache. Generally this is only needed for editor integrations that
  349. know that the file system has changed since the last format took place.
  350. #### Custom Parser API
  351. If you need to make modifications to the AST (such as codemods), or you want to provide an alternate parser, you can do so by setting the \`parser\` option to a function. The function signature of the parser function is:
  352. \`\`\`js
  353. (text: string, parsers: object, options: object) => AST;
  354. \`\`\`
  355. Prettier's built-in parsers are exposed as properties on the \`parsers\` argument.
  356. \`\`\`js
  357. prettier.format("lodash ( )", {
  358. parser(text, { babylon }) {
  359. const ast = babylon(text);
  360. ast.program.body[0].expression.callee.name = "_";
  361. return ast;
  362. }
  363. });
  364. // -> "_();\\n"
  365. \`\`\`
  366. The \`--parser\` CLI option may be a path to a node.js module exporting a parse function.
  367. ### Excluding code from formatting
  368. A JavaScript comment of \`// prettier-ignore\` will exclude the next node in the abstract syntax tree from formatting.
  369. For example:
  370. \`\`\`js
  371. matrix(
  372. 1, 0, 0,
  373. 0, 1, 0,
  374. 0, 0, 1
  375. )
  376. // prettier-ignore
  377. matrix(
  378. 1, 0, 0,
  379. 0, 1, 0,
  380. 0, 0, 1
  381. )
  382. \`\`\`
  383. will be transformed to:
  384. \`\`\`js
  385. matrix(1, 0, 0, 0, 1, 0, 0, 0, 1);
  386. // prettier-ignore
  387. matrix(
  388. 1, 0, 0,
  389. 0, 1, 0,
  390. 0, 0, 1
  391. )
  392. \`\`\`
  393. ## Options
  394. Prettier ships with a handful of customizable format options, usable in both the CLI and API.
  395. ### Print Width
  396. Specify the line length that the printer will wrap on.
  397. > **For readability we recommend against using more than 80 characters:**
  398. >
  399. >In code styleguides, maximum line length rules are often set to 100 or 120. However, when humans write code, they don't strive to reach the maximum number of columns on every line. Developers often use whitespace to break up long lines for readability. In practice, the average line length often ends up well below the maximum.
  400. >
  401. > Prettier, on the other hand, strives to fit the most code into every line. With the print width set to 120, prettier may produce overly compact, or otherwise undesirable code.
  402. Default | CLI Override | API Override
  403. --------|--------------|-------------
  404. \`80\` | \`--print-width <int>\` | \`printWidth: <int>\`
  405. ### Tab Width
  406. Specify the number of spaces per indentation-level.
  407. Default | CLI Override | API Override
  408. --------|--------------|-------------
  409. \`2\` | \`--tab-width <int>\` | \`tabWidth: <int>\`
  410. ### Tabs
  411. Indent lines with tabs instead of spaces
  412. Default | CLI Override | API Override
  413. --------|--------------|-------------
  414. \`false\` | \`--use-tabs\` | \`useTabs: <bool>\`
  415. ### Semicolons
  416. Print semicolons at the ends of statements.
  417. Valid options:
  418. * \`true\` - Add a semicolon at the end of every statement.
  419. * \`false\` - Only add semicolons at the beginning of lines that may introduce ASI failures.
  420. Default | CLI Override | API Override
  421. --------|--------------|-------------
  422. \`true\` | \`--no-semi\` | \`semi: <bool>\`
  423. ### Quotes
  424. Use single quotes instead of double quotes.
  425. Notes:
  426. * Quotes in JSX will always be double and ignore this setting.
  427. * If the number of quotes outweighs the other quote, the quote which is less used will be used to format the string - Example: \`"I'm double quoted"\` results in \`"I'm double quoted"\` and \`"This \\"example\\" is single quoted"\` results in \`'This "example" is single quoted'\`.
  428. Default | CLI Override | API Override
  429. --------|--------------|-------------
  430. \`false\` | \`--single-quote\` | \`singleQuote: <bool>\`
  431. ### Trailing Commas
  432. Print trailing commas wherever possible when multi-line. (A single-line array,
  433. for example, never gets trailing commas.)
  434. Valid options:
  435. * \`"none"\` - No trailing commas.
  436. * \`"es5"\` - Trailing commas where valid in ES5 (objects, arrays, etc.)
  437. * \`"all"\` - Trailing commas wherever possible (including function arguments). This requires node 8 or a [transform](https://babeljs.io/docs/plugins/syntax-trailing-function-commas/).
  438. Default | CLI Override | API Override
  439. --------|--------------|-------------
  440. \`"none"\` | <code>--trailing-comma <none&#124;es5&#124;all></code> | <code>trailingComma: "<none&#124;es5&#124;all>"</code>
  441. ### Bracket Spacing
  442. Print spaces between brackets in object literals.
  443. Valid options:
  444. * \`true\` - Example: \`{ foo: bar }\`.
  445. * \`false\` - Example: \`{foo: bar}\`.
  446. Default | CLI Override | API Override
  447. --------|--------------|-------------
  448. \`true\` | \`--no-bracket-spacing\` | \`bracketSpacing: <bool>\`
  449. ### JSX Brackets
  450. Put the \`>\` of a multi-line JSX element at the end of the last line instead of being alone on the next line (does not apply to self closing elements).
  451. Default | CLI Override | API Override
  452. --------|--------------|-------------
  453. \`false\` | \`--jsx-bracket-same-line\` | \`jsxBracketSameLine: <bool>\`
  454. ### Range
  455. Format only a segment of a file.
  456. These two options can be used to format code starting and ending at a given character offset (inclusive and exclusive, respectively). The range will extend:
  457. * Backwards to the start of the first line containing the selected statement.
  458. * Forwards to the end of the selected statement.
  459. These options cannot be used with \`cursorOffset\`.
  460. Default | CLI Override | API Override
  461. --------|--------------|-------------
  462. \`0\` | \`--range-start <int>\`| \`rangeStart: <int>\`
  463. \`Infinity\` | \`--range-end <int>\` | \`rangeEnd: <int>\`
  464. ### Parser
  465. Specify which parser to use.
  466. Both the \`babylon\` and \`flow\` parsers support the same set of JavaScript features (including Flow). Prettier automatically infers the parser from the input file path, so you shouldn't have to change this setting.
  467. Built-in parsers:
  468. * [\`babylon\`](https://github.com/babel/babylon/)
  469. * [\`flow\`](https://github.com/facebook/flow/tree/master/src/parser)
  470. * [\`typescript\`](https://github.com/eslint/typescript-eslint-parser) _Since v1.4.0_
  471. * [\`postcss\`](https://github.com/postcss/postcss) _Since v1.4.0_
  472. * [\`json\`](https://github.com/babel/babylon/tree/f09eb3200f57ea94d51c2a5b1facf2149fb406bf#babylonparseexpressioncode-options) _Since v1.5.0_
  473. * [\`graphql\`](https://github.com/graphql/graphql-js/tree/master/src/language) _Since v1.5.0_
  474. [Custom parsers](#custom-parser-api) are also supported. _Since v1.5.0_
  475. Default | CLI Override | API Override
  476. --------|--------------|-------------
  477. \`babylon\` | \`--parser <string>\`<br />\`--parser ./my-parser\` | \`parser: "<string>"\`<br />\`parser: require("./my-parser")\`
  478. ### Filepath
  479. Specify the input filepath. This will be used to do parser inference.
  480. For example, the following will use \`postcss\` parser:
  481. \`\`\`bash
  482. cat foo | prettier --stdin-filepath foo.css
  483. \`\`\`
  484. Default | CLI Override | API Override
  485. --------|--------------|-------------
  486. None | \`--stdin-filepath <string>\` | \`filepath: "<string>"\`
  487. ### Require pragma
  488. Prettier can restrict itself to only format files that contain a special comment, called a pragma, at the top of the file. This is very useful
  489. when gradually transitioning large, unformatted codebases to prettier.
  490. For example, a file with the following as its first comment will be formatted when \`--require-pragma\` is supplied:
  491. \`\`\`js
  492. /**
  493. * @prettier
  494. */
  495. \`\`\`
  496. or
  497. \`\`\`js
  498. /**
  499. * @format
  500. */
  501. \`\`\`
  502. Default | CLI Override | API Override
  503. --------|--------------|-------------
  504. \`false\` | \`--require-pragma\` | \`requirePragma: <bool>\`
  505. ## Configuration File
  506. Prettier uses [cosmiconfig](https://github.com/davidtheclark/cosmiconfig) for configuration file support.
  507. This means you can configure prettier via:
  508. * A \`.prettierrc\` file, written in YAML or JSON, with optional extensions: \`.yaml/.yml/.json/.js\`.
  509. * A \`prettier.config.js\` file that exports an object.
  510. * A \`"prettier"\` key in your \`package.json\` file.
  511. The configuration file will be resolved starting from the location of the file being formatted,
  512. and searching up the file tree until a config file is (or isn't) found.
  513. The options to the configuration file are the same the [API options](#options).
  514. ### Basic Configuration
  515. JSON:
  516. \`\`\`json
  517. // .prettierrc
  518. {
  519. "printWidth": 100,
  520. "parser": "flow"
  521. }
  522. \`\`\`
  523. YAML:
  524. \`\`\`yaml
  525. # .prettierrc
  526. printWidth: 100
  527. parser: flow
  528. \`\`\`
  529. ### Configuration Overrides
  530. Prettier borrows eslint's [override format](http://eslint.org/docs/user-guide/configuring#example-configuration).
  531. This allows you to apply configuration to specific files.
  532. JSON:
  533. \`\`\`json
  534. {
  535. "semi": false,
  536. "overrides": [{
  537. "files": "*.test.js",
  538. "options": {
  539. "semi": true
  540. }
  541. }]
  542. }
  543. \`\`\`
  544. YAML:
  545. \`\`\`yaml
  546. semi: false
  547. overrides:
  548. - files: "*.test.js"
  549. options:
  550. semi: true
  551. \`\`\`
  552. \`files\` is required for each override, and may be a string or array of strings.
  553. \`excludeFiles\` may be optionally provided to exclude files for a given rule, and may also be a string or array of strings.
  554. To get prettier to format its own \`.prettierrc\` file, you can do:
  555. \`\`\`json
  556. {
  557. "overrides": [{
  558. "files": ".prettierrc",
  559. "options": { "parser": "json" }
  560. }]
  561. }
  562. \`\`\`
  563. For more information on how to use the CLI to locate a file, see the [CLI](#cli) section.
  564. ### Configuration Schema
  565. If you'd like a JSON schema to validate your configuration, one is available here: http://json.schemastore.org/prettierrc.
  566. ## Editor Integration
  567. ### Atom
  568. Atom users can simply install the [prettier-atom](https://github.com/prettier/prettier-atom) package and use
  569. \`Ctrl+Alt+F\` to format a file (or format on save if enabled).
  570. ### Emacs
  571. Emacs users should see [this repository](https://github.com/prettier/prettier-emacs)
  572. for on-demand formatting.
  573. ### Vim
  574. Vim users can simply install either [sbdchd](https://github.com/sbdchd)/[neoformat](https://github.com/sbdchd/neoformat), [w0rp](https://github.com/w0rp)/[ale](https://github.com/w0rp/ale), or [prettier](https://github.com/prettier)/[vim-prettier](https://github.com/prettier/vim-prettier), for more details see [this directory](https://github.com/prettier/prettier/tree/master/editors/vim).
  575. ### Visual Studio Code
  576. Can be installed using the extension sidebar. Search for \`Prettier - JavaScript formatter\`.
  577. Can also be installed using \`ext install prettier-vscode\`.
  578. [Check its repository for configuration and shortcuts](https://github.com/prettier/prettier-vscode)
  579. ### Visual Studio
  580. Install the [JavaScript Prettier extension](https://github.com/madskristensen/JavaScriptPrettier).
  581. ### Sublime Text
  582. Sublime Text support is available through Package Control and
  583. the [JsPrettier](https://packagecontrol.io/packages/JsPrettier) plug-in.
  584. ### JetBrains WebStorm, PHPStorm, PyCharm...
  585. See the [WebStorm
  586. guide](https://github.com/jlongster/prettier/tree/master/editors/webstorm/README.md).
  587. ## Language Support
  588. Prettier attempts to support all JavaScript language features,
  589. including non-standardized ones. By default it uses the
  590. [Babylon](https://github.com/babel/babylon) parser with all language
  591. features enabled, but you can also use the
  592. [Flow](https://github.com/facebook/flow) parser with the
  593. \`parser\` API or \`--parser\` CLI [option](#options).
  594. All of JSX and Flow syntax is supported. In fact, the test suite in
  595. \`tests/flow\` *is* the entire Flow test suite and they all pass.
  596. Prettier also supports [TypeScript](https://www.typescriptlang.org/), CSS, [Less](http://lesscss.org/), [SCSS](http://sass-lang.com), [JSON](http://json.org/), and [GraphQL](http://graphql.org/).
  597. The minimum version of TypeScript supported is 2.1.3 as it introduces the ability to have leading \`|\` for type definitions which prettier outputs.
  598. ## Related Projects
  599. - [\`eslint-plugin-prettier\`](https://github.com/prettier/eslint-plugin-prettier) plugs Prettier into your ESLint workflow
  600. - [\`eslint-config-prettier\`](https://github.com/prettier/eslint-config-prettier) turns off all ESLint rules that are unnecessary or might conflict with Prettier
  601. - [\`prettier-eslint\`](https://github.com/prettier/prettier-eslint)
  602. passes \`prettier\` output to \`eslint --fix\`
  603. - [\`prettier-stylelint\`](https://github.com/hugomrdias/prettier-stylelint)
  604. passes \`prettier\` output to \`stylelint --fix\`
  605. - [\`prettier-standard\`](https://github.com/sheerun/prettier-standard)
  606. uses \`prettier\` and \`prettier-eslint\` to format code with standard rules
  607. - [\`prettier-standard-formatter\`](https://github.com/dtinth/prettier-standard-formatter)
  608. passes \`prettier\` output to \`standard --fix\`
  609. - [\`prettier-miscellaneous\`](https://github.com/arijs/prettier-miscellaneous)
  610. \`prettier\` with a few minor extra options
  611. - [\`neutrino-preset-prettier\`](https://github.com/SpencerCDixon/neutrino-preset-prettier) allows you to use Prettier as a Neutrino preset
  612. - [\`prettier_d\`](https://github.com/josephfrazier/prettier_d.js) runs Prettier as a server to avoid Node.js startup delay. It also supports configuration via \`.prettierrc\`, \`package.json\`, and \`.editorconfig\`.
  613. - [\`Prettier Bookmarklet\`](https://prettier.glitch.me/) provides a bookmarklet and exposes a REST API for Prettier that allows to format CodeMirror editor in your browser
  614. - [\`prettier-github\`](https://github.com/jgierer12/prettier-github) formats code in GitHub comments
  615. - [\`rollup-plugin-prettier\`](https://github.com/mjeanroy/rollup-plugin-prettier) allows you to use Prettier with Rollup
  616. - [\`markdown-magic-prettier\`](https://github.com/camacho/markdown-magic-prettier) allows you to use Prettier to format JS [codeblocks](https://help.github.com/articles/creating-and-highlighting-code-blocks/) in Markdown files via [Markdown Magic](https://github.com/DavidWells/markdown-magic)
  617. - [\`tslint-plugin-prettier\`](https://github.com/ikatyang/tslint-plugin-prettier) runs Prettier as a TSLint rule and reports differences as individual TSLint issues
  618. - [\`tslint-config-prettier\`](https://github.com/alexjoverm/tslint-config-prettier) use TSLint with Prettier without any conflict
  619. ## Technical Details
  620. This printer is a fork of
  621. [recast](https://github.com/benjamn/recast)'s printer with its
  622. algorithm replaced by the one described by Wadler in "[A prettier
  623. printer](http://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf)".
  624. There still may be leftover code from recast that needs to be cleaned
  625. up.
  626. The basic idea is that the printer takes an AST and returns an
  627. intermediate representation of the output, and the printer uses that
  628. to generate a string. The advantage is that the printer can "measure"
  629. the IR and see if the output is going to fit on a line, and break if
  630. not.
  631. This means that most of the logic of printing an AST involves
  632. generating an abstract representation of the output involving certain
  633. commands. For example, \`concat(["(", line, arg, line ")"])\` would
  634. represent a concatenation of opening parens, an argument, and closing
  635. parens. But if that doesn't fit on one line, the printer can break
  636. where \`line\` is specified.
  637. More (rough) details can be found in [commands.md](commands.md).
  638. ## Badge
  639. Show the world you're using *Prettier* → [![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
  640. \`\`\`md
  641. [![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
  642. \`\`\`
  643. ## Contributing
  644. See [CONTRIBUTING.md](CONTRIBUTING.md).
  645. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  646. # Prettier
  647. [![Gitter](https://badges.gitter.im/gitterHQ/gitter.svg)](https://gitter.im/jlongster/prettier)
  648. [![Build Status](https://travis-ci.org/prettier/prettier.svg?branch=master)](https://travis-ci.org/prettier/prettier)
  649. [![Codecov](https://img.shields.io/codecov/c/github/prettier/prettier.svg)](https://codecov.io/gh/prettier/prettier)
  650. [![NPM version](https://img.shields.io/npm/v/prettier.svg)](https://www.npmjs.com/package/prettier)
  651. [![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](#badge)
  652. Prettier is an opinionated code formatter with support for:
  653. - JavaScript, including
  654. [ES2017](https://github.com/tc39/proposals/blob/master/finished-proposals.md)
  655. - [JSX](https://facebook.github.io/jsx/)
  656. - [Flow](https://flow.org/)
  657. - [TypeScript](https://www.typescriptlang.org/)
  658. - CSS, [Less](http://lesscss.org/), and [SCSS](http://sass-lang.com)
  659. - [JSON](http://json.org/)
  660. - [GraphQL](http://graphql.org/)
  661. It removes all original styling[\\*](#styling-footnote) and ensures that all
  662. outputted code conforms to a consistent style. (See this
  663. [blog post](http://jlongster.com/A-Prettier-Formatter))
  664. <details>
  665. <summary><strong>Table of Contents</strong></summary>
  666. <!-- Do not edit TOC, regenerate with \`yarn toc\` -->
  667. <!-- toc -->
  668. - [What does Prettier do?](#what-does-prettier-do)
  669. - [Why Prettier?](#why-prettier)
  670. - [Building and enforcing a style guide](#building-and-enforcing-a-style-guide)
  671. - [Helping Newcomers](#helping-newcomers)
  672. - [Writing code](#writing-code)
  673. - [Easy to adopt](#easy-to-adopt)
  674. - [Clean up an existing codebase](#clean-up-an-existing-codebase)
  675. - [Ride the hype train](#ride-the-hype-train)
  676. - [How does it compare to ESLint (or TSLint, stylelint...)?](#how-does-it-compare-to-eslint-or-tslint-stylelint)
  677. - [Usage](#usage)
  678. - [CLI](#cli)
  679. - [ESLint](#eslint)
  680. - [Pre-commit Hook](#pre-commit-hook)
  681. - [API](#api)
  682. - [Excluding code from formatting](#excluding-code-from-formatting)
  683. - [Options](#options)
  684. - [Print Width](#print-width)
  685. - [Tab Width](#tab-width)
  686. - [Tabs](#tabs)
  687. - [Semicolons](#semicolons)
  688. - [Quotes](#quotes)
  689. - [Trailing Commas](#trailing-commas)
  690. - [Bracket Spacing](#bracket-spacing)
  691. - [JSX Brackets](#jsx-brackets)
  692. - [Range](#range)
  693. - [Parser](#parser)
  694. - [Filepath](#filepath)
  695. - [Configuration File](#configuration-file)
  696. - [Basic Configuration](#basic-configuration)
  697. - [Configuration Overrides](#configuration-overrides)
  698. - [Configuration Schema](#configuration-schema)
  699. - [Editor Integration](#editor-integration)
  700. - [Atom](#atom)
  701. - [Emacs](#emacs)
  702. - [Vim](#vim)
  703. - [Visual Studio Code](#visual-studio-code)
  704. - [Visual Studio](#visual-studio)
  705. - [Sublime Text](#sublime-text)
  706. - [JetBrains WebStorm, PHPStorm, PyCharm...](#jetbrains-webstorm-phpstorm-pycharm)
  707. - [Language Support](#language-support)
  708. - [Related Projects](#related-projects)
  709. - [Technical Details](#technical-details)
  710. - [Badge](#badge)
  711. - [Contributing](#contributing)
  712. <!-- tocstop -->
  713. </details>
  714. ---
  715. ## What does Prettier do?
  716. Prettier takes your code and reprints it from scratch by taking the line length
  717. into account.
  718. For example, take the following code:
  719. \`\`\`js
  720. foo(arg1, arg2, arg3, arg4);
  721. \`\`\`
  722. It fits in a single line so it's going to stay as is. However, we've all run
  723. into this situation:
  724. <!-- prettier-ignore -->
  725. \`\`\`js
  726. foo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne());
  727. \`\`\`
  728. Suddenly our previous format for calling function breaks down because this is
  729. too long. Prettier is going to do the painstaking work of reprinting it like
  730. that for you:
  731. \`\`\`js
  732. foo(
  733. reallyLongArg(),
  734. omgSoManyParameters(),
  735. IShouldRefactorThis(),
  736. isThereSeriouslyAnotherOne()
  737. );
  738. \`\`\`
  739. Prettier enforces a consistent code **style** (i.e. code formatting that won't
  740. affect the AST) across your entire codebase because it disregards the original
  741. styling[\\*](#styling-footnote) by parsing it away and re-printing the parsed AST
  742. with its own rules that take the maximum line length into account, wrapping code
  743. when necessary.
  744. <a href="#styling-footnote" name="styling-footnote">\\*</a>_Well actually, some
  745. original styling is preserved when practical—see [empty lines] and [multi-line
  746. objects]._
  747. [empty lines]: Rationale.md#empty-lines
  748. [multi-line objects]: Rationale.md#multi-line-objects
  749. If you want to learn more, these two conference talks are great introductions:
  750. <a href="https://www.youtube.com/watch?v=hkfBvpEfWdA"><img width="298" src="https://cloud.githubusercontent.com/assets/197597/24886367/dda8a6f0-1e08-11e7-865b-22492450f10f.png"></a>
  751. <a href="https://www.youtube.com/watch?v=0Q4kUNx85_4"><img width="298" src="https://cloud.githubusercontent.com/assets/197597/24886368/ddacd6f8-1e08-11e7-806a-9febd23cbf47.png"></a>
  752. ## Why Prettier?
  753. ### Building and enforcing a style guide
  754. By far the biggest reason for adopting Prettier is to stop all the on-going
  755. debates over styles. It is generally accepted that having a common style guide
  756. is valuable for a project and team but getting there is a very painful and
  757. unrewarding process. People get very emotional around particular ways of writing
  758. code and nobody likes spending time writing and receiving nits.
  759. - “We want to free mental threads and end discussions around style. While
  760. sometimes fruitful, these discussions are for the most part wasteful.”
  761. - “Literally had an engineer go through a huge effort of cleaning up all of our
  762. code because we were debating ternary style for the longest time and were
  763. inconsistent about it. It was dumb, but it was a weird on-going "great debate"
  764. that wasted lots of little back and forth bits. It's far easier for us all to
  765. agree now: just run Prettier, and go with that style.”
  766. - “Getting tired telling people how to style their product code.”
  767. - “Our top reason was to stop wasting our time debating style nits.”
  768. - “Having a githook set up has reduced the amount of style issues in PRs that
  769. result in broken builds due to ESLint rules or things I have to nit-pick or
  770. clean up later.”
  771. - “I don't want anybody to nitpick any other person ever again.”
  772. - “It reminds me of how Steve Jobs used to wear the same clothes every day
  773. because he has a million decisions to make and he didn't want to be bothered
  774. to make trivial ones like picking out clothes. I think Prettier is like that.”
  775. ### Helping Newcomers
  776. Prettier is usually introduced by people with experience in the current codebase
  777. and JavaScript but the people that disproportionally benefit from it are
  778. newcomers to the codebase. One may think that it's only useful for people with
  779. very limited programming experience, but we've seen it quicken the ramp up time
  780. from experienced engineers joining the company, as they likely used a different
  781. coding style before, and developers coming from a different programming
  782. language.
  783. - “My motivations for using Prettier are: appearing that I know how to write
  784. JavaScript well.”
  785. - “I always put spaces in the wrong place, now I don't have to worry about it
  786. anymore.”
  787. - “When you're a beginner you're making a lot of mistakes caused by the syntax.
  788. Thanks to Prettier, you can reduce these mistakes and save a lot of time to
  789. focus on what really matters.”
  790. - “As a teacher, I will also tell to my students to install Prettier to help
  791. them to learn the JS syntax and have readable files.”
  792. ### Writing code
  793. What usually happens once people are using Prettier is that they realize that
  794. they actually spend a lot of time and mental energy formatting their code. With
  795. Prettier editor integration, you can just press that magic key binding and poof,
  796. the code is formatted. This is an eye opening experience if anything else.
  797. - “I want to write code. Not spend cycles on formatting.”
  798. - “It removed 5% that sucks in our daily life - aka formatting”
  799. - “We're in 2017 and it's still painful to break a call into multiple lines when
  800. you happen to add an argument that makes it go over the 80 columns limit :(“
  801. ### Easy to adopt
  802. We've worked very hard to use the least controversial coding styles, went
  803. through many rounds of fixing all the edge cases and polished the getting
  804. started experience. When you're ready to push Prettier into your codebase, not
  805. only should it be painless for you to do it technically but the newly formatted
  806. codebase should not generate major controversy and be accepted painlessly by
  807. your co-workers.
  808. - “It's low overhead. We were able to throw Prettier at very different kinds of
  809. repos without much work.”
  810. - “It's been mostly bug free. Had there been major styling issues during the
  811. course of implementation we would have been wary about throwing this at our JS
  812. codebase. I'm happy to say that's not the case.”
  813. - “Everyone runs it as part of their pre commit scripts, a couple of us use the
  814. editor on save extensions as well.”
  815. - “It's fast, against one of our larger JS codebases we were able to run
  816. Prettier in under 13 seconds.”
  817. - “The biggest benefit for Prettier for us was being able to format the entire
  818. code base at once.”
  819. ### Clean up an existing codebase
  820. Since coming up with a coding style and enforcing it is a big undertaking, it
  821. often slips through the cracks and you are left working on inconsistent
  822. codebases. Running Prettier in this case is a quick win, the codebase is now
  823. uniform and easier to read without spending hardly any time.
  824. - “Take a look at the code :) I just need to restore sanity.”
  825. - “We inherited a ~2000 module ES6 code base, developed by 20 different
  826. developers over 18 months, in a global team. Felt like such a win without much
  827. research.”
  828. ### Ride the hype train
  829. Purely technical aspects of the projects aren't the only thing people look into
  830. when choosing to adopt Prettier. Who built and uses it and how quickly it
  831. spreads through the community has a non-trivial impact.
  832. - “The amazing thing, for me, is: 1) Announced 2 months ago. 2) Already adopted
  833. by, it seems, every major JS project. 3) 7000 stars, 100,000 npm downloads/mo”
  834. - “Was built by the same people as React & React Native.”
  835. - “I like to be part of the hot new things.”
  836. - “Because soon enough people are gonna ask for it.”
  837. A few of the [many projects](https://www.npmjs.com/browse/depended/prettier)
  838. using Prettier:
  839. <table>
  840. <tr>
  841. <td><p align="center"><a href="https://facebook.github.io/react/"><img src="website/static/images/react-200x100.png" alt="React" width="200" height="100"><br>React</a></p></td>
  842. <td><p align="center"><a href="https://facebook.github.io/jest/"><img src="website/static/images/jest-200x100.png" alt="Jest" width="200" height="100"><br>Jest</a></p></td>
  843. <td><p align="center"><a href="https://yarnpkg.com"><img src="website/static/images/yarn-200x100.png" alt="Yarn" width="200" height="100"><br>Yarn</a></p></td>
  844. </tr>
  845. <tr>
  846. <td><p align="center"><a href="https://babeljs.io/"><img src="website/static/images/babel-200x100.png" alt="Babel" width="200" height="100"><br>Babel</a></p></td>
  847. <td><p align="center"><a href="https://zeit.co/"><img src="website/static/images/zeit-200x100.png" alt="Zeit" width="200" height="100"><br>Zeit</a></p></td>
  848. <td><p align="center"><a href="https://webpack.js.org/api/cli/"><img src="website/static/images/webpack-200x100.png" alt="Webpack-cli" width="200" height="100"><br>Webpack-cli</a></p></td>
  849. </tr>
  850. </table>
  851. ## How does it compare to ESLint (or TSLint, stylelint...)?
  852. Linters have two categories of rules:
  853. **Formatting rules**: eg: [max-len](http://eslint.org/docs/rules/max-len),
  854. [no-mixed-spaces-and-tabs](http://eslint.org/docs/rules/no-mixed-spaces-and-tabs),
  855. [keyword-spacing](http://eslint.org/docs/rules/keyword-spacing),
  856. [comma-style](http://eslint.org/docs/rules/comma-style)...
  857. Prettier alleviates the need for this whole category of rules! Prettier is going
  858. to reprint the entire program from scratch in a consistent way, so it's not
  859. possible for the programmer to make a mistake there anymore :)
  860. **Code-quality rules**: eg
  861. [no-unused-vars](http://eslint.org/docs/rules/no-unused-vars),
  862. [no-extra-bind](http://eslint.org/docs/rules/no-extra-bind),
  863. [no-implicit-globals](http://eslint.org/docs/rules/no-implicit-globals),
  864. [prefer-promise-reject-errors](http://eslint.org/docs/rules/prefer-promise-reject-errors)...
  865. Prettier does nothing to help with those kind of rules. They are also the most
  866. important ones provided by linters as they are likely to catch real bugs with
  867. your code!
  868. ## Usage
  869. Install:
  870. \`\`\`
  871. yarn add prettier --dev --exact
  872. \`\`\`
  873. You can install it globally if you like:
  874. \`\`\`
  875. yarn global add prettier
  876. \`\`\`
  877. _We're using \`yarn\` but you can use \`npm\` if you like:_
  878. \`\`\`
  879. npm install --save-dev --save-exact prettier
  880. # or globally
  881. npm install --global prettier
  882. \`\`\`
  883. > We recommend pinning an exact version of prettier in your \`package.json\` as we
  884. > introduce stylistic changes in patch releases.
  885. ### CLI
  886. Run Prettier through the CLI with this script. Run it without any arguments to
  887. see the [options](#options).
  888. To format a file in-place, use \`--write\`. You may want to consider committing
  889. your code before doing that, just in case.
  890. \`\`\`bash
  891. prettier [opts] [filename ...]
  892. \`\`\`
  893. In practice, this may look something like:
  894. \`\`\`bash
  895. prettier --single-quote --trailing-comma es5 --write "{app,__{tests,mocks}__}/**/*.js"
  896. \`\`\`
  897. Don't forget the quotes around the globs! The quotes make sure that Prettier
  898. expands the globs rather than your shell, for cross-platform usage. The
  899. [glob syntax from the glob module](https://github.com/isaacs/node-glob/blob/master/README.md#glob-primer)
  900. is used.
  901. #### \`--debug-check\`
  902. If you're worried that Prettier will change the correctness of your code, add
  903. \`--debug-check\` to the command. This will cause Prettier to print an error
  904. message if it detects that code correctness might have changed. Note that
  905. \`--write\` cannot be used with \`--debug-check\`.
  906. #### \`--find-config-path\` and \`--config\`
  907. If you are repeatedly formatting individual files with \`prettier\`, you will
  908. incur a small performance cost when prettier attempts to look up a
  909. [configuration file](#configuration-file). In order to skip this, you may ask
  910. prettier to find the config file once, and re-use it later on.
  911. \`\`\`bash
  912. prettier --find-config-path ./my/file.js
  913. ./my/.prettierrc
  914. \`\`\`
  915. This will provide you with a path to the configuration file, which you can pass
  916. to \`--config\`:
  917. \`\`\`bash
  918. prettier --config ./my/.prettierrc --write ./my/file.js
  919. \`\`\`
  920. You can also use \`--config\` if your configuration file lives somewhere where
  921. prettier cannot find it, such as a \`config/\` directory.
  922. If you don't have a configuration file, or want to ignore it if it does exist,
  923. you can pass \`--no-config\` instead.
  924. #### \`--ignore-path\`
  925. Path to a file containing patterns that describe files to ignore. By default,
  926. prettier looks for \`./.prettierignore\`.
  927. #### \`--require-pragma\`
  928. Require a special comment, called a pragma, to be present in the file's first
  929. docblock comment in order for prettier to format it.
  930. \`\`\`js
  931. /**
  932. * @prettier
  933. */
  934. \`\`\`
  935. Valid pragmas are \`@prettier\` and \`@format\`.
  936. #### \`--list-different\`
  937. Another useful flag is \`--list-different\` (or \`-l\`) which prints the filenames
  938. of files that are different from Prettier formatting. If there are differences
  939. the script errors out, which is useful in a CI scenario.
  940. \`\`\`bash
  941. prettier --single-quote --list-different "src/**/*.js"
  942. \`\`\`
  943. #### \`--no-config\`
  944. Do not look for a configuration file. The default settings will be used.
  945. #### \`--config-precedence\`
  946. Defines how config file should be evaluated in combination of CLI options.
  947. **cli-override (default)**
  948. CLI options take precedence over config file
  949. **file-override**
  950. Config file take precedence over CLI options
  951. **prefer-file**
  952. If a config file is found will evaluate it and ignore other CLI options. If no
  953. config file is found CLI options will evaluate as normal.
  954. This option adds support to editor integrations where users define their default
  955. configuration but want to respect project specific configuration.
  956. #### \`--with-node-modules\`
  957. Prettier CLI will ignore files located in \`node_modules\` directory. To opt-out
  958. from this behavior use \`--with-node-modules\` flag.
  959. #### \`--write\`
  960. This rewrites all processed files in place. This is comparable to the
  961. \`eslint --fix\` workflow.
  962. ### ESLint
  963. If you are using ESLint, integrating Prettier to your workflow is
  964. straightforward:
  965. Just add Prettier as an ESLint rule using
  966. [eslint-plugin-prettier](https://github.com/prettier/eslint-plugin-prettier).
  967. \`\`\`js
  968. yarn add --dev prettier eslint-plugin-prettier
  969. // .eslintrc.json
  970. {
  971. "plugins": [
  972. "prettier"
  973. ],
  974. "rules": {
  975. "prettier/prettier": "error"
  976. }
  977. }
  978. \`\`\`
  979. We also recommend that you use
  980. [eslint-config-prettier](https://github.com/prettier/eslint-config-prettier) to
  981. disable all the existing formatting rules. It's a one liner that can be added
  982. on-top of any existing ESLint configuration.
  983. \`\`\`
  984. $ yarn add --dev eslint-config-prettier
  985. \`\`\`
  986. .eslintrc.json:
  987. \`\`\`json
  988. {
  989. "extends": ["prettier"]
  990. }
  991. \`\`\`
  992. ### Pre-commit Hook
  993. You can use Prettier with a pre-commit tool. This can re-format your files that
  994. are marked as "staged" via \`git add\` before you commit.
  995. ##### Option 1. [lint-staged](https://github.com/okonet/lint-staged)
  996. Install it along with [husky](https://github.com/typicode/husky):
  997. \`\`\`bash
  998. yarn add lint-staged husky --dev
  999. \`\`\`
  1000. and add this config to your \`package.json\`:
  1001. \`\`\`json
  1002. {
  1003. "scripts": {
  1004. "precommit": "lint-staged"
  1005. },
  1006. "lint-staged": {
  1007. "*.{js,json,css}": ["prettier --write", "git add"]
  1008. }
  1009. }
  1010. \`\`\`
  1011. There is a limitation where if you stage specific lines this approach will stage
  1012. the whole file after regardless. See this
  1013. [issue](https://github.com/okonet/lint-staged/issues/62) for more info.
  1014. See https://github.com/okonet/lint-staged#configuration for more details about
  1015. how you can configure lint-staged.
  1016. ##### Option 2. [pre-commit](https://github.com/pre-commit/pre-commit)
  1017. Copy the following config into your \`.pre-commit-config.yaml\` file:
  1018. \`\`\`yaml
  1019. - repo: https://github.com/prettier/prettier
  1020. sha: '' # Use the sha or tag you want to point at
  1021. hooks:
  1022. - id: prettier
  1023. \`\`\`
  1024. Find more info from [here](https://pre-commit.com).
  1025. ##### Option 3. bash script
  1026. Alternately you can save this script as \`.git/hooks/pre-commit\` and give it
  1027. execute permission:
  1028. \`\`\`bash
  1029. #!/bin/sh
  1030. jsfiles=$(git diff --cached --name-only --diff-filter=ACM | grep '\\.jsx\\?$' | tr '\\n' ' ')
  1031. [ -z "$jsfiles" ] && exit 0
  1032. # Prettify all staged .js files
  1033. echo "$jsfiles" | xargs ./node_modules/.bin/prettier --write
  1034. # Add back the modified/prettified files to staging
  1035. echo "$jsfiles" | xargs git add
  1036. exit 0
  1037. \`\`\`
  1038. ### API
  1039. \`\`\`js
  1040. const prettier = require("prettier");
  1041. \`\`\`
  1042. #### \`prettier.format(source [, options])\`
  1043. \`format\` is used to format text using Prettier. [Options](#options) may be
  1044. provided to override the defaults.
  1045. \`\`\`js
  1046. prettier.format("foo ( );", { semi: false });
  1047. // -> "foo()"
  1048. \`\`\`
  1049. #### \`prettier.check(source [, options])\`
  1050. \`check\` checks to see if the file has been formatted with Prettier given those
  1051. options and returns a \`Boolean\`. This is similar to the \`--list-different\`
  1052. parameter in the CLI and is useful for running Prettier in CI scenarios.
  1053. #### \`prettier.formatWithCursor(source [, options])\`
  1054. \`formatWithCursor\` both formats the code, and translates a cursor position from
  1055. unformatted code to formatted code. This is useful for editor integrations, to
  1056. prevent the cursor from moving when code is formatted.
  1057. The \`cursorOffset\` option should be provided, to specify where the cursor is.
  1058. This option cannot be used with \`rangeStart\` and \`rangeEnd\`.
  1059. \`\`\`js
  1060. prettier.formatWithCursor(" 1", { cursorOffset: 2 });
  1061. // -> { formatted: '1;\\n', cursorOffset: 1 }
  1062. \`\`\`
  1063. #### \`prettier.resolveConfig([filePath [, options]])\`
  1064. \`resolveConfig\` can be used to resolve configuration for a given source file.
  1065. The function optionally accepts an input file path as an argument, which
  1066. defaults to the current working directory. A promise is returned which will
  1067. resolve to:
  1068. - An options object, providing a [config file](#configuration-file) was found.
  1069. - \`null\`, if no file was found.
  1070. The promise will be rejected if there was an error parsing the configuration
  1071. file.
  1072. If \`options.useCache\` is \`false\`, all caching will be bypassed.
  1073. \`\`\`js
  1074. const text = fs.readFileSync(filePath, "utf8");
  1075. prettier.resolveConfig(filePath).then(options => {
  1076. const formatted = prettier.format(text, options);
  1077. });
  1078. \`\`\`
  1079. Use \`prettier.resolveConfig.sync([filePath [, options]])\` if you'd like to use
  1080. sync version.
  1081. #### \`prettier.clearConfigCache()\`
  1082. As you repeatedly call \`resolveConfig\`, the file system structure will be cached
  1083. for performance. This function will clear the cache. Generally this is only
  1084. needed for editor integrations that know that the file system has changed since
  1085. the last format took place.
  1086. #### Custom Parser API
  1087. If you need to make modifications to the AST (such as codemods), or you want to
  1088. provide an alternate parser, you can do so by setting the \`parser\` option to a
  1089. function. The function signature of the parser function is:
  1090. \`\`\`js
  1091. (text: string, parsers: object, options: object) => AST;
  1092. \`\`\`
  1093. Prettier's built-in parsers are exposed as properties on the \`parsers\` argument.
  1094. \`\`\`js
  1095. prettier.format("lodash ( )", {
  1096. parser(text, { babylon }) {
  1097. const ast = babylon(text);
  1098. ast.program.body[0].expression.callee.name = "_";
  1099. return ast;
  1100. }
  1101. });
  1102. // -> "_();\\n"
  1103. \`\`\`
  1104. The \`--parser\` CLI option may be a path to a node.js module exporting a parse
  1105. function.
  1106. ### Excluding code from formatting
  1107. A JavaScript comment of \`// prettier-ignore\` will exclude the next node in the
  1108. abstract syntax tree from formatting.
  1109. For example:
  1110. \`\`\`js
  1111. matrix(1, 0, 0, 0, 1, 0, 0, 0, 1);
  1112. // prettier-ignore
  1113. matrix(
  1114. 1, 0, 0,
  1115. 0, 1, 0,
  1116. 0, 0, 1
  1117. )
  1118. \`\`\`
  1119. will be transformed to:
  1120. \`\`\`js
  1121. matrix(1, 0, 0, 0, 1, 0, 0, 0, 1);
  1122. // prettier-ignore
  1123. matrix(
  1124. 1, 0, 0,
  1125. 0, 1, 0,
  1126. 0, 0, 1
  1127. )
  1128. \`\`\`
  1129. ## Options
  1130. Prettier ships with a handful of customizable format options, usable in both the
  1131. CLI and API.
  1132. ### Print Width
  1133. Specify the line length that the printer will wrap on.
  1134. > **For readability we recommend against using more than 80 characters:**
  1135. >
  1136. > In code styleguides, maximum line length rules are often set to 100 or 120.
  1137. > However, when humans write code, they don't strive to reach the maximum number
  1138. > of columns on every line. Developers often use whitespace to break up long
  1139. > lines for readability. In practice, the average line length often ends up well
  1140. > below the maximum.
  1141. >
  1142. > Prettier, on the other hand, strives to fit the most code into every line.
  1143. > With the print width set to 120, prettier may produce overly compact, or
  1144. > otherwise undesirable code.
  1145. | Default | CLI Override | API Override |
  1146. | ------- | --------------------- | ------------------- |
  1147. | \`80\` | \`--print-width <int>\` | \`printWidth: <int>\` |
  1148. ### Tab Width
  1149. Specify the number of spaces per indentation-level.
  1150. | Default | CLI Override | API Override |
  1151. | ------- | ------------------- | ----------------- |
  1152. | \`2\` | \`--tab-width <int>\` | \`tabWidth: <int>\` |
  1153. ### Tabs
  1154. Indent lines with tabs instead of spaces
  1155. | Default | CLI Override | API Override |
  1156. | ------- | ------------ | ----------------- |
  1157. | \`false\` | \`--use-tabs\` | \`useTabs: <bool>\` |
  1158. ### Semicolons
  1159. Print semicolons at the ends of statements.
  1160. Valid options:
  1161. - \`true\` - Add a semicolon at the end of every statement.
  1162. - \`false\` - Only add semicolons at the beginning of lines that may introduce ASI
  1163. failures.
  1164. | Default | CLI Override | API Override |
  1165. | ------- | ------------ | -------------- |
  1166. | \`true\` | \`--no-semi\` | \`semi: <bool>\` |
  1167. ### Quotes
  1168. Use single quotes instead of double quotes.
  1169. Notes:
  1170. - Quotes in JSX will always be double and ignore this setting.
  1171. - If the number of quotes outweighs the other quote, the quote which is less
  1172. used will be used to format the string - Example: \`"I'm double quoted"\`
  1173. results in \`"I'm double quoted"\` and \`"This \\"example\\" is single quoted"\`
  1174. results in \`'This "example" is single quoted'\`.
  1175. | Default | CLI Override | API Override |
  1176. | ------- | ---------------- | --------------------- |
  1177. | \`false\` | \`--single-quote\` | \`singleQuote: <bool>\` |
  1178. ### Trailing Commas
  1179. Print trailing commas wherever possible when multi-line. (A single-line array,
  1180. for example, never gets trailing commas.)
  1181. Valid options:
  1182. - \`"none"\` - No trailing commas.
  1183. - \`"es5"\` - Trailing commas where valid in ES5 (objects, arrays, etc.)
  1184. - \`"all"\` - Trailing commas wherever possible (including function arguments).
  1185. This requires node 8 or a
  1186. [transform](https://babeljs.io/docs/plugins/syntax-trailing-function-commas/).
  1187. | Default | CLI Override | API Override |
  1188. | -------- | ------------------------------------------------------ | ------------------------------------------------------ |
  1189. | \`"none"\` | <code>--trailing-comma <none&#124;es5&#124;all></code> | <code>trailingComma: "<none&#124;es5&#124;all>"</code> |
  1190. ### Bracket Spacing
  1191. Print spaces between brackets in object literals.
  1192. Valid options:
  1193. - \`true\` - Example: \`{ foo: bar }\`.
  1194. - \`false\` - Example: \`{foo: bar}\`.
  1195. | Default | CLI Override | API Override |
  1196. | ------- | ---------------------- | ------------------------ |
  1197. | \`true\` | \`--no-bracket-spacing\` | \`bracketSpacing: <bool>\` |
  1198. ### JSX Brackets
  1199. Put the \`>\` of a multi-line JSX element at the end of the last line instead of
  1200. being alone on the next line (does not apply to self closing elements).
  1201. | Default | CLI Override | API Override |
  1202. | ------- | ------------------------- | ---------------------------- |
  1203. | \`false\` | \`--jsx-bracket-same-line\` | \`jsxBracketSameLine: <bool>\` |
  1204. ### Range
  1205. Format only a segment of a file.
  1206. These two options can be used to format code starting and ending at a given
  1207. character offset (inclusive and exclusive, respectively). The range will extend:
  1208. - Backwards to the start of the first line containing the selected statement.
  1209. - Forwards to the end of the selected statement.
  1210. These options cannot be used with \`cursorOffset\`.
  1211. | Default | CLI Override | API Override |
  1212. | ---------- | --------------------- | ------------------- |
  1213. | \`0\` | \`--range-start <int>\` | \`rangeStart: <int>\` |
  1214. | \`Infinity\` | \`--range-end <int>\` | \`rangeEnd: <int>\` |
  1215. ### Parser
  1216. Specify which parser to use.
  1217. Both the \`babylon\` and \`flow\` parsers support the same set of JavaScript
  1218. features (including Flow). Prettier automatically infers the parser from the
  1219. input file path, so you shouldn't have to change this setting.
  1220. Built-in parsers:
  1221. - [\`babylon\`](https://github.com/babel/babylon/)
  1222. - [\`flow\`](https://github.com/facebook/flow/tree/master/src/parser)
  1223. - [\`typescript\`](https://github.com/eslint/typescript-eslint-parser) _Since
  1224. v1.4.0_
  1225. - [\`postcss\`](https://github.com/postcss/postcss) _Since v1.4.0_
  1226. - [\`json\`](https://github.com/babel/babylon/tree/f09eb3200f57ea94d51c2a5b1facf2149fb406bf#babylonparseexpressioncode-options)
  1227. _Since v1.5.0_
  1228. - [\`graphql\`](https://github.com/graphql/graphql-js/tree/master/src/language)
  1229. _Since v1.5.0_
  1230. [Custom parsers](#custom-parser-api) are also supported. _Since v1.5.0_
  1231. | Default | CLI Override | API Override |
  1232. | --------- | ----------------------------------------------- | ---------------------------------------------------------- |
  1233. | \`babylon\` | \`--parser <string>\`<br />\`--parser ./my-parser\` | \`parser: "<string>"\`<br />\`parser: require("./my-parser")\` |
  1234. ### Filepath
  1235. Specify the input filepath. This will be used to do parser inference.
  1236. For example, the following will use \`postcss\` parser:
  1237. \`\`\`bash
  1238. cat foo | prettier --stdin-filepath foo.css
  1239. \`\`\`
  1240. | Default | CLI Override | API Override |
  1241. | ------- | --------------------------- | ---------------------- |
  1242. | None | \`--stdin-filepath <string>\` | \`filepath: "<string>"\` |
  1243. ### Require pragma
  1244. Prettier can restrict itself to only format files that contain a special
  1245. comment, called a pragma, at the top of the file. This is very useful when
  1246. gradually transitioning large, unformatted codebases to prettier.
  1247. For example, a file with the following as its first comment will be formatted
  1248. when \`--require-pragma\` is supplied:
  1249. \`\`\`js
  1250. /**
  1251. * @prettier
  1252. */
  1253. \`\`\`
  1254. or
  1255. \`\`\`js
  1256. /**
  1257. * @format
  1258. */
  1259. \`\`\`
  1260. | Default | CLI Override | API Override |
  1261. | ------- | ------------------ | ----------------------- |
  1262. | \`false\` | \`--require-pragma\` | \`requirePragma: <bool>\` |
  1263. ## Configuration File
  1264. Prettier uses [cosmiconfig](https://github.com/davidtheclark/cosmiconfig) for
  1265. configuration file support. This means you can configure prettier via:
  1266. - A \`.prettierrc\` file, written in YAML or JSON, with optional extensions:
  1267. \`.yaml/.yml/.json/.js\`.
  1268. - A \`prettier.config.js\` file that exports an object.
  1269. - A \`"prettier"\` key in your \`package.json\` file.
  1270. The configuration file will be resolved starting from the location of the file
  1271. being formatted, and searching up the file tree until a config file is (or
  1272. isn't) found.
  1273. The options to the configuration file are the same the [API options](#options).
  1274. ### Basic Configuration
  1275. JSON:
  1276. \`\`\`json
  1277. // .prettierrc
  1278. {
  1279. "printWidth": 100,
  1280. "parser": "flow"
  1281. }
  1282. \`\`\`
  1283. YAML:
  1284. \`\`\`yaml
  1285. # .prettierrc
  1286. printWidth: 100
  1287. parser: flow
  1288. \`\`\`
  1289. ### Configuration Overrides
  1290. Prettier borrows eslint's
  1291. [override format](http://eslint.org/docs/user-guide/configuring#example-configuration).
  1292. This allows you to apply configuration to specific files.
  1293. JSON:
  1294. \`\`\`json
  1295. {
  1296. "semi": false,
  1297. "overrides": [
  1298. {
  1299. "files": "*.test.js",
  1300. "options": {
  1301. "semi": true
  1302. }
  1303. }
  1304. ]
  1305. }
  1306. \`\`\`
  1307. YAML:
  1308. \`\`\`yaml
  1309. semi: false
  1310. overrides:
  1311. - files: "*.test.js"
  1312. options:
  1313. semi: true
  1314. \`\`\`
  1315. \`files\` is required for each override, and may be a string or array of strings.
  1316. \`excludeFiles\` may be optionally provided to exclude files for a given rule, and
  1317. may also be a string or array of strings.
  1318. To get prettier to format its own \`.prettierrc\` file, you can do:
  1319. \`\`\`json
  1320. {
  1321. "overrides": [
  1322. {
  1323. "files": ".prettierrc",
  1324. "options": { "parser": "json" }
  1325. }
  1326. ]
  1327. }
  1328. \`\`\`
  1329. For more information on how to use the CLI to locate a file, see the [CLI](#cli)
  1330. section.
  1331. ### Configuration Schema
  1332. If you'd like a JSON schema to validate your configuration, one is available
  1333. here: http://json.schemastore.org/prettierrc.
  1334. ## Editor Integration
  1335. ### Atom
  1336. Atom users can simply install the
  1337. [prettier-atom](https://github.com/prettier/prettier-atom) package and use
  1338. \`Ctrl+Alt+F\` to format a file (or format on save if enabled).
  1339. ### Emacs
  1340. Emacs users should see
  1341. [this repository](https://github.com/prettier/prettier-emacs) for on-demand
  1342. formatting.
  1343. ### Vim
  1344. Vim users can simply install either
  1345. [sbdchd](https://github.com/sbdchd)/[neoformat](https://github.com/sbdchd/neoformat),
  1346. [w0rp](https://github.com/w0rp)/[ale](https://github.com/w0rp/ale), or
  1347. [prettier](https://github.com/prettier)/[vim-prettier](https://github.com/prettier/vim-prettier),
  1348. for more details see
  1349. [this directory](https://github.com/prettier/prettier/tree/master/editors/vim).
  1350. ### Visual Studio Code
  1351. Can be installed using the extension sidebar. Search for
  1352. \`Prettier - JavaScript formatter\`.
  1353. Can also be installed using \`ext install prettier-vscode\`.
  1354. [Check its repository for configuration and shortcuts](https://github.com/prettier/prettier-vscode)
  1355. ### Visual Studio
  1356. Install the
  1357. [JavaScript Prettier extension](https://github.com/madskristensen/JavaScriptPrettier).
  1358. ### Sublime Text
  1359. Sublime Text support is available through Package Control and the
  1360. [JsPrettier](https://packagecontrol.io/packages/JsPrettier) plug-in.
  1361. ### JetBrains WebStorm, PHPStorm, PyCharm...
  1362. See the
  1363. [WebStorm guide](https://github.com/jlongster/prettier/tree/master/editors/webstorm/README.md).
  1364. ## Language Support
  1365. Prettier attempts to support all JavaScript language features, including
  1366. non-standardized ones. By default it uses the
  1367. [Babylon](https://github.com/babel/babylon) parser with all language features
  1368. enabled, but you can also use the [Flow](https://github.com/facebook/flow)
  1369. parser with the \`parser\` API or \`--parser\` CLI [option](#options).
  1370. All of JSX and Flow syntax is supported. In fact, the test suite in \`tests/flow\`
  1371. _is_ the entire Flow test suite and they all pass.
  1372. Prettier also supports [TypeScript](https://www.typescriptlang.org/), CSS,
  1373. [Less](http://lesscss.org/), [SCSS](http://sass-lang.com),
  1374. [JSON](http://json.org/), and [GraphQL](http://graphql.org/).
  1375. The minimum version of TypeScript supported is 2.1.3 as it introduces the
  1376. ability to have leading \`|\` for type definitions which prettier outputs.
  1377. ## Related Projects
  1378. - [\`eslint-plugin-prettier\`](https://github.com/prettier/eslint-plugin-prettier)
  1379. plugs Prettier into your ESLint workflow
  1380. - [\`eslint-config-prettier\`](https://github.com/prettier/eslint-config-prettier)
  1381. turns off all ESLint rules that are unnecessary or might conflict with
  1382. Prettier
  1383. - [\`prettier-eslint\`](https://github.com/prettier/prettier-eslint) passes
  1384. \`prettier\` output to \`eslint --fix\`
  1385. - [\`prettier-stylelint\`](https://github.com/hugomrdias/prettier-stylelint)
  1386. passes \`prettier\` output to \`stylelint --fix\`
  1387. - [\`prettier-standard\`](https://github.com/sheerun/prettier-standard) uses
  1388. \`prettier\` and \`prettier-eslint\` to format code with standard rules
  1389. - [\`prettier-standard-formatter\`](https://github.com/dtinth/prettier-standard-formatter)
  1390. passes \`prettier\` output to \`standard --fix\`
  1391. - [\`prettier-miscellaneous\`](https://github.com/arijs/prettier-miscellaneous)
  1392. \`prettier\` with a few minor extra options
  1393. - [\`neutrino-preset-prettier\`](https://github.com/SpencerCDixon/neutrino-preset-prettier)
  1394. allows you to use Prettier as a Neutrino preset
  1395. - [\`prettier_d\`](https://github.com/josephfrazier/prettier_d.js) runs Prettier
  1396. as a server to avoid Node.js startup delay. It also supports configuration via
  1397. \`.prettierrc\`, \`package.json\`, and \`.editorconfig\`.
  1398. - [\`Prettier Bookmarklet\`](https://prettier.glitch.me/) provides a bookmarklet
  1399. and exposes a REST API for Prettier that allows to format CodeMirror editor in
  1400. your browser
  1401. - [\`prettier-github\`](https://github.com/jgierer12/prettier-github) formats code
  1402. in GitHub comments
  1403. - [\`rollup-plugin-prettier\`](https://github.com/mjeanroy/rollup-plugin-prettier)
  1404. allows you to use Prettier with Rollup
  1405. - [\`markdown-magic-prettier\`](https://github.com/camacho/markdown-magic-prettier)
  1406. allows you to use Prettier to format JS
  1407. [codeblocks](https://help.github.com/articles/creating-and-highlighting-code-blocks/)
  1408. in Markdown files via
  1409. [Markdown Magic](https://github.com/DavidWells/markdown-magic)
  1410. - [\`tslint-plugin-prettier\`](https://github.com/ikatyang/tslint-plugin-prettier)
  1411. runs Prettier as a TSLint rule and reports differences as individual TSLint
  1412. issues
  1413. - [\`tslint-config-prettier\`](https://github.com/alexjoverm/tslint-config-prettier)
  1414. use TSLint with Prettier without any conflict
  1415. ## Technical Details
  1416. This printer is a fork of [recast](https://github.com/benjamn/recast)'s printer
  1417. with its algorithm replaced by the one described by Wadler in
  1418. "[A prettier printer](http://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf)".
  1419. There still may be leftover code from recast that needs to be cleaned up.
  1420. The basic idea is that the printer takes an AST and returns an intermediate
  1421. representation of the output, and the printer uses that to generate a string.
  1422. The advantage is that the printer can "measure" the IR and see if the output is
  1423. going to fit on a line, and break if not.
  1424. This means that most of the logic of printing an AST involves generating an
  1425. abstract representation of the output involving certain commands. For example,
  1426. \`concat(["(", line, arg, line ")"])\` would represent a concatenation of opening
  1427. parens, an argument, and closing parens. But if that doesn't fit on one line,
  1428. the printer can break where \`line\` is specified.
  1429. More (rough) details can be found in [commands.md](commands.md).
  1430. ## Badge
  1431. Show the world you're using _Prettier_ →
  1432. [![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
  1433. \`\`\`md
  1434. [![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
  1435. \`\`\`
  1436. ## Contributing
  1437. See [CONTRIBUTING.md](CONTRIBUTING.md).
  1438. `;
  1439. exports[`real-world-case.md 2`] = `
  1440. # Prettier
  1441. [![Gitter](https://badges.gitter.im/gitterHQ/gitter.svg)](https://gitter.im/jlongster/prettier)
  1442. [![Build Status](https://travis-ci.org/prettier/prettier.svg?branch=master)](https://travis-ci.org/prettier/prettier)
  1443. [![Codecov](https://img.shields.io/codecov/c/github/prettier/prettier.svg)](https://codecov.io/gh/prettier/prettier)
  1444. [![NPM version](https://img.shields.io/npm/v/prettier.svg)](https://www.npmjs.com/package/prettier)
  1445. [![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](#badge)
  1446. Prettier is an opinionated code formatter with support for:
  1447. * JavaScript, including [ES2017](https://github.com/tc39/proposals/blob/master/finished-proposals.md)
  1448. * [JSX](https://facebook.github.io/jsx/)
  1449. * [Flow](https://flow.org/)
  1450. * [TypeScript](https://www.typescriptlang.org/)
  1451. * CSS, [Less](http://lesscss.org/), and [SCSS](http://sass-lang.com)
  1452. * [JSON](http://json.org/)
  1453. * [GraphQL](http://graphql.org/)
  1454. It removes all original styling[\\*](#styling-footnote) and ensures that all outputted code
  1455. conforms to a consistent style. (See this [blog post](http://jlongster.com/A-Prettier-Formatter))
  1456. <details>
  1457. <summary><strong>Table of Contents</strong></summary>
  1458. <!-- Do not edit TOC, regenerate with \`yarn toc\` -->
  1459. <!-- toc -->
  1460. * [What does Prettier do?](#what-does-prettier-do)
  1461. * [Why Prettier?](#why-prettier)
  1462. + [Building and enforcing a style guide](#building-and-enforcing-a-style-guide)
  1463. + [Helping Newcomers](#helping-newcomers)
  1464. + [Writing code](#writing-code)
  1465. + [Easy to adopt](#easy-to-adopt)
  1466. + [Clean up an existing codebase](#clean-up-an-existing-codebase)
  1467. + [Ride the hype train](#ride-the-hype-train)
  1468. * [How does it compare to ESLint (or TSLint, stylelint...)?](#how-does-it-compare-to-eslint-or-tslint-stylelint)
  1469. * [Usage](#usage)
  1470. + [CLI](#cli)
  1471. + [ESLint](#eslint)
  1472. + [Pre-commit Hook](#pre-commit-hook)
  1473. + [API](#api)
  1474. + [Excluding code from formatting](#excluding-code-from-formatting)
  1475. * [Options](#options)
  1476. + [Print Width](#print-width)
  1477. + [Tab Width](#tab-width)
  1478. + [Tabs](#tabs)
  1479. + [Semicolons](#semicolons)
  1480. + [Quotes](#quotes)
  1481. + [Trailing Commas](#trailing-commas)
  1482. + [Bracket Spacing](#bracket-spacing)
  1483. + [JSX Brackets](#jsx-brackets)
  1484. + [Range](#range)
  1485. + [Parser](#parser)
  1486. + [Filepath](#filepath)
  1487. * [Configuration File](#configuration-file)
  1488. + [Basic Configuration](#basic-configuration)
  1489. + [Configuration Overrides](#configuration-overrides)
  1490. + [Configuration Schema](#configuration-schema)
  1491. * [Editor Integration](#editor-integration)
  1492. + [Atom](#atom)
  1493. + [Emacs](#emacs)
  1494. + [Vim](#vim)
  1495. + [Visual Studio Code](#visual-studio-code)
  1496. + [Visual Studio](#visual-studio)
  1497. + [Sublime Text](#sublime-text)
  1498. + [JetBrains WebStorm, PHPStorm, PyCharm...](#jetbrains-webstorm-phpstorm-pycharm)
  1499. * [Language Support](#language-support)
  1500. * [Related Projects](#related-projects)
  1501. * [Technical Details](#technical-details)
  1502. * [Badge](#badge)
  1503. * [Contributing](#contributing)
  1504. <!-- tocstop -->
  1505. </details>
  1506. --------------------------------------------------------------------------------
  1507. ## What does Prettier do?
  1508. Prettier takes your code and reprints it from scratch by taking the line length into account.
  1509. For example, take the following code:
  1510. \`\`\`js
  1511. foo(arg1, arg2, arg3, arg4);
  1512. \`\`\`
  1513. It fits in a single line so it's going to stay as is. However, we've all run into this situation:
  1514. <!-- prettier-ignore -->
  1515. \`\`\`js
  1516. foo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne());
  1517. \`\`\`
  1518. Suddenly our previous format for calling function breaks down because this is too long. Prettier is going to do the painstaking work of reprinting it like that for you:
  1519. \`\`\`js
  1520. foo(
  1521. reallyLongArg(),
  1522. omgSoManyParameters(),
  1523. IShouldRefactorThis(),
  1524. isThereSeriouslyAnotherOne()
  1525. );
  1526. \`\`\`
  1527. Prettier enforces a consistent code **style** (i.e. code formatting that won't affect the AST) across your entire codebase because it disregards the original styling[\\*](#styling-footnote) by parsing it away and re-printing the parsed AST with its own rules that take the maximum line length
  1528. into account, wrapping code when necessary.
  1529. <a href="#styling-footnote" name="styling-footnote">\\*</a>_Well actually, some
  1530. original styling is preserved when practical—see [empty lines] and [multi-line
  1531. objects]._
  1532. [empty lines]:Rationale.md#empty-lines
  1533. [multi-line objects]:Rationale.md#multi-line-objects
  1534. If you want to learn more, these two conference talks are great introductions:
  1535. <a href="https://www.youtube.com/watch?v=hkfBvpEfWdA"><img width="298" src="https://cloud.githubusercontent.com/assets/197597/24886367/dda8a6f0-1e08-11e7-865b-22492450f10f.png"></a> <a href="https://www.youtube.com/watch?v=0Q4kUNx85_4"><img width="298" src="https://cloud.githubusercontent.com/assets/197597/24886368/ddacd6f8-1e08-11e7-806a-9febd23cbf47.png"></a>
  1536. ## Why Prettier?
  1537. ### Building and enforcing a style guide
  1538. By far the biggest reason for adopting Prettier is to stop all the on-going debates over styles. It is generally accepted that having a common style guide is valuable for a project and team but getting there is a very painful and unrewarding process. People get very emotional around particular ways of writing code and nobody likes spending time writing and receiving nits.
  1539. - “We want to free mental threads and end discussions around style. While sometimes fruitful, these discussions are for the most part wasteful.”
  1540. - “Literally had an engineer go through a huge effort of cleaning up all of our code because we were debating ternary style for the longest time and were inconsistent about it. It was dumb, but it was a weird on-going "great debate" that wasted lots of little back and forth bits. It's far easier for us all to agree now: just run Prettier, and go with that style.”
  1541. - “Getting tired telling people how to style their product code.”
  1542. - “Our top reason was to stop wasting our time debating style nits.”
  1543. - “Having a githook set up has reduced the amount of style issues in PRs that result in broken builds due to ESLint rules or things I have to nit-pick or clean up later.”
  1544. - “I don't want anybody to nitpick any other person ever again.”
  1545. - “It reminds me of how Steve Jobs used to wear the same clothes every day because he has a million decisions to make and he didn't want to be bothered to make trivial ones like picking out clothes. I think Prettier is like that.”
  1546. ### Helping Newcomers
  1547. Prettier is usually introduced by people with experience in the current codebase and JavaScript but the people that disproportionally benefit from it are newcomers to the codebase. One may think that it's only useful for people with very limited programming experience, but we've seen it quicken the ramp up time from experienced engineers joining the company, as they likely used a different coding style before, and developers coming from a different programming language.
  1548. - “My motivations for using Prettier are: appearing that I know how to write JavaScript well.”
  1549. - “I always put spaces in the wrong place, now I don't have to worry about it anymore.”
  1550. - “When you're a beginner you're making a lot of mistakes caused by the syntax. Thanks to Prettier, you can reduce these mistakes and save a lot of time to focus on what really matters.”
  1551. - “As a teacher, I will also tell to my students to install Prettier to help them to learn the JS syntax and have readable files.”
  1552. ### Writing code
  1553. What usually happens once people are using Prettier is that they realize that they actually spend a lot of time and mental energy formatting their code. With Prettier editor integration, you can just press that magic key binding and poof, the code is formatted. This is an eye opening experience if anything else.
  1554. - “I want to write code. Not spend cycles on formatting.”
  1555. - “It removed 5% that sucks in our daily life - aka formatting”
  1556. - “We're in 2017 and it's still painful to break a call into multiple lines when you happen to add an argument that makes it go over the 80 columns limit :(“
  1557. ### Easy to adopt
  1558. We've worked very hard to use the least controversial coding styles, went through many rounds of fixing all the edge cases and polished the getting started experience. When you're ready to push Prettier into your codebase, not only should it be painless for you to do it technically but the newly formatted codebase should not generate major controversy and be accepted painlessly by your co-workers.
  1559. - “It's low overhead. We were able to throw Prettier at very different kinds of repos without much work.”
  1560. - “It's been mostly bug free. Had there been major styling issues during the course of implementation we would have been wary about throwing this at our JS codebase. I'm happy to say that's not the case.”
  1561. - “Everyone runs it as part of their pre commit scripts, a couple of us use the editor on save extensions as well.”
  1562. - “It's fast, against one of our larger JS codebases we were able to run Prettier in under 13 seconds.”
  1563. - “The biggest benefit for Prettier for us was being able to format the entire code base at once.”
  1564. ### Clean up an existing codebase
  1565. Since coming up with a coding style and enforcing it is a big undertaking, it often slips through the cracks and you are left working on inconsistent codebases. Running Prettier in this case is a quick win, the codebase is now uniform and easier to read without spending hardly any time.
  1566. - “Take a look at the code :) I just need to restore sanity.”
  1567. - “We inherited a ~2000 module ES6 code base, developed by 20 different developers over 18 months, in a global team. Felt like such a win without much research.”
  1568. ### Ride the hype train
  1569. Purely technical aspects of the projects aren't the only thing people look into when choosing to adopt Prettier. Who built and uses it and how quickly it spreads through the community has a non-trivial impact.
  1570. - “The amazing thing, for me, is: 1) Announced 2 months ago. 2) Already adopted by, it seems, every major JS project. 3) 7000 stars, 100,000 npm downloads/mo”
  1571. - “Was built by the same people as React & React Native.”
  1572. - “I like to be part of the hot new things.”
  1573. - “Because soon enough people are gonna ask for it.”
  1574. A few of the [many projects](https://www.npmjs.com/browse/depended/prettier) using Prettier:
  1575. <table>
  1576. <tr>
  1577. <td><p align="center"><a href="https://facebook.github.io/react/"><img src="website/static/images/react-200x100.png" alt="React" width="200" height="100"><br>React</a></p></td>
  1578. <td><p align="center"><a href="https://facebook.github.io/jest/"><img src="website/static/images/jest-200x100.png" alt="Jest" width="200" height="100"><br>Jest</a></p></td>
  1579. <td><p align="center"><a href="https://yarnpkg.com"><img src="website/static/images/yarn-200x100.png" alt="Yarn" width="200" height="100"><br>Yarn</a></p></td>
  1580. </tr>
  1581. <tr>
  1582. <td><p align="center"><a href="https://babeljs.io/"><img src="website/static/images/babel-200x100.png" alt="Babel" width="200" height="100"><br>Babel</a></p></td>
  1583. <td><p align="center"><a href="https://zeit.co/"><img src="website/static/images/zeit-200x100.png" alt="Zeit" width="200" height="100"><br>Zeit</a></p></td>
  1584. <td><p align="center"><a href="https://webpack.js.org/api/cli/"><img src="website/static/images/webpack-200x100.png" alt="Webpack-cli" width="200" height="100"><br>Webpack-cli</a></p></td>
  1585. </tr>
  1586. </table>
  1587. ## How does it compare to ESLint (or TSLint, stylelint...)?
  1588. Linters have two categories of rules:
  1589. **Formatting rules**: eg: [max-len](http://eslint.org/docs/rules/max-len), [no-mixed-spaces-and-tabs](http://eslint.org/docs/rules/no-mixed-spaces-and-tabs), [keyword-spacing](http://eslint.org/docs/rules/keyword-spacing), [comma-style](http://eslint.org/docs/rules/comma-style)...
  1590. Prettier alleviates the need for this whole category of rules! Prettier is going to reprint the entire program from scratch in a consistent way, so it's not possible for the programmer to make a mistake there anymore :)
  1591. **Code-quality rules**: eg [no-unused-vars](http://eslint.org/docs/rules/no-unused-vars), [no-extra-bind](http://eslint.org/docs/rules/no-extra-bind), [no-implicit-globals](http://eslint.org/docs/rules/no-implicit-globals), [prefer-promise-reject-errors](http://eslint.org/docs/rules/prefer-promise-reject-errors)...
  1592. Prettier does nothing to help with those kind of rules. They are also the most important ones provided by linters as they are likely to catch real bugs with your code!
  1593. ## Usage
  1594. Install:
  1595. \`\`\`
  1596. yarn add prettier --dev --exact
  1597. \`\`\`
  1598. You can install it globally if you like:
  1599. \`\`\`
  1600. yarn global add prettier
  1601. \`\`\`
  1602. *We're using \`yarn\` but you can use \`npm\` if you like:*
  1603. \`\`\`
  1604. npm install --save-dev --save-exact prettier
  1605. # or globally
  1606. npm install --global prettier
  1607. \`\`\`
  1608. > We recommend pinning an exact version of prettier in your \`package.json\`
  1609. > as we introduce stylistic changes in patch releases.
  1610. ### CLI
  1611. Run Prettier through the CLI with this script. Run it without any
  1612. arguments to see the [options](#options).
  1613. To format a file in-place, use \`--write\`. You may want to consider
  1614. committing your code before doing that, just in case.
  1615. \`\`\`bash
  1616. prettier [opts] [filename ...]
  1617. \`\`\`
  1618. In practice, this may look something like:
  1619. \`\`\`bash
  1620. prettier --single-quote --trailing-comma es5 --write "{app,__{tests,mocks}__}/**/*.js"
  1621. \`\`\`
  1622. Don't forget the quotes around the globs! The quotes make sure that Prettier
  1623. expands the globs rather than your shell, for cross-platform usage.
  1624. The [glob syntax from the glob module](https://github.com/isaacs/node-glob/blob/master/README.md#glob-primer)
  1625. is used.
  1626. #### \`--debug-check\`
  1627. If you're worried that Prettier will change the correctness of your code, add \`--debug-check\` to the command.
  1628. This will cause Prettier to print an error message if it detects that code correctness might have changed.
  1629. Note that \`--write\` cannot be used with \`--debug-check\`.
  1630. #### \`--find-config-path\` and \`--config\`
  1631. If you are repeatedly formatting individual files with \`prettier\`, you will incur a small performance cost
  1632. when prettier attempts to look up a [configuration file](#configuration-file). In order to skip this, you may
  1633. ask prettier to find the config file once, and re-use it later on.
  1634. \`\`\`bash
  1635. prettier --find-config-path ./my/file.js
  1636. ./my/.prettierrc
  1637. \`\`\`
  1638. This will provide you with a path to the configuration file, which you can pass to \`--config\`:
  1639. \`\`\`bash
  1640. prettier --config ./my/.prettierrc --write ./my/file.js
  1641. \`\`\`
  1642. You can also use \`--config\` if your configuration file lives somewhere where prettier cannot find it,
  1643. such as a \`config/\` directory.
  1644. If you don't have a configuration file, or want to ignore it if it does exist,
  1645. you can pass \`--no-config\` instead.
  1646. #### \`--ignore-path\`
  1647. Path to a file containing patterns that describe files to ignore. By default, prettier looks for \`./.prettierignore\`.
  1648. #### \`--require-pragma\`
  1649. Require a special comment, called a pragma, to be present in the file's first docblock comment in order for prettier to format it.
  1650. \`\`\`js
  1651. /**
  1652. * @prettier
  1653. */
  1654. \`\`\`
  1655. Valid pragmas are \`@prettier\` and \`@format\`.
  1656. #### \`--list-different\`
  1657. Another useful flag is \`--list-different\` (or \`-l\`) which prints the filenames of files that are different from Prettier formatting. If there are differences the script errors out, which is useful in a CI scenario.
  1658. \`\`\`bash
  1659. prettier --single-quote --list-different "src/**/*.js"
  1660. \`\`\`
  1661. #### \`--no-config\`
  1662. Do not look for a configuration file. The default settings will be used.
  1663. #### \`--config-precedence\`
  1664. Defines how config file should be evaluated in combination of CLI options.
  1665. **cli-override (default)**
  1666. CLI options take precedence over config file
  1667. **file-override**
  1668. Config file take precedence over CLI options
  1669. **prefer-file**
  1670. If a config file is found will evaluate it and ignore other CLI options. If no config file is found CLI options will evaluate as normal.
  1671. This option adds support to editor integrations where users define their default configuration but want to respect project specific configuration.
  1672. #### \`--with-node-modules\`
  1673. Prettier CLI will ignore files located in \`node_modules\` directory. To opt-out from this behavior use \`--with-node-modules\` flag.
  1674. #### \`--write\`
  1675. This rewrites all processed files in place. This is comparable to the \`eslint --fix\` workflow.
  1676. ### ESLint
  1677. If you are using ESLint, integrating Prettier to your workflow is straightforward:
  1678. Just add Prettier as an ESLint rule using [eslint-plugin-prettier](https://github.com/prettier/eslint-plugin-prettier).
  1679. \`\`\`js
  1680. yarn add --dev prettier eslint-plugin-prettier
  1681. // .eslintrc.json
  1682. {
  1683. "plugins": [
  1684. "prettier"
  1685. ],
  1686. "rules": {
  1687. "prettier/prettier": "error"
  1688. }
  1689. }
  1690. \`\`\`
  1691. We also recommend that you use [eslint-config-prettier](https://github.com/prettier/eslint-config-prettier) to disable all the existing formatting rules. It's a one liner that can be added on-top of any existing ESLint configuration.
  1692. \`\`\`
  1693. $ yarn add --dev eslint-config-prettier
  1694. \`\`\`
  1695. .eslintrc.json:
  1696. \`\`\`json
  1697. {
  1698. "extends": [
  1699. "prettier"
  1700. ]
  1701. }
  1702. \`\`\`
  1703. ### Pre-commit Hook
  1704. You can use Prettier with a pre-commit tool. This can re-format your files that are marked as "staged" via \`git add\` before you commit.
  1705. ##### Option 1. [lint-staged](https://github.com/okonet/lint-staged)
  1706. Install it along with [husky](https://github.com/typicode/husky):
  1707. \`\`\`bash
  1708. yarn add lint-staged husky --dev
  1709. \`\`\`
  1710. and add this config to your \`package.json\`:
  1711. \`\`\`json
  1712. {
  1713. "scripts": {
  1714. "precommit": "lint-staged"
  1715. },
  1716. "lint-staged": {
  1717. "*.{js,json,css}": [
  1718. "prettier --write",
  1719. "git add"
  1720. ]
  1721. }
  1722. }
  1723. \`\`\`
  1724. There is a limitation where if you stage specific lines this approach will stage the whole file after regardless. See this [issue](https://github.com/okonet/lint-staged/issues/62) for more info.
  1725. See https://github.com/okonet/lint-staged#configuration for more details about how you can configure lint-staged.
  1726. ##### Option 2. [pre-commit](https://github.com/pre-commit/pre-commit)
  1727. Copy the following config into your \`.pre-commit-config.yaml\` file:
  1728. \`\`\`yaml
  1729. - repo: https://github.com/prettier/prettier
  1730. sha: '' # Use the sha or tag you want to point at
  1731. hooks:
  1732. - id: prettier
  1733. \`\`\`
  1734. Find more info from [here](https://pre-commit.com).
  1735. ##### Option 3. bash script
  1736. Alternately you can save this script as \`.git/hooks/pre-commit\` and give it execute permission:
  1737. \`\`\`bash
  1738. #!/bin/sh
  1739. jsfiles=$(git diff --cached --name-only --diff-filter=ACM | grep '\\.jsx\\?$' | tr '\\n' ' ')
  1740. [ -z "$jsfiles" ] && exit 0
  1741. # Prettify all staged .js files
  1742. echo "$jsfiles" | xargs ./node_modules/.bin/prettier --write
  1743. # Add back the modified/prettified files to staging
  1744. echo "$jsfiles" | xargs git add
  1745. exit 0
  1746. \`\`\`
  1747. ### API
  1748. \`\`\`js
  1749. const prettier = require("prettier");
  1750. \`\`\`
  1751. #### \`prettier.format(source [, options])\`
  1752. \`format\` is used to format text using Prettier. [Options](#options) may be provided to override the defaults.
  1753. \`\`\`js
  1754. prettier.format("foo ( );", { semi: false });
  1755. // -> "foo()"
  1756. \`\`\`
  1757. #### \`prettier.check(source [, options])\`
  1758. \`check\` checks to see if the file has been formatted with Prettier given those options and returns a \`Boolean\`.
  1759. This is similar to the \`--list-different\` parameter in the CLI and is useful for running Prettier in CI scenarios.
  1760. #### \`prettier.formatWithCursor(source [, options])\`
  1761. \`formatWithCursor\` both formats the code, and translates a cursor position from unformatted code to formatted code.
  1762. This is useful for editor integrations, to prevent the cursor from moving when code is formatted.
  1763. The \`cursorOffset\` option should be provided, to specify where the cursor is. This option cannot be used with \`rangeStart\` and \`rangeEnd\`.
  1764. \`\`\`js
  1765. prettier.formatWithCursor(" 1", { cursorOffset: 2 });
  1766. // -> { formatted: '1;\\n', cursorOffset: 1 }
  1767. \`\`\`
  1768. #### \`prettier.resolveConfig([filePath [, options]])\`
  1769. \`resolveConfig\` can be used to resolve configuration for a given source file.
  1770. The function optionally accepts an input file path as an argument, which defaults to the current working directory.
  1771. A promise is returned which will resolve to:
  1772. * An options object, providing a [config file](#configuration-file) was found.
  1773. * \`null\`, if no file was found.
  1774. The promise will be rejected if there was an error parsing the configuration file.
  1775. If \`options.useCache\` is \`false\`, all caching will be bypassed.
  1776. \`\`\`js
  1777. const text = fs.readFileSync(filePath, "utf8");
  1778. prettier.resolveConfig(filePath).then(options => {
  1779. const formatted = prettier.format(text, options);
  1780. })
  1781. \`\`\`
  1782. Use \`prettier.resolveConfig.sync([filePath [, options]])\` if you'd like to use sync version.
  1783. #### \`prettier.clearConfigCache()\`
  1784. As you repeatedly call \`resolveConfig\`, the file system structure will be cached for performance.
  1785. This function will clear the cache. Generally this is only needed for editor integrations that
  1786. know that the file system has changed since the last format took place.
  1787. #### Custom Parser API
  1788. If you need to make modifications to the AST (such as codemods), or you want to provide an alternate parser, you can do so by setting the \`parser\` option to a function. The function signature of the parser function is:
  1789. \`\`\`js
  1790. (text: string, parsers: object, options: object) => AST;
  1791. \`\`\`
  1792. Prettier's built-in parsers are exposed as properties on the \`parsers\` argument.
  1793. \`\`\`js
  1794. prettier.format("lodash ( )", {
  1795. parser(text, { babylon }) {
  1796. const ast = babylon(text);
  1797. ast.program.body[0].expression.callee.name = "_";
  1798. return ast;
  1799. }
  1800. });
  1801. // -> "_();\\n"
  1802. \`\`\`
  1803. The \`--parser\` CLI option may be a path to a node.js module exporting a parse function.
  1804. ### Excluding code from formatting
  1805. A JavaScript comment of \`// prettier-ignore\` will exclude the next node in the abstract syntax tree from formatting.
  1806. For example:
  1807. \`\`\`js
  1808. matrix(
  1809. 1, 0, 0,
  1810. 0, 1, 0,
  1811. 0, 0, 1
  1812. )
  1813. // prettier-ignore
  1814. matrix(
  1815. 1, 0, 0,
  1816. 0, 1, 0,
  1817. 0, 0, 1
  1818. )
  1819. \`\`\`
  1820. will be transformed to:
  1821. \`\`\`js
  1822. matrix(1, 0, 0, 0, 1, 0, 0, 0, 1);
  1823. // prettier-ignore
  1824. matrix(
  1825. 1, 0, 0,
  1826. 0, 1, 0,
  1827. 0, 0, 1
  1828. )
  1829. \`\`\`
  1830. ## Options
  1831. Prettier ships with a handful of customizable format options, usable in both the CLI and API.
  1832. ### Print Width
  1833. Specify the line length that the printer will wrap on.
  1834. > **For readability we recommend against using more than 80 characters:**
  1835. >
  1836. >In code styleguides, maximum line length rules are often set to 100 or 120. However, when humans write code, they don't strive to reach the maximum number of columns on every line. Developers often use whitespace to break up long lines for readability. In practice, the average line length often ends up well below the maximum.
  1837. >
  1838. > Prettier, on the other hand, strives to fit the most code into every line. With the print width set to 120, prettier may produce overly compact, or otherwise undesirable code.
  1839. Default | CLI Override | API Override
  1840. --------|--------------|-------------
  1841. \`80\` | \`--print-width <int>\` | \`printWidth: <int>\`
  1842. ### Tab Width
  1843. Specify the number of spaces per indentation-level.
  1844. Default | CLI Override | API Override
  1845. --------|--------------|-------------
  1846. \`2\` | \`--tab-width <int>\` | \`tabWidth: <int>\`
  1847. ### Tabs
  1848. Indent lines with tabs instead of spaces
  1849. Default | CLI Override | API Override
  1850. --------|--------------|-------------
  1851. \`false\` | \`--use-tabs\` | \`useTabs: <bool>\`
  1852. ### Semicolons
  1853. Print semicolons at the ends of statements.
  1854. Valid options:
  1855. * \`true\` - Add a semicolon at the end of every statement.
  1856. * \`false\` - Only add semicolons at the beginning of lines that may introduce ASI failures.
  1857. Default | CLI Override | API Override
  1858. --------|--------------|-------------
  1859. \`true\` | \`--no-semi\` | \`semi: <bool>\`
  1860. ### Quotes
  1861. Use single quotes instead of double quotes.
  1862. Notes:
  1863. * Quotes in JSX will always be double and ignore this setting.
  1864. * If the number of quotes outweighs the other quote, the quote which is less used will be used to format the string - Example: \`"I'm double quoted"\` results in \`"I'm double quoted"\` and \`"This \\"example\\" is single quoted"\` results in \`'This "example" is single quoted'\`.
  1865. Default | CLI Override | API Override
  1866. --------|--------------|-------------
  1867. \`false\` | \`--single-quote\` | \`singleQuote: <bool>\`
  1868. ### Trailing Commas
  1869. Print trailing commas wherever possible when multi-line. (A single-line array,
  1870. for example, never gets trailing commas.)
  1871. Valid options:
  1872. * \`"none"\` - No trailing commas.
  1873. * \`"es5"\` - Trailing commas where valid in ES5 (objects, arrays, etc.)
  1874. * \`"all"\` - Trailing commas wherever possible (including function arguments). This requires node 8 or a [transform](https://babeljs.io/docs/plugins/syntax-trailing-function-commas/).
  1875. Default | CLI Override | API Override
  1876. --------|--------------|-------------
  1877. \`"none"\` | <code>--trailing-comma <none&#124;es5&#124;all></code> | <code>trailingComma: "<none&#124;es5&#124;all>"</code>
  1878. ### Bracket Spacing
  1879. Print spaces between brackets in object literals.
  1880. Valid options:
  1881. * \`true\` - Example: \`{ foo: bar }\`.
  1882. * \`false\` - Example: \`{foo: bar}\`.
  1883. Default | CLI Override | API Override
  1884. --------|--------------|-------------
  1885. \`true\` | \`--no-bracket-spacing\` | \`bracketSpacing: <bool>\`
  1886. ### JSX Brackets
  1887. Put the \`>\` of a multi-line JSX element at the end of the last line instead of being alone on the next line (does not apply to self closing elements).
  1888. Default | CLI Override | API Override
  1889. --------|--------------|-------------
  1890. \`false\` | \`--jsx-bracket-same-line\` | \`jsxBracketSameLine: <bool>\`
  1891. ### Range
  1892. Format only a segment of a file.
  1893. These two options can be used to format code starting and ending at a given character offset (inclusive and exclusive, respectively). The range will extend:
  1894. * Backwards to the start of the first line containing the selected statement.
  1895. * Forwards to the end of the selected statement.
  1896. These options cannot be used with \`cursorOffset\`.
  1897. Default | CLI Override | API Override
  1898. --------|--------------|-------------
  1899. \`0\` | \`--range-start <int>\`| \`rangeStart: <int>\`
  1900. \`Infinity\` | \`--range-end <int>\` | \`rangeEnd: <int>\`
  1901. ### Parser
  1902. Specify which parser to use.
  1903. Both the \`babylon\` and \`flow\` parsers support the same set of JavaScript features (including Flow). Prettier automatically infers the parser from the input file path, so you shouldn't have to change this setting.
  1904. Built-in parsers:
  1905. * [\`babylon\`](https://github.com/babel/babylon/)
  1906. * [\`flow\`](https://github.com/facebook/flow/tree/master/src/parser)
  1907. * [\`typescript\`](https://github.com/eslint/typescript-eslint-parser) _Since v1.4.0_
  1908. * [\`postcss\`](https://github.com/postcss/postcss) _Since v1.4.0_
  1909. * [\`json\`](https://github.com/babel/babylon/tree/f09eb3200f57ea94d51c2a5b1facf2149fb406bf#babylonparseexpressioncode-options) _Since v1.5.0_
  1910. * [\`graphql\`](https://github.com/graphql/graphql-js/tree/master/src/language) _Since v1.5.0_
  1911. [Custom parsers](#custom-parser-api) are also supported. _Since v1.5.0_
  1912. Default | CLI Override | API Override
  1913. --------|--------------|-------------
  1914. \`babylon\` | \`--parser <string>\`<br />\`--parser ./my-parser\` | \`parser: "<string>"\`<br />\`parser: require("./my-parser")\`
  1915. ### Filepath
  1916. Specify the input filepath. This will be used to do parser inference.
  1917. For example, the following will use \`postcss\` parser:
  1918. \`\`\`bash
  1919. cat foo | prettier --stdin-filepath foo.css
  1920. \`\`\`
  1921. Default | CLI Override | API Override
  1922. --------|--------------|-------------
  1923. None | \`--stdin-filepath <string>\` | \`filepath: "<string>"\`
  1924. ### Require pragma
  1925. Prettier can restrict itself to only format files that contain a special comment, called a pragma, at the top of the file. This is very useful
  1926. when gradually transitioning large, unformatted codebases to prettier.
  1927. For example, a file with the following as its first comment will be formatted when \`--require-pragma\` is supplied:
  1928. \`\`\`js
  1929. /**
  1930. * @prettier
  1931. */
  1932. \`\`\`
  1933. or
  1934. \`\`\`js
  1935. /**
  1936. * @format
  1937. */
  1938. \`\`\`
  1939. Default | CLI Override | API Override
  1940. --------|--------------|-------------
  1941. \`false\` | \`--require-pragma\` | \`requirePragma: <bool>\`
  1942. ## Configuration File
  1943. Prettier uses [cosmiconfig](https://github.com/davidtheclark/cosmiconfig) for configuration file support.
  1944. This means you can configure prettier via:
  1945. * A \`.prettierrc\` file, written in YAML or JSON, with optional extensions: \`.yaml/.yml/.json/.js\`.
  1946. * A \`prettier.config.js\` file that exports an object.
  1947. * A \`"prettier"\` key in your \`package.json\` file.
  1948. The configuration file will be resolved starting from the location of the file being formatted,
  1949. and searching up the file tree until a config file is (or isn't) found.
  1950. The options to the configuration file are the same the [API options](#options).
  1951. ### Basic Configuration
  1952. JSON:
  1953. \`\`\`json
  1954. // .prettierrc
  1955. {
  1956. "printWidth": 100,
  1957. "parser": "flow"
  1958. }
  1959. \`\`\`
  1960. YAML:
  1961. \`\`\`yaml
  1962. # .prettierrc
  1963. printWidth: 100
  1964. parser: flow
  1965. \`\`\`
  1966. ### Configuration Overrides
  1967. Prettier borrows eslint's [override format](http://eslint.org/docs/user-guide/configuring#example-configuration).
  1968. This allows you to apply configuration to specific files.
  1969. JSON:
  1970. \`\`\`json
  1971. {
  1972. "semi": false,
  1973. "overrides": [{
  1974. "files": "*.test.js",
  1975. "options": {
  1976. "semi": true
  1977. }
  1978. }]
  1979. }
  1980. \`\`\`
  1981. YAML:
  1982. \`\`\`yaml
  1983. semi: false
  1984. overrides:
  1985. - files: "*.test.js"
  1986. options:
  1987. semi: true
  1988. \`\`\`
  1989. \`files\` is required for each override, and may be a string or array of strings.
  1990. \`excludeFiles\` may be optionally provided to exclude files for a given rule, and may also be a string or array of strings.
  1991. To get prettier to format its own \`.prettierrc\` file, you can do:
  1992. \`\`\`json
  1993. {
  1994. "overrides": [{
  1995. "files": ".prettierrc",
  1996. "options": { "parser": "json" }
  1997. }]
  1998. }
  1999. \`\`\`
  2000. For more information on how to use the CLI to locate a file, see the [CLI](#cli) section.
  2001. ### Configuration Schema
  2002. If you'd like a JSON schema to validate your configuration, one is available here: http://json.schemastore.org/prettierrc.
  2003. ## Editor Integration
  2004. ### Atom
  2005. Atom users can simply install the [prettier-atom](https://github.com/prettier/prettier-atom) package and use
  2006. \`Ctrl+Alt+F\` to format a file (or format on save if enabled).
  2007. ### Emacs
  2008. Emacs users should see [this repository](https://github.com/prettier/prettier-emacs)
  2009. for on-demand formatting.
  2010. ### Vim
  2011. Vim users can simply install either [sbdchd](https://github.com/sbdchd)/[neoformat](https://github.com/sbdchd/neoformat), [w0rp](https://github.com/w0rp)/[ale](https://github.com/w0rp/ale), or [prettier](https://github.com/prettier)/[vim-prettier](https://github.com/prettier/vim-prettier), for more details see [this directory](https://github.com/prettier/prettier/tree/master/editors/vim).
  2012. ### Visual Studio Code
  2013. Can be installed using the extension sidebar. Search for \`Prettier - JavaScript formatter\`.
  2014. Can also be installed using \`ext install prettier-vscode\`.
  2015. [Check its repository for configuration and shortcuts](https://github.com/prettier/prettier-vscode)
  2016. ### Visual Studio
  2017. Install the [JavaScript Prettier extension](https://github.com/madskristensen/JavaScriptPrettier).
  2018. ### Sublime Text
  2019. Sublime Text support is available through Package Control and
  2020. the [JsPrettier](https://packagecontrol.io/packages/JsPrettier) plug-in.
  2021. ### JetBrains WebStorm, PHPStorm, PyCharm...
  2022. See the [WebStorm
  2023. guide](https://github.com/jlongster/prettier/tree/master/editors/webstorm/README.md).
  2024. ## Language Support
  2025. Prettier attempts to support all JavaScript language features,
  2026. including non-standardized ones. By default it uses the
  2027. [Babylon](https://github.com/babel/babylon) parser with all language
  2028. features enabled, but you can also use the
  2029. [Flow](https://github.com/facebook/flow) parser with the
  2030. \`parser\` API or \`--parser\` CLI [option](#options).
  2031. All of JSX and Flow syntax is supported. In fact, the test suite in
  2032. \`tests/flow\` *is* the entire Flow test suite and they all pass.
  2033. Prettier also supports [TypeScript](https://www.typescriptlang.org/), CSS, [Less](http://lesscss.org/), [SCSS](http://sass-lang.com), [JSON](http://json.org/), and [GraphQL](http://graphql.org/).
  2034. The minimum version of TypeScript supported is 2.1.3 as it introduces the ability to have leading \`|\` for type definitions which prettier outputs.
  2035. ## Related Projects
  2036. - [\`eslint-plugin-prettier\`](https://github.com/prettier/eslint-plugin-prettier) plugs Prettier into your ESLint workflow
  2037. - [\`eslint-config-prettier\`](https://github.com/prettier/eslint-config-prettier) turns off all ESLint rules that are unnecessary or might conflict with Prettier
  2038. - [\`prettier-eslint\`](https://github.com/prettier/prettier-eslint)
  2039. passes \`prettier\` output to \`eslint --fix\`
  2040. - [\`prettier-stylelint\`](https://github.com/hugomrdias/prettier-stylelint)
  2041. passes \`prettier\` output to \`stylelint --fix\`
  2042. - [\`prettier-standard\`](https://github.com/sheerun/prettier-standard)
  2043. uses \`prettier\` and \`prettier-eslint\` to format code with standard rules
  2044. - [\`prettier-standard-formatter\`](https://github.com/dtinth/prettier-standard-formatter)
  2045. passes \`prettier\` output to \`standard --fix\`
  2046. - [\`prettier-miscellaneous\`](https://github.com/arijs/prettier-miscellaneous)
  2047. \`prettier\` with a few minor extra options
  2048. - [\`neutrino-preset-prettier\`](https://github.com/SpencerCDixon/neutrino-preset-prettier) allows you to use Prettier as a Neutrino preset
  2049. - [\`prettier_d\`](https://github.com/josephfrazier/prettier_d.js) runs Prettier as a server to avoid Node.js startup delay. It also supports configuration via \`.prettierrc\`, \`package.json\`, and \`.editorconfig\`.
  2050. - [\`Prettier Bookmarklet\`](https://prettier.glitch.me/) provides a bookmarklet and exposes a REST API for Prettier that allows to format CodeMirror editor in your browser
  2051. - [\`prettier-github\`](https://github.com/jgierer12/prettier-github) formats code in GitHub comments
  2052. - [\`rollup-plugin-prettier\`](https://github.com/mjeanroy/rollup-plugin-prettier) allows you to use Prettier with Rollup
  2053. - [\`markdown-magic-prettier\`](https://github.com/camacho/markdown-magic-prettier) allows you to use Prettier to format JS [codeblocks](https://help.github.com/articles/creating-and-highlighting-code-blocks/) in Markdown files via [Markdown Magic](https://github.com/DavidWells/markdown-magic)
  2054. - [\`tslint-plugin-prettier\`](https://github.com/ikatyang/tslint-plugin-prettier) runs Prettier as a TSLint rule and reports differences as individual TSLint issues
  2055. - [\`tslint-config-prettier\`](https://github.com/alexjoverm/tslint-config-prettier) use TSLint with Prettier without any conflict
  2056. ## Technical Details
  2057. This printer is a fork of
  2058. [recast](https://github.com/benjamn/recast)'s printer with its
  2059. algorithm replaced by the one described by Wadler in "[A prettier
  2060. printer](http://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf)".
  2061. There still may be leftover code from recast that needs to be cleaned
  2062. up.
  2063. The basic idea is that the printer takes an AST and returns an
  2064. intermediate representation of the output, and the printer uses that
  2065. to generate a string. The advantage is that the printer can "measure"
  2066. the IR and see if the output is going to fit on a line, and break if
  2067. not.
  2068. This means that most of the logic of printing an AST involves
  2069. generating an abstract representation of the output involving certain
  2070. commands. For example, \`concat(["(", line, arg, line ")"])\` would
  2071. represent a concatenation of opening parens, an argument, and closing
  2072. parens. But if that doesn't fit on one line, the printer can break
  2073. where \`line\` is specified.
  2074. More (rough) details can be found in [commands.md](commands.md).
  2075. ## Badge
  2076. Show the world you're using *Prettier* → [![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
  2077. \`\`\`md
  2078. [![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
  2079. \`\`\`
  2080. ## Contributing
  2081. See [CONTRIBUTING.md](CONTRIBUTING.md).
  2082. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2083. # Prettier
  2084. [![Gitter](https://badges.gitter.im/gitterHQ/gitter.svg)](https://gitter.im/jlongster/prettier)
  2085. [![Build Status](https://travis-ci.org/prettier/prettier.svg?branch=master)](https://travis-ci.org/prettier/prettier)
  2086. [![Codecov](https://img.shields.io/codecov/c/github/prettier/prettier.svg)](https://codecov.io/gh/prettier/prettier)
  2087. [![NPM version](https://img.shields.io/npm/v/prettier.svg)](https://www.npmjs.com/package/prettier)
  2088. [![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](#badge)
  2089. Prettier is an opinionated code formatter with support for:
  2090. - JavaScript, including
  2091. [ES2017](https://github.com/tc39/proposals/blob/master/finished-proposals.md)
  2092. - [JSX](https://facebook.github.io/jsx/)
  2093. - [Flow](https://flow.org/)
  2094. - [TypeScript](https://www.typescriptlang.org/)
  2095. - CSS, [Less](http://lesscss.org/), and [SCSS](http://sass-lang.com)
  2096. - [JSON](http://json.org/)
  2097. - [GraphQL](http://graphql.org/)
  2098. It removes all original styling[\\*](#styling-footnote) and ensures that all
  2099. outputted code conforms to a consistent style. (See this
  2100. [blog post](http://jlongster.com/A-Prettier-Formatter))
  2101. <details>
  2102. <summary><strong>Table of Contents</strong></summary>
  2103. <!-- Do not edit TOC, regenerate with \`yarn toc\` -->
  2104. <!-- toc -->
  2105. - [What does Prettier do?](#what-does-prettier-do)
  2106. - [Why Prettier?](#why-prettier)
  2107. - [Building and enforcing a style guide](#building-and-enforcing-a-style-guide)
  2108. - [Helping Newcomers](#helping-newcomers)
  2109. - [Writing code](#writing-code)
  2110. - [Easy to adopt](#easy-to-adopt)
  2111. - [Clean up an existing codebase](#clean-up-an-existing-codebase)
  2112. - [Ride the hype train](#ride-the-hype-train)
  2113. - [How does it compare to ESLint (or TSLint, stylelint...)?](#how-does-it-compare-to-eslint-or-tslint-stylelint)
  2114. - [Usage](#usage)
  2115. - [CLI](#cli)
  2116. - [ESLint](#eslint)
  2117. - [Pre-commit Hook](#pre-commit-hook)
  2118. - [API](#api)
  2119. - [Excluding code from formatting](#excluding-code-from-formatting)
  2120. - [Options](#options)
  2121. - [Print Width](#print-width)
  2122. - [Tab Width](#tab-width)
  2123. - [Tabs](#tabs)
  2124. - [Semicolons](#semicolons)
  2125. - [Quotes](#quotes)
  2126. - [Trailing Commas](#trailing-commas)
  2127. - [Bracket Spacing](#bracket-spacing)
  2128. - [JSX Brackets](#jsx-brackets)
  2129. - [Range](#range)
  2130. - [Parser](#parser)
  2131. - [Filepath](#filepath)
  2132. - [Configuration File](#configuration-file)
  2133. - [Basic Configuration](#basic-configuration)
  2134. - [Configuration Overrides](#configuration-overrides)
  2135. - [Configuration Schema](#configuration-schema)
  2136. - [Editor Integration](#editor-integration)
  2137. - [Atom](#atom)
  2138. - [Emacs](#emacs)
  2139. - [Vim](#vim)
  2140. - [Visual Studio Code](#visual-studio-code)
  2141. - [Visual Studio](#visual-studio)
  2142. - [Sublime Text](#sublime-text)
  2143. - [JetBrains WebStorm, PHPStorm, PyCharm...](#jetbrains-webstorm-phpstorm-pycharm)
  2144. - [Language Support](#language-support)
  2145. - [Related Projects](#related-projects)
  2146. - [Technical Details](#technical-details)
  2147. - [Badge](#badge)
  2148. - [Contributing](#contributing)
  2149. <!-- tocstop -->
  2150. </details>
  2151. ---
  2152. ## What does Prettier do?
  2153. Prettier takes your code and reprints it from scratch by taking the line length
  2154. into account.
  2155. For example, take the following code:
  2156. \`\`\`js
  2157. foo(arg1, arg2, arg3, arg4);
  2158. \`\`\`
  2159. It fits in a single line so it's going to stay as is. However, we've all run
  2160. into this situation:
  2161. <!-- prettier-ignore -->
  2162. \`\`\`js
  2163. foo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne());
  2164. \`\`\`
  2165. Suddenly our previous format for calling function breaks down because this is
  2166. too long. Prettier is going to do the painstaking work of reprinting it like
  2167. that for you:
  2168. \`\`\`js
  2169. foo(
  2170. reallyLongArg(),
  2171. omgSoManyParameters(),
  2172. IShouldRefactorThis(),
  2173. isThereSeriouslyAnotherOne()
  2174. );
  2175. \`\`\`
  2176. Prettier enforces a consistent code **style** (i.e. code formatting that won't
  2177. affect the AST) across your entire codebase because it disregards the original
  2178. styling[\\*](#styling-footnote) by parsing it away and re-printing the parsed AST
  2179. with its own rules that take the maximum line length into account, wrapping code
  2180. when necessary.
  2181. <a href="#styling-footnote" name="styling-footnote">\\*</a>_Well actually, some
  2182. original styling is preserved when practical—see [empty lines] and [multi-line
  2183. objects]._
  2184. [empty lines]: Rationale.md#empty-lines
  2185. [multi-line objects]: Rationale.md#multi-line-objects
  2186. If you want to learn more, these two conference talks are great introductions:
  2187. <a href="https://www.youtube.com/watch?v=hkfBvpEfWdA"><img width="298" src="https://cloud.githubusercontent.com/assets/197597/24886367/dda8a6f0-1e08-11e7-865b-22492450f10f.png"></a>
  2188. <a href="https://www.youtube.com/watch?v=0Q4kUNx85_4"><img width="298" src="https://cloud.githubusercontent.com/assets/197597/24886368/ddacd6f8-1e08-11e7-806a-9febd23cbf47.png"></a>
  2189. ## Why Prettier?
  2190. ### Building and enforcing a style guide
  2191. By far the biggest reason for adopting Prettier is to stop all the on-going
  2192. debates over styles. It is generally accepted that having a common style guide
  2193. is valuable for a project and team but getting there is a very painful and
  2194. unrewarding process. People get very emotional around particular ways of writing
  2195. code and nobody likes spending time writing and receiving nits.
  2196. - “We want to free mental threads and end discussions around style. While
  2197. sometimes fruitful, these discussions are for the most part wasteful.”
  2198. - “Literally had an engineer go through a huge effort of cleaning up all of our
  2199. code because we were debating ternary style for the longest time and were
  2200. inconsistent about it. It was dumb, but it was a weird on-going "great debate"
  2201. that wasted lots of little back and forth bits. It's far easier for us all to
  2202. agree now: just run Prettier, and go with that style.”
  2203. - “Getting tired telling people how to style their product code.”
  2204. - “Our top reason was to stop wasting our time debating style nits.”
  2205. - “Having a githook set up has reduced the amount of style issues in PRs that
  2206. result in broken builds due to ESLint rules or things I have to nit-pick or
  2207. clean up later.”
  2208. - “I don't want anybody to nitpick any other person ever again.”
  2209. - “It reminds me of how Steve Jobs used to wear the same clothes every day
  2210. because he has a million decisions to make and he didn't want to be bothered
  2211. to make trivial ones like picking out clothes. I think Prettier is like that.”
  2212. ### Helping Newcomers
  2213. Prettier is usually introduced by people with experience in the current codebase
  2214. and JavaScript but the people that disproportionally benefit from it are
  2215. newcomers to the codebase. One may think that it's only useful for people with
  2216. very limited programming experience, but we've seen it quicken the ramp up time
  2217. from experienced engineers joining the company, as they likely used a different
  2218. coding style before, and developers coming from a different programming
  2219. language.
  2220. - “My motivations for using Prettier are: appearing that I know how to write
  2221. JavaScript well.”
  2222. - “I always put spaces in the wrong place, now I don't have to worry about it
  2223. anymore.”
  2224. - “When you're a beginner you're making a lot of mistakes caused by the syntax.
  2225. Thanks to Prettier, you can reduce these mistakes and save a lot of time to
  2226. focus on what really matters.”
  2227. - “As a teacher, I will also tell to my students to install Prettier to help
  2228. them to learn the JS syntax and have readable files.”
  2229. ### Writing code
  2230. What usually happens once people are using Prettier is that they realize that
  2231. they actually spend a lot of time and mental energy formatting their code. With
  2232. Prettier editor integration, you can just press that magic key binding and poof,
  2233. the code is formatted. This is an eye opening experience if anything else.
  2234. - “I want to write code. Not spend cycles on formatting.”
  2235. - “It removed 5% that sucks in our daily life - aka formatting”
  2236. - “We're in 2017 and it's still painful to break a call into multiple lines when
  2237. you happen to add an argument that makes it go over the 80 columns limit :(“
  2238. ### Easy to adopt
  2239. We've worked very hard to use the least controversial coding styles, went
  2240. through many rounds of fixing all the edge cases and polished the getting
  2241. started experience. When you're ready to push Prettier into your codebase, not
  2242. only should it be painless for you to do it technically but the newly formatted
  2243. codebase should not generate major controversy and be accepted painlessly by
  2244. your co-workers.
  2245. - “It's low overhead. We were able to throw Prettier at very different kinds of
  2246. repos without much work.”
  2247. - “It's been mostly bug free. Had there been major styling issues during the
  2248. course of implementation we would have been wary about throwing this at our JS
  2249. codebase. I'm happy to say that's not the case.”
  2250. - “Everyone runs it as part of their pre commit scripts, a couple of us use the
  2251. editor on save extensions as well.”
  2252. - “It's fast, against one of our larger JS codebases we were able to run
  2253. Prettier in under 13 seconds.”
  2254. - “The biggest benefit for Prettier for us was being able to format the entire
  2255. code base at once.”
  2256. ### Clean up an existing codebase
  2257. Since coming up with a coding style and enforcing it is a big undertaking, it
  2258. often slips through the cracks and you are left working on inconsistent
  2259. codebases. Running Prettier in this case is a quick win, the codebase is now
  2260. uniform and easier to read without spending hardly any time.
  2261. - “Take a look at the code :) I just need to restore sanity.”
  2262. - “We inherited a ~2000 module ES6 code base, developed by 20 different
  2263. developers over 18 months, in a global team. Felt like such a win without much
  2264. research.”
  2265. ### Ride the hype train
  2266. Purely technical aspects of the projects aren't the only thing people look into
  2267. when choosing to adopt Prettier. Who built and uses it and how quickly it
  2268. spreads through the community has a non-trivial impact.
  2269. - “The amazing thing, for me, is: 1) Announced 2 months ago. 2) Already adopted
  2270. by, it seems, every major JS project. 3) 7000 stars, 100,000 npm downloads/mo”
  2271. - “Was built by the same people as React & React Native.”
  2272. - “I like to be part of the hot new things.”
  2273. - “Because soon enough people are gonna ask for it.”
  2274. A few of the [many projects](https://www.npmjs.com/browse/depended/prettier)
  2275. using Prettier:
  2276. <table>
  2277. <tr>
  2278. <td><p align="center"><a href="https://facebook.github.io/react/"><img src="website/static/images/react-200x100.png" alt="React" width="200" height="100"><br>React</a></p></td>
  2279. <td><p align="center"><a href="https://facebook.github.io/jest/"><img src="website/static/images/jest-200x100.png" alt="Jest" width="200" height="100"><br>Jest</a></p></td>
  2280. <td><p align="center"><a href="https://yarnpkg.com"><img src="website/static/images/yarn-200x100.png" alt="Yarn" width="200" height="100"><br>Yarn</a></p></td>
  2281. </tr>
  2282. <tr>
  2283. <td><p align="center"><a href="https://babeljs.io/"><img src="website/static/images/babel-200x100.png" alt="Babel" width="200" height="100"><br>Babel</a></p></td>
  2284. <td><p align="center"><a href="https://zeit.co/"><img src="website/static/images/zeit-200x100.png" alt="Zeit" width="200" height="100"><br>Zeit</a></p></td>
  2285. <td><p align="center"><a href="https://webpack.js.org/api/cli/"><img src="website/static/images/webpack-200x100.png" alt="Webpack-cli" width="200" height="100"><br>Webpack-cli</a></p></td>
  2286. </tr>
  2287. </table>
  2288. ## How does it compare to ESLint (or TSLint, stylelint...)?
  2289. Linters have two categories of rules:
  2290. **Formatting rules**: eg: [max-len](http://eslint.org/docs/rules/max-len),
  2291. [no-mixed-spaces-and-tabs](http://eslint.org/docs/rules/no-mixed-spaces-and-tabs),
  2292. [keyword-spacing](http://eslint.org/docs/rules/keyword-spacing),
  2293. [comma-style](http://eslint.org/docs/rules/comma-style)...
  2294. Prettier alleviates the need for this whole category of rules! Prettier is going
  2295. to reprint the entire program from scratch in a consistent way, so it's not
  2296. possible for the programmer to make a mistake there anymore :)
  2297. **Code-quality rules**: eg
  2298. [no-unused-vars](http://eslint.org/docs/rules/no-unused-vars),
  2299. [no-extra-bind](http://eslint.org/docs/rules/no-extra-bind),
  2300. [no-implicit-globals](http://eslint.org/docs/rules/no-implicit-globals),
  2301. [prefer-promise-reject-errors](http://eslint.org/docs/rules/prefer-promise-reject-errors)...
  2302. Prettier does nothing to help with those kind of rules. They are also the most
  2303. important ones provided by linters as they are likely to catch real bugs with
  2304. your code!
  2305. ## Usage
  2306. Install:
  2307. \`\`\`
  2308. yarn add prettier --dev --exact
  2309. \`\`\`
  2310. You can install it globally if you like:
  2311. \`\`\`
  2312. yarn global add prettier
  2313. \`\`\`
  2314. _We're using \`yarn\` but you can use \`npm\` if you like:_
  2315. \`\`\`
  2316. npm install --save-dev --save-exact prettier
  2317. # or globally
  2318. npm install --global prettier
  2319. \`\`\`
  2320. > We recommend pinning an exact version of prettier in your \`package.json\` as we
  2321. > introduce stylistic changes in patch releases.
  2322. ### CLI
  2323. Run Prettier through the CLI with this script. Run it without any arguments to
  2324. see the [options](#options).
  2325. To format a file in-place, use \`--write\`. You may want to consider committing
  2326. your code before doing that, just in case.
  2327. \`\`\`bash
  2328. prettier [opts] [filename ...]
  2329. \`\`\`
  2330. In practice, this may look something like:
  2331. \`\`\`bash
  2332. prettier --single-quote --trailing-comma es5 --write "{app,__{tests,mocks}__}/**/*.js"
  2333. \`\`\`
  2334. Don't forget the quotes around the globs! The quotes make sure that Prettier
  2335. expands the globs rather than your shell, for cross-platform usage. The
  2336. [glob syntax from the glob module](https://github.com/isaacs/node-glob/blob/master/README.md#glob-primer)
  2337. is used.
  2338. #### \`--debug-check\`
  2339. If you're worried that Prettier will change the correctness of your code, add
  2340. \`--debug-check\` to the command. This will cause Prettier to print an error
  2341. message if it detects that code correctness might have changed. Note that
  2342. \`--write\` cannot be used with \`--debug-check\`.
  2343. #### \`--find-config-path\` and \`--config\`
  2344. If you are repeatedly formatting individual files with \`prettier\`, you will
  2345. incur a small performance cost when prettier attempts to look up a
  2346. [configuration file](#configuration-file). In order to skip this, you may ask
  2347. prettier to find the config file once, and re-use it later on.
  2348. \`\`\`bash
  2349. prettier --find-config-path ./my/file.js
  2350. ./my/.prettierrc
  2351. \`\`\`
  2352. This will provide you with a path to the configuration file, which you can pass
  2353. to \`--config\`:
  2354. \`\`\`bash
  2355. prettier --config ./my/.prettierrc --write ./my/file.js
  2356. \`\`\`
  2357. You can also use \`--config\` if your configuration file lives somewhere where
  2358. prettier cannot find it, such as a \`config/\` directory.
  2359. If you don't have a configuration file, or want to ignore it if it does exist,
  2360. you can pass \`--no-config\` instead.
  2361. #### \`--ignore-path\`
  2362. Path to a file containing patterns that describe files to ignore. By default,
  2363. prettier looks for \`./.prettierignore\`.
  2364. #### \`--require-pragma\`
  2365. Require a special comment, called a pragma, to be present in the file's first
  2366. docblock comment in order for prettier to format it.
  2367. \`\`\`js
  2368. /**
  2369. * @prettier
  2370. */
  2371. \`\`\`
  2372. Valid pragmas are \`@prettier\` and \`@format\`.
  2373. #### \`--list-different\`
  2374. Another useful flag is \`--list-different\` (or \`-l\`) which prints the filenames
  2375. of files that are different from Prettier formatting. If there are differences
  2376. the script errors out, which is useful in a CI scenario.
  2377. \`\`\`bash
  2378. prettier --single-quote --list-different "src/**/*.js"
  2379. \`\`\`
  2380. #### \`--no-config\`
  2381. Do not look for a configuration file. The default settings will be used.
  2382. #### \`--config-precedence\`
  2383. Defines how config file should be evaluated in combination of CLI options.
  2384. **cli-override (default)**
  2385. CLI options take precedence over config file
  2386. **file-override**
  2387. Config file take precedence over CLI options
  2388. **prefer-file**
  2389. If a config file is found will evaluate it and ignore other CLI options. If no
  2390. config file is found CLI options will evaluate as normal.
  2391. This option adds support to editor integrations where users define their default
  2392. configuration but want to respect project specific configuration.
  2393. #### \`--with-node-modules\`
  2394. Prettier CLI will ignore files located in \`node_modules\` directory. To opt-out
  2395. from this behavior use \`--with-node-modules\` flag.
  2396. #### \`--write\`
  2397. This rewrites all processed files in place. This is comparable to the
  2398. \`eslint --fix\` workflow.
  2399. ### ESLint
  2400. If you are using ESLint, integrating Prettier to your workflow is
  2401. straightforward:
  2402. Just add Prettier as an ESLint rule using
  2403. [eslint-plugin-prettier](https://github.com/prettier/eslint-plugin-prettier).
  2404. \`\`\`js
  2405. yarn add --dev prettier eslint-plugin-prettier
  2406. // .eslintrc.json
  2407. {
  2408. "plugins": [
  2409. "prettier"
  2410. ],
  2411. "rules": {
  2412. "prettier/prettier": "error"
  2413. }
  2414. }
  2415. \`\`\`
  2416. We also recommend that you use
  2417. [eslint-config-prettier](https://github.com/prettier/eslint-config-prettier) to
  2418. disable all the existing formatting rules. It's a one liner that can be added
  2419. on-top of any existing ESLint configuration.
  2420. \`\`\`
  2421. $ yarn add --dev eslint-config-prettier
  2422. \`\`\`
  2423. .eslintrc.json:
  2424. \`\`\`json
  2425. {
  2426. "extends": ["prettier"]
  2427. }
  2428. \`\`\`
  2429. ### Pre-commit Hook
  2430. You can use Prettier with a pre-commit tool. This can re-format your files that
  2431. are marked as "staged" via \`git add\` before you commit.
  2432. ##### Option 1. [lint-staged](https://github.com/okonet/lint-staged)
  2433. Install it along with [husky](https://github.com/typicode/husky):
  2434. \`\`\`bash
  2435. yarn add lint-staged husky --dev
  2436. \`\`\`
  2437. and add this config to your \`package.json\`:
  2438. \`\`\`json
  2439. {
  2440. "scripts": {
  2441. "precommit": "lint-staged"
  2442. },
  2443. "lint-staged": {
  2444. "*.{js,json,css}": ["prettier --write", "git add"]
  2445. }
  2446. }
  2447. \`\`\`
  2448. There is a limitation where if you stage specific lines this approach will stage
  2449. the whole file after regardless. See this
  2450. [issue](https://github.com/okonet/lint-staged/issues/62) for more info.
  2451. See https://github.com/okonet/lint-staged#configuration for more details about
  2452. how you can configure lint-staged.
  2453. ##### Option 2. [pre-commit](https://github.com/pre-commit/pre-commit)
  2454. Copy the following config into your \`.pre-commit-config.yaml\` file:
  2455. \`\`\`yaml
  2456. - repo: https://github.com/prettier/prettier
  2457. sha: '' # Use the sha or tag you want to point at
  2458. hooks:
  2459. - id: prettier
  2460. \`\`\`
  2461. Find more info from [here](https://pre-commit.com).
  2462. ##### Option 3. bash script
  2463. Alternately you can save this script as \`.git/hooks/pre-commit\` and give it
  2464. execute permission:
  2465. \`\`\`bash
  2466. #!/bin/sh
  2467. jsfiles=$(git diff --cached --name-only --diff-filter=ACM | grep '\\.jsx\\?$' | tr '\\n' ' ')
  2468. [ -z "$jsfiles" ] && exit 0
  2469. # Prettify all staged .js files
  2470. echo "$jsfiles" | xargs ./node_modules/.bin/prettier --write
  2471. # Add back the modified/prettified files to staging
  2472. echo "$jsfiles" | xargs git add
  2473. exit 0
  2474. \`\`\`
  2475. ### API
  2476. \`\`\`js
  2477. const prettier = require('prettier');
  2478. \`\`\`
  2479. #### \`prettier.format(source [, options])\`
  2480. \`format\` is used to format text using Prettier. [Options](#options) may be
  2481. provided to override the defaults.
  2482. \`\`\`js
  2483. prettier.format('foo ( );', { semi: false });
  2484. // -> "foo()"
  2485. \`\`\`
  2486. #### \`prettier.check(source [, options])\`
  2487. \`check\` checks to see if the file has been formatted with Prettier given those
  2488. options and returns a \`Boolean\`. This is similar to the \`--list-different\`
  2489. parameter in the CLI and is useful for running Prettier in CI scenarios.
  2490. #### \`prettier.formatWithCursor(source [, options])\`
  2491. \`formatWithCursor\` both formats the code, and translates a cursor position from
  2492. unformatted code to formatted code. This is useful for editor integrations, to
  2493. prevent the cursor from moving when code is formatted.
  2494. The \`cursorOffset\` option should be provided, to specify where the cursor is.
  2495. This option cannot be used with \`rangeStart\` and \`rangeEnd\`.
  2496. \`\`\`js
  2497. prettier.formatWithCursor(' 1', { cursorOffset: 2 });
  2498. // -> { formatted: '1;\\n', cursorOffset: 1 }
  2499. \`\`\`
  2500. #### \`prettier.resolveConfig([filePath [, options]])\`
  2501. \`resolveConfig\` can be used to resolve configuration for a given source file.
  2502. The function optionally accepts an input file path as an argument, which
  2503. defaults to the current working directory. A promise is returned which will
  2504. resolve to:
  2505. - An options object, providing a [config file](#configuration-file) was found.
  2506. - \`null\`, if no file was found.
  2507. The promise will be rejected if there was an error parsing the configuration
  2508. file.
  2509. If \`options.useCache\` is \`false\`, all caching will be bypassed.
  2510. \`\`\`js
  2511. const text = fs.readFileSync(filePath, 'utf8');
  2512. prettier.resolveConfig(filePath).then(options => {
  2513. const formatted = prettier.format(text, options);
  2514. });
  2515. \`\`\`
  2516. Use \`prettier.resolveConfig.sync([filePath [, options]])\` if you'd like to use
  2517. sync version.
  2518. #### \`prettier.clearConfigCache()\`
  2519. As you repeatedly call \`resolveConfig\`, the file system structure will be cached
  2520. for performance. This function will clear the cache. Generally this is only
  2521. needed for editor integrations that know that the file system has changed since
  2522. the last format took place.
  2523. #### Custom Parser API
  2524. If you need to make modifications to the AST (such as codemods), or you want to
  2525. provide an alternate parser, you can do so by setting the \`parser\` option to a
  2526. function. The function signature of the parser function is:
  2527. \`\`\`js
  2528. (text: string, parsers: object, options: object) => AST;
  2529. \`\`\`
  2530. Prettier's built-in parsers are exposed as properties on the \`parsers\` argument.
  2531. \`\`\`js
  2532. prettier.format('lodash ( )', {
  2533. parser(text, { babylon }) {
  2534. const ast = babylon(text);
  2535. ast.program.body[0].expression.callee.name = '_';
  2536. return ast;
  2537. }
  2538. });
  2539. // -> "_();\\n"
  2540. \`\`\`
  2541. The \`--parser\` CLI option may be a path to a node.js module exporting a parse
  2542. function.
  2543. ### Excluding code from formatting
  2544. A JavaScript comment of \`// prettier-ignore\` will exclude the next node in the
  2545. abstract syntax tree from formatting.
  2546. For example:
  2547. \`\`\`js
  2548. matrix(1, 0, 0, 0, 1, 0, 0, 0, 1);
  2549. // prettier-ignore
  2550. matrix(
  2551. 1, 0, 0,
  2552. 0, 1, 0,
  2553. 0, 0, 1
  2554. )
  2555. \`\`\`
  2556. will be transformed to:
  2557. \`\`\`js
  2558. matrix(1, 0, 0, 0, 1, 0, 0, 0, 1);
  2559. // prettier-ignore
  2560. matrix(
  2561. 1, 0, 0,
  2562. 0, 1, 0,
  2563. 0, 0, 1
  2564. )
  2565. \`\`\`
  2566. ## Options
  2567. Prettier ships with a handful of customizable format options, usable in both the
  2568. CLI and API.
  2569. ### Print Width
  2570. Specify the line length that the printer will wrap on.
  2571. > **For readability we recommend against using more than 80 characters:**
  2572. >
  2573. > In code styleguides, maximum line length rules are often set to 100 or 120.
  2574. > However, when humans write code, they don't strive to reach the maximum number
  2575. > of columns on every line. Developers often use whitespace to break up long
  2576. > lines for readability. In practice, the average line length often ends up well
  2577. > below the maximum.
  2578. >
  2579. > Prettier, on the other hand, strives to fit the most code into every line.
  2580. > With the print width set to 120, prettier may produce overly compact, or
  2581. > otherwise undesirable code.
  2582. | Default | CLI Override | API Override |
  2583. | ------- | --------------------- | ------------------- |
  2584. | \`80\` | \`--print-width <int>\` | \`printWidth: <int>\` |
  2585. ### Tab Width
  2586. Specify the number of spaces per indentation-level.
  2587. | Default | CLI Override | API Override |
  2588. | ------- | ------------------- | ----------------- |
  2589. | \`2\` | \`--tab-width <int>\` | \`tabWidth: <int>\` |
  2590. ### Tabs
  2591. Indent lines with tabs instead of spaces
  2592. | Default | CLI Override | API Override |
  2593. | ------- | ------------ | ----------------- |
  2594. | \`false\` | \`--use-tabs\` | \`useTabs: <bool>\` |
  2595. ### Semicolons
  2596. Print semicolons at the ends of statements.
  2597. Valid options:
  2598. - \`true\` - Add a semicolon at the end of every statement.
  2599. - \`false\` - Only add semicolons at the beginning of lines that may introduce ASI
  2600. failures.
  2601. | Default | CLI Override | API Override |
  2602. | ------- | ------------ | -------------- |
  2603. | \`true\` | \`--no-semi\` | \`semi: <bool>\` |
  2604. ### Quotes
  2605. Use single quotes instead of double quotes.
  2606. Notes:
  2607. - Quotes in JSX will always be double and ignore this setting.
  2608. - If the number of quotes outweighs the other quote, the quote which is less
  2609. used will be used to format the string - Example: \`"I'm double quoted"\`
  2610. results in \`"I'm double quoted"\` and \`"This \\"example\\" is single quoted"\`
  2611. results in \`'This "example" is single quoted'\`.
  2612. | Default | CLI Override | API Override |
  2613. | ------- | ---------------- | --------------------- |
  2614. | \`false\` | \`--single-quote\` | \`singleQuote: <bool>\` |
  2615. ### Trailing Commas
  2616. Print trailing commas wherever possible when multi-line. (A single-line array,
  2617. for example, never gets trailing commas.)
  2618. Valid options:
  2619. - \`"none"\` - No trailing commas.
  2620. - \`"es5"\` - Trailing commas where valid in ES5 (objects, arrays, etc.)
  2621. - \`"all"\` - Trailing commas wherever possible (including function arguments).
  2622. This requires node 8 or a
  2623. [transform](https://babeljs.io/docs/plugins/syntax-trailing-function-commas/).
  2624. | Default | CLI Override | API Override |
  2625. | -------- | ------------------------------------------------------ | ------------------------------------------------------ |
  2626. | \`"none"\` | <code>--trailing-comma <none&#124;es5&#124;all></code> | <code>trailingComma: "<none&#124;es5&#124;all>"</code> |
  2627. ### Bracket Spacing
  2628. Print spaces between brackets in object literals.
  2629. Valid options:
  2630. - \`true\` - Example: \`{ foo: bar }\`.
  2631. - \`false\` - Example: \`{foo: bar}\`.
  2632. | Default | CLI Override | API Override |
  2633. | ------- | ---------------------- | ------------------------ |
  2634. | \`true\` | \`--no-bracket-spacing\` | \`bracketSpacing: <bool>\` |
  2635. ### JSX Brackets
  2636. Put the \`>\` of a multi-line JSX element at the end of the last line instead of
  2637. being alone on the next line (does not apply to self closing elements).
  2638. | Default | CLI Override | API Override |
  2639. | ------- | ------------------------- | ---------------------------- |
  2640. | \`false\` | \`--jsx-bracket-same-line\` | \`jsxBracketSameLine: <bool>\` |
  2641. ### Range
  2642. Format only a segment of a file.
  2643. These two options can be used to format code starting and ending at a given
  2644. character offset (inclusive and exclusive, respectively). The range will extend:
  2645. - Backwards to the start of the first line containing the selected statement.
  2646. - Forwards to the end of the selected statement.
  2647. These options cannot be used with \`cursorOffset\`.
  2648. | Default | CLI Override | API Override |
  2649. | ---------- | --------------------- | ------------------- |
  2650. | \`0\` | \`--range-start <int>\` | \`rangeStart: <int>\` |
  2651. | \`Infinity\` | \`--range-end <int>\` | \`rangeEnd: <int>\` |
  2652. ### Parser
  2653. Specify which parser to use.
  2654. Both the \`babylon\` and \`flow\` parsers support the same set of JavaScript
  2655. features (including Flow). Prettier automatically infers the parser from the
  2656. input file path, so you shouldn't have to change this setting.
  2657. Built-in parsers:
  2658. - [\`babylon\`](https://github.com/babel/babylon/)
  2659. - [\`flow\`](https://github.com/facebook/flow/tree/master/src/parser)
  2660. - [\`typescript\`](https://github.com/eslint/typescript-eslint-parser) _Since
  2661. v1.4.0_
  2662. - [\`postcss\`](https://github.com/postcss/postcss) _Since v1.4.0_
  2663. - [\`json\`](https://github.com/babel/babylon/tree/f09eb3200f57ea94d51c2a5b1facf2149fb406bf#babylonparseexpressioncode-options)
  2664. _Since v1.5.0_
  2665. - [\`graphql\`](https://github.com/graphql/graphql-js/tree/master/src/language)
  2666. _Since v1.5.0_
  2667. [Custom parsers](#custom-parser-api) are also supported. _Since v1.5.0_
  2668. | Default | CLI Override | API Override |
  2669. | --------- | ----------------------------------------------- | ---------------------------------------------------------- |
  2670. | \`babylon\` | \`--parser <string>\`<br />\`--parser ./my-parser\` | \`parser: "<string>"\`<br />\`parser: require("./my-parser")\` |
  2671. ### Filepath
  2672. Specify the input filepath. This will be used to do parser inference.
  2673. For example, the following will use \`postcss\` parser:
  2674. \`\`\`bash
  2675. cat foo | prettier --stdin-filepath foo.css
  2676. \`\`\`
  2677. | Default | CLI Override | API Override |
  2678. | ------- | --------------------------- | ---------------------- |
  2679. | None | \`--stdin-filepath <string>\` | \`filepath: "<string>"\` |
  2680. ### Require pragma
  2681. Prettier can restrict itself to only format files that contain a special
  2682. comment, called a pragma, at the top of the file. This is very useful when
  2683. gradually transitioning large, unformatted codebases to prettier.
  2684. For example, a file with the following as its first comment will be formatted
  2685. when \`--require-pragma\` is supplied:
  2686. \`\`\`js
  2687. /**
  2688. * @prettier
  2689. */
  2690. \`\`\`
  2691. or
  2692. \`\`\`js
  2693. /**
  2694. * @format
  2695. */
  2696. \`\`\`
  2697. | Default | CLI Override | API Override |
  2698. | ------- | ------------------ | ----------------------- |
  2699. | \`false\` | \`--require-pragma\` | \`requirePragma: <bool>\` |
  2700. ## Configuration File
  2701. Prettier uses [cosmiconfig](https://github.com/davidtheclark/cosmiconfig) for
  2702. configuration file support. This means you can configure prettier via:
  2703. - A \`.prettierrc\` file, written in YAML or JSON, with optional extensions:
  2704. \`.yaml/.yml/.json/.js\`.
  2705. - A \`prettier.config.js\` file that exports an object.
  2706. - A \`"prettier"\` key in your \`package.json\` file.
  2707. The configuration file will be resolved starting from the location of the file
  2708. being formatted, and searching up the file tree until a config file is (or
  2709. isn't) found.
  2710. The options to the configuration file are the same the [API options](#options).
  2711. ### Basic Configuration
  2712. JSON:
  2713. \`\`\`json
  2714. // .prettierrc
  2715. {
  2716. "printWidth": 100,
  2717. "parser": "flow"
  2718. }
  2719. \`\`\`
  2720. YAML:
  2721. \`\`\`yaml
  2722. # .prettierrc
  2723. printWidth: 100
  2724. parser: flow
  2725. \`\`\`
  2726. ### Configuration Overrides
  2727. Prettier borrows eslint's
  2728. [override format](http://eslint.org/docs/user-guide/configuring#example-configuration).
  2729. This allows you to apply configuration to specific files.
  2730. JSON:
  2731. \`\`\`json
  2732. {
  2733. "semi": false,
  2734. "overrides": [
  2735. {
  2736. "files": "*.test.js",
  2737. "options": {
  2738. "semi": true
  2739. }
  2740. }
  2741. ]
  2742. }
  2743. \`\`\`
  2744. YAML:
  2745. \`\`\`yaml
  2746. semi: false
  2747. overrides:
  2748. - files: "*.test.js"
  2749. options:
  2750. semi: true
  2751. \`\`\`
  2752. \`files\` is required for each override, and may be a string or array of strings.
  2753. \`excludeFiles\` may be optionally provided to exclude files for a given rule, and
  2754. may also be a string or array of strings.
  2755. To get prettier to format its own \`.prettierrc\` file, you can do:
  2756. \`\`\`json
  2757. {
  2758. "overrides": [
  2759. {
  2760. "files": ".prettierrc",
  2761. "options": { "parser": "json" }
  2762. }
  2763. ]
  2764. }
  2765. \`\`\`
  2766. For more information on how to use the CLI to locate a file, see the [CLI](#cli)
  2767. section.
  2768. ### Configuration Schema
  2769. If you'd like a JSON schema to validate your configuration, one is available
  2770. here: http://json.schemastore.org/prettierrc.
  2771. ## Editor Integration
  2772. ### Atom
  2773. Atom users can simply install the
  2774. [prettier-atom](https://github.com/prettier/prettier-atom) package and use
  2775. \`Ctrl+Alt+F\` to format a file (or format on save if enabled).
  2776. ### Emacs
  2777. Emacs users should see
  2778. [this repository](https://github.com/prettier/prettier-emacs) for on-demand
  2779. formatting.
  2780. ### Vim
  2781. Vim users can simply install either
  2782. [sbdchd](https://github.com/sbdchd)/[neoformat](https://github.com/sbdchd/neoformat),
  2783. [w0rp](https://github.com/w0rp)/[ale](https://github.com/w0rp/ale), or
  2784. [prettier](https://github.com/prettier)/[vim-prettier](https://github.com/prettier/vim-prettier),
  2785. for more details see
  2786. [this directory](https://github.com/prettier/prettier/tree/master/editors/vim).
  2787. ### Visual Studio Code
  2788. Can be installed using the extension sidebar. Search for
  2789. \`Prettier - JavaScript formatter\`.
  2790. Can also be installed using \`ext install prettier-vscode\`.
  2791. [Check its repository for configuration and shortcuts](https://github.com/prettier/prettier-vscode)
  2792. ### Visual Studio
  2793. Install the
  2794. [JavaScript Prettier extension](https://github.com/madskristensen/JavaScriptPrettier).
  2795. ### Sublime Text
  2796. Sublime Text support is available through Package Control and the
  2797. [JsPrettier](https://packagecontrol.io/packages/JsPrettier) plug-in.
  2798. ### JetBrains WebStorm, PHPStorm, PyCharm...
  2799. See the
  2800. [WebStorm guide](https://github.com/jlongster/prettier/tree/master/editors/webstorm/README.md).
  2801. ## Language Support
  2802. Prettier attempts to support all JavaScript language features, including
  2803. non-standardized ones. By default it uses the
  2804. [Babylon](https://github.com/babel/babylon) parser with all language features
  2805. enabled, but you can also use the [Flow](https://github.com/facebook/flow)
  2806. parser with the \`parser\` API or \`--parser\` CLI [option](#options).
  2807. All of JSX and Flow syntax is supported. In fact, the test suite in \`tests/flow\`
  2808. _is_ the entire Flow test suite and they all pass.
  2809. Prettier also supports [TypeScript](https://www.typescriptlang.org/), CSS,
  2810. [Less](http://lesscss.org/), [SCSS](http://sass-lang.com),
  2811. [JSON](http://json.org/), and [GraphQL](http://graphql.org/).
  2812. The minimum version of TypeScript supported is 2.1.3 as it introduces the
  2813. ability to have leading \`|\` for type definitions which prettier outputs.
  2814. ## Related Projects
  2815. - [\`eslint-plugin-prettier\`](https://github.com/prettier/eslint-plugin-prettier)
  2816. plugs Prettier into your ESLint workflow
  2817. - [\`eslint-config-prettier\`](https://github.com/prettier/eslint-config-prettier)
  2818. turns off all ESLint rules that are unnecessary or might conflict with
  2819. Prettier
  2820. - [\`prettier-eslint\`](https://github.com/prettier/prettier-eslint) passes
  2821. \`prettier\` output to \`eslint --fix\`
  2822. - [\`prettier-stylelint\`](https://github.com/hugomrdias/prettier-stylelint)
  2823. passes \`prettier\` output to \`stylelint --fix\`
  2824. - [\`prettier-standard\`](https://github.com/sheerun/prettier-standard) uses
  2825. \`prettier\` and \`prettier-eslint\` to format code with standard rules
  2826. - [\`prettier-standard-formatter\`](https://github.com/dtinth/prettier-standard-formatter)
  2827. passes \`prettier\` output to \`standard --fix\`
  2828. - [\`prettier-miscellaneous\`](https://github.com/arijs/prettier-miscellaneous)
  2829. \`prettier\` with a few minor extra options
  2830. - [\`neutrino-preset-prettier\`](https://github.com/SpencerCDixon/neutrino-preset-prettier)
  2831. allows you to use Prettier as a Neutrino preset
  2832. - [\`prettier_d\`](https://github.com/josephfrazier/prettier_d.js) runs Prettier
  2833. as a server to avoid Node.js startup delay. It also supports configuration via
  2834. \`.prettierrc\`, \`package.json\`, and \`.editorconfig\`.
  2835. - [\`Prettier Bookmarklet\`](https://prettier.glitch.me/) provides a bookmarklet
  2836. and exposes a REST API for Prettier that allows to format CodeMirror editor in
  2837. your browser
  2838. - [\`prettier-github\`](https://github.com/jgierer12/prettier-github) formats code
  2839. in GitHub comments
  2840. - [\`rollup-plugin-prettier\`](https://github.com/mjeanroy/rollup-plugin-prettier)
  2841. allows you to use Prettier with Rollup
  2842. - [\`markdown-magic-prettier\`](https://github.com/camacho/markdown-magic-prettier)
  2843. allows you to use Prettier to format JS
  2844. [codeblocks](https://help.github.com/articles/creating-and-highlighting-code-blocks/)
  2845. in Markdown files via
  2846. [Markdown Magic](https://github.com/DavidWells/markdown-magic)
  2847. - [\`tslint-plugin-prettier\`](https://github.com/ikatyang/tslint-plugin-prettier)
  2848. runs Prettier as a TSLint rule and reports differences as individual TSLint
  2849. issues
  2850. - [\`tslint-config-prettier\`](https://github.com/alexjoverm/tslint-config-prettier)
  2851. use TSLint with Prettier without any conflict
  2852. ## Technical Details
  2853. This printer is a fork of [recast](https://github.com/benjamn/recast)'s printer
  2854. with its algorithm replaced by the one described by Wadler in
  2855. "[A prettier printer](http://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf)".
  2856. There still may be leftover code from recast that needs to be cleaned up.
  2857. The basic idea is that the printer takes an AST and returns an intermediate
  2858. representation of the output, and the printer uses that to generate a string.
  2859. The advantage is that the printer can "measure" the IR and see if the output is
  2860. going to fit on a line, and break if not.
  2861. This means that most of the logic of printing an AST involves generating an
  2862. abstract representation of the output involving certain commands. For example,
  2863. \`concat(["(", line, arg, line ")"])\` would represent a concatenation of opening
  2864. parens, an argument, and closing parens. But if that doesn't fit on one line,
  2865. the printer can break where \`line\` is specified.
  2866. More (rough) details can be found in [commands.md](commands.md).
  2867. ## Badge
  2868. Show the world you're using _Prettier_ →
  2869. [![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
  2870. \`\`\`md
  2871. [![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
  2872. \`\`\`
  2873. ## Contributing
  2874. See [CONTRIBUTING.md](CONTRIBUTING.md).
  2875. `;
  2876. exports[`test-case.md 1`] = `
  2877. [View raw (TEST.md)](https://raw.github.com/adamschwartz/github-markdown-kitchen-sink/master/README.md)
  2878. This is a paragraph.
  2879. This is a paragraph.
  2880. Header 1
  2881. ========
  2882. Header 2
  2883. --------
  2884. Header 1
  2885. ========
  2886. Header 2
  2887. --------
  2888. # Header 1
  2889. ## Header 2
  2890. ### Header 3
  2891. #### Header 4
  2892. ##### Header 5
  2893. ###### Header 6
  2894. # Header 1
  2895. ## Header 2
  2896. ### Header 3
  2897. #### Header 4
  2898. ##### Header 5
  2899. ###### Header 6
  2900. # Header 1 #
  2901. ## Header 2 ##
  2902. ### Header 3 ###
  2903. #### Header 4 ####
  2904. ##### Header 5 #####
  2905. ###### Header 6 ######
  2906. # Header 1 #
  2907. ## Header 2 ##
  2908. ### Header 3 ###
  2909. #### Header 4 ####
  2910. ##### Header 5 #####
  2911. ###### Header 6 ######
  2912. > Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.
  2913. > Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.
  2914. > ## This is a header.
  2915. > 1. This is the first list item.
  2916. > 2. This is the second list item.
  2917. >
  2918. > Here's some example code:
  2919. >
  2920. > Markdown.generate();
  2921. > ## This is a header.
  2922. > 1. This is the first list item.
  2923. > 2. This is the second list item.
  2924. >
  2925. > Here's some example code:
  2926. >
  2927. > Markdown.generate();
  2928. - Red
  2929. - Green
  2930. - Blue
  2931. + Red
  2932. + Green
  2933. + Blue
  2934. * Red
  2935. * Green
  2936. * Blue
  2937. \`\`\`markdown
  2938. - Red
  2939. - Green
  2940. - Blue
  2941. + Red
  2942. + Green
  2943. + Blue
  2944. * Red
  2945. * Green
  2946. * Blue
  2947. \`\`\`
  2948. 1. Buy flour and salt
  2949. 1. Mix together with water
  2950. 1. Bake
  2951. \`\`\`markdown
  2952. 1. Buy flour and salt
  2953. 1. Mix together with water
  2954. 1. Bake
  2955. \`\`\`
  2956. Paragraph:
  2957. Code
  2958. <!-- -->
  2959. Paragraph:
  2960. Code
  2961. * * *
  2962. ***
  2963. *****
  2964. - - -
  2965. ---------------------------------------
  2966. * * *
  2967. ***
  2968. *****
  2969. - - -
  2970. ---------------------------------------
  2971. This is [an example](http://example.com "Example") link.
  2972. [This link](http://example.com) has no title attr.
  2973. This is [an example] [id] reference-style link.
  2974. [id]: http://example.com "Optional Title"
  2975. This is [an example](http://example.com "Example") link.
  2976. [This link](http://example.com) has no title attr.
  2977. This is [an example] [id] reference-style link.
  2978. [id]: http://example.com "Optional Title"
  2979. *single asterisks*
  2980. _single underscores_
  2981. **double asterisks**
  2982. __double underscores__
  2983. *single asterisks*
  2984. _single underscores_
  2985. **double asterisks**
  2986. __double underscores__
  2987. This paragraph has some \`code\` in it.
  2988. This paragraph has some \`code\` in it.
  2989. ![Alt Text](http://placehold.it/200x50 "Image Title")
  2990. ![Alt Text](http://placehold.it/200x50 "Image Title")
  2991. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2992. [View raw (TEST.md)](https://raw.github.com/adamschwartz/github-markdown-kitchen-sink/master/README.md)
  2993. This is a paragraph.
  2994. This is a paragraph.
  2995. # Header 1
  2996. ## Header 2
  2997. Header 1
  2998. ========
  2999. Header 2
  3000. --------
  3001. # Header 1
  3002. ## Header 2
  3003. ### Header 3
  3004. #### Header 4
  3005. ##### Header 5
  3006. ###### Header 6
  3007. # Header 1
  3008. ## Header 2
  3009. ### Header 3
  3010. #### Header 4
  3011. ##### Header 5
  3012. ###### Header 6
  3013. # Header 1
  3014. ## Header 2
  3015. ### Header 3
  3016. #### Header 4
  3017. ##### Header 5
  3018. ###### Header 6
  3019. # Header 1 #
  3020. ## Header 2 ##
  3021. ### Header 3 ###
  3022. #### Header 4 ####
  3023. ##### Header 5 #####
  3024. ###### Header 6 ######
  3025. > Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam hendrerit mi
  3026. > posuere lectus. Vestibulum enim wisi, viverra nec, fringilla in, laoreet
  3027. > vitae, risus.
  3028. > Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.
  3029. > ## This is a header.
  3030. >
  3031. > 1. This is the first list item.
  3032. > 2. This is the second list item.
  3033. >
  3034. > Here's some example code:
  3035. >
  3036. > Markdown.generate();
  3037. > ## This is a header.
  3038. > 1. This is the first list item.
  3039. > 2. This is the second list item.
  3040. >
  3041. > Here's some example code:
  3042. >
  3043. > Markdown.generate();
  3044. - Red
  3045. - Green
  3046. - Blue
  3047. * Red
  3048. * Green
  3049. * Blue
  3050. - Red
  3051. - Green
  3052. - Blue
  3053. \`\`\`markdown
  3054. - Red
  3055. - Green
  3056. - Blue
  3057. * Red
  3058. * Green
  3059. * Blue
  3060. - Red
  3061. - Green
  3062. - Blue
  3063. \`\`\`
  3064. 1. Buy flour and salt
  3065. 1. Mix together with water
  3066. 1. Bake
  3067. \`\`\`markdown
  3068. 1. Buy flour and salt
  3069. 1. Mix together with water
  3070. 1. Bake
  3071. \`\`\`
  3072. Paragraph:
  3073. Code
  3074. <!-- -->
  3075. Paragraph:
  3076. Code
  3077. ---
  3078. ---
  3079. ---
  3080. ---
  3081. ---
  3082. * * *
  3083. ***
  3084. *****
  3085. - - -
  3086. ---------------------------------------
  3087. This is [an example](http://example.com "Example") link.
  3088. [This link](http://example.com) has no title attr.
  3089. This is [an example][id] reference-style link.
  3090. [id]: http://example.com "Optional Title"
  3091. This is [an example](http://example.com "Example") link.
  3092. [This link](http://example.com) has no title attr.
  3093. This is [an example] [id] reference-style link.
  3094. [id]: http://example.com "Optional Title"
  3095. _single asterisks_
  3096. _single underscores_
  3097. **double asterisks**
  3098. **double underscores**
  3099. *single asterisks*
  3100. _single underscores_
  3101. **double asterisks**
  3102. __double underscores__
  3103. This paragraph has some \`code\` in it.
  3104. This paragraph has some \`code\` in it.
  3105. ![Alt Text](http://placehold.it/200x50 "Image Title")
  3106. ![Alt Text](http://placehold.it/200x50 "Image Title")
  3107. `;
  3108. exports[`test-case.md 2`] = `
  3109. [View raw (TEST.md)](https://raw.github.com/adamschwartz/github-markdown-kitchen-sink/master/README.md)
  3110. This is a paragraph.
  3111. This is a paragraph.
  3112. Header 1
  3113. ========
  3114. Header 2
  3115. --------
  3116. Header 1
  3117. ========
  3118. Header 2
  3119. --------
  3120. # Header 1
  3121. ## Header 2
  3122. ### Header 3
  3123. #### Header 4
  3124. ##### Header 5
  3125. ###### Header 6
  3126. # Header 1
  3127. ## Header 2
  3128. ### Header 3
  3129. #### Header 4
  3130. ##### Header 5
  3131. ###### Header 6
  3132. # Header 1 #
  3133. ## Header 2 ##
  3134. ### Header 3 ###
  3135. #### Header 4 ####
  3136. ##### Header 5 #####
  3137. ###### Header 6 ######
  3138. # Header 1 #
  3139. ## Header 2 ##
  3140. ### Header 3 ###
  3141. #### Header 4 ####
  3142. ##### Header 5 #####
  3143. ###### Header 6 ######
  3144. > Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.
  3145. > Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.
  3146. > ## This is a header.
  3147. > 1. This is the first list item.
  3148. > 2. This is the second list item.
  3149. >
  3150. > Here's some example code:
  3151. >
  3152. > Markdown.generate();
  3153. > ## This is a header.
  3154. > 1. This is the first list item.
  3155. > 2. This is the second list item.
  3156. >
  3157. > Here's some example code:
  3158. >
  3159. > Markdown.generate();
  3160. - Red
  3161. - Green
  3162. - Blue
  3163. + Red
  3164. + Green
  3165. + Blue
  3166. * Red
  3167. * Green
  3168. * Blue
  3169. \`\`\`markdown
  3170. - Red
  3171. - Green
  3172. - Blue
  3173. + Red
  3174. + Green
  3175. + Blue
  3176. * Red
  3177. * Green
  3178. * Blue
  3179. \`\`\`
  3180. 1. Buy flour and salt
  3181. 1. Mix together with water
  3182. 1. Bake
  3183. \`\`\`markdown
  3184. 1. Buy flour and salt
  3185. 1. Mix together with water
  3186. 1. Bake
  3187. \`\`\`
  3188. Paragraph:
  3189. Code
  3190. <!-- -->
  3191. Paragraph:
  3192. Code
  3193. * * *
  3194. ***
  3195. *****
  3196. - - -
  3197. ---------------------------------------
  3198. * * *
  3199. ***
  3200. *****
  3201. - - -
  3202. ---------------------------------------
  3203. This is [an example](http://example.com "Example") link.
  3204. [This link](http://example.com) has no title attr.
  3205. This is [an example] [id] reference-style link.
  3206. [id]: http://example.com "Optional Title"
  3207. This is [an example](http://example.com "Example") link.
  3208. [This link](http://example.com) has no title attr.
  3209. This is [an example] [id] reference-style link.
  3210. [id]: http://example.com "Optional Title"
  3211. *single asterisks*
  3212. _single underscores_
  3213. **double asterisks**
  3214. __double underscores__
  3215. *single asterisks*
  3216. _single underscores_
  3217. **double asterisks**
  3218. __double underscores__
  3219. This paragraph has some \`code\` in it.
  3220. This paragraph has some \`code\` in it.
  3221. ![Alt Text](http://placehold.it/200x50 "Image Title")
  3222. ![Alt Text](http://placehold.it/200x50 "Image Title")
  3223. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3224. [View raw (TEST.md)](https://raw.github.com/adamschwartz/github-markdown-kitchen-sink/master/README.md)
  3225. This is a paragraph.
  3226. This is a paragraph.
  3227. # Header 1
  3228. ## Header 2
  3229. Header 1
  3230. ========
  3231. Header 2
  3232. --------
  3233. # Header 1
  3234. ## Header 2
  3235. ### Header 3
  3236. #### Header 4
  3237. ##### Header 5
  3238. ###### Header 6
  3239. # Header 1
  3240. ## Header 2
  3241. ### Header 3
  3242. #### Header 4
  3243. ##### Header 5
  3244. ###### Header 6
  3245. # Header 1
  3246. ## Header 2
  3247. ### Header 3
  3248. #### Header 4
  3249. ##### Header 5
  3250. ###### Header 6
  3251. # Header 1 #
  3252. ## Header 2 ##
  3253. ### Header 3 ###
  3254. #### Header 4 ####
  3255. ##### Header 5 #####
  3256. ###### Header 6 ######
  3257. > Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam hendrerit mi
  3258. > posuere lectus. Vestibulum enim wisi, viverra nec, fringilla in, laoreet
  3259. > vitae, risus.
  3260. > Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.
  3261. > ## This is a header.
  3262. >
  3263. > 1. This is the first list item.
  3264. > 2. This is the second list item.
  3265. >
  3266. > Here's some example code:
  3267. >
  3268. > Markdown.generate();
  3269. > ## This is a header.
  3270. > 1. This is the first list item.
  3271. > 2. This is the second list item.
  3272. >
  3273. > Here's some example code:
  3274. >
  3275. > Markdown.generate();
  3276. - Red
  3277. - Green
  3278. - Blue
  3279. * Red
  3280. * Green
  3281. * Blue
  3282. - Red
  3283. - Green
  3284. - Blue
  3285. \`\`\`markdown
  3286. - Red
  3287. - Green
  3288. - Blue
  3289. * Red
  3290. * Green
  3291. * Blue
  3292. - Red
  3293. - Green
  3294. - Blue
  3295. \`\`\`
  3296. 1. Buy flour and salt
  3297. 1. Mix together with water
  3298. 1. Bake
  3299. \`\`\`markdown
  3300. 1. Buy flour and salt
  3301. 1. Mix together with water
  3302. 1. Bake
  3303. \`\`\`
  3304. Paragraph:
  3305. Code
  3306. <!-- -->
  3307. Paragraph:
  3308. Code
  3309. ---
  3310. ---
  3311. ---
  3312. ---
  3313. ---
  3314. * * *
  3315. ***
  3316. *****
  3317. - - -
  3318. ---------------------------------------
  3319. This is [an example](http://example.com 'Example') link.
  3320. [This link](http://example.com) has no title attr.
  3321. This is [an example][id] reference-style link.
  3322. [id]: http://example.com 'Optional Title'
  3323. This is [an example](http://example.com "Example") link.
  3324. [This link](http://example.com) has no title attr.
  3325. This is [an example] [id] reference-style link.
  3326. [id]: http://example.com "Optional Title"
  3327. _single asterisks_
  3328. _single underscores_
  3329. **double asterisks**
  3330. **double underscores**
  3331. *single asterisks*
  3332. _single underscores_
  3333. **double asterisks**
  3334. __double underscores__
  3335. This paragraph has some \`code\` in it.
  3336. This paragraph has some \`code\` in it.
  3337. ![Alt Text](http://placehold.it/200x50 'Image Title')
  3338. ![Alt Text](http://placehold.it/200x50 "Image Title")
  3339. `;