【问题标题】:Enforce camelCase using ESLint but not PascalCase使用 ESLint 而不是 PascalCase 强制执行 camelCase
【发布时间】:2020-01-18 17:04:19
【问题描述】:

我正在为我的项目设置 ESlint,我有一个问题。

我希望以下工作:

class MyClass {

}

function awesomeFunction() {

}

let myVariable = "a";

但不是这个:

class myClass {

}

function AwesomeFunction() {

}

let MyVariable = "a";

我希望所有的变量和函数都是 camelCase(而不是 PascalCase),所有的类都是 PascalCase(而不是 camelCase)。

谁能帮我解决这个问题? 提前致谢!

【问题讨论】:

标签: javascript eslint eslintrc


【解决方案1】:

这对于打字稿来说似乎是可能的

根据https://github.com/typescript-eslint/typescript-eslint/pull/1318,您可以像这样指定配置:

{
"@typescript-eslint/naming-conventions": ["error",
  { selector: "default", format: ["camelCase"] },

  { selector: "variableLike", format: ["camelCase"] },
  { selector: "variable", format: ["camelCase", "UPPER_CASE"] },
  { selector: "parameter", format: ["camelCase"], leadingUnderscore: "allow" },

  { selector: "memberLike", format: ["camelCase"] },
  { selector: "memberLike", modifiers: ["private"], format: ["camelCase"], leadingUnderscore: "require"  },

  { selector: "typeLike", format: ["PascalCase"] },
  { selector: "typeParameter", format: ["PascalCase"], prefix: ["T"] },

  { selector: "interface", format: ["PascalCase"], custom: { regex: "^I[A-Z]", match: false } },
],
}

【讨论】:

    【解决方案2】:

    应该可以借助 id-match 规则在 eslint 中完成。我找到了一个plugin,它可以解决它,但目前它似乎有点坏了。

    【讨论】:

      【解决方案3】:

      应该是“@typescript-eslint/naming-convention”而不是“@typescript-eslint/naming-conventions”,如 Ian 的回答所示。

      这里是我的 .eslintrc.json 文件:-

      {
          "parser": "@typescript-eslint/parser",
          "plugins": [
              "@typescript-eslint"
          ],
          "extends": [
              "eslint:recommended",
              "plugin:@typescript-eslint/eslint-recommended",
              "plugin:@typescript-eslint/recommended"
          ],
          "rules": {
              "@typescript-eslint/type-annotation-spacing": "error",
              "@typescript-eslint/space-infix-ops": "error",
              "@typescript-eslint/naming-convention": [
                  "error",
                  {
                      "selector": "default",
                      "format": [
                          "camelCase"
                      ]
                  },
                  {
                      "selector": "function",
                      "format": [
                          "StrictPascalCase"
                      ]
                  },
                  {
                      "selector": "variable",
                      "format": [
                          "camelCase",
                          "UPPER_CASE"
                      ]
                  },
                  {
                      "selector": "parameter",
                      "format": [
                          "camelCase"
                      ],
                      "leadingUnderscore": "allow"
                  },
                  {
                      "selector": "memberLike",
                      "format": [
                          "camelCase"
                      ]
                  },
                  {
                      "selector": "memberLike",
                      "modifiers": [
                          "private"
                      ],
                      "format": [
                          "camelCase"
                      ],
                      "leadingUnderscore": "require"
                  },
                  {
                      "selector": "typeLike",
                      "format": [
                          "PascalCase"
                      ]
                  },
                  {
                      "selector": "typeParameter",
                      "format": [
                          "PascalCase"
                      ],
                      "prefix": [
                          "T"
                      ]
                  },
                  {
                      "selector": "interface",
                      "format": [
                          "PascalCase"
                      ],
                      "custom": {
                          "regex": "^I[A-Z]",
                          "match": false
                      }
                  }
              ]
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2021-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-05
        • 2017-06-13
        • 1970-01-01
        • 1970-01-01
        • 2022-07-22
        相关资源
        最近更新 更多