vscode配置ESLint+Prettier - Code formatter+Vetur

首先是安装ESLint+prettier+vetur:
在这里插入图片描述
vscode 搜索安装即可,然后是注意package是否有这些:
在这里插入图片描述
没有的话npm安装。
之后就是配置这个文件:
在这里插入图片描述

module.exports = {
  root: true,
  parser: 'babel-eslint',
  parserOptions: {
    sourceType: 'module',
  },
  env: {
    browser: true,
    node: true,
  },
  extends: 'eslint:recommended',
  // required to lint *.vue files
  plugins: ['html', 'vue'],
  // check if imports actually resolve
  settings: {
    'import/resolver': {
      webpack: {
        config: 'build/webpack.base.conf.js',
      },
    },
  },
  // add your custom rules here
  rules: {
    // don't require .vue extension when importing
    // 'import/extensions': ['error', 'always', {
    //     'js': 'never',
    //     'vue': 'never'
    // }],
    // allow debugger during development
    'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0,
    /*
     * Possible Errors
     */

    // disallow unnecessary parentheses
    'no-extra-parens': ['error', 'all', { nestedBinaryExpressions: false }],

    // disallow negating the left operand of relational operators
    'no-unsafe-negation': 'error',

    // enforce valid JSDoc comments
    'valid-jsdoc': 'off',

    /*
     * Best Practices
     */

    // enforce return statements in callbacks of array methods
    'array-callback-return': 'error',

    // enforce consistent brace style for all control statements
    curly: ['error', 'multi-line'],

    // enforce consistent newlines before and after dots
    'dot-location': ['error', 'property'],

    // enforce dot notation whenever possible
    'dot-notation': 'error',

    // require the use of === and !==
    eqeqeq: ['error', 'smart'],

    // disallow the use of arguments.caller or arguments.callee
    'no-caller': 'error',

    // disallow empty functions
    'no-empty-function': 'error',

    // disallow unnecessary calls to .bind()
    'no-extra-bind': 'error',

    // disallow unnecessary labels
    'no-extra-label': 'error',

    // disallow leading or trailing decimal points in numeric literals
    'no-floating-decimal': 'error',

    // disallow assignments to native objects or read-only global variables
    'no-global-assign': 'error',

    // disallow the use of eval()-like methods
    'no-implied-eval': 'error',

    // disallow the use of the __iterator__ property
    'no-iterator': 'error',

    // disallow unnecessary nested blocks
    'no-lone-blocks': 'error',

    // disallow multiple spaces
    'no-multi-spaces': 'error',

    // disallow new operators with the String, Number, and Boolean objects
    'no-new-wrappers': 'error',

    // disallow octal escape sequences in string literals
    'no-octal-escape': 'error',

    // disallow the use of the __proto__ property
    'no-proto': 'error',

    // disallow comparisons where both sides are exactly the same
    'no-self-compare': 'error',

    // disallow throwing literals as exceptions
    'no-throw-literal': 'error',

    // disallow unused expressions
    'no-unused-expressions': 'error',

    // disallow unnecessary calls to .call() and .apply()
    'no-useless-call': 'error',

    // disallow unnecessary concatenation of literals or template literals
    'no-useless-concat': 'error',

    // disallow unnecessary escape characters
    'no-useless-escape': 'error',

    // disallow void operators
    'no-void': 'error',

    // require parentheses around immediate function invocations
    'wrap-iife': 'error',

    // require or disallow “Yoda” conditions
    yoda: 'error',

    /*
     * Variables
     */

    // disallow labels that share a name with a variable
    'no-label-var': 'error',

    // disallow initializing variables to undefined
    'no-undef-init': 'error',
    'no-undef': 'off',
    // disallow the use of variables before they are defined
    'no-use-before-define': 'error',

    /*
     * Node.js and CommonJS
     */

    // disallow new operators with calls to require
    'no-new-require': 'error',

    /*
     * Stylistic Issues
     */

    // enforce consistent spacing inside array brackets
    'array-bracket-spacing': 'error',

    // enforce consistent spacing inside single-line blocks
    'block-spacing': 'error',

    // enforce consistent brace style for blocks
    'brace-style': ['error', '1tbs', { allowSingleLine: true }],

    // require or disallow trailing commas
    'comma-dangle': 'error',

    // enforce consistent spacing before and after commas
    'comma-spacing': 'error',

    // enforce consistent comma style
    'comma-style': 'error',

    // enforce consistent spacing inside computed property brackets
    'computed-property-spacing': 'error',

    // require or disallow spacing between function identifiers and their invocations
    'func-call-spacing': 'error',

    // enforce consistent indentation
    indent: ['error', 2, { SwitchCase: 1 }],

    // enforce the consistent use of either double or single quotes in JSX attributes
    'jsx-quotes': 'error',

    // enforce consistent spacing between keys and values in object literal properties
    'key-spacing': 'error',

    // enforce consistent spacing before and after keywords
    'keyword-spacing': 'error',

    // enforce consistent linebreak style
    'linebreak-style': 'error',

    // require or disallow newlines around directives
    'lines-around-directive': 'error',

    // require constructor names to begin with a capital letter
    'new-cap': 'off',

    // require parentheses when invoking a constructor with no arguments
    'new-parens': 'error',

    // disallow Array constructors
    'no-array-constructor': 'error',

    // disallow Object constructors
    'no-new-object': 'error',

    // disallow trailing whitespace at the end of lines
    'no-trailing-spaces': 'error',

    // disallow ternary operators when simpler alternatives exist
    'no-unneeded-ternary': 'error',

    // disallow whitespace before properties
    'no-whitespace-before-property': 'error',

    // enforce consistent spacing inside braces
    'object-curly-spacing': ['error', 'always'],

    // require or disallow padding within blocks
    'padded-blocks': ['error', 'never'],

    // require quotes around object literal property names
    'quote-props': ['error', 'as-needed'],

    // enforce the consistent use of either backticks, double, or single quotes
    quotes: ['error', 'single'],

    // enforce consistent spacing before and after semicolons
    'semi-spacing': 'error',

    // require or disallow semicolons instead of ASI
    // semi: ['error', 'never'],

    // enforce consistent spacing before blocks
    'space-before-blocks': 'error',

    'no-console': 'off',

    // enforce consistent spacing before function definition opening parenthesis
    'space-before-function-paren': ['error', 'never'],

    // enforce consistent spacing inside parentheses
    'space-in-parens': 'error',

    // require spacing around infix operators
    'space-infix-ops': 'error',

    // enforce consistent spacing before or after unary operators
    'space-unary-ops': 'error',

    // enforce consistent spacing after the // or /* in a comment
    'spaced-comment': 'error',

    // require or disallow Unicode byte order mark (BOM)
    'unicode-bom': 'error',

    /*
     * ECMAScript 6
     */

    // require braces around arrow function bodies
    'arrow-body-style': 'error',

    // require parentheses around arrow function arguments
    'arrow-parens': ['error', 'as-needed'],

    // enforce consistent spacing before and after the arrow in arrow functions
    'arrow-spacing': 'error',

    // enforce consistent spacing around * operators in generator functions
    'generator-star-spacing': ['error', 'after'],

    // disallow duplicate module imports
    'no-duplicate-imports': 'error',

    // disallow unnecessary computed property keys in object literals
    'no-useless-computed-key': 'error',

    // disallow unnecessary constructors
    'no-useless-constructor': 'error',

    // disallow renaming import, export, and destructured assignments to the same name
    'no-useless-rename': 'error',

    // require let or const instead of var
    'no-var': 'error',

    // require or disallow method and property shorthand syntax for object literals
    'object-shorthand': 'error',

    // require arrow functions as callbacks
    'prefer-arrow-callback': 'error',

    // require const declarations for variables that are never reassigned after declared
    'prefer-const': 'error',

    // disallow parseInt() in favor of binary, octal, and hexadecimal literals
    'prefer-numeric-literals': 'error',

    // require rest parameters instead of arguments
    'prefer-rest-params': 'error',

    // require spread operators instead of .apply()
    'prefer-spread': 'error',

    // enforce spacing between rest and spread operators and their expressions
    'rest-spread-spacing': 'error',

    // require or disallow spacing around embedded expressions of template strings
    'template-curly-spacing': 'error',

    // require or disallow spacing around the * in yield* expressions
    'yield-star-spacing': 'error',
  },
}

然后在vscode的setting.json文件中配置:
在这里插入图片描述
在这里插入图片描述

{
	// 保存后自动修复格式
	"editor.codeActionsOnSave": {
		"source.fixAll.eslint": true
	},
	//eslint
	"editor.formatOnType": true,
	"eslint.options": {
		//指定eslint配置文件位置
		"extensions": [".js", ".vue"],
		"configFile": ".eslintrc.js" //指定项目根目录中的eslint配置文件
	},
	// vscode默认启用了根据文件类型自动设置tabsize的选项
	"editor.detectIndentation": false,
	// 重新设定tabsize
	"editor.tabSize": 2,
	// #值设置为true时,每次保存的时候自动格式化;值设置为false时,代码格式化请按shift+alt+F
	"editor.formatOnSave": true,
	// 添加 vue 支持
	"eslint.validate": [
		//开启对.vue文件中错误的检查
		"javascript",
		"javascriptreact",
		"vue",
		"html"
	],
	"prettier.semi": false, //去掉代码结尾的分号
	"prettier.singleQuote": true, //使用带引号替代双引号
	//  #让prettier使用eslint的代码格式进行校验
	"prettier.eslintIntegration": true,
	"prettier.tabWidth": 2,
	//  #让函数(名)和后面的括号之间加个空格
	"javascript.format.insertSpaceBeforeFunctionParenthesis": true,
	// #这个按用户自身习惯选择
	// 选择 vue 文件中 template 的格式化工具
	"vetur.format.defaultFormatter.html": "js-beautify-html",
	// vetur 的自定义设置
	"vetur.format.defaultFormatterOptions": {
		"js-beautify-html": {
			// #vue组件中html代码格式化样式
			"wrap_attributes": "auto", //也可以设置为“force-aligned”,效果会不一样
			"wrap_line_length": 160,
			"end_with_newline": false,
			"semi": false,
			"singleQuote": true
		},
		"prettier": {
			"semi": false, // 格式化不加分号
			"singleQuote": true // 格式化以单引号为主
		}
	},
	"vetur.format.defaultFormatter.js": "vscode-typescript",
	"[jsonc]": {
		"editor.defaultFormatter": "esbenp.prettier-vscode"
	},
	"prettier.useTabs": true,
	"files.autoSave": "off",
	"explorer.confirmDelete": false,
	"[json]": {
		"editor.defaultFormatter": "esbenp.prettier-vscode"
	},
	"diffEditor.ignoreTrimWhitespace": false,
	"[vue]": {
		"editor.defaultFormatter": "octref.vetur"
	} // 两个选择器中是否换行
}

设置完之后每次保存就会直接格式化了。

2020/11/10日更新.eslintrc.js文件

module.exports = {
  root: true,
  parserOptions: {
    parser: 'babel-eslint',
    sourceType: 'module'
  },
  env: {
    browser: true,
    node: true,
    es6: true,
  },
  extends: ['plugin:vue/recommended', 'eslint:recommended'],

  // add your custom rules here
  //it is base on https://github.com/vuejs/eslint-config-vue
  rules: {
    "vue/max-attributes-per-line": [2, {
      "singleline": 10,
      "multiline": {
        "max": 1,
        "allowFirstLine": false
      }
    }],
    "vue/singleline-html-element-content-newline": "off",
    "vue/multiline-html-element-content-newline":"off",
    "vue/name-property-casing": ["error", "PascalCase"],
    "vue/no-v-html": "off",
    'accessor-pairs': 2,

    // 禁止或强制在代码块中开括号前和闭括号后有空格
    'block-spacing': [2, 'always'],
    // 强制在代码块中使用一致的大括号风格
    'brace-style': [2, '1tbs', {
      'allowSingleLine': true
    }],
    'camelcase': [0, {
      'properties': 'always'
    }],
    // 要求或禁止末尾逗号
    'comma-dangle': [2, 'never'],
    // 强制在逗号前后使用一致的空格
    'comma-spacing': [2, {
      'before': false,
      'after': true
    }],
    // 强制使用一致的逗号风格
    'comma-style': [2, 'last'],
    // 禁止或强制在计算属性中使用空格
    'computed-property-spacing': 'error',
    'constructor-super': 2,
    // 要求遵循大括号约定 (curly)
    'curly': [2, 'multi-line'],
    // 强制在点号之前或之后换行
    'dot-location': [2, 'property'],
    'eol-last': 2,
    // 要求使用 ===!==
    'eqeqeq': ["error", "always", { "null": "ignore" }],
    // 要求或禁止在函数标识符和其调用之间有空格
    'func-call-spacing': 'error',
    
    'handle-callback-err': [2, '^(err|error)$'],
    'indent': [2, 2, {
      'SwitchCase': 1
    }],
    // 强制在 JSX 属性中一致地使用双引号或单引号
    'jsx-quotes': [2, 'prefer-single'],
    // 强制在对象字面量的属性中键和值之间使用一致的间距
    'key-spacing': [2, {
      'beforeColon': false,
      'afterColon': true
    }],
    // 强制在关键字前后使用一致的空格
    'keyword-spacing': [2, {
      'before': true,
      'after': true
    }],
    // 强制使用一致的换行风格
    'linebreak-style': 'error',
    // 要求构造函数首字母大写
    'new-cap': [2, {
      'newIsCap': true,
      'capIsNew': false
    }],
    // 强制或禁止调用无参构造函数时有圆括号
    'new-parens': 2,
    // 禁用 Array 构造函数
    'no-array-constructor': 2,
    'no-caller': 2,
    // 禁用 console
    'no-console': 'off',
    'no-class-assign': 2,
    'no-cond-assign': 2,
    'no-const-assign': 2,
    'no-control-regex': 0,
    'no-delete-var': 2,
    'no-dupe-args': 2,
    'no-dupe-class-members': 2,
    'no-dupe-keys': 2,
    'no-duplicate-case': 2,
    'no-empty-character-class': 2,
    // 空方法
    'no-empty-function': ["error", { "allow": ["methods"] }],
    'no-empty-pattern': 2,
    'no-eval': 2,
    'no-ex-assign': 2,
    'no-extend-native': 2,
    // 禁止不必要的函数绑定 
    'no-extra-bind': 2,
    'no-extra-boolean-cast': 2,
    // 禁止不必要的括号
    'no-extra-parens': [2, 'functions'],
    // 禁用不必要的标签
    'no-extra-label': 'error',
    'no-fallthrough': 2,
    // 禁止浮点小数
    'no-floating-decimal': 2,
    'no-func-assign': 2,
    // 禁止对原生对象或只读的全局对象进行赋值
    "no-global-assign": ["error", { "exceptions": ["Object"] }],
    // 禁用隐式的eval()
    'no-implied-eval': 2,
    'no-inner-declarations': [2, 'functions'],
    'no-invalid-regexp': 2,
    'no-irregular-whitespace': 2,
    // 禁用迭代器
    'no-iterator': 2,
    // 不允许标签与变量同名
    'no-label-var': 2,
    'no-labels': [2, {
      'allowLoop': false,
      'allowSwitch': false
    }],
    // 禁用不必要的嵌套块
    'no-lone-blocks': 2,
    'no-mixed-spaces-and-tabs': 2,
    // 禁止出现多个空格
    'no-multi-spaces': 2,
    'no-multi-str': 2,
    // 不允许有空行
    'no-multiple-empty-lines': [2, {
      // 最大空行数
      'max': 0
    }],
    'no-native-reassign': 2,
    'no-negated-in-lhs': 2,
    // 禁用 Object 的构造函数
    'no-new-object': 2,
    // 禁止调用 require 时使用 new 操作符
    'no-new-require': 2,
    'no-new-symbol': 2,
    // 此规则目的在于消除通过 new 操作符使用 String、Number 和 Boolean 。
    'no-new-wrappers': 2,
    'no-obj-calls': 2,
    'no-octal': 2,
    // 该规则禁止在字符串字面量中使用八进制转义序列。
    'no-octal-escape': 2,
    'no-path-concat': 2,
    // 禁用__proto__
    'no-proto': 2,
    'no-redeclare': 2,
    'no-regex-spaces': 2,
    'no-return-assign': [2, 'except-parens'],
    'no-self-assign': 2,
    // 禁止自身比较
    'no-self-compare': 2,
    'no-sequences': 2,
    'no-shadow-restricted-names': 2,
    'no-spaced-func': 2,
    'no-sparse-arrays': 2,
    'no-this-before-super': 2,
    // 此规则目的在于保持异常抛出的一致性,通过禁止抛出字面量和那些不可能是 Error 对象的表达式。
    'no-throw-literal': 2,
    // 禁用行尾空格
    'no-trailing-spaces': 2,
    // 未使用的变量
    'no-undef': 0,
    // 禁止将变量初始化为 undefined
    'no-undef-init': 2,
    // 禁止出现令人困惑的多行表达式
    'no-unexpected-multiline': 2,
    // 禁用一成不变的循环条件
    'no-unmodified-loop-condition': 2,
    // 禁止可以在有更简单的可替代的表达式时使用三元操作符
    'no-unneeded-ternary': [2, {
      'defaultAssignment': false
    }],
    // 禁止在 return、throw、continue 和 break 语句之后出现不可达代码
    'no-unreachable': 2,
    // 禁止对关系运算符的左操作数使用否定操作符
    'no-unsafe-negation': 'error',
    // 禁止在 finally 语句块中出现控制流语句
    'no-unsafe-finally': 2,
    // 此规则旨在消除不使用的表达式,这些表达式对程序的状态没有影响。
    'no-unused-expressions': 'error',
    // 禁止出现未使用过的变量
    'no-unused-vars': [0, {
      'vars': 'all',
      'args': 'none'
    }],
    // 禁止不必要的 .call() 和 .apply()
    'no-useless-call': 2,
    // 禁止在变量定义之前使用它们
    'no-use-before-define': 'error',
    // 禁止没有必要的字符拼接
    'no-useless-concat': 'error',
    // 禁用不必要的转义字符
    'no-useless-escape': 0,
    // 禁止使用void操作符
    'no-void': 'error',
    // 禁止属性前有空白
    'no-whitespace-before-property': 2,
    // 禁用 with 语句
    'no-with': 2,
    // 强制函数中的变量要么一起声明要么分开声明
    'one-var': [2, {
      'initialized': 'never'
    }],
    // 强制操作符使用一致的换行符
    'operator-linebreak': [2, 'after', {
      'overrides': {
        '?': 'before',
        ':': 'before'
      }
    }],
    // 要求或禁止块内填充
    'padded-blocks': [2, 'never'],
    // 强制使用一致的反勾号、双引号或单引号
    'quotes': [2, 'single', {
      'avoidEscape': true,
      'allowTemplateLiterals': true
    }],
    // 要求对象字面量属性名称使用引号
    'quote-props': ['error', 'as-needed'],
    // 要求或禁止使用分号代替 ASI
    'semi': [2, 'never'],
    // 强制分号之前和之后使用一致的空格
    'semi-spacing': [2, {
      'before': false,
      'after': true
    }],
    // 强制在块之前使用一致的空格
    'space-before-blocks': [2, 'always'],
    // 强制在 function的左括号之前使用一致的空格
    'space-before-function-paren': [0, 'never'],
    // 强制在圆括号内使用一致的空格
    'space-in-parens': [2, 'never'],
    // 要求操作符周围有空格
    'space-infix-ops': 2,
    // 强制在一元操作符前后使用一致的空格
    'space-unary-ops': [2, {
      'words': true,
      'nonwords': false
    }],
    // 强制在注释中 // 或 /* 使用一致的空格
    'spaced-comment': [2, 'always', {
      'markers': ['global', 'globals', 'eslint', 'eslint-disable', '*package', '!', ',']
    }],
    // 要求或禁止 Unicode 字节顺序标记 (BOM)
    'unicode-bom': 'error',
    // 要求使用 isNaN() 检查 NaN
    'use-isnan': 2,
    // 强制 typeof 表达式与有效的字符串进行比较
    'valid-typeof': 2,
    // 该规则要求所有的立即执行函数表达式使用括号包裹起来。
    'wrap-iife': [2, 'any'],
    // 要求或者禁止Yoda条件
    'yoda': [2, 'never'],
    // 禁用 debugger
    'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0,
    // 强制在大括号中使用一致的空格
    'object-curly-spacing': [2, 'always', {
      objectsInObjects: false
    }],
    // 强制数组方括号中使用一致的空格
    'array-bracket-spacing': [2, 'never'],

    /*
     * ECMAScript 6
     */
    // 该规则可以强制或禁止在箭头函数体的周围使用大括号。
    'arrow-body-style': 'error',

    // 无论参数如何,该规则都会在箭头函数参数周围加上括号
    // "always" (默认)在所有情况下都需要参数。
    // "as-needed" 当只有一个参数时允许省略参数。
    'arrow-parens': ['error', 'as-needed'],
    // 规则在箭头函数的箭头(=>)之前/之后标准化间距样式。
    'arrow-spacing': [2, {
      'before': true,
      'after': true
    }],
    // 强制 generator 函数中 * 号周围使用一致的空格
    'generator-star-spacing': [2, {
      'before': true,
      'after': true
    }],
    // 禁止重复模块导入
    'no-duplicate-imports': 'error',
    'no-useless-computed-key': 2,
    'no-useless-constructor': 2,
    // 禁止在 importexport 和解构赋值时将引用重命名为相同的名字
    'no-useless-rename': 'error',
    // 不允许有var
    'no-var': 'error',
    // 要求或禁止对象字面量中方法和属性使用简写语法
    'object-shorthand': 'error',
    // 要求回调函数使用箭头函数
    'prefer-arrow-callback': 'error',
    // 变量优先const
    "prefer-const": 2,
    // 禁用 parseInt() 和 Number.parseInt(),使用二进制,八进制和十六进制字面量
    'prefer-numeric-literals': 'error',
    // 要求使用剩余参数而不是 arguments
    'prefer-rest-params': 'error',
    // 要求使用扩展运算符而非 .apply()
    'prefer-spread': 'error',
    // 强制剩余和扩展运算符及其表达式之间有空格
    'rest-spread-spacing': 'error',
    // 要求或禁止模板字符串中的嵌入表达式周围空格的使用
    'template-curly-spacing': [2, 'never'],
    // 强制在 yield* 表达式中 * 周围使用空格
    'yield-star-spacing': [2, 'both'],
  }
}

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

别出声~~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值