Skip to content

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.

不确定这样是否正确

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant