Add ESLint to vue project

There are two ways to configure eslint:

Annotation configuration: Use js annotations to embed ESLint configuration information directly into a file
Configuration file: Use a js, JSON or YAML file to specify configuration information for the entire directory and its subdirectories. These configurations can be written in a file named .eslintrc.* or in the eslintConfig item in the package.json file, both of which ESLint will automatically find and read, or you can specify a configuration on the command line document.
There are several things that can be configured:

Environment: The environment in which your script will run. Each environment brings a specific set of predefined global variables.
Global variables: Additional global variables are accessed during script execution.
Rules: Those rules are used, and what is the level of the rules.
We use the configuration file .eslintrc.js to configure here, which exports a module for ESLint to recognize.

// http://eslint.org/docs/user-guide/configuring

module.exports = {
  root: true,
  parser: 'babel-eslint',//Parser, here we use babel-eslint
  parserOptions: {
    sourceType: 'module'//The type is module, because the code uses ECMAScript modules
  },
  env: {
    browser: true,//Predefined global variables, here is the browser environment
  },
  // https://github.com/feross/standard/blob/master/RULES.md#javascript-standard-style
  //extends: 'standard', //Extensions, rules can be extended by strings or an array
  // required to lint *.vue files
  plugins: [
   'html' //Plugin, this plugin is used to identify the js code in the file, it can be identified without MIME type identification and script tag, so it is used to identify the js code in the .vue file
  ],
  // add your custom rules here
  'rules': {
    //Write custom rules here
  }
}


The rules of ESLint have three levels:

"off" or 0, if this rule is not enabled,
"warn" or 1, there will be a warning
"error" or 2, if there is a problem, an error will be reported
Sometimes there are special cases in the code that require us to add a line Or to turn off ESLint detection on certain lines, you can use comments:

the following code will turn off all rules
/* eslint-disable */

alert('foo');

/* eslint-enable */


The following code will turn off all rules for a row
alert('foo'); // eslint-disable-line

// eslint-disable-next-line
alert('foo');


The following code turns off the specified rule on a line
alert('foo'); // eslint-disable-line no-alert

// eslint-disable-next-line no-alert
alert('foo');


Common rules:

For details of the rules, please visit the official ESLint website http://eslint.org/docs/rules/

'rules': {
      "comma-dangle": ["error", "never"], // Whether to allow trailing commas in objects
      "no-cond-assign": 2, //The assignment operator is not allowed in the condition of the conditional statement
      "no-console": 2, // console statement is not allowed
      "no-constant-condition": 2, //Constant quantities are not allowed in the condition of the conditional statement
      "no-control-regex": 2, //Control characters are not allowed in regular expressions
      "no-debugger": 2, //The debugger statement is not allowed
      "no-dupe-args": 2, //Dupe arguments are not allowed in function definition
      "no-dupe-keys": 2, //Dupe keys are not allowed in the object
      "no-duplicate-case": 2, //Duplicate case labels are not allowed in switch statements
      "no-empty": 2, //Empty code blocks are not allowed
      "no-empty-character-class": 2, //Empty character groups are not allowed in regular expressions
      "no-ex-assign": 2, //Reassign exception variable is not allowed in try catch statement
      "no-extra-boolean-cast": 2, // do not allow unnecessary boolean casts
      "no-extra-parens": 0, // do not allow unnecessary parentheses
      "no-extra-semi": 2, // Do not allow unnecessary semicolons
      "no-func-assign": 2, // disallow reassignment of function declarations
      "no-inner-declarations": ["error", "functions"], // Function declarations in nested code blocks are not allowed
      "no-invalid-regexp": 2, //Do not allow invalid regular expressions in RegExp constructor
      "no-irregular-whitespace": 2, //Irregular spaces are not allowed
      "no-negated-in-lhs": 2, // do not allow the negation of the leftmost operand in an in expression statement
      "no-obj-calls": 2, // disallow calling global object properties as functions
      "no-regex-spaces": 2, // Multiple consecutive spaces are not allowed in regular expressions
      "quote-props": 2, //Does the property name in the object need to be enclosed in quotes
      "no-sparse-arrays": 2, //Empty positions are not allowed in the array
      "no-unreachable": 2, //Unreachable statements are not allowed after return, throw, continue, break statements
      "use-isnan": 2, // use isNaN() when asking to check for NaN
      "valid-jsdoc": ["error", {
          "requireReturn": false,
          "requireParamDescription": false,
          "requireReturnDescription": true
      }], //Force JSDoc comments
      "valid-typeof": ["error", {
          "requireStringLiterals": true
      }], // Force a valid string when comparing with typeof expressions
      "block-scoped-var": 2, // put variable declarations in appropriate code blocks
      "complexity": 0, // limit the complexity of the conditional statement
      "consistent-return": 2, // Force the return statement to return a value with or without a return value
      "curly": ["error", "all"], //Force the style of curly braces
      "default-case": 0, //A default statement is required in the switch statement
      "dot-notation": ["error", {"allowKeywords": false, "allowPattern": ""}], //Use dots when getting object properties
      "eqeqeq": ["error", "smart"], //Use strict equality when comparing
      "no-alert": 1, //Alert, confirm, prompt statements are not allowed
      "no-caller": 2, //The arguments.callee and arguments.caller properties are not allowed
      "guard-for-in": 0, //Monitor the for in loop to prevent unexpected situations
      "no-div-regex": 2, //Cannot use regular expressions that look like division
      "no-else-return": 0, //If the if statement has a return, the return in the else does not need to be placed in the else
      "no-labels": ["error", {
          "allowLoop": false,
          "allowSwitch": false
      }], // tag statements are not allowed
      "no-eq-null": 2, // do not allow == or != for null
      "no-eval": 2, // eval() is not allowed
      "no-extend-native": 2, // not allowed to extend native objects
      "no-extra-bind": 2, // disallow unnecessary function binding
      "no-fallthrough": 2, // does not allow switch to execute all cases in sequence
      "no-floating-decimal": 2, // disallow missing digits for floating point numbers
      "no-implied-eval": 2, // Implied eval() is not allowed
      "no-iterator": 2, //The __iterator__ attribute is not allowed
      "no-lone-blocks": 2, // disallow unnecessary nested code blocks
      "no-loop-func": 2, // function declarations are not allowed in loop statements
      "no-multi-spaces": 2, // no extra spaces allowed
      "no-multi-str": 2, //Do not allow \ to wrap strings
      "no-global-assign": 2, // disallow reallocation of native objects
      "no-new": 2, //No assignment or comparison after new instance is not allowed
      "no-new-func": 2, // new Function is not allowed
      "no-new-wrappers": 2, // new String, Number and Boolean objects are not allowed
      "no-octal": 2, // octal literals are not allowed
      "no-octal-escape": 2, // octal escape sequences are not allowed
      "no-param-reassign": 0, //Reassignment of function parameters is not allowed "no-proto": 2, //The __proto__ property is not allowed
      "no-redeclare": 2, //Do not allow repeated declaration of variables
      "no-return-assign": 2, // assign statement is not allowed in return statement
      "no-script-url": 2, // javascript is not allowed: void(0)
      "no-self-compare": 2, //Don't allow yourself to compare with yourself
      "no-sequences": 2, //comma expressions are not allowed
      "no-throw-literal": 2, // disallow throwing literal errors throw "error"
      "no-unused-expressions": 2, //unused expressions are not allowed
      "no-void": 2, // the void operator is not allowed
      "no-warning-comments": [1, {"terms": ["todo", "fixme", "any other term"]}], //不允许警告备注
      "no-with": 2, //with statement is not allowed
      "radix": 1, //Force use of radix when using parseInt to specify decimal or other bases
      "vars-on-top": 0, //vars must be placed at the top of the scope
      "wrap-iife": [2, "any"], //Bracket style for immediate execution of expressions
      "yoda": [2, "never", {"exceptRange": true}], // yoda condition is not allowed in if condition
      "strict": [2, "function"], //use strict mode
      "no-catch-shadow": 2, //The err variable accepted by the try catch statement is not allowed to have the same name as the external variable "no-delete-var": 2, //The delete operator is not allowed
      "no-label-var": 2, //Do not allow labels and variables with the same name
      "no-shadow": 2, //A variable in the outer scope cannot have the same name as a variable or parameter in the scope it contains
      "no-shadow-restricted-names": 2, //js keywords and reserved words cannot be used as function names or variable names
      "no-undef": 2, // undeclared variables are not allowed
      "no-undef-init": 2, // Assign undefined to variables when initializing variables is not allowed
      "no-undefined": 2, // not allowed to use undefined as an identifier
      "no-unused-vars": [2, {"vars": "all", "args": "after-used"}], // Variables or parameters not used after declaration are not allowed
      "no-use-before-define": [2, "nofunc"], // not allowed to use variable before undefined
      "indent": 2, //enforce consistent indentation style
      "brace-style": [2, "1tbs", { "allowSingleLine": false}], // Braces style
      "camelcase": [2, {"properties": "never"}], //Force camelcase naming rules
      "comma-style": [2, "last"], //comma style
      "consistent-this": [0, "self"], //The same style is used when getting the this of the current environment
      "eol-last": 2, //The file ends with a newline
      "func-names": 0, //function expressions must have names
      "func-style": 0, //Function style, which specifies that only function declarations or function expressions can be used
      "key-spacing": [2, {"beforeColon": false, "afterColon": true}], // spaces before and after colons in object literals
      "max-nested-callbacks": 0, //callback nesting depth
      "new-cap": [2, {"newIsCap": true, "capIsNew": false}], //The first letter of the constructor name should be capitalized
      "new-parens": 2, //The constructor must have parentheses when new
      "newline-after-var": 0, //There must be an empty line after variable declaration
      "no-array-constructor": 2, // array constructor is not allowed
      "no-inline-comments": 0, // disallow inline comments
      "no-lonely-if": 0, //don't allow only if statement inside else statement
      "no-mixed-spaces-and-tabs": [2, "smart-tabs"], // Mixed tabs and spaces are not allowed
      "no-multiple-empty-lines": [2, {"max": 2}], //Empty lines cannot exceed two lines
      "no-nested-ternary": 2, // Nested ternary operators are not allowed
      "no-new-object": 2, // prohibit the use of new Object()
      "fun-call-spacing": 2, //When the function is called, there can be no space between the function name and ()
      "no-ternary": 0, // ternary operator is not allowed
      "no-trailing-spaces": 2, //No spaces are allowed at the end of a line
      "no-underscore-dangle": 2, // Disallow identifiers starting with underscores
      "no-extra-parens": 0, // no extra parentheses are allowed
      "one-var": 0, // Force variable declarations together
      "operator-assignment": 0, // style of assignment operator
      "padded-blocks": [2, "never"], //Whether there is an empty line at the beginning and end of the line in the block
      "quote-props": 0, // put quotes around property names in object literals
      "quotes": [1, "single", "avoid-escape"], //引号风格
      "semi": [2, "always"], //Force statement to end with semicolon
      "semi-spacing": [2, {"before": false, "after": true}], // Spaces before and after points
      "sort-vars": 0, // sort when variables are declared
      "space-before-blocks": [2, "always"], //space before blocks
      "space-before-function-paren": [2, {"anonymous": "always", "named": "never"}], //space before function definition parentheses
      "space-infix-ops": [2, {"int32Hint": true}], //space around operators
      "keyword-spacing": 2, //spaces before and after keywords
      "space-unary-ops": [2, { "words": true, "nonwords": false}], //Do not add spaces before and after unary operators
      "wrap-regex": 2, //Regular expression literals are enclosed in parentheses
      "no-var": 0, // use let and const instead of var
      "generator-star-spacing": [2, "both"], // spaces before and after the generator function
      "max-depth": 0, //nested block depth
      "max-len": 0, //Maximum length of a line, in characters
      "max-params": 0, // How many parameters the function can have at most
      "max-statements": 0, // There are at most several declarations in the function
      "no-bitwise": 0, // bitwise operators are not allowed
      "no-plusplus": 0 // The ++ -- operator is not allowed
  }

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326847516&siteId=291194637