Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(cli): explicitly narrow config object types
  • Loading branch information
Jarvis636431 committed Oct 26, 2026
commit 0d65b9c9a7c8fa294f615bcfc331d7d13836acc3
4 changes: 2 additions & 2 deletions packages/taro-cli/src/util/createPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { NodePath } from 'babel__traverse'
const t = babelKit.types

const getObjectProperty = (node: Node | null | undefined, name: string): ObjectProperty | undefined => {

Copy link
Copy Markdown
Collaborator

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。

Copy link
Copy Markdown
Contributor Author

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 返回的选项不包含它;已通过直接调用该插件确认这一差异。

if (!t.isObjectExpression(node)) return
if (node?.type !== 'ObjectExpression') return
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
// 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]
Expand Down Expand Up @@ -57,7 +57,7 @@ const addNewSubPackage = (node: ObjectExpression, page: string, subPackage: stri
value.elements.push(subPkgItemObject)
}

if (targetSubPkgObject.type !== 'ObjectExpression' || !isValidSubPkgObject(targetSubPkgObject)) return ConfigModificationState.Fail
if (!targetSubPkgObject || targetSubPkgObject.type !== 'ObjectExpression' || !isValidSubPkgObject(targetSubPkgObject)) return ConfigModificationState.Fail
const pagesProperty = getObjectProperty(targetSubPkgObject, 'pages')!
const currentPages = (pagesProperty.value as ArrayExpression).elements
const isPageExists = Boolean(currentPages.find(node => (node as any).value === page))
Expand Down
Loading