eslint安装配置-自动格式化

eslint安装配置-自动格式化

1、推荐编辑器vscode,文件-首选项-设置-打开设置(json),添加代码

{
  "eslint.options": {
    "extensions": [
      ".js",
      ".vue"
    ]
  },
  "eslint.validate": [
    "javascript",
    {
      "language": "vue",
      "autoFix": true
    },
    "html",
    "vue"
  ],
  "eslint.autoFixOnSave": true,
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
  },
  "eslint.codeAction.disableRuleComment": {},
  "eslint.alwaysShowStatus": true,
  "[vue]": {
    "editor.defaultFormatter": "yoyo930021.vuter"
  },
}

在这里插入图片描述
2、在vscode扩展中安装eslint扩展报包,点击允许,右下角为启用状态
在这里插入图片描述
3、添加.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/html-self-closing": ["error", {
      "html": {
        "void": "always",
        "normal": "never",
        "component": "always"
      },
      "svg": "always",
      "math": "always"
    }],
    "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,
    'arrow-spacing': [2, {
      'before': true,
      'after': true
    }],
    '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'],
    'constructor-super': 2,
    'curly': [2, 'multi-line'],
    'dot-location': [2, 'property'],
    'eol-last': 2,
    'eqeqeq': ["error", "always", {"null": "ignore"}],
    'generator-star-spacing': [2, {
      'before': true,
      'after': true
    }],
    'handle-callback-err': [2, '^(err|error)$'],
    'indent': [2, 2, {
      'SwitchCase': 1
    }],
    'jsx-quotes': [2, 'prefer-single'],
    'key-spacing': [2, {
      'beforeColon': false,
      'afterColon': true
    }],
    'keyword-spacing': [2, {
      'before': true,
      'after': true
    }],
    'new-cap': [2, {
      'newIsCap': true,
      'capIsNew': false
    }],
    'new-parens': 2,
    'no-array-constructor': 2,
    'no-caller': 2,
    '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-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-fallthrough': 2,
    'no-floating-decimal': 2,
    'no-func-assign': 2,
    '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': 1
    }],
    'no-native-reassign': 2,
    'no-negated-in-lhs': 2,
    'no-new-object': 2,
    'no-new-require': 2,
    'no-new-symbol': 2,
    'no-new-wrappers': 2,
    'no-obj-calls': 2,
    'no-octal': 2,
    'no-octal-escape': 2,
    'no-path-concat': 2,
    '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,
    'no-throw-literal': 2,
    'no-trailing-spaces': 2,
    'no-undef': 2,
    'no-undef-init': 2,
    'no-unexpected-multiline': 2,
    'no-unmodified-loop-condition': 2,
    'no-unneeded-ternary': [2, {
      'defaultAssignment': false
    }],
    'no-unreachable': 2,
    'no-unsafe-finally': 2,
    'no-unused-vars': [2, {
      'vars': 'all',
      'args': 'none'
    }],
    'no-useless-call': 2,
    'no-useless-computed-key': 2,
    'no-useless-constructor': 2,
    'no-useless-escape': 0,
    'no-whitespace-before-property': 2,
    '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
    }],
    'semi': [2, 'never'],
    'semi-spacing': [2, {
      'before': false,
      'after': true
    }],
    'space-before-blocks': [2, 'always'],
    'space-before-function-paren': [2, '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', '!', ',']
    }],
    'template-curly-spacing': [2, 'never'],
    'use-isnan': 2,
    'valid-typeof': 2,
    'wrap-iife': [2, 'any'],
    'yield-star-spacing': [2, 'both'],
    'yoda': [2, 'never'],
    'prefer-const': 2,
    'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0,
    'object-curly-spacing': [2, 'always', {
      objectsInObjects: false
    }],
    'array-bracket-spacing': [2, 'never']
  }
}

常用注释

	== 报错 //eslint-disable-line 
	全局变量 epgis   /*global epgis*/

4.0的脚手架搭的项目应该都没问题,保存时自动格式化代码,版本老点的需要安装babel-eslint ,eslint、eslint-plugin-vue,这个需要测试,正常情况都能好使

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: eslint-plugin-organize-imports是一个用于ESLint的插件,它可以帮助我们自动整理和优化我们的导入语句。 在编写JavaScript代码时,我们通常需要引入其他模块或库的功能。在大型项目中,可能会有许多导入语句,而这些语句的顺序和组织方式可能会导致代码的可读性和维护性下降。 eslint-plugin-organize-imports提供了一种自动化的方式来整理和优化导入语句。通过在代码中添加特定的注释指令,我们可以告诉该插件如何组织我们的导入语句。例如,我们可以使用注释指令`/* organize-imports */`来启用导入语句的整理功能。 一旦启用了eslint-plugin-organize-imports,它将扫描我们的代码并分析我们的导入语句。然后,它会根据一些配置规则和选项,对导入语句进行排序、分组和优化。 这个插件可以处理各种类型的导入语句,包括默认导入、命名导入和命名空间导入。它可以将相似的导入语句合并到同一个组中,删除不需要的导入语句,并根据规则对它们进行排序。这样,我们就可以更清楚地看到我们的模块依赖,并提高代码的可读性。 总结起来,eslint-plugin-organize-imports是一个帮助我们自动整理和优化导入语句的ESLint插件。它可以根据我们的配置规则对导入语句进行排序、分组和优化,从而提高代码的可读性和维护性。 ### 回答2: eslint-plugin-organize-imports是一个ESLint插件,用于自动整理和规范化import语句。它可以帮助开发者在代码中保持一致的import风格和结构,提高代码的可读性和维护性。 该插件提供了一系列规则,用于检测和修复import语句的错误和风格问题。它可以自动排序和格式化import语句,使其按照一定的顺序和规范排列。例如,可以按照字母顺序对import语句进行排序,将默认导出放在非默认导出之前,将外部模块的导入语句放在内部模块之前等等。通过使用该插件,可以确保在不同的文件和团队成员之间保持一致的import风格,减少代码冲突和维护难度。 除了排序和格式化import语句,eslint-plugin-organize-imports还提供了其他的规则,如删除未使用的导入、合并重复的导入、使用正确的导入别名等。这些规则可以帮助开发者减少无用的导入语句和重复的代码,提高代码质量和执行效率。 总之,eslint-plugin-organize-imports是一个功能强大的ESLint插件,可以帮助开发者自动整理和规范化import语句。它可以提高代码的可读性和维护性,减少冲突和错误的发生,是一个非常实用的工具。 ### 回答3: eslint-plugin-organize-imports 是一个 ESLint 插件,用于帮助开发者组织和管理 JavaScript 代码文件的 import 语句。 在 JavaScript 开发中,我们经常需要引入其他模块或库,以便使用它们提供的功能。代码中的 import 语句就是用来实现这一功能的。 eslint-plugin-organize-imports 通过对 import 语句的静态分析,提供了一些自动化的代码组织和规范化功能,以保持代码的整洁性和一致性。 该插件可以帮助我们实现以下功能: 1. 自动按照字母顺序对 import 语句进行排序。这样做可以提高代码可读性,减少重复的 import 语句,避免遗漏某些模块。 2. 自动移除未使用的 import 语句。当项目逐渐变大时,可能会出现一些无用的 import 语句,通过该插件可以自动删除这些未使用的 import 语句,提高代码的执行效率。 3. 自动分组 import 语句。当引入的模块来自不同的来源时,可以将它们按照不同的分组进行组织,以提高代码的可读性和维护性。 4. 自定义配置选项。该插件提供了丰富的配置选项,可以根据项目的需求,对不同的组织规则进行灵活配置。 借助 eslint-plugin-organize-imports,我们可以轻松地优化 JavaScript 代码的 import 语句,使其更加整齐规范,提高代码的可读性和可维护性。这对于大型项目和团队合作开发尤其重要。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值