文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

React项目配置prettier和eslint的方法

2024-04-02 19:55

关注

参考视频: https://www.bilibili.com/video/BV1rh411e7E5?vd_source=eee62ea3954ac01bff9e87e2a7b40084

配置prettier和eslint

1.初始化React项目

npx create-react-app study_react

2.安装vscode插件 prettiereslint, 配置vscode

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

3.安装相关依赖

yarn add -D prettier eslint

在代码格式化时采用Perttier规则,而我们代码校验使用的是ESLint,如果同一个规则配置不一致,往往就会出现冲突问题;
比如:字符串单、双引号的配置,eslint后把字符串变成单引号,更新文件代码过后,重新保存(Prettier)又自动格式化后变成双引号,导致代码校验异常。

解决方案1: 两者配置文件部分配置修改成一致.
解决方案2: 安装相关插件(Prettier 和 ESLint 冲突解决方案 eslint-config-prettier eslint-plugin-prettier)

eslint-config-prettier 禁用 eslint 冲突配置
eslint-plugin-prettier Prettier先格式化 (默认是先eslint格式化,再Prettier格式化)
yarn add -D eslint-config-prettier eslint-plugin-prettier

4.优雅的提示错误

“extends”: [“eslint:recommended”, “plugin:react/recommended”], 默认是eslint:recommended,(步骤6后面会看到这个配置)
https://www.npmjs.com/package/eslint-config-airbnb

npx install-peerdeps --dev eslint-config-airbnb

5.初始化.eslintrc.json文件

npx eslint --init

如果全局安装了eslint (npm install -g eslint )了, 可以直接使用eslint --init

根据提示勾选:

在这里插入图片描述

安装完成的eslintrc.json文件

{
 
  "env": {
 
    "browser": true,
    "es2021": true
  },
  "extends": ["eslint:recommended", "plugin:react/recommended"],
  "parserOptions": {
 
    "ecmaFeatures": {
 
      "jsx": true
    },
    "ecmaVersion": "latest",
    "sourceType": "module"
  },
  "plugins": ["react"],
  "rules": {
 
    "indent": ["error", "tab"],
    "linebreak-style": ["error", "windows"],
    "quotes": ["error", "double"],
    "semi": ["error", "always"]
  }
}

6.修改eslintrc.json优雅提示(也就是步骤4所提到)

"extends": ["airbnb", "prettier", "plugin:react/recommended"],
  "plugins": ["prettier", "react"],

7.新建.prettierrc文件

更多规则: https://www.prettier.cn/docs/options.html

{
 
  "singleQuote": false,
  "endOfLine": "lf"
}

8.可自行定义eslintrc.json规则

https://eslint.bootcss.com/docs/rules/

9.效果

在这里插入图片描述

10.让提示更细致

eslintrc.json追加rules, "prettier/prettier": "error",

在这里插入图片描述

最终的两个文件的配置

// eslint
{
 
  "env": {
 
    "browser": true,
    "es2021": true
  },
  "extends": ["airbnb", "prettier", "plugin:react/recommended"],
  "parserOptions": {
 
    "ecmaFeatures": {
 
      "jsx": true
    },
    "ecmaVersion": "latest",
    "sourceType": "module"
  },
  "plugins": ["prettier", "react"],
  "rules": {
 
    "prettier/prettier": "error",
    "indent": ["off", "tab"],
    "linebreak-style": ["off", "windows"],
    "quotes": ["error", "double"],
    "semi": ["error", "always"]
  }
}
===============================================================================
// prettier
{
 
  "singleQuote": false,
  "endOfLine": "lf"
}

配置stylelint

1.安装相关依赖

yarn add stylelint stylelint-config-stardand stylelint-config-prettier stylelint-order -D
- stylelint-config-prettier解决和prettier冲突
- stylelint-order 排序css属性

2.新建.stylelint.json文件

stylelint相关规则: http://stylelint.docschina.org/user-guide/rules/

{
 
  "plugins": ["stylelint-order"],
  "extends": ["stylelint-config-standard", "stylelint-config-prettier"],
  "rules": {
 
    "property-no-unknown": true,
    "comment-no-empty": [
      true,
      {
 
        "message": "禁止空注释"
      }
    ],
    "order/properties-order": [
      "position",
      "top",
      "right",
      "bottom",
      "left",
      "z-index",
      "display",
      "float",
      "width",
      "height",
      "max-width",
      "max-height",
      "min-width",
      "min-height",
      "padding",
      "padding-top",
      "padding-right",
      "padding-bottom",
      "padding-left",
      "margin",
      "margin-top",
      "margin-right",
      "margin-bottom",
      "margin-left",
      "margin-collapse",
      "margin-top-collapse",
      "margin-right-collapse",
      "margin-bottom-collapse",
      "margin-left-collapse",
      "overflow",
      "overflow-x",
      "overflow-y",
      "clip",
      "clear",
      "font",
      "font-family",
      "font-size",
      "font-smoothing",
      "osx-font-smoothing",
      "font-style",
      "font-weight",
      "hyphens",
      "src",
      "line-height",
      "letter-spacing",
      "word-spacing",
      "color",
      "text-align",
      "text-decoration",
      "text-indent",
      "text-overflow",
      "text-rendering",
      "text-size-adjust",
      "text-shadow",
      "text-transform",
      "word-break",
      "word-wrap",
      "white-space",
      "vertical-align",
      "list-style",
      "list-style-type",
      "list-style-position",
      "list-style-image",
      "pointer-events",
      "cursor",
      "background",
      "background-attachment",
      "background-color",
      "background-image",
      "background-position",
      "background-repeat",
      "background-size",
      "border",
      "border-collapse",
      "border-top",
      "border-right",
      "border-bottom",
      "border-left",
      "border-color",
      "border-image",
      "border-top-color",
      "border-right-color",
      "border-bottom-color",
      "border-left-color",
      "border-spacing",
      "border-style",
      "border-top-style",
      "border-right-style",
      "border-bottom-style",
      "border-left-style",
      "border-width",
      "border-top-width",
      "border-right-width",
      "border-bottom-width",
      "border-left-width",
      "border-radius",
      "border-top-right-radius",
      "border-bottom-right-radius",
      "border-bottom-left-radius",
      "border-top-left-radius",
      "border-radius-topright",
      "border-radius-bottomright",
      "border-radius-bottomleft",
      "border-radius-topleft",
      "content",
      "quotes",
      "outline",
      "outline-offset",
      "opacity",
      "filter",
      "visibility",
      "size",
      "zoom",
      "transform",
      "box-align",
      "box-flex",
      "box-orient",
      "box-pack",
      "box-shadow",
      "box-sizing",
      "table-layout",
      "animation",
      "animation-delay",
      "animation-duration",
      "animation-iteration-count",
      "animation-name",
      "animation-play-state",
      "animation-timing-function",
      "animation-fill-mode",
      "transition",
      "transition-delay",
      "transition-duration",
      "transition-property",
      "transition-timing-function",
      "background-clip",
      "backface-visibility",
      "resize",
      "appearance",
      "user-select",
      "interpolation-mode",
      "direction",
      "marks",
      "page",
      "set-link-source",
      "unicode-bidi",
      "speak"
    ]
  }
}

3.效果

在这里插入图片描述

保存自动修复

采用的vscode编辑器, 往setting.json添加配置

"editor.codeActionsOnSave": {
 
	"source.fixAll.stylelint": true,
	"source.fixAll.eslint": true
},

到此这篇关于React项目配置prettier和eslint的文章就介绍到这了,更多相关React配置prettier和eslint内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-前端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯