能,eslint需通过@typescript-eslint/naming-convention插件检查函数命名,配置selector:"function"和format:["camelcase"]可强制小驼峰,但依赖typescript解析器及正确tsconfig路径,否则规则静默失效。

ESLint 能否检查函数命名?
能,但默认规则不覆盖。ESLint 本身不内置 function-name-casing 或类似规则,必须靠插件扩展。常见方案是用 @typescript-eslint/naming-convention(TS 项目)或 eslint-plugin-import 的 import/no-unused-modules 辅助识别未导出函数——但真正管“命名格式”的,只有 @typescript-eslint/naming-convention。
如何配置函数名驼峰规则?
在 .eslintrc.cjs 的 rules 中启用并定制该规则:
["@typescript-eslint/naming-convention", "error", {
selector: "function",
format: ["camelCase"],
leadingUnderscore: "forbid",
trailingUnderscore: "forbid"
}]
注意三点:
-
selector: "function"只作用于普通函数,不包括方法、类名、变量;若要覆盖类中方法,需额外加一条selector: "method" -
format: ["camelCase"]允许getUserInfo,但会报错get_user_info或GetUserInfo - 若项目混用钩子(如
useFetch),建议加filter: { regex: "^use[A-Z]", match: true }排除,否则会被当成非法命名
为什么改了配置却没标红?
常见静默失效原因不是规则写错,而是 ESLint 没加载到 TypeScript 解析器:
- 检查
parser是否设为"@typescript-eslint/parser",且parserOptions.project指向正确的tsconfig.json路径 - 确认
node_modules/@typescript-eslint/eslint-plugin已安装,且版本与 ESLint 兼容(v8.53+ 推荐配 v7.x 插件) - VS Code 必须打开项目根目录(不是
src/子目录),否则parserOptions.project解析失败,规则退化为纯 JS 检查,functionselector 不生效 - 重启 VS Code 或运行命令
ESLint: Restart ESLint Server,LSP 配置变更需重载才生效
和 Prettier 冲突怎么办?
Prettier 不管命名,只管空格/引号/换行。但如果你开了 editor.formatOnSave 且同时启用 Prettier 和 ESLint 自动修复,可能因保存顺序导致波浪线闪现或修复不完整:
- 禁用
editor.formatOnSave,改用"editor.codeActionsOnSave": {"source.fixAll.eslint": true} - 确保
eslint-config-prettier在extends数组末尾,避免它意外关闭@typescript-eslint/naming-convention - 不要在
.prettierrc里写semi: true这类与命名无关的配置来“凑数”——它不影响命名检查,但可能干扰其他 lint 规则加载
真正容易被忽略的是:命名规则依赖 TypeScript 类型信息才能准确区分函数声明和变量赋值,parserOptions.project 缺失时,const foo = () => {} 这种写法不会被识别为 function selector,也就不会触发驼峰检查。











