简体   繁体   English

使用 ESLint 而不是 PascalCase 强制使用camelCase

[英]Enforce camelCase using ESLint but not PascalCase

I am setting up ESlint for my project and I have a question.我正在为我的项目设置 ESlint,我有一个问题。

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).我希望所有变量和函数都是驼峰式(而不是 PascalCase),所有类都是 PascalCase(而不是驼峰式)。

Could anyone help me with that?有人可以帮我吗? Thanks in advance!提前致谢!

This appears to be possible for typescript now.现在这对于打字稿来说似乎是可能

According to https://github.com/typescript-eslint/typescript-eslint/pull/1318 you can specify config like this:根据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 } },
],
}

It should be possible to do in eslint with the help of the id-match rule.应该可以借助 id-match 规则在 eslint 中进行。 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.应该是“@typescript-eslint/naming-convention”而不是“@typescript-eslint/naming-conventions”,如伊恩的回答所示。

for context here's my .eslintrc.json file:-对于上下文,这是我的 .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
                }
            }
        ]
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScriptSerializer()。Serialize:PascalCase到CamelCase - JavaScriptSerializer().Serialize : PascalCase to CamelCase 在单个单词中匹配 UPPERCASE、PascalCase 和 camelCase - Matching UPPERCASE, PascalCase and camelCase in single word 如何在 JavaScript 中使用 eslint 强制不使用 myStr = myStr + 'Some string' - How to enforce in JavaScript using eslint not to use myStr = myStr + 'Some string' 流星是否强制使用CamelCase作为模板名称? - Does Meteor Enforce CamelCase for Template Names? 如何将PascalCase属性中的JavaScript对象克隆到camelCase属性(在JavaScript中)? - How do I clone a JavaScript object from PascalCase properties to camelCase properties (in JavaScript)? 未从PascalCase服务器属性映射的Breeze js客户端camelCase扩展属性 - Breeze js client-side camelCase extension property not mapped from PascalCase server property 如何在camelCase PascalCase snake_case kebab-case (JS) 之间进行转换 - How to convert between camelCase PascalCase snake_case kebab-case (JS) eslint规则来强制在块的顶部声明变量? - eslint rule to enforce that variables are declared at the top of the block? 仅对新文件强制执行 eslint 规则 - Enforce eslint rules only on new files JSHINT:有没有办法忽略camelCase only属性并在变量和函数上强制执行它 - JSHINT: is there a way in to ignore camelCase only property and enforce it on variable and function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM