vueプロジェクトカスタムESLintルール

10046 ワード

Eslintの構成方法は2つあります.
  • 注釈構成:js注釈を使用してESLint構成情報を1つのファイルに直接埋め込む
  • プロファイル:js、JSONまたはYAMLファイルを使用して、ディレクトリ全体とそのサブディレクトリに構成情報を指定します.これらの構成は、.eslintrc.*というファイルまたはpackage.jsonファイルのeslintConfigアイテムに書くことができます.この2つの方法でESLintは自動的に探して読み込むか、コマンドラインで構成ファイルを指定することもできます.

  • 配置できるものはいくつかあります.
  • 環境:スクリプトがどの環境で実行されますか.各環境は、特定の事前定義されたグローバル変数のセットをもたらします.
  • グローバル変数:スクリプトの実行中に追加のグローバル変数にアクセスします.
  • ルール:それらのルールを使用し、ルールのレベルはいくらですか.

  • ここでは、ESLint識別用のモジュールをエクスポートするプロファイル.eslintrc.jsを使用して構成します.
    // http://eslint.org/docs/user-guide/configuring
    
    module.exports = {
      root: true,
      parser: 'babel-eslint',//   ,      babel-eslint
      parserOptions: {
        sourceType: 'module'//   module,          ECMAScript  
      },
      env: {
        browser: true,//        ,        
      },
      // https://github.com/feross/standard/blob/master/RULES.md#javascript-standard-style
      //extends: 'standard', //  ,                  
      // required to lint *.vue files
      plugins: [
       'html' //  ,           js  ,  MIME      script        ,      .vue    js  
      ],
      // add your custom rules here
      'rules': {
        //        
      }
    }

    ESLintのルールには3つのレベルがあります.
  • 「off」または0、このルールを有効にしない
  • 「warn」または1、問題が発生すると警告
  • 「error」または2、問題が発生すると
  • とエラーが発生します.
    コードには、ある行またはいくつかの行でESLint検出を閉じる必要がある場合があります.コメントを使用することができます.
    次のコードは、すべてのルールを閉じます.
    /* eslint-disable */
    
    alert('foo');
    
    /* eslint-enable */

    次のコードは、ローのすべてのルールを閉じます.
    alert('foo'); // eslint-disable-line no-alert
    
    // eslint-disable-next-line no-alert
    alert('foo');

    共通ルール:
    'rules': {
          "comma-dangle": ["error", "never"], //             
          "no-cond-assign": 2, //                  
          "no-console": 2, //     console  
          "no-constant-condition": 2, //                   
          "no-control-regex": 2, //               
          "no-debugger": 2, //     debugger  
          "no-dupe-args": 2, //                 
          "no-dupe-keys": 2, //            
          "no-duplicate-case": 2, //switch           case  
          "no-empty": 2, //          
          "no-empty-character-class": 2, //                
          "no-ex-assign": 2, // try catch              
          "no-extra-boolean-cast": 2, //              
          "no-extra-parens": 0, //            
          "no-extra-semi": 2, //           
          "no-func-assign": 2, //           
          "no-inner-declarations": ["error", "functions"], //              
          "no-invalid-regexp": 2, //    RegExp               
          "no-irregular-whitespace": 2, //           
          "no-negated-in-lhs": 2, //    in                    
          "no-obj-calls": 2, //                 
          "no-regex-spaces": 2, //                 
          "quote-props": 2, //                 
          "no-sparse-arrays": 2, //           
          "no-unreachable": 2, // return,throw,continue,break                
          "use-isnan": 2, //    NaN     isNaN()
          "valid-jsdoc": ["error", {
              "requireReturn": false,
              "requireParamDescription": false,
              "requireReturnDescription": true
          }], //  JSDoc  
          "valid-typeof": ["error", {
              "requireStringLiterals": true
          }], //   typeof                  
          "block-scoped-var": 2, //              
          "complexity": 0, //          
          "consistent-return": 2, //             return       
          "curly": ["error", "all"], //          
          "default-case": 0, // switch      default  
          "dot-notation": ["error", {"allowKeywords": false, "allowPattern": ""}], //             
          "eqeqeq": ["error", "smart"], //           
          "no-alert": 1, //     alert,confirm,prompt  
          "no-caller": 2, //     arguments.callee arguments.caller  
          "guard-for-in": 0, //  for in  ,           
          "no-div-regex": 2, //                
          "no-else-return": 0, //  if   return,else  return    else 
          "no-labels": ["error", {
              "allowLoop": false,
              "allowSwitch": false
          }], //       
          "no-eq-null": 2, //    null ==  !=
          "no-eval": 2, //     eval()
          "no-extend-native": 2, //         
          "no-extra-bind": 2, //           
          "no-fallthrough": 2, //   switch         case
          "no-floating-decimal": 2, //          
          "no-implied-eval": 2, //       eval()
          "no-iterator": 2, //     __iterator__  
          "no-lone-blocks": 2, //            
          "no-loop-func": 2, //               
          "no-multi-spaces": 2, //          
          "no-multi-str": 2, //    \       
          "no-global-assign": 2, //           
          "no-new": 2, //   new             
          "no-new-func": 2, //     new Function
          "no-new-wrappers": 2, //     new String,Number Boolean  
          "no-octal": 2, //           
          "no-octal-escape": 2, //            
          "no-param-reassign": 0, //           "no-proto": 2, //     __proto__  
          "no-redeclare": 2, //         
          "no-return-assign": 2, //    return         
          "no-script-url": 2, //     javascript:void(0)
          "no-self-compare": 2, //          
          "no-sequences": 2, //          
          "no-throw-literal": 2, //           throw "error"
          "no-unused-expressions": 2, //         
          "no-void": 2, //   void   
          "no-warning-comments": [1, {"terms": ["todo", "fixme", "any other term"]}], //       
          "no-with": 2, //     with  
          "radix": 1, //  parseInt                    
          "vars-on-top": 0, //var         
          "wrap-iife": [2, "any"], //            
          "yoda": [2, "never", {"exceptRange": true}], //    if     yoda  
          "strict": [2, "function"], //      
          "no-catch-shadow": 2, //   try catch     err         "no-delete-var": 2, //     delete   
          "no-label-var": 2, //          
          "no-shadow": 2, //                             
          "no-shadow-restricted-names": 2, //js                   
          "no-undef": 2, //         
          "no-undef-init": 2, //              undefined
          "no-undefined": 2, //    undefined       
          "no-unused-vars": [2, {"vars": "all", "args": "after-used"}], //                 
          "no-use-before-define": [2, "nofunc"], //              "indent": 2, //         
          "brace-style": [2, "1tbs", { "allowSingleLine": false}], //     
          "camelcase": [2, {"properties": "never"}], //        
          "comma-style": [2, "last"], //    
          "consistent-this": [0, "self"], //        this       
          "eol-last": 2, //        
          "func-names": 0, //          
          "func-style": 0, //    ,                 
          "key-spacing": [2, {"beforeColon": false, "afterColon": true}], //             
          "max-nested-callbacks": 0, //      
          "new-cap": [2, {"newIsCap": true, "capIsNew": false}], //            
          "new-parens": 2, //new           
          "newline-after-var": 0, //          
          "no-array-constructor": 2, //          
          "no-inline-comments": 0, //       
          "no-lonely-if": 0, //   else     if  
          "no-mixed-spaces-and-tabs": [2, "smart-tabs"], //     tab   
          "no-multiple-empty-lines": [2, {"max": 2}], //          
          "no-nested-ternary": 2, //             
          "no-new-object": 2, //    new Object()
          "fun-call-spacing": 2, //     ,    ()       
          "no-ternary": 0, //          
          "no-trailing-spaces": 2, //          
          "no-underscore-dangle": 2, //            
          "no-extra-parens": 0, //          
          "one-var": 0, //          
          "operator-assignment": 0, //        
          "padded-blocks": [2, "never"], //          
          "quote-props": 0, //            
          "quotes": [1, "single", "avoid-escape"], //    
          "semi": [2, "always"], //        
          "semi-spacing": [2, {"before": false, "after": true}], //      
          "sort-vars": 0, //       
          "space-before-blocks": [2, "always"], //     
          "space-before-function-paren": [2, {"anonymous": "always", "named": "never"}], //           
          "space-infix-ops": [2, {"int32Hint": true}], //        
          "keyword-spacing": 2, //        
          "space-unary-ops": [2, { "words": true, "nonwords": false}], //            
          "wrap-regex": 2, //              
          "no-var": 0, //  let const  var
          "generator-star-spacing": [2, "both"], //         
          "max-depth": 0, //     
          "max-len": 0, //      ,     
          "max-params": 0, //           
          "max-statements": 0, //          
          "no-bitwise": 0, //         
          "no-plusplus": 0 //     ++ --   
      }

    詳細については、ESlintの公式サイトを参照してください.