You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

迁移ESLint格式化规则至@stylistic/eslint-plugin遇TypeError求助

解决@stylistic/eslint-plugin运行lint时报错Cannot read properties of undefined (reading 'tokensAndComments')

可能的原因及对应解决方案

1. 版本兼容性问题

@stylistic/eslint-plugin要求ESLint版本至少为8.40.0,版本不匹配会触发规则加载异常:

  • 执行npm list eslint @stylistic/eslint-plugin查看当前版本
  • 若ESLint版本过低,升级至符合要求的版本:npm i -D eslint@latest(或使用rush对应命令升级)

2. 隐式规则冲突

即使未显式配置@stylistic/comma-spacing,也可能通过继承的共享配置(如@typescript-eslint/recommended、eslint:recommended)间接引入旧规则,与新插件冲突:

  • 在.eslintrc.json的rules字段中显式禁用旧规则并指定新规则:
    "rules": {
      "comma-spacing": "off",
      "@stylistic/comma-spacing": ["error", { "before": false, "after": true }]
    }
    
  • 检查extends数组,避免同时引入旧的格式化类共享配置。

3. ESLint缓存干扰

ESLint的缓存机制可能残留旧配置数据,导致新插件加载异常:

  • 运行lint命令时添加--no-cache参数:npm run lint -- --no-cache
  • 或手动删除项目根目录下的.eslintcache文件

4. 依赖安装不完整

使用rush添加依赖后可能存在安装不彻底的情况:

  • 删除node_modules目录及锁文件(package-lock.json/pnpm-lock.yaml),重新安装依赖
  • 重新执行rush添加命令:rush add -D @stylistic/eslint-plugin

5. 插件配置错误

确保.eslintrc.json中正确引入@stylistic插件,避免错误的配置导致规则自动加载异常:

{
  "plugins": ["@stylistic"],
  "extends": [
    // 保留你的业务相关共享配置
    // 若不需要全部推荐规则,不要添加@stylistic/recommended,按需启用单个规则即可
  ]
}

内容的提问来源于stack exchange,提问作者Mark Blades

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 14:46:04