前言
最近发现公司的项目代码修改保存后,代码会自动进行格式化。但是自己搭建的小项目不能进行格式化,虽然安装了一个格式化插件,但是格式化的效果很不舒服。
ESLint官方中文文档
实现
安装eslint插件 上面只是在vscode里安装了,实际项目里还需要安装
npm install eslint babel-eslint -D
生产配置文件
./node_modules/.bin/eslint --init
运行完会有以下提示:
你要如何使用ESLint,选择第三个 看自己项目的实际情况,我的项目用的第一个 项目是否用了框架,我选择的第二个 是否使用ts 输入a选择全部 选择第一个 选择标准,我用的第二个 创建的配置文件的格式,我用的第一个
上面完成之后会安装依赖并生成下面的两个文件
.eslintignore 告诉 ESLint 去忽略特定的文件和目录。.eslintignore 文件是一个纯文本文件,其中的每一行都是一个 glob 模式表明哪些路径应该忽略检测 .eslintrc.js 校验规则
这些都有了后,可以看到 现在还没完,改一下校验规则,把下面的代码替换掉.eslintrc.js 原有代码
vue项目的规则
module.exports = {
root: true,
env: {
browser: true,
node: true
},
parserOptions: {
parser: 'babel-eslint',
ecmaVersion: 6
},
extends: [
'plugin:vue/vue3-essential',
'eslint:recommended',
],
rules: {
'eqeqeq': [0, 'allow-null'],
'prefer-const': 0,
'consistent-this': [1, 'that'],
'no-debugger': 1,
'no-console': 1,
'no-alert': 1,
'comma-dangle': [1, 'never'],
'eol-last': 1,
'vue/html-indent': [2, 4],
'vue/no-multi-spaces': [2, { ignoreProperties: false }],
'vue/no-spaces-around-equal-signs-in-attribute': [2],
'vue/prop-name-casing': [2],
'vue/require-prop-types': 2,
'vue/attribute-hyphenation': [2, 'always'],
'vue/html-closing-bracket-spacing': [2, {
'startTag': 'never',
'endTag': 'never',
'selfClosingTag': 'always'
}],
'vue/html-end-tags': [2],
'vue/html-quotes': [2, 'double'],
'vue/multiline-html-element-content-newline': [2, {
'ignoreWhenEmpty': true,
'allowEmptyLines': true
}],
'vue/mustache-interpolation-spacing': [2, 'always'],
'vue/v-bind-style': [2, 'shorthand'],
'vue/v-on-style': [2, 'shorthand'],
'vue/component-name-in-template-casing': [2, 'kebab-case', {
'registeredComponentsOnly': false
}],
'vue/html-closing-bracket-newline': [2, {
'singleline': 'never',
'multiline': 'never'
}],
'semi': [2, 'always'],
'no-label-var': 2,
'no-undef': 2,
'no-unused-vars': [1, { 'vars': 'all', 'args': 'after-used', 'ignoreRestSiblings': false }],
'no-undef-init': 2,
'use-isnan': 2,
'no-floating-decimal': 2,
'no-multi-str': 2,
'no-sequences': 2,
'yoda': [2, 'never'],
'indent': [2, 4, { SwitchCase: 1 }],
'no-mixed-spaces-and-tabs': 2,
'no-multiple-empty-lines': [2, { max: 2 }],
'no-trailing-spaces': 2,
'padded-blocks': [2, 'never'],
'no-sparse-arrays': 2,
'quotes': [2, 'single', { avoidEscape: true, allowTemplateLiterals: true }],
'curly': [2, 'all'],
'no-dupe-keys': 2,
'no-duplicate-case': 2,
'no-ex-assign': 2,
'no-unreachable': 2,
'valid-typeof': 2,
'dot-location': [2, 'property'],
'no-empty-pattern': 2,
'no-eval': 2,
'no-implied-eval': 2,
'no-extend-native': 2,
'no-fallthrough': 2,
'no-lone-blocks': 2,
'no-new-wrappers': 2,
'no-redeclare': 2,
'no-return-assign': [2, 'except-parens'],
'no-self-assign': 2,
'no-self-compare': 2,
'no-unmodified-loop-condition': 2,
'no-useless-escape': 2,
'no-class-assign': 2,
'no-const-assign': 2,
'no-dupe-class-members': 2,
'no-useless-computed-key': 2,
'new-cap': [2, { newIsCap: true, capIsNew: false }],
'new-parens': 2,
'no-whitespace-before-property': 2,
'no-cond-assign': 2,
'no-control-regex': 0,
'no-empty-character-class': 2,
'no-extra-boolean-cast': 2,
'no-func-assign': 2,
'no-inner-declarations': [2, 'functions'],
'no-obj-calls': 2,
'no-delete-var': 2,
'no-shadow-restricted-names': 2,
'no-dupe-args': 2,
'for-direction': 2,
'no-unsafe-negation': 2,
'comma-style': [2, 'last'],
'operator-linebreak': [2, 'after', { overrides: { '?': 'before', ':': 'before' } }],
'no-unexpected-multiline': 2,
'no-multi-spaces': 2,
'no-extra-parens': [2, 'functions'],
'no-regex-spaces': 2,
'brace-style': [2, '1tbs', { allowSingleLine: true }],
'arrow-spacing': [2, { before: true, after: true }],
'template-curly-spacing': [2, 'never'],
'space-before-blocks': [2, 'always'],
'space-infix-ops': 2,
'space-unary-ops': [2, { words: true, nonwords: false }],
'spaced-comment': [1, 'always', { markers: ['global', 'globals', 'eslint', 'eslint-disable', '*package', '!', ','] }],
'space-in-parens': [2, 'never'],
'array-bracket-spacing': [2, 'never'],
'block-spacing': [2, 'always'],
'key-spacing': [2, { beforeColon: false, afterColon: true }]
}
};
效果如下图: crrl + s 可以看到下面的效果
备注: 如果不好使,点开右下角的ESLint,看一下日志是不是哪里报错了
react的配置规则
安装依赖
npm install --save-dev eslint babel-eslint eslint-plugin-react eslint-plugin-import eslint-plugin-jsx-a11y
基本流程如上,就是最后用下面的规则就好
module.exports = {
"env": {
"browser": true,
"commonjs": true,
"es6": true
},
"extends": "eslint:recommended",
"globals": {
"$": true,
"process": true,
"__dirname": true
},
"parser": "babel-eslint",
"parserOptions": {
"ecmaFeatures": {
"experimentalObjectRestSpread": true,
"jsx": true
},
"sourceType": "module",
"ecmaVersion": 7
},
"plugins": [
"react"
],
"rules": {
"quotes": [2, "single"],
"no-console": 0,
"no-debugger": 2,
"no-var": 0,
"semi": 0,
"no-irregular-whitespace": 0,
"no-trailing-spaces": 1,
"eol-last": 0,
"no-unused-vars": [2, {"vars": "all", "args": "after-used"}],
"no-underscore-dangle": 0,
"no-alert": 2,
"no-lone-blocks": 0,
"no-class-assign": 2,
"no-cond-assign": 2,
"no-const-assign": 2,
"no-delete-var": 2,
"no-dupe-keys": 2,
"no-duplicate-case": 2,
"no-dupe-args": 2,
"no-empty": 2,
"no-func-assign": 2,
"no-invalid-this": 0,
"no-redeclare": 2,
"no-spaced-func": 2,
"no-this-before-super": 0,
"no-undef": 2,
"no-use-before-define": 2,
"camelcase": 0,
"jsx-quotes": [2, "prefer-double"],
"react/display-name": 0,
"react/forbid-prop-types": [2, {"forbid": ["any"]}],
"react/jsx-boolean-value": 2,
"react/jsx-closing-bracket-location": 1,
"react/jsx-curly-spacing": [2, {"when": "never", "children": true}],
"react/jsx-indent-props": [2, 4],
"react/jsx-key": 2,
"react/jsx-max-props-per-line": [1, {"maximum": 1}],
"react/jsx-no-bind": 0,
"react/jsx-no-duplicate-props": 2,
"react/jsx-no-literals": 0,
"react/jsx-no-undef": 1,
"react/jsx-pascal-case": 0,
"react/jsx-sort-props": 2,
"react/jsx-uses-react": 1,
"react/jsx-uses-vars": 2,
"react/no-danger": 0,
"react/no-did-mount-set-state": 0,
"react/no-did-update-set-state": 1,
"react/no-direct-mutation-state": 2,
"react/no-multi-comp": 2,
"react/no-set-state": 0,
"react/no-unknown-property": 2,
"react/prefer-es6-class": 2,
"react/prop-types": 0,
"react/react-in-jsx-scope": 2,
"react/self-closing-comp": 0,
"react/sort-comp": 2,
"no-extra-boolean-cast": 0,
"react/no-array-index-key": 0,
"react/no-deprecated": 1,
"react/jsx-equals-spacing": 2,
"no-unreachable": 1,
"comma-dangle": 2,
"no-mixed-spaces-and-tabs": 0,
"prefer-arrow-callback": 0,
"arrow-parens": 0,
"arrow-spacing": 0
},
"settings": {
"import/ignore": [
"node_modules"
]
}
};
|