✨ Monaco 编辑器新增 @resource 名称重复警告 - #1762
Open
cyfung1031 wants to merge 2 commits into
Open
cyfung1031 wants to merge 2 commits into
cyfung1031 wants to merge 2 commits into
Conversation
Add a diagnostic-only ScriptCat Monaco marker (scriptcat/duplicate-resource-name) that flags @resource declarations sharing the same case-sensitive name within the first valid ==UserScript== block, reusing parseResourceDeclaration for parsing. No quick fix is offered and no save-time validation is added, since ScriptCat cannot infer whether the user intends to rename, delete, or keep a different URL. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Collaborator
Author
…te-resource-warning-1e420b # Conflicts: # src/pkg/utils/monaco-editor/index.ts # src/pkg/utils/monaco-editor/langs/de-DE.ts # src/pkg/utils/monaco-editor/langs/en-US.ts # src/pkg/utils/monaco-editor/langs/ja-JP.ts # src/pkg/utils/monaco-editor/langs/ko-KR.ts # src/pkg/utils/monaco-editor/langs/pt-BR.ts # src/pkg/utils/monaco-editor/langs/ru-RU.ts # src/pkg/utils/monaco-editor/langs/tr-TR.ts # src/pkg/utils/monaco-editor/langs/vi-VN.ts # src/pkg/utils/monaco-editor/langs/zh-CN.ts # src/pkg/utils/monaco-editor/langs/zh-TW.ts # src/pkg/utils/monaco-editor/metadata.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Checklist / 检查清单
N/A — 无关联 issue(本次改动为主动新增的编辑器诊断能力,非已报告问题的修复)。
背景
@resource声明重复同一个资源名称时(例如复制粘贴导致两行@resource用了相同的名字),当前 Monaco 编辑器不会给出任何提示,用户只能在运行时才发现GM_getResourceURL/GM_getResourceText取到的是后一条声明覆盖前一条的结果。这属于纯编辑器诊断缺口,不影响保存或运行时逻辑本身。本次改动
src/pkg/utils/monaco-editor/metadata.ts新增getDuplicateResourceNameMatches:只扫描第一个成对闭合的==UserScript==区块(与现有 metadata 解析器语义一致),只检查@resource声明,复用src/pkg/utils/resource.ts的parseResourceDeclaration解析<name> <url>(不新增第二套解析器),按declaration.name做大小写敏感分组,一个名称出现多次时为每条冲突声明都返回一个匹配,返回的列范围只覆盖资源名称本身,不包含整行。src/pkg/utils/monaco-editor/index.ts注册新规则scriptcat/duplicate-resource-name,产出MarkerSeverity.Warning级别的 marker,并接入updateScriptcatMetadataMarkers()。src/pkg/utils/monaco-editor/langs/*.ts全部 10 个语言文件中新增duplicateResourceName文案键,保持EditorLangEntry结构一致(pnpm run check:i18n校验通过)。@resource可能是要删除一条、改名,还是保留另一个 URL,ScriptCat 无法安全替用户做决定,因此只报告警告。实现考虑
getMetadataAlignmentBlocks已解析出的MetadataAlignmentLine(含tag/value/valueColumn),因为合法的@resource声明必然带有[ \t]+分隔的值,一定会出现在对齐行集合里,因此无需像getUndefinedMetadataTagMatches那样额外用model.getLineContent重新匹配裸标签(@resource不存在“无值裸标签”的情形)。line.value做trim(),与src/pkg/utils/script.ts中parseMetadata/parseMetadataLines对元数据值统一.trim()后再使用的既有语义保持一致。已知限制
src/pkg/utils/script.ts未改动,hasDuplicatedMetaline()对其他重复元数据行的既有行为不在本次范围内。foo与Foo不视为冲突),与parseResourceDeclaration对name字段的既有语义一致。建议审查重点
getDuplicateResourceNameMatches的分组与列范围计算(metadata.test.ts中的"返回的列范围应精确覆盖资源名称"用例)。scriptcat/undefined-metadata-tag、对齐等规则。Ctrl+S)未被新规则新增任何阻断。验证
命令与结果对应本 PR 头部提交 978dd1b(分支
claude/monaco-duplicate-resource-warning-1e420b,基于main):pnpm exec vitest run --no-coverage src/pkg/utils/monaco-editor/metadata.test.ts— 21 个用例全部通过,覆盖任务要求的完整矩阵:不同 URL 重复、相同 URL 重复、不同名称不告警、foo/Foo大小写不冲突、格式不合法声明被忽略、@require等其他标签下同值被忽略、区块外的@resource被忽略、第二个UserScript区块被忽略、精确列范围。pnpm run typecheck— 通过,无错误。pnpm run lint(prettier --check、tsc --noEmit、check:i18n、check:issue-templates、eslint)— 全部通过。docs/verification.md流程):pnpm run build构建扩展后,用e2e/session.mjs+e2e/drive.mjs驱动真实 options 页脚本编辑器,输入两条同名不同 URL 的@resource Resource_vConsoleVueDevtools声明。截图与鼠标悬停确认:两行都出现橙色警告波浪线且范围只覆盖资源名称本身;悬停提示文案为@resource name "Resource_vConsoleVueDevtools" is declared more than once. ScriptCat(scriptcat/duplicate-resource-name),且显示 "No quick fixes available"(未注册 quick fix);随后Ctrl+S保存后经chrome.storage.local确认脚本已正常保存/安装,保存流程未被新规则阻断。🤖 Generated with Claude Code