前端代码规范

5,172

不用会直接抄

  1. 京东凹凸实验室:guide.aotu.io/docs/html/t…
  2. 腾讯alloyteam: alloyteam.github.io/CodeGuide/#…
  3. Vue: cn.vuejs.org/v2/style-gu…

事倍功半+1

  1. eslint: JavaScript和JSX强检查工具,注重代码质量和代码格式,可autoFix (提示,修改)
  2. prettier:Prettier is an opinionated code formatter with support for: 固执己见的代码格式化工具,支持以下:(直接干就完事,你的 -> AST -> 我的)
  3. editorConfig:有助于对跨不同编辑器和ide处理同一项目的多个开发人员维护一致的编码风格

甜豆腐脑 or 咸豆腐脑

  1. 两者都能格式化代码

    • eslint基于规则给出报错提示 可以自动修复
    • prettier直接修改代码成符合规则的内容不给别人机会
  2. eslint更侧重于"质量" 如定义变量未使用等!,prettier则不关心。

  3. 其实在prettier的文档上都说明了怎么配合使用

    ESLint (and other linters)

    If you use ESLint, install eslint-config-prettier to make ESLint and Prettier play nice with each other. It turns off all ESLint rules that are unnecessary or might conflict with Prettier. There’s a similar config for Stylelint: stylelint-config-prettier

    (See Prettier vs. Linters to learn more about formatting vs linting, Integrating with Linters for more in-depth information on configuring your linters, and Related projects for even more integration possibilities, if needed.)

  4. 在vscode上可以考虑使用插件prettier Eslint,(感觉哦)核心都是用prettier去执行eslint的格式化规则

小孩子才做选择题, 我 都 要!

  1. 在eslint之前先使用prettier格式化规范代码
  2. eslint再着重处理语法上的规则

我还是喜欢咸的111