配置:vscode vue项目代码检测配置eslint 和prettier
方法一
eslint init
或者直接添加eslilntrc.js
eslint index.js --fix 格式化index.js文件
npx prettier --write index.css
npm i prettier -D
npm i eslint @vue/eslint-config-prettier eslint-config-prettier eslint-plugin-prettier eslint-plugin-vue prettier -D
.eslintrc.js
module.exports = {env: {browser: true,es2021: true,node: true},extends: ["@vue/prettier", "plugin:vue/vue3-essential", "eslint:recommended"],parserOptions: {ecmaVersion: 7,sourceType: "module"},plugins: ["vue"],rules: {"prettier/prettier": "off","no-console": process.env.NODE_ENV === "production" ? "warn" : "off","no-debugger": process.env.NODE_ENV === "production" ? "warn" : "off","vue/no-deprecated-filter": "off",quotes: [0,"single",{avoidEscape: true,allowTemplateLiterals: true}],camelcase: 2, //强制驼峰法命名"comma-dangle": [2, "never"],semi: 0,"semi-spacing": [2,{before: false,after: false}],"comma-spacing": [2,{before: false,after: true}],"comma-style": [2, "last"],// allow async-await"generator-star-spacing": "off",// 三等号eqeqeq: 0,// 强制在注释中 // 或 /* 使用一致的空格"spaced-comment": 0,// 关键字后面使用一致的空格"keyword-spacing": 0,// 强制在 function的左括号之前使用一致的空格"space-before-function-paren": 0,// 引号类型// 禁止出现未使用过的变量// 'no-unused-vars': 0,// 要求或禁止末尾逗号"no-trailing-spaces": 2}
}
module.exports = {root: true, // 此项是用来告诉eslint找当前配置文件不能往父级查找env: { // 预定义的全局变量,这里是浏览器环境amd: true,es6: true,commonjs: true,node: true,jquery: true,browser: true},extends: ['plugin:vue/essential','@vue/standard'],parserOptions: {sourceType: 'module',parser: 'babel-eslint' // 解析器,默认使用 Espree},rules: {'array-bracket-spacing': ['error', 'never'], // 是否允许非空数组里面有多余的空格'arrow-parens': 'off', // 箭头函数用小括号括起来'block-spacing': ['error', 'always'], // =>的前/后括号'accessor-pairs': 'off', // 在对象中使用getter/setter'block-scoped-var': 'off', // 块语句中使用var'brace-style': ['warn', '1tabs'], // 大括号风格'callback-return': 'warn', // 避免多次调用回调什么的'camelcase': 'error', // 强制驼峰法命名'comma-dangle': ['error', 'never'], // 对象字面量项尾不能有逗号'comma-spacing': 'off', // 逗号前后的空格'comma-style': ['error', 'last'], // 逗号风格,换行时在行首还是行尾'complexity': ['off', 11], // 循环复杂度'computed-property-spacing': ['off', 'never'], // 是否允许计算后的键名什么的'consistent-return': 'off', // return 后面是否允许省略'consistent-this': ['error', 'that'], // this别名'constructor-super': 'off', // 非派生类不能调用super,派生类必须调用super'curly': ['error', 'all'], // 必须使用 if(){} 中的{}'default-case': 'error', // switch语句最后必须有default'dot-location': 'off', // 对象访问符的位置,换行的时候在行首还是行尾'dot-notation': ['off', { 'allowKeywords': true }], // 避免不必要的方括号'eol-last': 'off', // 文件以单一的换行符结束'eqeqeq': 'error', // 要求使用 === 和 !=='func-names': 'off', // 函数表达式必须有名字'func-style': ['off', 'declaration'], // 函数风格,规定只能使用函数声明/函数表达式'generator-star-spacing': 'off', // 生成器函数*的前后空格'guard-for-in': 'off', // for in循环要用if语句过滤'handle-callback-err': 'off', // nodejs 处理错误'id-length': 'off', // 变量名长度'indent': ['error', 4], // 缩进风格'init-declarations': 'off', // 声明时必须赋初始值'key-spacing': ['off', { 'beforeColon': false, 'afterColon': true }], // 对象字面量中冒号的前后空格'lines-around-comment': 'off', // 行前/行后备注'max-depth': ['off', 4], // 嵌套块深度'max-len': ['off', 80, 4], // 字符串最大长度'max-nested-callbacks': ['off', 'error'], // 回调嵌套深度'max-params': ['off', 3], // 函数最多只能有3个参数'max-statements': ['off', 10], // 函数内最多有几个声明'new-cap': 'error', // 函数名首行大写必须使用new方式调用,首行小写必须用不带new方式调用'new-parens': 'error', // new时必须加小括号'newline-after-var': 'error', // 变量声明后是否需要空一行'no-alert': 'off', // 禁止使用alert confirm prompt'no-array-constructor': 'error', // 禁止使用数组构造器'no-bitwise': 'off', // 禁止使用按位运算符'no-caller': 'warn', // 禁止使用arguments.caller或arguments.callee'no-catch-shadow': 'error', // 禁止catch子句参数与外部作用域变量同名'no-class-assign': 'error', // 禁止给类赋值'no-cond-assign': 'error', // 禁止在条件表达式中使用赋值语句'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off', // 是否使用 console'no-const-assign': 'error', // 禁止修改const声明的变量'no-constant-condition': 'error', // 禁止在条件中使用常量表达式 if(true) if('warn')'no-continue': 'off', // 禁止使用continue'no-control-regex': 'error', // 禁止在正则表达式中使用控制字符'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off', // 是否使用 debugger'no-delete-var': 'error', // 不能对var声明的变量使用delete操作符'no-div-regex': 'warn', // 不能使用看起来像除法的正则表达式/=foo/'no-dupe-keys': 'error', // 在创建对象字面量时不允许键重复 {a:'warn',a:'warn'}'no-dupe-args': 'error', // 函数参数不能重复'no-duplicate-case': 'error', // switch中的case标签不能重复'no-else-return': 'error', // 如果if语句里面有return,后面不能跟else语句'no-empty': 'error', // 块语句中的内容不能为空'no-empty-character-class': 'error', // 正则表达式中的[]内容不能为空'no-empty-label': 'error', // 禁止使用空label'no-eq-null': 'error', // 禁止对null使用==或!=运算符'no-eval': 'warn', // 禁止使用eval'no-ex-assign': 'error', // 禁止给catch语句中的异常参数赋值'no-extend-native': 'error', // 禁止扩展native对象'no-extra-bind': 'error', // 禁止不必要的函数绑定'no-extra-boolean-cast': 'error', // 禁止不必要的bool转换'no-extra-parens': 'error', // 禁止非必要的括号'no-extra-semi': 'error', // 禁止多余的冒号'no-fallthrough': 'warn', // 禁止switch穿透'no-floating-decimal': 'error', // 禁止省略浮点数中的'off' .5 3.'no-func-assign': 'error', // 禁止重复的函数声明'no-implicit-coercion': 'warn', // 禁止隐式转换'no-implied-eval': 'error', // 禁止使用隐式eval'no-inline-comments': 'off', // 禁止行内备注'no-inner-declarations': ['error', 'functions'], // 禁止在块语句中使用声明(变量或函数)'no-invalid-regexp': 'error', // 禁止无效的正则表达式'no-invalid-this': 'error', // 禁止无效的this,只能用在构造器,类,对象字面量'no-irregular-whitespace': 'error', // 不能有不规则的空格'no-iterator': 'error', // 禁止使用__iterator__ 属性'no-label-var': 'error', // label名不能与var声明的变量名相同'no-labels': 'error', // 禁止标签声明'no-lone-blocks': 'error', // 禁止不必要的嵌套块'no-lonely-if': 'error', // 禁止else语句内只有if语句'no-loop-func': 'warn', // 禁止在循环中使用函数(如果没有引用外部变量不形成闭包就可以)'no-mixed-requires': ['off', false], // 声明时不能混用声明类型'no-mixed-spaces-and-tabs': ['error', false], // 禁止混用tab和空格'linebreak-style': ['off', 'windows'], // 换行风格'no-multi-spaces': 'warn', // 不能用多余的空格'no-multi-str': 'error', // 字符串不能用\换行'no-multiple-empty-lines': ['warn', {'max': 'error'}], // 空行最多不能超过'error'行'no-native-reassign': 'error', // 不能重写native对象'no-negated-in-lhs': 'error', // in 操作符的左边不能有!'no-nested-ternary': 'off', // 禁止使用嵌套的三目运算'no-new': 'warn', // 禁止在使用new构造一个实例后不赋值'no-new-func': 'warn', // 禁止使用new Function'no-new-object': 'error', // 禁止使用new Object()'no-new-require': 'error', // 禁止使用new require'no-new-wrappers': 'error', // 禁止使用new创建包装实例,new String new Boolean new Number'no-obj-calls': 'error', // 不能调用内置的全局对象,比如Math() JSON()'no-octal': 'error', // 禁止使用八进制数字'no-octal-escape': 'error', // 禁止使用八进制转义序列'no-param-reassign': 'error', // 禁止给参数重新赋值'no-path-concat': 'off', // node中不能使用__dirname或__filename做路径拼接'no-plusplus': 'off', // 禁止使用++,--'no-process-env': 'off', // 禁止使用process.env'no-process-exit': 'off', // 禁止使用process.exit()'no-proto': 'error', // 禁止使用__proto__属性'no-redeclare': 'error', // 禁止重复声明变量'no-regex-spaces': 'error', // 禁止在正则表达式字面量中使用多个空格 /foo bar/'no-restricted-modules': 'off', // 如果禁用了指定模块,使用就会报错'no-return-assign': 'warn', // return 语句中不能有赋值表达式'no-script-url': 'off', // 禁止使用javascript:void('off')'no-self-compare': 'error', // 不能比较自身'no-sequences': 'off', // 禁止使用逗号运算符'no-shadow': 'error', // 外部作用域中的变量不能与它所包含的作用域中的变量或参数同名'no-shadow-restricted-names': 'error', // 严格模式中规定的限制标识符不能作为声明时的变量名使用'no-spaced-func': 'error', // 函数调用时 函数名与()之间不能有空格'no-sparse-arrays': 'error', // 禁止稀疏数组, ['warn',,'error']'no-sync': 'off', // nodejs 禁止同步方法'no-ternary': 'off', // 禁止使用三目运算符'no-trailing-spaces': 'warn', // 一行结束后面不要有空格'no-this-before-super': 'off', // 在调用super()之前不能使用this或super'no-throw-literal': 'error', // 禁止抛出字面量错误 throw 'error';'no-undef': 'warn', // 不能有未定义的变量'no-undef-init': 'error', // 变量初始化时不能直接给它赋值为undefined'no-undefined': 'error', // 不能使用undefined'no-unexpected-multiline': 'error', // 避免多行表达式'no-underscore-dangle': 'warn', // 标识符不能以_开头或结尾'no-unneeded-ternary': 'error', // 禁止不必要的嵌套 var isYes = answer === 'warn' ? true : false;'no-unreachable': 'error', // 不能有无法执行的代码'no-unused-expressions': 'error', // 禁止无用的表达式'no-unused-vars': ['error', {'vars': 'all', 'args': 'after-used'}], // 不能有声明后未被使用的变量或参数'no-use-before-define': 'error', // 未定义前不能使用'no-useless-call': 'error', // 禁止不必要的call和apply'no-void': 'error', // 禁用void操作符'no-var': 'off', // 禁用var,用let和const代替'no-warning-comments': ['warn', { 'terms': ['todo', 'fixme', 'xxx'], 'location': 'start' }], // 不能有警告备注'no-with': 'error', // 禁用with'object-curly-spacing': ['off', 'never'], // 大括号内是否允许不必要的空格'object-shorthand': 'off', // 强制对象字面量缩写语法'one-var': 'warn', // 连续声明'operator-assignment': ['off', 'always'], // 赋值运算符 += -=什么的'operator-linebreak': ['error', 'after'], // 换行时运算符在行尾还是行首'padded-blocks': 'off', // 块语句内行首行尾是否要空行'prefer-const': 'off', // 首选const'prefer-spread': 'off', // 首选展开运算'prefer-reflect': 'off', // 首选Reflect的方法'quotes': ['warn', 'single'], // 引号类型 `` '' '''quote-props':['error', 'always'], // 对象字面量中的属性名是否强制双引号'radix': 'error', // parseInt必须指定第二个参数'id-match': 'off', // 命名检测'require-yield': 'off', // 生成器函数必须有yield'semi': ['error', 'always'], // 语句强制分号结尾'semi-spacing': ['off', {'before': false, 'after': true}], // 分号前后空格'sort-vars': 'off', // 变量声明时排序'space-after-keywords': ['off', 'always'], // 关键字后面是否要空一格'space-before-blocks': ['off', 'always'], // 不以新行开始的块{前面要不要有空格'space-before-function-paren': ['off', 'always'], // 函数定义时括号前面要不要有空格'space-in-parens': ['off', 'never'], // 小括号里面要不要有空格'space-infix-ops': 'off', // 中缀操作符周围要不要有空格'space-return-throw-case': 'error', // return throw case后面要不要加空格'space-unary-ops': ['off', { 'words': true, 'nonwords': false }], // 一元运算符的前/后要不要加空格'spaced-comment': 'off', // 注释风格要不要有空格什么的'strict': 'error', // 使用严格模式'use-isnan': 'error', // 禁止比较时使用NaN,只能用isNaN()'valid-jsdoc': 'off', // jsdoc规则'valid-typeof': 'error', // 必须使用合法的typeof的值'vars-on-top': 'error', // var必须放在作用域顶部'vue/require-v-for-key': 'off', // vue的for循环是否必须有key'wrap-iife': ['error', 'inside'], // 立即执行函数表达式的小括号风格'wrap-regex': 'off', // 正则表达式字面量用小括号包起来'yoda': ['error', 'never'] // 禁止尤达条件}
}
.prettierrc.js
/.prettierrc.jsmodule.exports = {printWidth: 120, //一行的字符数,如果超过会进行换行,默认为80proseWrap: "always",tabWidth: 2,useTabs: false,singleQuote: true, //字符串是否使用单引号,默认为false,使用双引号semi: false, // 不加分号trailingComma: "none", // 结尾处不加逗号htmlWhitespaceSensitivity: "ignore", // 忽略'>'下落问题jsxBracketSameLine: false,arrowParens: "avoid",// 对象的 key 仅在必要时用引号quoteProps: "as-needed",// 大括号内的首尾需要空格bracketSpacing: true,endOfLine: "auto", //避免报错delete (cr)的错// 不需要写文件开头的 @prettierrequirePragma: false,// 不需要自动在文件开头插入 @prettierinsertPragma: false,
}
module.exports = {// 一行最多 100 字符printWidth: 100,// 不使用缩进符,而使用空格useTabs: false,// 使用 2 个空格缩进tabWidth: 2,tabSize: 2,// 行尾需要有分号semi: true,// 使用单引号singleQuote: true,// 对象的 key 仅在必要时用引号quoteProps: 'as-needed',// jsx 不使用单引号,而使用双引号jsxSingleQuote: false,// 末尾不需要逗号 'es5' nonetrailingComma: 'es5',// 大括号内的首尾需要空格bracketSpacing: true,// jsx 标签的反尖括号需要换行jsxBracketSameLine: false,// 箭头函数,只有一个参数的时候,也需要括号arrowParens: 'always',// 每个文件格式化的范围是文件的全部内容rangeStart: 0,rangeEnd: Infinity,// 不需要写文件开头的 @prettierrequirePragma: false,// 不需要自动在文件开头插入 @prettierinsertPragma: false,// 使用默认的折行标准proseWrap: 'preserve',// 根据显示样式决定 html 要不要折行htmlWhitespaceSensitivity: 'css',// 换行符使用 lf 结尾是 \n \r \n\r autoendOfLine: 'lf',
};
npm i eslint@7 -D
{"editor.suggestSelection": "first","terminal.integrated.profiles.windows": {"Git-Bash": {"path": "C:\\Program Files\\Git\\bin\\bash.exe","args": []}},"terminal.integrated.defaultProfile.windows": "Git-Bash","editor.formatOnSave": true, // 不同文件使用默认格式化工具 ,可能会与eslint 冲突"editor.codeActionsOnSave": {"source.fixAll.eslint": false}, // 使用eslint fix"vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue","files.associations": {"*.vue": "vue","*.wpy": "vue","*.wxml": "html","*.wxss": "css"},"eslint.validate": ["html", "vue", "javascript", "javascriptreact"],"vetur.format.defaultFormatter.html": "js-beautify-html","vetur.format.defaultFormatter.js": "prettier-eslint","vetur.format.defaultFormatter.css": "prettier","vetur.format.defaultFormatter.scss": "prettier", // 不检查语法"vetur.format.defaultFormatterOptions": {"js-beautify-html": {"wrap_attributes": "force-aligned","indent_size": 2}},"[json]": {"editor.defaultFormatter": "esbenp.prettier-vscode"},"[html]": {"editor.defaultFormatter": "esbenp.prettier-vscode"},"[vue]": {"editor.defaultFormatter": "esbenp.prettier-vscode"},"[jsonc]": {"editor.defaultFormatter": "esbenp.prettier-vscode"},"[less]": {"editor.defaultFormatter": "esbenp.prettier-vscode"},"[javascript]": {"editor.defaultFormatter": "esbenp.prettier-vscode"},"workbench.colorTheme": "SynthWave '84","editor.fontSize": 16,"explorer.confirmDelete": false,"terminal.integrated.altClickMovesCursor": false,"[scss]": {"editor.defaultFormatter": "esbenp.prettier-vscode"},"[javascriptreact]": {"editor.defaultFormatter": "esbenp.prettier-vscode"},"workbench.editor.enablePreview": false,"git.autofetch": true,"vetur.validation.template": false,"workbench.experimental.editor.dropIntoEditor.enabled": false,"html.format.contentUnformatted": "","security.workspace.trust.untrustedFiles": "open","workbench.layoutControl.enabled": false,"git.enableSmartCommit": true,"window.commandCenter": true,"editor.fontLigatures": false
}
配置二
//.eslintrc.js"rules": {"quotes": 2,"semi": 1,"no-console": 1,"space-before-function-paren": 0}
setting 配置
{"editor.suggestSelection": "first","terminal.integrated.profiles.windows": {"Git-Bash": {"path": "C:\\Program Files\\Git\\bin\\bash.exe","args": []}},"terminal.integrated.defaultProfile.windows": "Git-Bash","editor.formatOnSave": true, // 不同文件使用默认格式化工具 ,可能会与eslint 冲突"editor.formatOnType": true,"eslint.codeAction.showDocumentation": {"enable": true},"editor.codeActionsOnSave": {"source.fixAll.eslint": true}, // 使用eslint fix"vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue","files.associations": {"*.vue": "vue","*.wpy": "vue","*.wxml": "html","*.wxss": "css"},"eslint.validate": ["html", "vue", "javascript", "javascriptreact"],"vetur.format.defaultFormatter.html": "js-beautify-html","vetur.format.defaultFormatter.js": "prettier-eslint","vetur.format.defaultFormatter.css": "prettier","vetur.format.defaultFormatter.scss": "prettier", // 不检查语法"vetur.format.defaultFormatterOptions": {"js-beautify-html": {"wrap_attributes": "force-aligned","indent_size": 2}},"[json]": {"editor.defaultFormatter": "esbenp.prettier-vscode"},"[html]": {"editor.defaultFormatter": "vscode.html-language-features"},"[vue]": {"editor.defaultFormatter": "Vue.volar"},"[jsonc]": {"editor.defaultFormatter": "esbenp.prettier-vscode"},"[less]": {"editor.defaultFormatter": "esbenp.prettier-vscode"},"[javascript]": {"editor.defaultFormatter": "vscode.typescript-language-features"},"workbench.colorTheme": "SynthWave '84","editor.fontSize": 16,"explorer.confirmDelete": false,"terminal.integrated.altClickMovesCursor": false,"[scss]": {"editor.defaultFormatter": "esbenp.prettier-vscode"},"[javascriptreact]": {"editor.defaultFormatter": "esbenp.prettier-vscode"},"workbench.editor.enablePreview": false,"git.autofetch": true,"vetur.validation.template": false,"workbench.experimental.editor.dropIntoEditor.enabled": false,"html.format.contentUnformatted": "","security.workspace.trust.untrustedFiles": "open","workbench.layoutControl.enabled": false,"git.enableSmartCommit": true,"window.commandCenter": true
}
.stylelintrc.js
module.exports = {extends: ["stylelint-config-standard"],/*** 本规则文件,主要对样式进行一些规则限制* 详细规则可以查看* https://stylelint.docschina.org/user-guide/rules/color-hex-length/#color-hex-length*/rules: {// 缩进indentation: 4,// 不允许颜色缩写"color-hex-length": "long",// 指定 16 进制颜色的大小写。"color-hex-case": "lower",// 指定伪元素适用单冒号还是双冒号表示法。"selector-pseudo-element-colon-notation": "double",// 禁止零长度的单位。"length-zero-no-unit": true,// 禁止无效的 16 进制颜色。"color-no-invalid-hex": true,// 允许一行空行"max-empty-lines": 1,// 要求在块的开大括号之前必须有一个空格或不能有空白符。"block-opening-brace-space-before": "always",// 选择器列表的逗号后需要换行符或不允许使用空格。"selector-list-comma-newline-after": "always",// 忽略字体类型格式"font-family-no-missing-generic-family-keyword": null,// 尾符号"declaration-block-trailing-semicolon": null,"declaration-colon-space-after": "always"}
}
.prettierignore
src/common/auth/*
src/package.json
src/package-lock.json
src/yarn.lock
src/yarn-error.log
src/.babelrc
src/.browserslistrc
src/.eslintignore
src/assets
.gitignore
.prettierignore
.editorconfig
.DS_Store
.project
**/node_modules/*
**/.vscode/*
public/*
docker
**/*.yml
**/*.svg
**/*.png
**/*.jpg
**/*.gif
**/*.md
**/*.ejs
**/*.min.js
**/*.min.css
可以参考【建议收藏】全网最全的讲清eslint和prettier的npm包和vscode插件的文章 - 掘金
Vue中ESlint配置文件eslintrc.js文件详解
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
