![](/img/trans.png)
[英]JavaScriptSerializer().Serialize : PascalCase to CamelCase
[英]Enforce camelCase using ESLint but not PascalCase
我正在为我的项目设置 ESlint,我有一个问题。
我希望以下内容起作用:
class MyClass {
}
function awesomeFunction() {
}
let myVariable = "a";
但不是这个:
class myClass {
}
function AwesomeFunction() {
}
let MyVariable = "a";
我希望所有变量和函数都是驼峰式(而不是 PascalCase),所有类都是 PascalCase(而不是驼峰式)。
有人可以帮我吗? 提前致谢!
现在这对于打字稿来说似乎是可能的。
根据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 } },
],
}
应该可以借助 id-match 规则在 eslint 中进行。 我找到了一个可以解决它的插件,但目前它似乎有点坏。
应该是“@typescript-eslint/naming-convention”而不是“@typescript-eslint/naming-conventions”,如伊恩的回答所示。
对于上下文,这是我的 .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.