-
Notifications
You must be signed in to change notification settings - Fork 292
parseMetadata 代码优化 #903
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
parseMetadata 代码优化 #903
Conversation
cb2ff0c to
5a2ffe6
Compare
* 单元测试优化(错误修正,注入修正,concurrent,加速) * 统一 `vitest.test` -> `vitest.it` * #903 * 代码修正
079038e to
94faaea
Compare
There was a problem hiding this 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重构了用户脚本元数据解析逻辑,简化了代码并提升了可读性。
主要变化:
- 将元数据解析的正则表达式提取为常量,改善代码组织
- 重构
parseMetadata函数,使用更简洁的逻辑和更清晰的变量命名 - 增强验证逻辑,对空白的
name和version字段进行检查
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/pkg/utils/script.ts | 重构元数据解析逻辑,提取正则表达式常量,简化parseMetadata函数实现,增强字段验证 |
| src/pkg/utils/script.test.ts | 新增多个测试用例,覆盖元数据解析的各种边界情况,包括分行、空白处理、兼容性等场景 |
| values.push(val); | ||
| } | ||
| return ret; | ||
| if (!metadata.name || Object.keys(metadata).length < 3) return null; |
Copilot
AI
Nov 6, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] 这个条件包含两个不相关的验证逻辑合并在一行。!metadata.name检查name字段是否存在,而Object.keys(metadata).length < 3检查元数据字段数量。建议拆分为两个独立的if语句以提高可读性和可维护性,并为每个验证添加清晰的注释说明其目的。
| if (!metadata.name || Object.keys(metadata).length < 3) return null; | |
| // 检查 name 字段是否存在 | |
| if (!metadata.name) return null; | |
| // 检查元数据字段数量是否足够 | |
| if (Object.keys(metadata).length < 3) return null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不用改。现在这样就可以
| // 不接受空白name | ||
| if (!metadata.name?.[0]) { |
Copilot
AI
Nov 6, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
验证逻辑不一致。第82行使用!metadata.name?.[0]检查name是否为空,但第41行已经检查了!metadata.name。如果metadata.name已经在第41行确保存在,那么第82行的可选链?.是不必要的。建议统一验证方式,要么在parseMetadata中就验证非空值,要么在这里进行完整验证。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
意义不同
没@name 是 error_metadata_invalid
有@name 但空白是 error_script_name_required
虽然是有双重检查
但没办法。parseMetadata 被引用的地方也很多
保持跟原来一致
2d590ed to
43dc6b8
Compare
* 单元测试优化(错误修正,注入修正,concurrent,加速) * 统一 `vitest.test` -> `vitest.it` * scriptscat#903 * 代码修正
43dc6b8 to
7e34ed4
Compare
概述 Descriptions
变更内容 Changes
截图 Screenshots