[v1.4] 修复编辑器中 ESLint 修复功能失效的问题(globalCache.get("eslint-fix") 为 undefined)- 修复 #1079#1184
Conversation
There was a problem hiding this comment.
Pull request overview
该 PR 旨在修复脚本编辑器中 ESLint “Fix/Quick Fix” 在特定情况下失效(globalCache.get("eslint-fix") 为 undefined)的问题,通过将 ESLint fix 的存储从页面内的临时缓存迁移到 Monaco 的页面级全局环境(window.MonacoEnvironment)来提高稳定性,避免因缓存生命周期/重启导致的取值异常。
Changes:
- 抽离 Monaco 编辑器多语言文案到
langs.ts,并在编辑器初始化时动态更新语言。 - 重构 linter worker 为页面级单例:新增
LinterWorkerController+deferred控制 worker 初始化时序。 - 将 ESLint fix 缓存从
globalCache改为window.MonacoEnvironment.eslintFixMap,并调整相关调用方的导入/使用方式(默认导出改为具名导出)。
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/pkg/utils/monaco-editor/langs.ts | 新增编辑器多语言文案与类型导出,供 Monaco hover/quickfix 使用 |
| src/pkg/utils/monaco-editor/index.ts | 重构 registerEditor 初始化流程,引入 worker 单例控制与 eslintFixMap 存储位置变更 |
| src/pages/options/main.tsx | 更新为具名导入 registerEditor |
| src/pages/install/main.tsx | 更新为具名导入 registerEditor |
| src/pages/components/CodeEditor/index.tsx | ESLint lint/marker 处理改用 LinterWorkerController,并将 fix 写入 MonacoEnvironment 的 map |
| if (eslintFixMap) { | ||
| message.markers.forEach((m: TMarker) => { | ||
| if (m.fix) { | ||
| const key = `${m.code.value}|${m.startLineNumber}|${m.endLineNumber}|${m.startColumn}|${m.endColumn}`; | ||
| eslintFixMap.set(key, m.fix); | ||
| } | ||
| }); |
There was a problem hiding this comment.
这里把 eslintFixMap 作为全局 Map 只做 set、不做清理,会留下已不存在 marker 的旧 fix;更关键的是当同一个 key 的 marker 之后不再提供 fix 时,旧值仍会被命中,导致 Quick Fix 显示/应用过期修复。建议每次收到 message 时先按当前 editor/model 重建(或至少先删除本次 message 未包含 fix 的 key),并考虑按 model.uri 或 editor id 做隔离,避免不同编辑器之间相互污染。
| if (eslintFixMap) { | |
| message.markers.forEach((m: TMarker) => { | |
| if (m.fix) { | |
| const key = `${m.code.value}|${m.startLineNumber}|${m.endLineNumber}|${m.startColumn}|${m.endColumn}`; | |
| eslintFixMap.set(key, m.fix); | |
| } | |
| }); | |
| if (eslintFixMap) { | |
| // 本次 message 中實際存在的 key,用於後續清理過期的 fix | |
| const currentKeys = new Set<string>(); | |
| message.markers.forEach((m: TMarker) => { | |
| if (m.fix) { | |
| const key = `${m.code.value}|${m.startLineNumber}|${m.endLineNumber}|${m.startColumn}|${m.endColumn}`; | |
| currentKeys.add(key); | |
| eslintFixMap.set(key, m.fix); | |
| } | |
| }); | |
| // 清理本次未出現的 key,避免保留已不存在 marker 的舊 fix | |
| for (const key of Array.from(eslintFixMap.keys())) { | |
| if (!currentKeys.has(key)) { | |
| eslintFixMap.delete(key); | |
| } | |
| } |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Code Review 建议审查了整体改动,有几个点想和你讨论一下: 1. 旧代码每次收到 lint 结果时会用新 const fix = new Map();
// ...
globalCache.set("eslint-fix", fix);新代码只往 这意味着用户编辑代码后,已解决的 lint 问题的 fix 条目仍然留在 map 中。如果之后出现相同 key(相同 code + 行列号),会命中过期的 fix,导致 Quick Fix 应用错误的修复。 建议:每次 lint 结果到来时替换整个 map(或先清除再写入),而不是只追加。 2.
scopedKey 查找永远 miss,始终回退到 baseKey,多编辑器实例间的 key 隔离没有生效。建议写入端也加上 3. 当 建议将 worker 初始化和 provider 注册分离—— 以上是主要的功能性建议,其他重构和代码清理部分看起来不错。 🤖 Generated with Claude Code |
|
nickyc975 ? 1, 2, 3 这3个问题我都不太清楚 让AI提交修正代码吧 |
AI搞错了。。。。 我也不太明白这块,eslint最开始也是由另外的贡献者处理的,我让ai处理,我再测测,没问题 我就合并 |
1. eslintFixMap 每次 lint 结果到来时先 clear 再写入,避免过期条目残留 2. 移除未完整实现的 scopedKey 逻辑,统一使用 baseKey 3. registerEditor 中 worker 复用不再提前 return,provider 注册始终执行
Fixes #1079
globalCache.get("eslint-fix")為 undefined #1079问题描述:
在某些情况下(例如 Service Worker 重启后),
globalCache.get("eslint-fix")返回undefined,导致 Monaco Editor 中的 ESLint 自动修复功能无法正常工作,报错或无响应。修复内容:
globalCache的依赖,避免缓存失效导致的问题。"eslint-fix"相关配置或状态,确保值实时可用。改动范围:
测试建议: