4

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

我希望以下工作:

class MyClass {

}

function awesomeFunction() {

}

let myVariable = "a";

但不是这个:

class myClass {

}

function AwesomeFunction() {

}

let MyVariable = "a";

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

有人可以帮我吗?提前致谢!

4

3 回答 3

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 } },
],
}
于 2020-08-04T12:57:18.527 回答
0

在 id-match 规则的帮助下,应该可以在 eslint 中进行操作。我找到了一个可以解决它的插件,但目前它似乎有点坏了。

于 2021-09-09T09:24:14.807 回答
0

应该是“@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-12-29T15:38:09.250 回答