JS代码检查工具ESLint介绍与使用方法(4)

"eslint": "^4.15.0", "eslint-config-standard": "^10.2.1", "eslint-friendly-formatter": "^3.0.0", "eslint-loader": "^1.7.1", "eslint-plugin-import": "^2.7.0", "eslint-plugin-node": "^5.2.0", "eslint-plugin-promise": "^3.4.0", "eslint-plugin-standard": "^3.0.1", "eslint-plugin-vue": "^4.0.0",

与此同时,在根目录下自动生成.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/base', // 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', } }

如果需要更改为更严格的验证,可以下载并使用airbnb规范

npm install --save-dev eslint-config-airbnb-base

在extends字段中,将standard更改为airbnb-base,将plugin:vue/base更改为plugin: vue/recommended

extends: [ 'plugin:vue/recommended', 'airbnb-base' ],

添加一些自定义的规则,最终的配置文件如下所示

// 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/recommended', // https://github.com/standard/standard/blob/master/docs/RULES-en.md 'airbnb-base' ], // 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', "linebreak-style": "off", // 取消换行符\n或\r\n的验证 "semi": ["error", "never"], // 不使用分号 "arrow-parens": ["error", "as-needed"], // 箭头函数的参数可以不使用圆括号 "comma-dangle": ["error", "never"], // 不允许末尾逗号 "consistent-return": "off", // 关闭函数中return的检测 "object-curly-newline": ["error", { "consistent": true }], // 花括号内的换行符不一定要格式一致 "global-require": "off", // 取消对require的验证,使得可以使用require来加载图片的相对路径 "function-paren-newline": "off", // 不验证函数括号内的换行 "import/no-unresolved": "off", // 取消自动解析路径,以此开启alias的别名路径设置 "no-param-reassign": "off", // 允许对函数参数进行再赋值 "import/extensions": "off", // 取消对文件扩展名的验证 "max-len": "off", // 取消行的最大长度的验证,使SVG不用重新调整格式 "no-underscore-dangle": "off", //允许标识符中有下划线,从而支持vue中插件的使用 "no-console": "off", // 启用console控制台 "no-unused-expressions": "off", // 允许使用未使用过的表达式,以此来支持a && a()的代码形式 "no-shadow": "off", // 取消变量声明覆盖的验证 'vue/attribute-hyphenation': 0, // 取消对元素特性只能使用中划线或小驼峰形式的验证 'vue/max-attributes-per-line': 0 // 取消元素有多个特性时,每个特性独占一行的验证 } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/65f97a9e84dd9b66f3a13dd33a0b7362.html