[英]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 } },
],
}
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.