最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Enforce camelCase using ESLint but not PascalCase - Stack Overflow

programmeradmin0浏览0评论

I am setting up ESlint for my project and I have a question.

I want the following to work:

class MyClass {

}

function awesomeFunction() {

}

let myVariable = "a";

But not this :

class myClass {

}

function AwesomeFunction() {

}

let MyVariable = "a";

I want all variables and functions to be camelCase (and NOT PascalCase) and all classes to be PascalCase (and NOT camelCase).

Could anyone help me with that? Thanks in advance!

I am setting up ESlint for my project and I have a question.

I want the following to work:

class MyClass {

}

function awesomeFunction() {

}

let myVariable = "a";

But not this :

class myClass {

}

function AwesomeFunction() {

}

let MyVariable = "a";

I want all variables and functions to be camelCase (and NOT PascalCase) and all classes to be PascalCase (and NOT camelCase).

Could anyone help me with that? Thanks in advance!

Share Improve this question asked Sep 17, 2019 at 19:32 BlueskyFRBlueskyFR 1651 gold badge3 silver badges10 bronze badges 1
  • Apparently, there's nothing quite like that: github./eslint/eslint/issues/8085 – acdcjunior Commented Sep 17, 2019 at 20:33
Add a ment  | 

3 Answers 3

Reset to default 4

This appears to be possible for typescript now.

According to https://github./typescript-eslint/typescript-eslint/pull/1318 you can specify config like this:

{
"@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 } },
],
}

It should be possible to do in eslint with the help of the id-match rule. I found a plugin which kind of solves it but it seems a bit broken at the moment.

the should be "@typescript-eslint/naming-convention" not "@typescript-eslint/naming-conventions" as shown in Ian's answer.

for context here's my .eslintrc.json file:-

{
    "parser": "@typescript-eslint/parser",
    "plugins": [
        "@typescript-eslint"
    ],
    "extends": [
        "eslint:remended",
        "plugin:@typescript-eslint/eslint-remended",
        "plugin:@typescript-eslint/remended"
    ],
    "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
                }
            }
        ]
    }
}
发布评论

评论列表(0)

  1. 暂无评论