Eslint pagsasaayos at paglalarawan ng panuntunan
Eslint Configuration
pag-install
Maaari itong mai-install sa buong mundo o sa ilalim ng proyekto.
Ang sumusunod ay isang halimbawa ng pag-install ng proyekto sa proyekto. Idagdag lamang ang sumusunod na pagsasaayos sa package.json at i-install ito:
“eslint”: “^4.11.0”
Pag-configure
Mayroong dalawang mga pamamaraan sa pagsasaayos, ngunit inirerekumenda na gamitin ang form ng pagsasaayos ng file, na medyo malaya at madaling mapanatili.
Paano gamitin ang pagsasaayos ng file: Sa direktoryo ng ugat ng proyekto, lumikha ng isang bagong file na pinangalanang .eslintrc at magdagdag ng ilang mga panuntunan sa pag-check sa file na ito.
Paraan ng pagsasaayos ng file
Env: Anong kapaligiran ang tatakbo sa iyong script
Maaaring i-preset ng kapaligiran ang mga pandaigdigang variable ng iba pang mga kapaligiran, tulad ng brower, variable ng node environment, es6 variable ng kapaligiran, variable ng mocha environment, atbp.
'env': { 'browser': true, 'commonjs': true, 'es6': true },
Globals: karagdagang mga variable sa buong mundo
globals: { vue: true, wx: true },
Mga Panuntunan: ang mga patakaran ay binubuksan at ang antas ng ulat kapag naganap ang isang error
Mayroong tatlong mga antas ng error sa panuntunan:
0 or 'off': Close the rule. 1 or 'warn’: Opens the rule and acts as a warning (and does not cause the check to fail). 2 or 'error': Open the rule and as an error (the exit code is 1, the check does not pass). Parameter Description: Parameter 1: Error level Parameter 2: Processing method
I-configure ang mode ng komento sa code
Minsan maaari naming balewalain ang ilang uri ng eslint check sa code, o magdagdag ng isang tukoy na tseke, sa kasong ito maaari naming gamitin ang sumusunod na paraan:
Halimbawa:
Huwag pansinin ang tseke na walang-undef
/ * eslint-huwag paganahin ang walang-undef * /
Huwag pansinin ang walang bagong tseke
/ * eslint-huwag paganahin ang hindi bago * /
Suriin ang setting
/ eslint eqeqeq: off /
/ eslint eqeqeq: 0 /
Pagtuturo ng eslint check
suriin at ayusin
eslint * --fix
Suriin ang tinukoy na file
eslint app.js --fix
Gamitin sa WebStorm
Mga Kagustuhan -> Mga Wika at Framework -> JavaScript -> Mga Tool sa Kalidad ng Code -> Eslint -> Paganahin (suriin) -> Ilapat -> OK
Paglalarawan ng panuntunan
'no-alert': 0, / / prohibit the use of alert confirm prompt 'no-array-constructor': 2, / / prohibit the use of array constructor 'no-bitwise': 0, / / prohibit using the bitwise operator 'no-caller': 1, / / prohibit the use of arguments.caller or arguments.callee 'no-catch-shadow': 2, / / prohibit the catch clause parameter with the same name as the external scope variable 'no-class-assign': 2, / / prohibit assignment to the class 'no-cond-assign': 2, / / prohibit the use of assignment statements in conditional expressions 'no-console': 2, / / prohibit the use of console 'no-const-assign': 2, / / prohibit modification of const declared variables 'no-constant-condition': 2, / / prohibit the use of constant expressions in the condition if (true) if (1) 'no-continue': 0, / / prohibit using continue 'no-control-regex': 2, / / prohibit the use of control characters in regular expressions 'no-debugger': 2, / / prohibit the use of debugger 'no-delete-var': 2, / / can not use the delete operator on the variable declared by var 'no-div-regex': 1, / / can not use regular expression /=foo / looks like division 'no-dupe-keys': 2,//Do not allow key repeats when creating object literals {a:1,a:1} 'no-dupe-args': 2, / / function parameters can not be repeated 'no-duplicate-case': 2, the case label in / / switch can not be repeated 'no-else-return': 2, / / If the if statement has a return, can not be followed by the else statement 'no-empty': 2, / / block statement can not be empty 'no-empty-character-class': 2, / / [] content in the regular expression can not be empty 'no-empty-label': 2, / / prohibit the use of an empty label 'no-eq-null': 2, / / prohibit the use of == or != operator for null 'no-eval': 1, / / prohibit the use of eval 'no-ex-assign': 2, / / prohibit the assignment of exception parameters in the catch statement 'no-extend-native': 2, / / prohibit extended native object 'no-extra-bind': 2, / / prohibit unnecessary function binding 'no-extra-boolean-cast': 2, / / prohibit unnecessary bool conversion 'no-extra-parens': 2, / / prohibit non-essential brackets 'no-extra-semi': 2, / / prohibit redundant colon 'no-fallthrough': 1, / / prohibit switch penetration 'no-floating-decimal': 2, / / prohibits omitting the floating point number of 0. 5 3. 'no-func-assign': 2, / / prohibit duplicate function declaration 'no-implicit-coercion': 1, / / prohibit implicit conversion 'no-implied-eval': 2, / / prohibit the use of implicit eval 'no-inline-comments': 0, / / prohibit in-line notes 'no-inner-declarations': [2, 'functions'], / / prohibit the use of declarations (variables or functions) in block statements 'no-invalid-regexp': 2, / / prohibit invalid regular expression 'no-invalid-this': 2, / / prohibit invalid this, can only be used in constructors, classes, object literals 'no-irregular-whitespace': 2, / / can not have irregular spaces 'no-iterator': 2, / / prohibit using the __iterator__ attribute 'no-label-var': 2, / / label name can not be the same as the variable name declared by var 'no-labels': 2, / / prohibit label declaration 'no-lone-blocks': 2, / / prohibit unnecessary nested blocks 'no-lonely-if': 2, / / prohibit the only statement in the else statement 'no-loop-func': 1, / / prohibit the use of functions in the loop (if no external variables are referenced, no closures can be formed) 'no-mixed-requires': [0, false], / / can not be mixed with the declaration type 'no-mixed-spaces-and-tabs': [2, false], / / prohibit mixing tabs and spaces 'linebreak-style': [0, 'windows'], / / wrap style 'no-multi-spaces': 1, / / can not use extra spaces 'no-multi-str': 2, / / string can not be used line 'no-multiple-empty-lines': [1, {'max': 2}], // empty lines can not exceed 2 lines at most 'no-native-reassign': 2, / / can not override the native object 'no-negated-in-lhs': 2, / / in the left side of the operator can not! 'no-nested-ternary': 0, / / prohibits the use of nested trinocular operations 'no-new': 1, / / prohibits the use of new construct an instance is not assigned 'no-new-func': 1, / / prohibit the use of new Function 'no-new-object': 2, / / prohibit the use of new Object () 'no-new-require': 2, / / prohibit the use of new require 'no-new-wrappers': 2, / / prohibit the use of new to create a wrapper instance, new String new Boolean new Number 'no-obj-calls': 2, / / can not call the built-in global object, such as Math () JSON () 'no-octal': 2, / / prohibit the use of octal numbers 'no-octal-escape': 2, / / prohibit the use of octal escape sequences 'no-param-reassign': 2, / / prohibit reassigning parameters 'no-path-concat': 0, / / node can not use __dirname or __filename for path stitching 'no-plusplus': 0, / / prohibit using ++, -- 'no-process-env': 0, / / prohibit the use of process.env 'no-process-exit': 0, / / prohibit using process.exit () 'no-proto': 2, / / prohibit the use of __proto__ attribute 'no-redeclare': 2, / / prohibit duplicate declaration variables 'no-regex-spaces': 2,// prohibits the use of multiple spaces in regular expression literals /foo bar/ 'no-restricted-modules': 0, / / If the specified module is disabled, the use will give an error 'no-return-assign': 1, can not have an assignment expression in the return statement 'no-script-url': 0, / / prohibit using javascript:void(0) 'no-self-compare': 2, / / can not compare themselves 'no-sequences': 0, / / prohibit using the comma operator 'no-shadow': 2, / / The variable in the external scope cannot have the same name as the variable or parameter in the scope it contains 'no-shadow-restricted-names': 2,//The restriction identifier specified in strict mode cannot be used as the variable name when declaring 'no-spaced-func': 2, / / function call when there can be no space between the function name and () 'no-sparse-arrays': 2, / / prohibit sparse arrays, [1,, 2] 'no-sync': 0, / / nodejs disable synchronization method 'no-ternary': 0, / / prohibit the use of trinocular operators 'no-trailing-spaces': 1, / / do not have spaces after the end of a line 'no-this-before-super': 0, / / can not use this or super before calling super () 'no-throw-literal': 2, / / forbidden to throw a literal error throw 'error' 'no-undef': 1, / / can not have undefined variables 'no-undef-init': 2, / / variable can not be directly assigned to the value of undefined 'no-undefined': 2, / / can not use undefined 'no-unexpected-multiline': 2, / / avoid multi-line expressions 'no-underscore-dangle': 1, / / identifier can not start or end with _ 'no-unneeded-ternary': 2, / / prohibit unnecessary nesting var isYes = answer === 1 ? true : false 'no-unreachable': 2, / / can not have code that cannot be executed 'no-unused-expressions': 2, / / prohibit useless expressions 'no-unused-vars': [2, {'vars': 'all', 'args': 'after-used'}], // can not have variables or parameters that are not used after declaration 'no-use-before-define': 2, / / can not be used before undefined 'no-useless-call': 2, / / prohibit unnecessary calls and apply 'no-void': 2, / / disable the void operator 'no-var': 0, / / disable var, replace with let and const 'no-warning-comments': [1, { 'terms': ['todo', 'fixme', 'xxx'], 'location': 'start' }], //Cannot have warning notes 'no-with': 2, / / disable with 'array-bracket-spacing': [2, 'never'], / / whether to allow extra spaces in non-empty arrays 'arrow-parens': 0, / / arrow function enclosed in parentheses 'arrow-spacing': front/back parentheses of 0,//=> 'accessor-pairs': 0, / / use getter / setter in the object 'block-scoped-var': 0, / / use var in block statements 'brace-style': [1, '1tbs'], // Braces style 'callback-return': 1, / / avoid calling callbacks multiple times 'camelcase': 2, / / forced hump method name 'comma-dangle': [2, 'never'], / / object literals can not have a comma at the end 'comma-spacing': 0, / / spaces before and after the comma 'comma-style': [2, 'last'], / / comma style, at the beginning or end of the line when wrapping 'complexity': [0, 11], / / loop complexity 'computed-property-spacing': [0, 'never'], / / Whether to allow the calculation of the key name or something 'consistent-return': 0, / / return is allowed to be omitted behind 'consistent-this': [2, 'that'], //this alias 'constructor-super': 0, / / non-derived class can not call super, the derived class must call super 'curly': [2, 'all'], / / must use {} in if(){} 'default-case': 2, / / switch statement must have default 'dot-location': 0, / / the location of the object accessor, when the line is at the beginning or end of the line 'dot-notation': [0, { 'allowKeywords': true }], / / avoid unnecessary square brackets 'eol-last': 0, / / file ends with a single newline 'eqeqeq': 2, / / must use congruent 'func-names': 0, / / function expression must have a name 'func-style': [0, 'declaration'], / / function style, stipulates that only function declarations / function expressions can be used 'generator-star-spacing': 0, / / generator function * before and after spaces 'guard-for-in': 0, / / for in loop to filter with if statement 'handle-callback-err': 0,//nodejs handles errors 'id-length': 0, / / variable name length 'indent': [2, 4], / / indented style 'init-declarations': 0, / / must be assigned when initializing 'key-spacing': [0, { 'beforeColon': false, 'afterColon': true }], // before and after the colon in the object literal 'lines-around-comment': 0, / / pre-line / post-line notes 'max-depth': [0, 4], // nested block depth 'max-len': [0, 80, 4], / / string maximum length 'max-nested-callbacks': [0, 2], / / callback nesting depth 'max-params': [0, 3], / / function can only have up to 3 parameters 'max-statements': [0, 10], / / There are a few declarations in the function 'new-cap': 2, / / function name first line uppercase must be called with new way, the first line lowercase must be called with no new way 'new-parens': 2, / / new must be added with parentheses 'newline-after-var': 2, / / need to empty a line after the variable declaration 'object-curly-spacing': [0, 'never'], // Whether unnecessary spaces are allowed in braces 'object-shorthand': 0, / / force object literal abbreviation grammar 'one-var': 1, / / consecutive statements 'operator-assignment': [0, 'always'], / / assignment operator += -= what 'operator-linebreak': [2, 'after'], //When the line break operator is at the end of the line or at the beginning of the line 'padded-blocks': 0, / / block statement in the first line of the line is to be blank 'prefer-const': 0, / / preferred const 'prefer-spread': 0, / / preferred expansion operation 'prefer-reflect': 0, / / preferred method of Reflect 'quotes': [1, 'single'], / / quote type `` '' '' 'quote-props':[2, 'always'],//Does the attribute name in the object literal force double quotes? 'radix': 2, / / parseInt must specify the second parameter 'id-match': 0, / / name detection 'require-yield': 0, / / generator function must have yield 'semi': [2, 'always'], // statement forces the end of the semicolon 'semi-spacing': [0, {'before': false, 'after': true}], // before and after the semicolon 'sort-vars': 0, / / variable when sorting 'space-after-keywords': [0, 'always'], / / Is there a space behind the keyword? 'space-before-blocks': [0, 'always'], // Blocks that do not start with a new line {Do you have spaces before? 'space-before-function-paren': [0, 'always'], / / function definition when there are no spaces before the brackets 'space-in-parens': [0, 'never'], / / Do not have spaces in the parentheses 'space-infix-ops': 0, / / Do not have spaces around the infix operator 'space-return-throw-case': 2,//return throw case should be followed by a space 'space-unary-ops': [0, { 'words': true, 'nonwords': false }],//Do you want to add spaces before/after the unary operator? 'spaced-comment': 0, / / comment style whether there is a space or something 'strict': 2, / / use strict mode 'use-isnan': 2, / / prohibit the use of NaN when comparing, can only use isNaN () 'valid-jsdoc': 0, / / jsdoc rules 'valid-typeof': 2, / / must use the value of the legal typeof 'vars-on-top': 2, //var must be placed at the top of the scope 'wrap-iife': [2, 'inside'], / / immediately execute the parenthetical style of the function expression 'wrap-regex': 0, / / regular expression literals wrapped in parentheses 'yoda': [2, 'never']// prohibits Yoda conditions