vue 如何配置eslint代码检查

目录
  • 1.安装依赖
  • 2.webstorm代码格式化快捷键为win + alt + L
  • 3.设置webstorm校验规则为本地项目安装的eslint
  • 4.在项目本地新建.editorconfig文件
  • 5.在项目本地新建.eslintrc.js文件

1.安装依赖

"eslint": "^5.12.0",
"eslint-config-standard": "^12.0.0",
"eslint-friendly-formatter": "^4.0.1",
"eslint-loader": "^2.1.1",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-node": "^8.0.1",
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^4.0.0",
"eslint-plugin-vue": "^5.1.0",
"acorn": "^6.0.5",
"babel-eslint": "^8.2.1",

2.webstorm代码格式化快捷键为win + alt + L

在webstorm preference里面找到code style,在里面设置webstorm代码快捷格式化选项。

比如这里勾选上in empty tag时,当我们按win + alt + L格式化代码时,就会自动在闭合标签前面添加空格。

如果webstorm格式化与eslint规则冲突,大多时候也可以从这里设置规则。

3.设置webstorm校验规则为本地项目安装的eslint

如下:

4.在项目本地新建.editorconfig文件

设置webstorm格式。

root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
in_empty_tag = true

5.在项目本地新建.eslintrc.js文件

// https://eslint.org/docs/user-guide/configuring

module.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: {
    // allow async-await
    'generator-star-spacing': 'off',
    // allow debugger during development
    'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
    "vue/no-use-v-if-with-v-for": ["error", {
      "allowUsingIterationVar": false
    }],
    "vue/return-in-computed-property": ["error", {
      "treatUndefinedAsUnspecified": false
    }],
    "vue/no-unused-components": ["error", {
      "ignoreWhenBindingPresent": true
    }],
    "vue/attribute-hyphenation": ["error", "always", {
      "ignore": []
    }],
    "vue/component-name-in-template-casing": ["error", "kebab-case", {
      "ignores": []
    }],
    "vue/html-closing-bracket-newline": ["error", {
      "singleline": "never",
      "multiline": "always"
    }],
    "vue/html-closing-bracket-spacing": ["error", {
      "startTag": "never",
      "endTag": "never",
      "selfClosingTag": "always"
    }],
    "vue/html-indent": ["error", 2, {
      "attribute": 1,
      "baseIndent": 1,
      "closeBracket": 0,
      "alignAttributesVertically": true,
      "ignores": []
    }],
    "vue/html-quotes": ["error", "double"],
    "vue/html-self-closing": ["error", {
      "html": {
        "void": "never",
        "normal": "never",
        "component": "always"
      },
      "svg": "always",
      "math": "always"
    }],
    "vue/max-attributes-per-line": ["error", {
      "singleline": 3,
      "multiline": {
        "max": 3,
        "allowFirstLine": true
      }
    }],
    "vue/multiline-html-element-content-newline": ["error", {
      "ignoreWhenEmpty": true,
      "ignores": ["pre", "textarea"]
    }],
    "vue/mustache-interpolation-spacing": ["error", "always"],
    "vue/name-property-casing": ["error", "kebab-case"],
    "vue/no-multi-spaces": ["error", {
      "ignoreProperties": false
    }],
    "vue/no-spaces-around-equal-signs-in-attribute": ["error"],
    "vue/no-template-shadow": ["error"],
    "vue/prop-name-casing": ["error", "camelCase"],
    "vue/require-default-prop": ["error"],
    "vue/v-bind-style": ["error", "shorthand"],
    "vue/v-on-style": ["error", "shorthand"],
    "vue/attributes-order": ["error", {
      "order": [
        "DEFINITION",
        "LIST_RENDERING",
        "CONDITIONALS",
        "RENDER_MODIFIERS",
        "GLOBAL",
        "UNIQUE",
        "TWO_WAY_BINDING",
        "OTHER_DIRECTIVES",
        "OTHER_ATTR",
        "EVENTS",
        "CONTENT"
      ]
    }],
    "vue/order-in-components": ["error", {
      "order": [
        "el",
        "name",
        "parent",
        "functional",
        ["delimiters", "comments"],
        ["components", "directives", "filters"],
        "extends",
        "mixins",
        "inheritAttrs",
        "model",
        ["props", "propsData"],
        "data",
        "computed",
        "watch",
        "LIFECYCLE_HOOKS",
        "methods",
        ["template", "render"],
        "renderError"
      ]
    }],
    "vue/this-in-template": ["error", "never"]
  }
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • 详解vue-cli下ESlint 配置说明

    1,关闭eslint 这里只说vue-cli脚手架的关闭方法,其实很简单,就是把 build/webpack.base.conf.js 配置文件中的eslint rules注释掉即可. module: { rules: [ // { // test: /\.(js|vue)$/, // loader: 'eslint-loader', // enforce: 'pre', // include: [resolve('src'), resolve('test')], // options: { /

  • vue项目中使用eslint+prettier规范与检查代码的方法

    1.前言   在团队协作中,为避免低级 Bug.以及团队协作时不同代码风格对彼此造成的困扰与影响,会预先制定编码规范.使用 Lint 工具和代码风格检测工具,则可以辅助编码规范执行,有效控制代码质量.EsLint 则是其中一个很好的工具. EsLint 提供以下支持: ES6 AngularJS JSX Style 检查 自定义错误和提示 EsLint 提供以下几种校验: 语法错误校验 不重要或丢失的标点符号,如分号 未被使用的参数提醒 漏掉的结束符,如} 确保样式的统一规则,如 sass 或者

  • ESLint 是如何检查 .vue 文件的

    近期要做一个类似的内容,学习了一下 Vue 是如何做的. ESLint 中的扩展机制 首先需要了解一下如何才能扩展 ESLint 的功能.ESLint 扩展机制主要有 Rules.Plugins.Formatters.Parsers,Formatters 目前不需要用到,因此着重看一下其他三种方式. Parser 用于自定义的解析文件内容,返回 AST 给后续步骤使用. Rules 用于定义校验规则. Plugins plugin 是多种功能的一个集合,可以定义以下内容: Rules:校验规则.

  • vue-cli3项目配置eslint代码规范的完整步骤

    前言 最近接手了一个项目,由于之前为了快速开发,没有做代码检查.为了使得代码更加规范以及更易读,所以就要eslint上场了. 安装依赖 安装依赖有两种方法: 在cmd中打上把相应的依赖加到devDependencies下,再npm install对应依赖. 在package.json文件加上相应依赖: "eslint-plugin-html": "^6.0.3", "@vue/cli-plugin-eslint": "^3.3.0&qu

  • vue 如何配置eslint代码检查

    目录 1.安装依赖 2.webstorm代码格式化快捷键为win + alt + L 3.设置webstorm校验规则为本地项目安装的eslint 4.在项目本地新建.editorconfig文件 5.在项目本地新建.eslintrc.js文件 1.安装依赖 "eslint": "^5.12.0", "eslint-config-standard": "^12.0.0", "eslint-friendly-forma

  • VSCode使用之Vue工程配置eslint

    首先确保VS Code 安装了 Vetur 和 Eslint 插件. 然后使用了vue-cli提供的简单webpack模板,这样创建的工程默认没有添加ESLint. vue init webpack-simple test-vscode 配置过程 全局安装最新的eslint npm i -g eslint@latest 运行 eslint --init 会安装以下依赖 eslint-config-standard@latest eslint-plugin-import@>=2.2.0 eslin

  • 在React项目中使用Eslint代码检查工具及常见问题

    背景 最近使用 create-react-app 创建了一个项目.但是众所周知的是,这个脚手架创建的项目并没有默认加入 Eslint 等 lint 插件来规范代码. 考虑到项目中很多项目没有使用类似的代码检查工具,为了规范开发.这次有必要记录一下流程. 使用 Eslint 流程 1. 安装 Eslint 首先,先安装 Eslint 到项目本地(全局安装亦可). npm --save-dev install eslint 安装完成之后,我们先创建基础的 .eslintrc.yml (建议使用 .y

  • vue项目配置eslint保存自动格式化问题

    目录 vue配置eslint保存自动格式化 插件实现按照 ESLint 规则自动格式化 1. 需求插件 2. 修改默认格式化插件 3. 「Ctrl + S」保存时按照 ESLint 规则自动格式化 vue配置eslint保存自动格式化 vue项目中有保存自动格式化,还是很舒服的,满足了大多数强迫症 1,用户设置和工作区设置 2,如何找到配置文件 3,setting.json 4,需要安装的插件 5,设置默认格式化程序 在VSCode中,两个层级的设置分别为: 用户设置:应用于所有工作区的全局设置

  • 详解VS Code使用之Vue工程配置format代码格式化

    编辑器另外一个很重要的功能就是代码格式化了,VS Code默认提供常见文件的格式化,如.js,.html等. 添加对.vue文件的格式化支持 这里我们添加对 .vue 文件的格式化支持. 1. 安装 Vetur 插件 2. 在 VS Code 的设置中添加如下规则: { "vetur.format.defaultFormatter": { "html": "prettier", "css": "prettier&qu

  • vue+webpack中配置ESLint

    一.ESLint 协同开发过程中,经常感受到来自代码检视的恶意.代码习惯不一致,看半天:居然提交低级错误,我的天(╯‵□′)╯︵┻━┻!...研究了一番,决定使用ESLint来做代码规范检查. 二.配置方式 JavaScript注释:通过JavaScript注释把配置信息嵌入代码中. package.json:在package.json文件中的eslintConfig字段中指定配置. 配置文件:通过.eslintrc.(js/json/yaml/yml)的独立文件来为整个目录或者子目录指定配置信

  • 实例详解Vue项目使用eslint + prettier规范代码风格

    团队开发的项目,如果没有对代码风格作要求,有多少团队成员,就当然会出现多少种不同的代码风格.因此,我们需要一种能够统一团队代码风格的工具,作为强制性的规范,统一整个项目的代码风格. 幸好,我们有 eslint 和 prettier . eslint VS prettier 应该大多数项目都已经采用eslint来对代码进行质量检查,可能少部分还会采用其进行一定程度上的统一风格.那为什么还需要prettier呢?我们先来对它们作一个简单的了解. 各种linters 总体来说,linters有两种能力

  • JS代码检查工具ESLint介绍与使用方法

    ESLint不但提供一些默认的规则,也提供用户自定义规则来约束所写的JavaScript代码. 发展历史 Douglas Crockford大神根据自己的理念用JavaScript写了一个JavaScript代码规范检查工具,这就是JSLint.后来非常流行,也的确帮助了广大的JavaScript程序员.但是,大神对于自己的代码规范不做丝毫的妥协,对开源社区的反馈的回应也不礼貌.于是,JSLint从一个帮助程序员规范代码,避免Bug的工具,变成了一个让代码像Crockford的工具.在最不信神的

  • vue项目配置使用flow类型检查的步骤

    你是否经常在debug那些简单可避免的bug?可能你给函数传参的时候搞错了参数的顺序,或者本来应该传个Number类型的参数,你传了一个String类型?JavaScript的弱类型是这一类bug的罪魁祸首,静态类型语言中不存在此类bug.Flow就是JavaScript的静态类型检查工具,该库的目标在于检查JavaScript中的类型错误,开发者通常不需要修改代码即可使用,故使用成本很低.同时,它也提供额外语法支持,使得开发者能更大程度地发挥Flow的作用. 一.flow的安装 flow可以直

随机推荐