Skip to content

[v1.4] 修复编辑器中 ESLint 修复功能失效的问题(globalCache.get("eslint-fix") 为 undefined)- 修复 #1079#1184

Merged
CodFrm merged 22 commits intoscriptscat:release/v1.4from
cyfung1031:pr-eslint-fix-003
Mar 22, 2026
Merged

[v1.4] 修复编辑器中 ESLint 修复功能失效的问题(globalCache.get("eslint-fix") 为 undefined)- 修复 #1079#1184
CodFrm merged 22 commits intoscriptscat:release/v1.4from
cyfung1031:pr-eslint-fix-003

Conversation

@cyfung1031
Copy link
Collaborator

@cyfung1031 cyfung1031 commented Feb 2, 2026

Fixes #1079


问题描述:
在某些情况下(例如 Service Worker 重启后),globalCache.get("eslint-fix") 返回 undefined,导致 Monaco Editor 中的 ESLint 自动修复功能无法正常工作,报错或无响应。

修复内容:

  • 移除对 globalCache 的依赖,避免缓存失效导致的问题。
  • 改为直接从当前 Monaco 编辑器实例 / 页面实际运行环境中获取 "eslint-fix" 相关配置或状态,确保值实时可用。
  • 修改位置主要在 Monaco Editor 初始化或 ESLint 插件加载逻辑中。

改动范围:

  • 只影响内置代码编辑器的 ESLint 修复功能,其他功能不受影响。
  • 经过本地测试,编辑脚本时 ESLint → Fix 按钮/快捷键恢复正常。

测试建议:

  1. 打开脚本编辑器
  2. 故意写一段有 ESLint 警告的代码
  3. 使用 ESLint 修复功能(按钮或快捷键)
  4. 验证是否能正确修复
  5. 可尝试 Service Worker 重启后重试

@cyfung1031 cyfung1031 changed the title [v1.3] 修复 #1079 [v1.3] 修复编辑器中 ESLint 修复功能失效的问题(globalCache.get("eslint-fix") 为 undefined)- 修复 #1079 Feb 2, 2026
@cyfung1031 cyfung1031 linked an issue Feb 2, 2026 that may be closed by this pull request
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment on lines +257 to +263
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);
}
});
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里把 eslintFixMap 作为全局 Map 只做 set、不做清理,会留下已不存在 marker 的旧 fix;更关键的是当同一个 key 的 marker 之后不再提供 fix 时,旧值仍会被命中,导致 Quick Fix 显示/应用过期修复。建议每次收到 message 时先按当前 editor/model 重建(或至少先删除本次 message 未包含 fix 的 key),并考虑按 model.uri 或 editor id 做隔离,避免不同编辑器之间相互污染。

Suggested change
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);
}
}

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不确定这样是否正确

@cyfung1031 cyfung1031 added the P2 🐌 非重要也不紧急的内容 label Feb 15, 2026
@cyfung1031 cyfung1031 changed the title [v1.3] 修复编辑器中 ESLint 修复功能失效的问题(globalCache.get("eslint-fix") 为 undefined)- 修复 #1079 [v1.4] 修复编辑器中 ESLint 修复功能失效的问题(globalCache.get("eslint-fix") 为 undefined)- 修复 #1079 Feb 24, 2026
@cyfung1031 cyfung1031 changed the base branch from release/v1.3 to release/v1.4 March 15, 2026 05:02
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

@CodFrm
Copy link
Member

CodFrm commented Mar 21, 2026

Code Review 建议

审查了整体改动,有几个点想和你讨论一下:


1. eslintFixMap 过期条目未清理(行为退化)

旧代码每次收到 lint 结果时会用新 Map 替换整个缓存:

const fix = new Map();
// ...
globalCache.set("eslint-fix", fix);

新代码只往 window.MonacoEnvironment.eslintFixMap 追加,从不删除旧条目:

https://github.com/nickyc975/scriptcat/blob/c69f98b9fb1ec8fbc0dc1ef09e4485504e15dfbb/src/pages/components/CodeEditor/index.tsx#L256-L265

这意味着用户编辑代码后,已解决的 lint 问题的 fix 条目仍然留在 map 中。如果之后出现相同 key(相同 code + 行列号),会命中过期的 fix,导致 Quick Fix 应用错误的修复。

建议:每次 lint 结果到来时替换整个 map(或先清除再写入),而不是只追加。


2. scopedKey 读写不一致

provideCodeActions 读取端支持 scopedKey(带 model.uri 前缀),但 messageHandler 写入端只写了 baseKey(不含 model.uri):

https://github.com/nickyc975/scriptcat/blob/c69f98b9fb1ec8fbc0dc1ef09e4485504e15dfbb/src/pkg/utils/monaco-editor/index.ts#L131-L139

写入端:
https://github.com/nickyc975/scriptcat/blob/c69f98b9fb1ec8fbc0dc1ef09e4485504e15dfbb/src/pages/components/CodeEditor/index.tsx#L259-L263

scopedKey 查找永远 miss,始终回退到 baseKey,多编辑器实例间的 key 隔离没有生效。建议写入端也加上 model.uri 前缀,或者暂时移除 scopedKey 逻辑等后续完整实现。


3. registerEditor() 提前 return 跳过 provider 注册

window.MonacoEnvironment.myLinterWorker 已存在时(SW 重启后模块重新加载),函数提前 return,跳过了 registerHoverProviderregisterCodeActionProvider 和 TS 编译器选项设置:

https://github.com/nickyc975/scriptcat/blob/c69f98b9fb1ec8fbc0dc1ef09e4485504e15dfbb/src/pkg/utils/monaco-editor/index.ts#L69-L74

建议将 worker 初始化和 provider 注册分离——myLinterWorker 检查只复用已有 worker,但 provider 注册应始终执行(由 isRegisterEditorDone 统一防重复)。


以上是主要的功能性建议,其他重构和代码清理部分看起来不错。

🤖 Generated with Claude Code

@cyfung1031
Copy link
Collaborator Author

nickyc975 ?

1, 2, 3 这3个问题我都不太清楚
AI有更好的方案就跟AI去改吧
反正问题有解决就好

让AI提交修正代码吧

@CodFrm
Copy link
Member

CodFrm commented Mar 21, 2026

nickyc975 ?

AI搞错了。。。。

我也不太明白这块,eslint最开始也是由另外的贡献者处理的,我让ai处理,我再测测,没问题 我就合并

cyfung1031 and others added 3 commits March 22, 2026 04:23
1. eslintFixMap 每次 lint 结果到来时先 clear 再写入,避免过期条目残留
2. 移除未完整实现的 scopedKey 逻辑,统一使用 baseKey
3. registerEditor 中 worker 复用不再提前 return,provider 注册始终执行
@CodFrm CodFrm merged commit 00e1f9c into scriptscat:release/v1.4 Mar 22, 2026
2 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 🐌 非重要也不紧急的内容

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] globalCache.get("eslint-fix") 為 undefined

3 participants