当前位置: 首页 > 编程日记 > 正文

不要只是为您的代码做些毛-用Prettier修复它

Linting makes our lives easier because it tells us what’s wrong with our code. But how can we avoid doing the actual work that goes into fixing it?

Linting使我们的生活更轻松,因为它告诉我们代码有什么问题。 但是,如何避免进行修复工作呢?

Previously I wrote about linting, what it is, and how it makes your life easier. At the end, I actually included a way that you could automatically fix your code. So why am I writing this?

以前,我写过关于棉绒的介绍 ,它是什么,以及它如何使您的生活更轻松。 最后,我实际上提供了一种可以自动修复代码的方法。 那我为什么要写这个?

你是什​​么意思解决? (What do you mean fix it?)

Before we roll into it, let’s hit this quick. Linters are powerful and provide an easy way to scan your code for syntax errors that could lead to bugs. Or they can simply help keep a codebase clean, healthy, and consistent. When run, it will show all the issues and let you go through each one individually to fix them.

在深入探讨之前,让我们快速开始吧。 Linters功能强大,可提供一种简便的方法来扫描代码以查找可能导致错误的语法错误。 或者,他们可以简单地帮助保持代码库干净,健康和一致。 运行时,它将显示所有问题,并让您逐一解决每个问题。

Taking that to the next level, some linters will actually allow you to pass in an argument to the command running the linter that allows it to fix it for you automagically. This means you don’t have to manually go through and make all of those little whitespace and semicolon (add them! 🔥) tweaks yourself!

将其带入一个新的水平,实际上一些短毛猫实际上将允许您将参数传递给运行短毛猫的命令,从而可以自动为您修复它。 这意味着您不必手动检查并调整所有这些小空格和分号(添加它们!🔥)。

那么,我还能做些什么来解决问题呢? (So what more can I do to fix things?)

If you already use the fix option, thats a good start. But there are tools out there that have been developed specifically to tackle this problem beyond just a flag into your command. The one I’m going to cover is Prettier.

如果您已经使用了修复选项,那就是一个好的开始。 但是,有一些专门开发的工具可以解决此问题,而不仅仅是在您的命令中添加标记。 我要讲的是Prettier。

什么是漂亮? (What is Prettier?)

Prettier pegs itself as “an opinionated code formatter." It takes an input of your code and outputs it in a consistent format stripping any of the original code style. It actually converts your code to a syntax tree, then rewrites it using the styles and rules you and Prettier provide together via your ESLint config and Prettier’s default rules.

Prettier认为自己是“自以为是的代码格式化程序”。它接收您的代码输入,并以一致的格式输出它,以剥离任何原始代码样式。它实际上将您的代码转换为语法树 ,然后使用这些样式和您和Prettier通过您的ESLint配置和Prettier的默认规则一起提供的规则。

You can easily use Prettier alone just to format your code, which works just fine. But if you combine this with an underlying ESLint process, you get both a powerful linter and a powerful fixer. I’m going to show you how to make those work together.

您可以轻松地单独使用Prettier来格式化您的代码,这很好用。 但是,如果将此与基础的ESLint流程结合使用,则将获得功能强大的Linter和强大的修复程序。 我将向您展示如何使它们一起工作。

Prettier入门 (Getting started with Prettier)

For this walkthrough, I’m going to assume that you have ESLint set up and configured in an application. Particularly, I’m going to pick up where I left off in my previous walkthrough where we installed ESLint to a React application.

在本演练中,我将假定您已在应用程序中设置并配置了ESLint。 特别是,我将在上一个演练中将我从ESLint安装到React应用程序中的上一个停下来的地方继续学习 。

Additionally of note, Prettier tells us right from the start that it's an opinionated code formatter. You should expect that it will format your code in a consistent way, but maybe a different way than you currently have it configured. But don’t fret! You can tweak this configuration.

另外值得注意的是,Prettier从一开始就告诉我们这是一个自以为是的代码格式化程序。 您应该期望它会以一致的方式设置代码格式,但可能与您当前配置的方式不同。 但是不要担心! 您可以调整此配置。

So what are we starting off with? We already:

那么,我们从什么开始呢? 我们已经:

  • Have installed ESLint

    已安装ESLint

  • Have added Babel as our parser

    已将Babel添加为我们的解析器

  • Have added a plugin that includes React configurations

    添加了一个包含React配置的插件

Next, let’s get started by installing a few packages:

接下来,让我们开始安装一些软件包:

yarn add prettier prettier-eslint prettier-eslint-cli -D

Note: the command above is similar to using npm. If your project doesn't use yarn, swap out to npm as appropriate.

注意:上面的命令类似于使用npm 如果您的项目不使用yarn ,则视情况换成npm

Above, we’re installing:

上面,我们正在安装:

  • prettier: core Prettier package and engine

    漂亮 :核心漂亮的软件包和引擎

  • prettier-lint: passes the Prettier result to ESLint to fix using your ESLint config

    prettier -lint :将Prettier结果传递给ESLint以使用您的ESLint配置进行修复

  • prettier-eslint-cli: helps Prettier and ESLint work together on various files across your project

    prettier-eslint-cli :帮助Prettier和ESLint在项目中的各种文件上协同工作

And we’re installing them as a dev dependency, as we don’t need it outside development.

而且我们正在将它们安装为开发依赖项,因为我们不需要在开发之外使用它。

配置新的格式化程序 (Configuring your new formatter)

Now that our packages are installed, we can set up yarn to run this command for us.

现在已经安装了我们的软件包,我们可以设置yarn为我们运行此命令。

Previously, we set up a lint script to look like this in our package.json:

以前,我们在package.json设置了一个lint脚本,如下所示:

"scripts": {..."lint": "eslint . --ext .js"...
}

We’re going to leave that as it is, but we’ll do something similar and create a new script right next to it called format for our formatter Prettier:

我们要留下,因为它是,但我们会做同样的事情,并创建一个新的脚本就在旁边叫format为我们的格式更漂亮:

"scripts": {..."format": "prettier-eslint --eslint-config-path ./.eslintrc.js --write '**/*.js'","lint": "eslint . --ext .js"...
}

So what’s going on there? We’re:

那到底是怎么回事? 我们是:

  • Adding a new script called format, that we can run as yarn format

    添加一个名为format的新脚本,我们可以将其作为yarn format运行

  • We’re utilizing our prettier-eslint-cli package to run the formatting for us

    我们正在利用我们prettier-eslint-cli软件包为我们运行格式化

  • We’re passing in our ESLint config located next to our package.json in the root of the project (change this if it’s in a different location)

    我们在项目根目录中传入位于package.json旁边的ESLint配置(如果位于其他位置,请更改此设置)

  • And finally, we’re telling prettier to write all files matching **/*.js, or any JS files it finds recursively through our project

    最后,我们告诉漂亮的是写所有与**/*.js匹配的文件,或通过我们的项目递归找到的所有JS文件。

The beauty here is that we're passing in our ESLint config to Prettier. This means we only have to maintain 1 config for both tools, but we still leverage the linting power of ESLint along with the formatting power of Prettier.

这里的好处是我们正在将我们的ESLint配置传递给Prettier。 这意味着我们只需要为这两个工具维护1个配置,但是我们仍然利用ESLint的整理功能以及Prettier的格式化功能。

运行格式化程序! (Run your formatter!)

Now that we’re all set up, let’s run it! Run this following:

现在我们已经完成设置,让我们运行它吧! 运行以下命令:

yarn format

and immediately, we see that it works:

马上,我们看到它起作用:

嘿,我的代码看起来不一样! (Hey, my code looks different!)

As I mentioned earlier, Prettier tells us straight up, it’s an opinionated formatter. It ships with its own rules, sort of like its own ESLint config, so it will go through and make those changes as well.

正如我之前提到的,Prettier直截了当地告诉我们,这是一个自以为是的格式化程序。 它附带了自己的规则,有点像自己的ESLint配置,因此它也会进行更改。

Don’t abandon your code! Instead, you can review the changes, see if maybe it makes sense to keep it that way (it will be very consistent) or you can update your ESLint config (.eslintrc.js) to overwrite the rules you don’t like. This is also a good way to maybe learn some new things that you might not have expected to get caught before.

不要放弃您的代码! 相反,您可以查看所做的更改,以使其保持这种方式(这将是非常一致的)是否有意义,或者可以更新ESLint配置( .eslintrc.js )以覆盖您不喜欢的规则。 这也是学习一些新事物的好方法,而这些新事物可能是您以前可能不会想到的。

那么,这会把我们留在哪里呢? (So where does this leave us?)

If you’ve followed along so far, we now have two commands:

如果到目前为止,您现在有两个命令:

  • lint: which will check your code for you and tell you what's wrong

    lint :它将为您检查代码并告诉您什么地方不对

  • format: will automatically try to fix the problems for you

    format :将自动尝试为您解决问题

When using these in practice, your best bet is to always run format first to let it try to automatically fix anything it can. Then immediately run lint to catch anything Prettier wasn’t able to fix automatically.

在实践中使用它们时,最好的选择是始终始终运行format以使其尝试自动修复所有可能的问题。 然后立即运行lint来捕获Prettier无法自动修复的所有内容。

下一步是什么? (What’s next?)

Now that we can format our code automatically, we should be able to fix our code automatically!

现在我们可以自动格式化代码了,我们应该能够自动修复代码!

Next time we’ll take this a step further and set up a git hook that will allow this to run before you commit. This means you won't ever have to worry about forgetting to run this again!

下次,我们将更进一步,并设置一个git钩子, git可以在提交之前运行。 这意味着您将不必担心忘记再次运行它!

Follow me for more Javascript, UX, and other interesting things!

  • 🐦 Follow Me On Twitter

    Twitter在Twitter上关注我

  • 📽️ Subscribe To My Youtube

    Subscribe️订阅我的YouTube

  • ✉️ Sign Up For My Newsletter

    ✉️注册我的时事通讯

Originally published at https://www.colbyfayock.com/2019/11/dont-just-lint-your-code-fix-it-with-prettier/

最初发布于https://www.colbyfayock.com/2019/11/dont-just-lint-your-code-fix-it-with-prettier/

翻译自: https://www.freecodecamp.org/news/dont-just-lint-your-code-fix-it-with-prettier/

相关文章:

循环语句(2)

for的嵌套 //99乘法表for (int a 1; a < 9; a)-----控制行{for (int i 1; i < a; i)------控制列{Console.Write(i "*" a "" (a * i) "\t");}Console.WriteLine();}Console.ReadLine(); 结果 打印星号 //直角在左上for (int i …

通过Shell脚本将VSS项目批量创建并且提交迁移至Gitlab

脚本运行环境&#xff1a;Git Bash 系统环境&#xff1a;Windows 10 Pro 1709 VSS版本&#xff1a;Microsoft Visual SourceSafe 2005 我的VSS工作目录结构如下&#xff1a; D:\work\ --vss ----project1 ------src ------README.md ------ ...... ----project2 ------doc ----…

样式集(11)注册页面样式,全部代码附效果图

效果图&#xff1a; 代码&#xff1a; <template><view class"page"><view class"top">新用户注册</view><image :src"sanjiao" mode"widthFix" class"sanjiao"></image><!-- <…

quickselect_QuickSelect:使用代码示例解释的快速选择算法

quickselect什么是QuickSelect&#xff1f; (What is QuickSelect?) QuickSelect is a selection algorithm to find the K-th smallest element in an unsorted list.QuickSelect是一种选择算法&#xff0c;用于在未排序的列表中查找第K个最小的元素。 算法说明 (The Algori…

《Linux命令行与shell脚本编程大全 第3版》Shell脚本编程基础---34

以下为阅读《Linux命令行与shell脚本编程大全 第3版》的读书笔记&#xff0c;为了方便记录&#xff0c;特地与书的内容保持同步&#xff0c;特意做成一节一次随笔&#xff0c;特记录如下&#xff1a;转载于:https://www.cnblogs.com/guochaoxxl/p/7894620.html

CSS超出部分隐藏,显示滚动条

实现功能&#xff1a; 固定一个高度&#xff0c;超出该高度的部分就隐藏&#xff0c;并且显示滚动条能上拉下滑滚动 实现代码&#xff1a; height: 500rpx; overflow-x: hidden; overflow-y: scroll; 实现功能&#xff1a; 固定一个宽度&#xff0c;超出该宽度的部分就隐藏…

第二周学习进度

好久的编程实现&#xff0c;居然没有编完整&#xff0c;看来自己需要加班学习了&#xff01;第二周学习进度如下&#xff1a; 第二周所花时间&#xff08;包括上课&#xff09;共计21小时代码量&#xff08;行&#xff09;220博客量&#xff08;篇&#xff09;4了解到的知识 1.…

如何在C ++中从容器中删除元素

How to remove elements from container is a common C interview question, so you can earn some brownie points if you read this page carefully. 如何从容器中删除元素是C 常见的面试问题&#xff0c;因此&#xff0c;如果仔细阅读此页&#xff0c;可以赚取布朗尼积分。 …

【BZOJ4282】慎二的随机数列 乱搞

【BZOJ4282】慎二的随机数列 Description 间桐慎二是间桐家著名的废柴&#xff0c;有一天&#xff0c;他在学校随机了一组随机数列&#xff0c; 准备使用他那强大的人工智能求出其最长上升子序列&#xff0c;但是天有不测风云&#xff0c;人有旦夕祸福&#xff0c;柳洞一成路过…

git phpstorm 配置

http://jingyan.baidu.com/album/a948d65105faed0a2dcd2ea2.html?stepindex2&st2&os0&bd_page_type1&net_type3 http://jingyan.baidu.com/article/20095761cbef40cb0721b417.html转载于:https://www.cnblogs.com/fyy-888/p/5272862.html

CSS动画无限循环

实现代码 div{animation:myanimation 5s infinite; }keyframes myanimation {from {top:0px;}to {top:200px;} } 注:animation ->Css3动画属性 myanimation->随便命名 infinite 可重复 ,去掉就不重复了 top可以改为宽高,或者方向等等任何CSS属性 form 开始 to结束…

apple id无法创建_我如何为我的Apple收藏夹创建网站

apple id无法创建A while ago I started an Apple collection. Ive been following Apple hardware (and its aesthetics) since I was a teenager, but at that time I didnt the have money to own a Mac. 前一段时间&#xff0c;我开始了一个苹果系列。 从我十几岁起我就一直…

bzoj1562[NOI2009]变换序列——2016——3——12

任意门&#xff1a;http://www.lydsy.com/JudgeOnline/problem.php?id1562 题目&#xff1a; 对于0,1,…,N-1的N个整数&#xff0c;给定一个距离序列D0,D1,…,DN-1&#xff0c;定义一个变换序列T0,T1,…,TN-1使得每个i,Ti的环上距离等于Di。一个合法的变换序列应是0,1,…,N-1的…

把view或者div绘制 canvas ,导出图片功能实现完整源码附效果图(兼容H5和小程序)

先看下效果图&#xff1a;&#xff08;上面灰色块内的用div和CSS写出来的&#xff0c;然后绘制到canvas&#xff09; 实现此功能需要使用到一个微信小程序的插件&#xff0c;插件官方文档地址&#xff1a; wxml-to-canvas | 微信开放文档 本博客代码环境&#xff0c;uniapp&a…

C 语言中的 switch 语句 case 后面是否需要加大括号

事件原由为编辑器的自动缩进&#xff0c;当 case 换行后不自动缩进。 于是在在想可以可否在 case 后面再大括号&#xff0c;让其自动缩进。 查了资料&#xff0c;发现 case 是可以加大括号的&#xff0c;相当于代码块。 而且还有另外一个用途&#xff0c;可以代码块头部定义变量…

问题 c: 插入排序_插入排序:它是什么,以及它如何工作

问题 c: 插入排序Insertion sort is a simple sorting algorithm for a small number of elements.插入排序是一种针对少量元素的简单排序算法。 例&#xff1a; (Example:) In Insertion sort, you compare the key element with the previous elements. If the previous ele…

在Java连接hbase时出现的问题

问题1&#xff1a; java.net.ConnectException: Connection refused: no further information zookeeper.ClientCnxn: Session 0x0 for server null zookeeper未启动&#xff0c;或无法连接&#xff0c;从查看各节点zookeeper启动状态、端口占用、防火墙等方面查看原因。问题2&…

codeforces 8C. Looking for Order 状压dp

题目链接 给n个物品的坐标&#xff0c; 和一个包裹的位置&#xff0c; 包裹不能移动。 每次最多可以拿两个物品&#xff0c; 然后将它们放到包里&#xff0c; 求将所有物品放到包里所需走的最小路程。 直接状压dp就好了。 #include <iostream> #include <vector> #…

H5刷新当前页面

location.reload();

sql的外键约束和主键约束_SQL主键约束用示例解释

sql的外键约束和主键约束A primary key is a column or a set of columns that uniquely identifies each row in a table.主键是一列或一组列&#xff0c;它们唯一地标识表中的每一行。 It’s called a “constraint” because it causes the system to restrict the data al…

str.format() 格式化字符串函数

语法 它通过{}和:来代替%。 “映射”示例 通过位置 In [1]: {0},{1}.format(kzc,18) Out[1]: kzc,18 In [2]: {},{}.format(kzc,18) Out[2]: kzc,18 In [3]: {1},{0},{1}.format(kzc,18) Out[3]: 18,kzc,18字符串的format函数可以接受不限个参数&#xff0c;位置可以…

css学习任务二:切图写代码

今天的任务是根据UI给的图进行切图&#xff0c;然后写出相应的页面&#xff0c;UI如下&#xff1a; 收获&#xff1a;学习前端知识一年有余&#xff0c;却因为老是找不到实战项目而得不到实际的提高&#xff0c;直到今天的学习我才知道切图是怎么一回事&#xff0c;明白了你看到…

Vue mixins(混入) 附代码示例详解

mixins 我们称它为 “混入” &#xff1b; 官方的解释&#xff1a; 混入 (mixin) 提供了一种非常灵活的方式&#xff0c;来分发 Vue 组件中的可复用功能。一个混入对象可以包含任意组件选项。当组件使用混入对象时&#xff0c;所有混入对象的选项将被“混合”进入该组件本身的…

软件开发面试_如何为成功的软件开发工作面试做准备

软件开发面试Job interviews are stressful for many people. Besides the pressure of getting hired, you have to answer various questions before and during the interview – like what to wear, how to get prepared, how much money to ask for, and much more.求职面…

bzoj1070————2016——3——14

传送门&#xff1a;http://www.lydsy.com/JudgeOnline/problem.php?id1070&#xff1b; 题目概括&#xff1a; Description 同一时刻有N位车主带着他们的爱车来到了汽车维修中心。维修中心共有M位技术人员&#xff0c;不同的技术人员对不同的车进行维修所用的时间是不同的。现…

CSS兼容性汇总

http://www.jb51.net/css/469020.html CSS属性Hack 把属性hack分为 前缀属性hack和 后缀属性hack CSS属性Hack&#xff08;前缀&#xff09;针对的浏览器_color:red;IE6及其以下的版本*color:red ;或者 color:red;IE7及其以下的版本CSS属性Hack&#xff08;后缀&#xff09;针对…

Vue 过渡组件,可实现组件或者页面的动画过渡或者css过渡

使用过渡效果&#xff0c;可以优化用户体验&#xff0c;Vue给我们封装了一个很好用的组件&#xff0c;专门用来处理过渡效果&#xff0c;下面我们来看看怎么使用它&#xff1b; Vue 提供了 transition 的封装组件&#xff0c;在下列情形中&#xff0c;可以给任何元素和组件添加…

解释型和编译型编程语言_解释型和编译型编程语言:有什么区别?

解释型和编译型编程语言Every program is a set of instructions, whether it’s to add two numbers or send a request over the internet. Compilers and interpreters take human-readable code and convert it to computer-readable machine code. 每个程序都是一组指令&a…

Beta 冲刺 (1/7)

队名&#xff1a;天机组 组员1友林 228&#xff08;组长&#xff09; 今日完成&#xff1a;查找了相关资料及api文档。明天计划&#xff1a;继续相关资料及源码。剩余任务&#xff1a;优化网络通讯机制主要困难&#xff1a;查找的代码调试较为困难。收获及疑问&#xff1a;暂无…

Vue全局路由侦听beforeEach路由守卫附代码使用示例

使用路由守卫beforeEach&#xff0c;可以实现路由侦听&#xff1b; 全局侦听路由跳转的实现代码&#xff1a; app.vue onLaunch: function(e) {this.$router.beforeEach((to, from, next) > {console.log($router,to,from);next();}); } to 是跳转路由之后的page对象&am…