You're (Probably) Using Prettier Wrong

Theo - t3․gg
5 Jun 202306:04

Summary

TLDRこの動画は、コードフォーマッターとリンターの役割の違いについて説明しています。プレゼンターは、プレゼンターはES lintをコードフォーマッターとして使用することは適切ではなく、代わりにPrettierやDprintなどの専用フォーマッターを使用するべきだと主張しています。ES lintはコードの品質を管理するリンターであり、フォーマッティングとは異なる役割を持っているため、両者を混同すると効率が悪くなり、メンテナンスも大変になると警告しています。適切なツールを使い分けることで、コードの一貫性と品質が向上し、開発体験も改善されると述べられています。

Takeaways

  • 👔 Prettierはコードのフォーマッタであり、eslintはコードのリンターであり、それぞれの役割が異なることを強調している。
  • ✂️ コードのフォーマットとリンティングは根本的に異なる問題であり、eslintでフォーマットを行うべきではない。
  • 🔄 Prettierは高速でコードをフォーマットするためのツールであり、eslintは複雑なコード解析とルールチェックを行うためのツールである。
  • 🔩 Prettierを使ってフォーマットを行い、その後eslintでリンティングを行うのが適切なワークフローである。
  • ⚙️ eslint-config-prettierプラグインを使用して、prettierと競合するeslintルールを無効にすることが推奨されている。
  • 🚫 eslint-plugin-prettierを使ってprettierのルールをeslintで実行するべきではない。
  • 🎯 Prettierはコードのフォーマットに専念し、eslintはコードの品質とベストプラクティスに専念すべきである。
  • 🏆 eslintとprettierはオープンソースプロジェクトであり、メンテナがベストプラクティスを提供しているため、それに従うべきである。
  • 🌱 create-react-appなどの初期テンプレートでは適切なリンティングとフォーマッティングの設定がデフォルトで含まれている。
  • 💡 コードのフォーマットとリンティングを適切に分離することで、開発体験が改善され、コードの一貫性が向上する。

Q & A

  • prettierとeslintの役割の違いは何ですか?

    -prettierはコードをフォーマットするためのツールで、eslintはコードの品質とスタイルを管理するためのリンターです。prettierは単純にコードの書式を整えるのみですが、eslintはコード間の関係性やルールの適用状況をチェックします。

  • eslintでコードをフォーマットするのは適切ではないのはなぜですか?

    -eslintはリンターであり、コードのフォーマッティングを主な目的としていません。フォーマッティングとリンティングは根本的に異なる問題であり、eslintでフォーマッティングを行うと処理が重くなり、eslintのメンテナンスも困難になります。

  • prettierとeslintを併用する適切な方法は何ですか?

    -eslintとprettierを併用する場合は、eslint-config-prettierプラグインを使用してprettierと競合するeslintのルールを無効化し、まずprettierでコードをフォーマットしてからeslintでリンティングを行うのが適切です。

  • prettierの代替ツールにはどのようなものがありますか?

    -prettierの代替ツールとして、Rustで書かれた高速なfprintがあげられています。fprintはprettierよりも設定がカスタマイズしやすく、フォーマッティングにかかる時間がほぼゼロです。

  • なぜ講演者はprettierの設定を最小限に抑えたいと言っているのですか?

    -講演者はコードのスタイルを統一するためにprettierを使用していますが、デフォルトの設定で十分だと考えているため、prettierの設定をほとんど変更する必要がないと述べています。

  • eslintを適切に設定することでどのようなメリットがありますか?

    -eslintを適切に設定することで、間違ったPromiseの使用を防止したり、TypeScriptの振る舞いを改善したり、メモリリークを減らすことができます。つまり、開発者とチームの一貫性を高め、コード品質を向上させることができます。

  • 講演者はeslintのコアチームに対してどのようなアドバイスをしていますか?

    -講演者は、eslintのコアチームに対して、eslintをコードのフォーマッターとして使用するのは避けるべきであり、フォーマッティングとリンティングを明確に分離するよう助言しています。

  • eslint-plugin-prettierを使うべきではない理由は何ですか?

    -eslint-plugin-prettierは、eslint内でprettierのルールを実行するプラグインですが、リンターとフォーマッターの役割が異なるため、このプラグインを使うと適切にフォーマッティングできない可能性があります。

  • コードのフォーマッティングとリンティングを分離することで得られる利点は何ですか?

    -フォーマッティングとリンティングを分離することで、それぞれの処理が単純化され、高速化されます。また、フォーマッターとリンターの役割が明確になり、メンテナンスが容易になります。

  • 開発者がeslintを避ける理由と、それに対する講演者の見解は何ですか?

    -講演者は、多くの開発者がeslintの設定や理解を避けがちですが、eslintに慣れ親しむことで開発がより簡単で一貫性が高まると述べています。eslintは実際それほど難しくはありません。

Outlines

00:00

😡 あまりにも多くの人が誤ってprettierを使っている

話者はあまりにも多くの人がESLintをコードのフォーマッターとして間違って使っていることに懸念を示しています。フォーマッターとリンターは根本的に異なる問題であり、コードを変更するからといってESLintをフォーマッターとして使うべきではありません。フォーマッターは単にコードをフォーマットするだけですが、リンターはコード全体の関係性を検査し、特定のルールに従っているかどうかを確認します。これらは別々のプロセスであり、リンターにフォーマッターの機能を求めるのは間違いです。代わりに、prettierやprintといったフォーマッターを使い、その後ESLintでリンティングを行うのが適切な方法です。

05:00

😤 ESLintはリンターとしてのみ使うべき

話者はESLintをリンターとしてのみ使うべきであり、フォーマッターとして使うべきではないと主張しています。ESLintチームは、ESLintをフォーマッターとして使うことに反対しており、prettier ESlint configプラグインを使ってprettierでフォーマットし、その後ESLintでリントするよう推奨しています。ESLint Prettier pluginは避けるべきです。フォーマッターとリンターは根本的に異なる役割を持っているため、両者を混同してはいけません。開発者はリンター側に専念し、フォーマッターはprettierなどに任せるべきです。

Mindmap

Keywords

The video is abnormal, and we are working hard to fix it.
Please replace the link and try again.

Highlights

I love prettier and I'm concerned with, how many people are using pretty or, wrong to be clear it's not actually, prettier they're using wrong it's eslint, because linting and formatting are very, very different things

I've seen a lot of, misconceptions around eslint and how it, should be used in your code base just, because eslint can change your code, doesn't mean it should

eslint is not a formatter eslint is, a linter if you talk to anyone on the, eslint core team you tell them that, you're formatting your code with eslint, they're going to give you a weird look, because it is a bad bad idea

formatting, and linting are fundamentally different, problems

the role of a formatter is to format, your code which means it does one really, quick pass across your code anything, that isn't formatted correctly it fixes, but doesn't keep track of anything it, doesn't check the relationships between, things

a linter is trying to confirm, specific discrete rule tools are being, followed in your code base which means, the relationship between a file and, specific lines in it as well as between, different files between different rules, all of the complexity involved in that, is necessary for the linter to work

the way that eslint recommends you, run prettier is using eslint config, prettier which is a plug-in that turns, off all of the ESL and stuff that, conflicts it prettier so you can use, prettier for formatting first into then, do a lint pass after it

it lets you, use prettier for what it is a very very, fast formatter and you don't even have, to use prettier D print is a great, alternative that is insanely fast it's, written in Rust and lets you format your, files effectively instantaneously

my reason for using, prettier is I don't want to think about, the config anymore I just want my code, to always look roughly the same and, prettier does an absolutely phenomenal, job at that

the role of eslist in the role of, prettier are just so fundamentally, different prettier is a thing I don't, want to think about I don't want to, change rules and I don't want to look at, I just wanted to run and make my code, consistent

a linter is a thing I'm going, to sit there and configure and make me a, better Dev with and make my team more, consistent with yes lint makes so I, don't use promises wrong it helps me be, better with my typescript behaviors and, make sure I'm not leaking enemies all, over my code base

it's a complex, process to do that and it's even more, complex if during that like set of, passes it also has to worry about, formatting because it might do a pass, realize the formatting is off adjust the, formatting and now the lint rules are, applied differently notice to do yet, another pass

he couldn't see, how doing two passes would ever be, faster than one the way they're doing, two passes faster than one is if it's, not two versus one it's like twenty, versus one because that's what your, linter does it does some very complex, analysis of your code

these are such, fundamentally different problems I need, to make sure I link this talk in the, description because man there are so, many little gems in here around, configuring yeslet correctly around, getting better error reporting in Dev, and in like your builds

Josh helps here, with eslint and I find most people are, scared to better understand it I, personally was one of those people I, avoided digging into eslint for a while, but after listening to Josh a bit more I, realized it's not that scary it's, actually pretty important and if you get, familiar enough with it things are much, easier and consistent and some of these, rules are dope

Transcripts

play00:00

I love prettier and I'm concerned with

play00:01

how many people are using pretty or

play00:03

wrong to be clear it's not actually

play00:04

prettier they're using wrong it's eslint

play00:06

because linting and formatting are very

play00:08

very different things I've seen a lot of

play00:10

misconceptions around eslint and how it

play00:13

should be used in your code base just

play00:14

because eslint can change your code

play00:16

doesn't mean it should and the big thing

play00:18

I've seen too much of is people using

play00:20

eslint as the formatter for their code

play00:22

base eslint is not a formatter eslint is

play00:24

a linter if you talk to anyone on the

play00:26

eslint core team you tell them that

play00:28

you're formatting your code with eslint

play00:29

they're going to give you a weird look

play00:30

because it is a bad bad idea formatting

play00:32

and linting are fundamentally different

play00:34

problems a lot of this rant is directly

play00:36

inspired and stolen from Josh Goldberg's

play00:39

talk he did at react Miami and I want to

play00:40

highlight this particular slide from it

play00:42

the role of a formatter is to format

play00:44

your code which means it does one really

play00:45

quick pass across your code anything

play00:47

that isn't formatted correctly it fixes

play00:49

but doesn't keep track of anything it

play00:50

doesn't check the relationships between

play00:52

things it just runs through your code

play00:53

and fixes things that are broken in your

play00:54

formatting and then it's done and that's

play00:56

it whereas a linter is trying to confirm

play00:58

specific discrete rule tools are being

play01:00

followed in your code base which means

play01:02

the relationship between a file and

play01:04

specific lines in it as well as between

play01:05

different files between different rules

play01:07

all of the complexity involved in that

play01:09

is necessary for the linter to work and

play01:11

that complexity can and almost certainly

play01:13

should exist outside of your formatter

play01:14

and the way that eslint recommends you

play01:16

run prettier is using eslint config

play01:18

prettier which is a plug-in that turns

play01:20

off all of the ESL and stuff that

play01:22

conflicts it prettier so you can use

play01:23

prettier for formatting first into then

play01:25

do a lint pass after it just runs so

play01:28

much better and makes your editing

play01:29

experience better too and it lets you

play01:31

use prettier for what it is a very very

play01:33

fast formatter and you don't even have

play01:34

to use prettier D print is a great

play01:36

alternative that is insanely fast it's

play01:38

written in Rust and lets you format your

play01:40

files effectively instantaneously it's

play01:42

also a bit more configurable so if your

play01:43

reason for using eslint instead of

play01:45

prettier for formatting is configuration

play01:47

you should use a formatter that has

play01:48

better configuration my reason for using

play01:50

prettier is I don't want to think about

play01:51

the config anymore I just want my code

play01:53

to always look roughly the same and

play01:55

prettier does an absolutely phenomenal

play01:57

job at that I don't usually have a

play01:59

prettier config unless I having a bug

play02:00

with one of the like built-in packages

play02:02

like the plug-in for Tailwind in

play02:04

prettier I'm using pnpm I have to

play02:06

manually put that in but other than that

play02:07

I almost never have a prettier config

play02:09

because the defaults are fine deals are

play02:10

actually pretty good I hope I'm properly

play02:12

emphasizing my point here is that like

play02:14

the role of eslist in the role of

play02:15

prettier are just so fundamentally

play02:17

different prettier is a thing I don't

play02:19

want to think about I don't want to

play02:20

change rules and I don't want to look at

play02:22

I just wanted to run and make my code

play02:23

consistent a linter is a thing I'm going

play02:25

to sit there and configure and make me a

play02:27

better Dev with and make my team more

play02:29

consistent with yes lint makes so I

play02:31

don't use promises wrong it helps me be

play02:33

better with my typescript behaviors and

play02:34

make sure I'm not leaking enemies all

play02:36

over my code base and it's a complex

play02:37

process to do that and it's even more

play02:39

complex if during that like set of

play02:41

passes it also has to worry about

play02:43

formatting because it might do a pass

play02:44

realize the formatting is off adjust the

play02:46

formatting and now the lint rules are

play02:48

applied differently notice to do yet

play02:49

another pass I saw a pretty bad take

play02:51

from Anthony Foo which sucks because

play02:53

he's normally really good about these

play02:54

things his words were he couldn't see

play02:57

how doing two passes would ever be

play02:58

faster than one the way they're doing

play03:00

two passes faster than one is if it's

play03:01

not two versus one it's like twenty

play03:03

versus one because that's what your

play03:05

linter does it does some very complex

play03:06

analysis of your code to make sure

play03:09

things are where they're supposed to be

play03:10

in your formatter just make sure the

play03:12

output's the right shape these are such

play03:13

fundamentally different problems I need

play03:15

to make sure I link this talk in the

play03:16

description because man there are so

play03:18

many little gems in here around

play03:19

configuring yeslet correctly around

play03:21

getting better error reporting in Dev

play03:23

and in like your builds there's just so

play03:25

many little things that Josh helps here

play03:27

with eslint and I find most people are

play03:29

scared to better understand it I

play03:31

personally was one of those people I

play03:32

avoided digging into eslint for a while

play03:34

but after listening to Josh a bit more I

play03:35

realized it's not that scary it's

play03:37

actually pretty important and if you get

play03:39

familiar enough with it things are much

play03:40

easier and consistent and some of these

play03:42

rules are dope there's a typescript set

play03:44

of rules too the recommended typescript

play03:45

rules are incredible and will make your

play03:47

code better and more resilient that is a

play03:49

different problem from formatting and we

play03:50

need to stop pretending that they're the

play03:51

same doing formatting in eslint is using

play03:53

yes wrong it's making it slower it's

play03:55

making the lives of the maintainers of

play03:56

eslint harder because now they have to

play03:57

deal with all these weird patterns

play03:59

people are using es14 the docs are being

play04:01

updated to be very specific about this

play04:02

and if you go to prettier stocks too

play04:03

they're pretty clear as well cool there

play04:05

it is integrating with linters linters

play04:07

usually contain not only code quality

play04:08

rules but also stylistic rules most

play04:10

stylistic rules are unnecessary when

play04:12

using prettier but worse they might

play04:13

conflict use prettier for code

play04:15

formatting and linters for code quality

play04:16

as outlined in prettier versus linters

play04:19

this is a pretty well established thing

play04:21

and it is frustrating to me that people

play04:24

will not listen to both the eslint team

play04:26

and the prettier team because they think

play04:27

they know better than both when they

play04:29

actually don't understand either and

play04:30

that's why I felt like I had to make a

play04:31

video about this because I'm so goddamn

play04:33

tired of people acting like they know

play04:35

better than these hard-working open

play04:36

source maintainers they don't these

play04:38

people work really hard to make great

play04:39

software and they've finally made it

play04:41

easier for us to make the software

play04:42

cooperate together and when I'm taking

play04:44

advantage of it or complaining about it

play04:45

I don't get it it's just the formatters

play04:47

are really good and fast if you let them

play04:49

do one pass and do their thing just use

play04:50

the formatters for formatting and use

play04:52

the linters for linting don't use lender

play04:53

for formatting and certainly don't use

play04:55

the formatter for lenting goddamn and

play04:56

thankfully in here they say you can use

play04:58

these two plugins that will turn off

play05:00

prettier conflicting rules in eslint and

play05:02

they even have here eslin plug-in for

play05:04

the year which they specify you

play05:05

shouldn't use because what this does is

play05:06

it runs prettier's rules in eslint

play05:08

because the lender has to worry about so

play05:10

many conflicting rules and so much

play05:11

context that exists outside of the

play05:13

specific line of code being formatted it

play05:15

also doesn't necessarily do formatting

play05:16

great and I don't remember the specific

play05:18

examples but I know Josh gave examples

play05:20

of weird tab behaviors that the linter

play05:22

couldn't handle well that prettier

play05:23

handles really well and those types of

play05:24

things are more complex when your

play05:26

processed to read the code is more

play05:28

complex so if you want to do this stuff

play05:29

right the way to do it right is you use

play05:31

prettier ESL and config prettier and

play05:33

then whatever ESL config you want to on

play05:34

top of that but you should not be using

play05:36

ESL plugin pretty year as you're

play05:38

prettier run it just doesn't make sense

play05:39

I hope this one was helpful the thing I

play05:41

run into a lot and I've seen way too

play05:43

much generally if you set up crazy app

play05:44

or even create next app you're gonna get

play05:46

good enough linting then you can just

play05:47

npm install prettier and you're done it

play05:48

should be that simple can be that simple

play05:49

it is that simple please for the love of

play05:52

God just let it be simple if you want to

play05:54

hear more about things people use wrong

play05:55

I'm gonna pin a video in the corner

play05:57

right here where I talk all about how

play05:58

people have used JavaScript wrong

play05:59

because it annoys me so check that out

play06:01

if you haven't already good seeing y'all

play06:02

as always

Rate This

5.0 / 5 (0 votes)

関連タグ
プログラミング開発ツールコーディングフォーマッターリンターeslintprettierベストプラクティス効率化オープンソース