为了账号安全,请及时绑定邮箱和手机立即绑定

vscode 中格式化代码[vue2支持,插件ESLint、Prettier、Vetur]

标签:
Html5

1. 安装vscode插件

  • ESLint

  • Prettier formatter for Visual Studio Code

  • Vetur

2. 配置文件

.editorconfig

root = true[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = truetrim_trailing_whitespace = truetab_width = 1

.eslintrc.js

// https://eslint.org/docs/user-guide/configuringmodule.exports = {
  root: true,
  parserOptions: {
    parser: 'babel-eslint'
  },
  env: {
    browser: true,
  },
  extends: [    // https://github.com/vuejs/eslint-plugin-vue#priority-a-essential-error-prevention
    // consider switching to `plugin:vue/strongly-recommended` or `plugin:vue/recommended` for stricter rules.
    'plugin:vue/essential',    // https://github.com/standard/standard/blob/master/docs/RULES-en.md
    'standard'
  ],  // required to lint *.vue files
  plugins: [    'vue'
  ],  // add your custom rules here
  rules: {    // 融合了 jquery 所以暂时屏蔽
    "no-undef": 0, //不允许未声明的变量
    "no-unused-vars": [0, {"vars": "all", "args": "after-used"}], //不允许有声明后未使用的变量或者参数
    "no-tabs": 0, //不允许tabs

    "no-mixed-spaces-and-tabs": [0, "smart-tabs"], //不允许混用tab和空格
    "indent": [0, 4],//缩进风格
    "camelcase": [0, {"properties": "never"}], //强制驼峰命名规则
    "space-before-function-paren": [0, {"anonymous": "always", "named": "never"}], //函数定义时括号前的空格

    // allow paren-less arrow functions
    'arrow-parens': 0,    'no-extra-semi': 0, //不允许出现不必要的分号

    // allow async-await
    'generator-star-spacing': 'off',    // allow debugger during development
    'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off'
  }
}

用户设置 setting.json

{  "files.autoSave": "afterDelay",  "editor.fontSize": 14,  "editor.tabSize": 2,  "eslint.autoFixOnSave": true,  "eslint.validate": [    "javascript",    "javascriptreact",    "vue",
    {      "language": "html",      "autoFix": true
    }
  ],  "prettier.singleQuote": true,  "prettier.semi": false}

3. 使用

  • 方式一:右键格式化代码

  • 方式二:打开命令面板 (>eslint fix all auto-fixable problems)



作者:会煮咖啡的猫咪
链接:https://www.jianshu.com/p/879456a0d925
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
Web前端工程师
手记
粉丝
37
获赞与收藏
165

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消