admin管理员组文章数量:1425221
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
3 Answers
Reset to default 4This 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
}
}
]
}
}
本文标签: javascriptEnforce camelCase using ESLint but not PascalCaseStack Overflow
版权声明:本文标题:javascript - Enforce camelCase using ESLint but not PascalCase - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745372208a2655779.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论