Skip to content

✨ 未定义元数据标签在编辑器中显示警告#1608

Merged
CodFrm merged 3 commits into
scriptscat:mainfrom
cyfung1031:claude/undefined-prompt-warning-e01600
Jul 20, 2026
Merged

✨ 未定义元数据标签在编辑器中显示警告#1608
CodFrm merged 3 commits into
scriptscat:mainfrom
cyfung1031:claude/undefined-prompt-warning-e01600

Conversation

@cyfung1031

@cyfung1031 cyfung1031 commented Jul 17, 2026

Copy link
Copy Markdown
Collaborator

Checklist / 检查清单

  • Fixes: unrecognized @tag metadata lines only surfaced as a hover tooltip; they now also show as an editor warning marker
  • Code reviewed by human / 代码通过人工检查
  • Changes tested / 已完成测试

背景

编辑器悬浮提示已能识别不在 ScriptCat 元数据提示表中的 @tag(显示 "Undefined Prompt"),但这些标签在编辑器中没有任何可见的警告标记,用户只有主动悬浮才能发现该标签暂不受支持。

本次改动

  • 新增 getUndefinedMetadataTagMarkerssrc/pkg/utils/monaco-editor/index.ts),扫描 ==UserScript== 元数据块内的每一行,复用悬浮提示已用的 metadataHoverPattern 提取标签
  • 若标签(小写化后)不在 promptByMetadataTag 中,则在编辑器中生成一个 MarkerSeverity.Warning 标记(rule id scriptcat/undefined-metadata-tag),消息复用既有的 currentEditorLang.undefinedPrompt 文案(已支持所有语言),提示用户暂不受支持
  • 未提供快速修复(quick fix)动作,仅作提示

已知限制

  • 不提供自动修复或建议;仅提示该标签尚不受支持
  • 沿用已有的 undefinedPrompt 文案,未新增专门的“不支持”措辞

建议审查重点

  • 已知/受支持的元数据标签(如 namegrantrun-at 等)不应被误标记为警告
  • 布尔型标签(无值,如 @noframes)也应被正确识别,不受 metadataAlignmentPattern 需要尾随空格+值的限制

验证

  • pnpm run typecheck — 通过
  • pnpm exec eslint src/pkg/utils/monaco-editor/index.ts — 无报错
  • 独立 Node 脚本验证正则对已知/未知/布尔型标签的匹配与列号计算正确
  • 提交前 husky pre-commit(prettier + typecheck)通过
  • 由用户在浏览器中加载扩展手动验证生效

@tag 若不在已知元数据提示列表中,现在会在编辑器中以警告标记显示(沿用悬浮提示的 undefinedPrompt 文案),提示用户该标签暂未被 ScriptCat 支持。

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@cyfung1031

Copy link
Copy Markdown
Collaborator Author

i have checked the code and confirmed with browser testing.

// ==UserScript==
// @name        A Script 1234
// @namespace   http://tampermonkey.net/
// @version     2025-02-28
// @description try to take over the world!
// @author      You
// @match       https://*/*
// @icon        https://www.google.com/s2/favicons?sz=64&domain=github.com
// @grant       none
// @sandbox     JavaScript
// @run-at      document-start
// ==/UserScript==

(function() {
    'use strict';

    // Your code here...
    console.error("history length: " + history.length);
})();
Screenshot 2026-07-18 at 4 55 28 Screenshot 2026-07-18 at 4 55 44

@cyfung1031 cyfung1031 added the P1 🔥 重要但是不紧急的内容 label Jul 18, 2026
@cyfung1031 cyfung1031 added this to the 2026七月 Milestone milestone Jul 18, 2026
@CodFrm

CodFrm commented Jul 18, 2026

Copy link
Copy Markdown
Member

复核了当前 head e002574 的实现。现有浏览器验证覆盖了常见标签,但下面几类问题仍可稳定复现,因此目前不建议直接合并

  1. 合法的本地化标签会被误报src/pkg/utils/monaco-editor/index.ts:773
    当前使用 promptByMetadataTag 是否存在作为“标签是否受支持”的判断,但它只是 hover 文案表,不是运行时支持列表。ScriptCat 实际支持并消费 @name:zh-CN@description:en(见 src/locales/locales.ts:114-134),该实现会把它们标为 undefined。其他运行时支持但没有 hover 文案的标准标签也有相同风险。建议建立与运行时共享、与语言无关的 metadata classifier,显式支持 name:<locale>/description:<locale> 等模式,不要用提示文案表充当语义白名单。

  2. 会扫描未闭合或运行时不会采用的 metadata blockindex.ts:457-495, 765
    getMetadataAlignmentBlocks 在 EOF 时无条件结束 block,也会收集多个 block;而运行时解析要求成对的 ==UserScript==/==/UserScript==,并采用首个匹配块。实测缺少结束符后,后续普通 // @foo 注释也会被标警告;第二个完整 block 也会被扫描。诊断范围应与实际 parseMetadata 语义一致。

  3. 部分前缀编辑会留下过期 markerindex.ts:734-756, 840-844
    例如在 // @foo 行首插入 x,编辑后已不再是 metadata 注释,但 contentChangeCanAffectMetadataMarkers 会返回 false,旧 marker 不会重新计算/清除。

  4. 新行为没有自动化回归测试
    建议将标签分类和 block 选择抽成纯函数,并覆盖:本地化标签、已知 alias、大小写、布尔标签、连字符标签、未闭合/多个 block、精确 range、marker 新增/替换/清除及语言切换。

修复以上误报、扫描范围和 marker 生命周期后,再跑 typecheck、lint 与定向测试即可。FOSSA License Compliance 外部状态与这些实现问题应分开判断。

- 本地化标签(name:<locale>/description:<locale>)不再被误报为未定义标签
- 区块扫描改为与运行时 parseMetadata 一致:仅识别首个成对闭合的 UserScript 区块
- 编辑注释行首字符后不再遗留过期 marker
- 将纯逻辑抽取到 metadata.ts 并补充单元测试

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@cyfung1031

Copy link
Copy Markdown
Collaborator Author

Thanks for the detailed review @CodFrm — confirmed all four points against e002574 and pushed fixes in 5603b7c.

  1. 本地化标签误报:新增 resolveMetadataTagBasesrc/pkg/utils/monaco-editor/metadata.ts),在查表前剥离 :<locale> 后缀,name:zh-CN/description:en 等不再被标为未定义;hover 提示同样复用该逻辑。
  2. 区块扫描范围getMetadataAlignmentBlocks 改为与运行时 parseMetadatasrc/pkg/utils/script.ts:21)语义一致——只识别首个成对闭合的 ==UserScript==/==/UserScript== 区块,未闭合区块不再产出任何诊断,多余的第二个区块也不再被扫描。
  3. 过期 marker:改为记录每个 model 上一次识别到的 metadata 区块行范围(WeakMap),只要编辑发生在该区块范围内就强制重新计算 marker,不再依赖对编辑前后文本形态的正则猜测,因此在 // @foo 行首插入字符这类场景下 marker 会正确刷新。
  4. 测试覆盖:把上述纯逻辑抽到 src/pkg/utils/monaco-editor/metadata.ts,新增 metadata.test.ts,覆盖本地化标签、大小写/连字符标签、未闭合与多区块、精确 marker range、以及区块内编辑触发重算等场景。

已过 tsc --noEmiteslint、以及完整测试套件(305 files / 3380 tests)。麻烦再看一下。

@cyfung1031
cyfung1031 marked this pull request as draft July 20, 2026 06:21
@cyfung1031
cyfung1031 marked this pull request as ready for review July 20, 2026 06:24
@cyfung1031

Copy link
Copy Markdown
Collaborator Author

checked by me.

@CodFrm
CodFrm merged commit ebafdcd into scriptscat:main Jul 20, 2026
7 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P1 🔥 重要但是不紧急的内容

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants