这个问题可以分为两部分:
- 如何为不同的文件提供两组规则?
- 如何自定义@typescript-eslint/naming-convention 规则以支持我的自定义格式?
ESLint 多组规则:
从 eslint v4.1.0 开始,您可以根据 glob 模式创建配置,文档:https://eslint.org/docs/user-guide/configuring/configuration-files#configuration-based-on-glob-patterns
来自上述链接的示例:
{
"rules": {
"quotes": ["error", "double"]
},
"overrides": [
{
"files": ["bin/*.js", "lib/*.js"],
"excludedFiles": "*.test.js",
"rules": {
"quotes": ["error", "single"]
}
}
]
}
@typescript-eslint/naming-convention 的自定义格式
您可以从@jsejcksn 的评论中找到该文档:https://github.com/typescript-eslint/typescript-eslint/blob/87cfc6ad3e3312d7b6f98a592fb37e69d5d6880a/packages/eslint-plugin/docs/rules/naming-convention.md
类似这样的:
{
'@typescript-eslint/naming-convention': ['error', {
selector: 'variableLike',
format: null,
custom: {
regex: '^[A-Z]\\w*_\\w+___\\w+$',
match: true
}
}]
}
最终解决方案:
module.exports = {
rules: {
'@typescript-eslint/naming-convention': ['error', {
selector: 'variableLike',
leadingUnderscore: 'allow',
trailingUnderscore: 'allow',
format: ['camelCase', 'PascalCase', 'UPPER_CASE']
}],
},
overrides: [{
files: ['**/*.style.ts'],
rules: {
'@typescript-eslint/naming-convention': ['error', {
selector: 'variableLike',
format: null,
custom: {
regex: '^[A-Z]\\w*_\\w+___\\w+$',
match: true
}
}]
}
}]
}
更改正则表达式以适合您的实际情况。