Repository navigation
fix(cli): 修复创建页面时配置属性识别 #19561
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
fix(cli): 修复创建页面时配置属性识别 #19561
Changes from 1 commit
ce2ca51
6600101
0d65b9c
00a7f4d
f71dfca
7d42d01
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,11 +9,13 @@ const t = babelKit.types | |
|
|
||
| const getObjectProperty = (node: Node | null | undefined, name: string): ObjectProperty | undefined => { | ||
| if (!t.isObjectExpression(node)) return | ||
| return node.properties.find((property): property is ObjectProperty => | ||
| t.isObjectProperty(property) && !property.computed && ( | ||
| // Later explicit properties override earlier properties with the same key. | ||
| for (let index = node.properties.length - 1; index >= 0; index--) { | ||
| const property = node.properties[index] | ||
| if (t.isObjectProperty(property) && !property.computed && ( | ||
| t.isIdentifier(property.key, { name }) || t.isStringLiteral(property.key, { value: name }) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里只匹配 ObjectProperty,跳过了同名的 ObjectMethod/getter/setter。 例如: export default { 或者: export default { Babel 会将这些 method/getter 解析为 ObjectMethod。运行时真正生效的是后面的成员,但当前实现会跳过它,转而修改前面的 pages 数组,并返回 Success,导致新页面实际上不会注册。 旧实现会命中同名 method,并因为其值不是数组而返回 Fail,因此这里会把原本安全失败的情况变成静默修改无效配置的回归。subPackages 中的 root 和 pages 也存在同样问题。 建议从右向左扫描所有同名且非 computed 的成员:如果最后生效的成员是 ObjectProperty,再校验其值;如果是 ObjectMethod/getter/setter,则直接视为不支持并返回 Fail,不要回退到更早的属性。同时补充上述场景的回归测试。
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 感谢指出,确实存在跳过后置 ObjectMethod 后修改无效属性并返回 Success 的回归。已在 00a7f4d 中修复:
补充了普通对象与 defineAppConfig 两种形式的主包 pages、subPackages、分包 root/pages 回归测试,以及后置显式属性正常覆盖的用例。修复前该测试集 42 个失败、79 个通过;修复后 121 个全部通过。CLI 的 tsc 构建、修改文件 ESLint 和 git diff --check 均通过。本轮未重跑 CLI 全量测试;此前 doctor 插件选项与测试预期不一致的两项失败已在前面的回复中说明。 烦请复查。 |
||
| ) | ||
| ) | ||
| )) return property | ||
| } | ||
| } | ||
|
|
||
| const generateNewSubPackageItem = (subPackage: string) => { | ||
|
|
||
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.
这里的参数类型是
Node | null | undefined,但当前 Babel types 的类型守卫不会把node收窄到ObjectExpression,因此访问node.properties会导致 TypeScript 编译失败(TS18049/TS2339)。建议将参数类型限制为
ObjectExpression | null | undefined,或先通过显式类型守卫得到一个ObjectExpression局部变量后再访问properties。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.
感谢指出,已在 0d65b9c 中改为直接检查
node?.type !== 'ObjectExpression',通过 AST 的 type 字段显式收窄类型,再访问properties,避免依赖这里的 Babel 类型守卫。修改后 CLI 的
tsc构建、ESLint 和 69 个页面配置测试均通过。本机原代码也能通过构建,暂未复现您报告的编译错误,因此补上了明确的类型检查,烦请复查。另外如实补充全量测试结果:226 个通过、7 个跳过、2 个失败。两项失败均位于
doctor-config.spec.ts,预期的 framework 选项包含none,但本机安装的@tarojs/plugin-doctor返回的选项不包含它;已通过直接调用该插件确认这一差异。