首页 > 其他分享 >IDEA 配置 ESLint

IDEA 配置 ESLint

时间:2023-07-25 23:00:45浏览次数:39  
标签:false no 配置 spacing IDEA ESLint true before

一直想将 ESLint 配置推广到团队中,只是自己也没完全摸透,也不好推广,于是记录学习一下。平时工作的环境主要为 IDEA ,所以配置 ESLint 也主要从 IDEA 的角度入手。

ESLint 简介

官网介绍:ESLint 查找并修复 JavaScript 代码中的问题,静态地分析你的代码以快速发现问题。它内置于大多数文本编辑器中,但你也可以在持续集成管道中运行 ESLint。

关键词: 发现问题,修复问题。

使用方式:大家使用一套相同的 ESLint 配置来约束大家的代码,规避基础语法问题,保证代码的一致性。

官网地址:https://eslint.nodejs.cn/

IDEA 配置 ESLint

IDEA 配置 ESLint 的位置如截图所示:

image-20230715213956567

其中 ESLint 配置文件一般会放到项目的根目录下,配置文件名称叫.eslintrc.js。这个规则配置起来一大坨,绝大多数人都不是很清楚每一个细节,如果你不知道自己的项目该怎么配置,可以先看看自己的项目主要用的框架是什么,例如Element UI,再去那个框架的源码库中找.eslintrc.js配置文件,直接拿过来使用,这样你的代码和框架的代码的规范就能保持一致,或者你只需要稍作修改就能拿到项目上使用,过程中遇到不合适的地方再微调即可。

// ESlint 检查配置
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,
    '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']
  }
}

配置完毕后,IDEA 会按照 ESLint 文件中配置的规则来对代码进行检查,并且报红提示,如下图。

image-20230715214529183

修复 ESLint 报红

我们可以借助工具来修复不符合语法规则的报红错误,毕竟能让程序来做的事情尽量让程序来实现,主要有几种方式:
一、量很少的情况下,自己手动一行行修复报红的位置。

二、保存时执行eslint --fix脚本自动修复,需要勾选配置项中的“保存时操作”,配置后按Ctrl+S保存时就会自动帮你修复。

image-20230715215307582

三、 快捷修复,右键点击唤出快捷菜单,点击“Fix ESLint Problems"。

image-20230715215334806

四、持续集成的时候执行eslint --fix脚本来自动修复,其实和第二种方式类似,只是执行的时机不一致。

标签:false,no,配置,spacing,IDEA,ESLint,true,before
From: https://www.cnblogs.com/zehai-cool/p/17581263.html

相关文章

  • 多环境开发配置
         ......
  • 修改jar 配置文件
    解压jar包修改配置文件添加MANIFEST.MF并打成jar包(注意不压缩)执行jar包//复制jar包cp/data/single/job/champion-job-single-sms-1.0/champion-job-single-sms-1.0.jar.//解压jar包jar-xvfchampion-job-single-sms-1.0.jar//修改配置文件cdconfigs/vimdb.si......
  • FeignClient 动态获取nacos中的配置,线上更改后能热更新
    1.设置nacos变量在一个类中@Component@RefreshScope@ConfigurationProperties(prefix="testC")publicclassSupplierInfoRakuten{privateStringversion;publicStringgetVersion(){returnversion;}publicvoidsetVersion(Str......
  • quartz.net 配置UseClustering
    Quartz.NET配置UseClustering概述在开始之前,我们需要明确一些概念。Quartz.NET是一个强大且灵活的开源任务调度库,它可以帮助我们在.NET应用程序中实现各种定时任务。而UseClustering则是Quartz.NET提供的一项功能,用于在多个节点之间分配和处理任务。本文将教你如何通过......
  • mcan报文过滤配置
    mcan报文过滤配置reference:https://onlinedocs.microchip.com/oxy/GUID-2ACDA668-0A87-46A1-B7FC-9DC74A5461AD-en-US-2/GUID-38D27E02-B5E3-4459-9DBA-FD12144C7EE4.html......
  • CentOS-7配置YUM源
    前言:Yum源分类:网络Yum源:在能连接互联网的服务器上进行配置,配置简单,可选择性强,能获取的包也更多;本地Yum源:利用挂载系统镜像包进行配置,受限于镜像包,能获取的包相对较少;一):网络Yum源配置(以阿里源举例)备份系统自带源文件cd/etc/yum.repos.d/mkdirbakmv*.repobak配置阿里云yum......
  • 平台关闭影响业务开展?IPIDEA为您提供稳定可靠的全球IP代理服务
    最近几天非常抱歉收到这样的消息:某些平台效仿911代理商,突然关闭服务,这使得大量用户的业务不能正常开展。对于用户来讲,遭遇这种突如其来的情况实在令人沮丧,我们建议您尽快与该平台的客服团队联系,了解具体情况。同时,如果您不想因为平台的原因影响到公司业务的进度,也可以寻找其他可靠......
  • springsession 配置redis集群
    SpringSession配置Redis集群教程1.流程概述在本教程中,我们将详细介绍如何使用SpringSession来配置Redis集群。整个流程可以总结为以下几个步骤:添加SpringSession和Redis依赖配置Redis集群连接信息配置SpringSession使用Redis集群测试SpringSession与Redis集群的连接......
  • springbootredis集群配置
    SpringBootRedis集群配置在分布式系统中,Redis是一个常用的内存数据库,用于缓存和存储数据。Redis集群是多个Redis实例的组合,通过分片和复制技术,提供高可用性和可扩展性。本文将介绍如何在SpringBoot中配置Redis集群,并提供相应的代码示例。1.搭建Redis集群首先,我们需要搭建Red......
  • springboot mysql 配置 propertis
    SpringBootMySQL配置Properties在SpringBoot应用程序中,我们经常需要使用MySQL数据库来存储和检索数据。为了连接和配置MySQL数据库,我们可以使用application.properties文件。这篇文章将向您展示如何使用SpringBoot的application.properties文件来配置MySQL数据库连接。1.引......