Skip to content
Merged
Show file tree
Hide file tree
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): reject unresolved config property overrides
  • Loading branch information
Jarvis636431 committed Oct 26, 2026
commit 00a7f4d46f4f42e49e24153d7e42ee8b56ae5ad0
75 changes: 72 additions & 3 deletions packages/taro-cli/src/__tests__/create-page.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,82 @@ describe.each(['object', 'defineAppConfig'])('create page config: %s', (format)
expect(result.code).toContain('"root":"pkg/","pages":["home/index","new/index"]')
})

it('adds a missing subPackages property after a spread', () => {
const result = updateConfig(wrap('{ ...baseConfig, pages: [] }'), 'pkg')
it('adds a missing subPackages property', () => {
const result = updateConfig(wrap('{ pages: [] }'), 'pkg')
expect(result.state).toBe(ConfigModificationState.Success)
expect(result.code).toContain('...baseConfig')
expect(result.code).toContain('subPackages:[{root:"pkg/",pages:["new/index"]}]')
})

it.each([
'pages() {}',
'get pages() { return [] }',
'set pages(value) {}',
'[key]: []',
'...baseConfig'
])('does not update pages shadowed by %s', (member) => {
const result = updateConfig(wrap(`{ pages: [], ${member} }`))
expect(result.state).toBe(ConfigModificationState.Fail)
expect(result.code).toBe(result.originalCode)
})

it.each([
'subPackages() {}',
'get subPackages() { return [] }',
'set subPackages(value) {}',
'[key]: []',
'...baseConfig'
])('does not update or add subPackages shadowed by %s', (member) => {
for (const properties of [member, `subPackages: [], ${member}`]) {
const result = updateConfig(wrap(`{ ${properties} }`), 'pkg')
expect(result.state).toBe(ConfigModificationState.Fail)
expect(result.code).toBe(result.originalCode)
}
})

it.each([
'root() {}',
'get root() { return "pkg/" }',
'set root(value) {}',
'[key]: "pkg/"',
'...subConfig'
])('does not update or add a subpackage with root shadowed by %s', (member) => {
for (const properties of [`${member}, pages: []`, `root: "pkg/", ${member}, pages: []`]) {
const result = updateConfig(wrap(`{ subPackages: [{ ${properties} }] }`), 'pkg')
expect(result.state).toBe(ConfigModificationState.Fail)
expect(result.code).toBe(result.originalCode)
}
})

it.each([
'pages() {}',
'get pages() { return [] }',
'set pages(value) {}',
'[key]: []',
'...subConfig'
])('does not update subpackage pages shadowed by %s', (member) => {
const result = updateConfig(wrap(`{ subPackages: [{ root: "pkg/", pages: [], ${member} }] }`), 'pkg')
expect(result.state).toBe(ConfigModificationState.Fail)
expect(result.code).toBe(result.originalCode)
})

it.each([
'{ pages() {}, pages: [] }',
'{ get pages() { return [] }, pages: [] }',
'{ set pages(value) {}, pages: [] }',
'{ [key]: [], pages: [] }',
'{ ...baseConfig, pages: [] }'
])('updates pages when an explicit array overrides earlier members: %s', (config) => {
const result = updateConfig(wrap(config))
expect(result.state).toBe(ConfigModificationState.Success)
expect(result.code).toContain('pages:["pages/new/index"]')
})

it('does not add a subpackage when an existing root is dynamic', () => {
const result = updateConfig(wrap('{ subPackages: [{ root: packageRoot, pages: [] }] }'), 'pkg')
expect(result.state).toBe(ConfigModificationState.Fail)
expect(result.code).toBe(result.originalCode)
})

it.each([
'{ "subPackages": subPackages }',
'{ "subPackages": [{ "root": "pkg/", "pages": pageList }] }'
Expand Down
28 changes: 19 additions & 9 deletions packages/taro-cli/src/util/createPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import type { NodePath } from 'babel__traverse'

const t = babelKit.types

const getObjectProperty = (node: Node | null | undefined, name: string): ObjectProperty | undefined => {
if (node?.type !== 'ObjectExpression') return
// Later explicit properties override earlier properties with the same key.
// undefined means absent; null means the effective property cannot be safely resolved.
const getObjectProperty = (node: Node | null | undefined, name: string): ObjectProperty | null | undefined => {
if (node?.type !== 'ObjectExpression') return null
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 })
)) return property
if (property.type === 'SpreadElement' || property.computed) return null
if (t.isIdentifier(property.key, { name }) || t.isStringLiteral(property.key, { value: name })) {
return property.type === 'ObjectProperty' ? property : null
}
}
}

Expand All @@ -36,6 +37,7 @@ const isValidSubPkgObject = (subPkgObject: ObjectExpression) => {
const addNewSubPackage = (node: ObjectExpression, page: string, subPackage: string): ConfigModificationState => {
if (!t.isObjectExpression(node)) return ConfigModificationState.Fail
let subPackages = getObjectProperty(node, 'subPackages')
if (subPackages === null) return ConfigModificationState.Fail
if (!subPackages) {
// config 文件不存在 subPackages 字段的情况,给该字段赋予默认值
const subPkgObject = t.objectProperty(t.identifier('subPackages'), t.arrayExpression([]))
Expand All @@ -46,9 +48,17 @@ const addNewSubPackage = (node: ObjectExpression, page: string, subPackage: stri

// 文件格式不对的情况
if (!value || value?.type !== 'ArrayExpression') return ConfigModificationState.Fail
let targetSubPkgObject = value.elements.find(node =>
t.isStringLiteral(getObjectProperty(node, 'root')?.value, { value: subPackage })
) as ObjectExpression | undefined
let targetSubPkgObject: ObjectExpression | undefined
for (const element of value.elements) {
const rootProperty = getObjectProperty(element, 'root')
if (rootProperty === null) return ConfigModificationState.Fail

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.

这里遇到一个无法确定 root 的分包配置时就直接返回了,后面的分包不会再继续检查。

比如:

subPackages: [
  { root: rootVar, pages: [] },
  { root: 'pkg/', pages: [] }
]

这时 pkg/ 其实是能确定的,应该可以正常添加页面,但现在会因为前面的 rootVar 直接失败。之前的逻辑会继续查找,不会受这个无关配置影响。

建议先继续检查后面的分包;如果找到了明确匹配的 root 就更新它。只有找不到匹配项、又存在无法判断的配置时,再返回失败。最好补一个对应的测试。

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.

感谢指出,这里确实不应该在遇到无法确定 root 的分包项时直接结束查找。已在 f71dfca 中修改:

  • 遇到动态 root、getter、计算属性或展开项时,记录不确定状态并继续查找;找到明确匹配的 root 后,按现有校验更新该分包。
  • 只有未找到明确匹配项且存在不确定项时才返回 Fail,避免贸然新增分包;所有现有 root 均可确定且不匹配时,仍正常新增。
  • 保留同一对象内部的后置成员覆盖判断。

新增 22 个回归用例,覆盖普通对象和 defineAppConfig 两种形式、不确定项位于明确目标之前或之后、未匹配时拒绝新增,以及全部 root 已知时正常新增;同时检查其他配置保持原样。修复前该测试集 10 个失败、133 个通过,修复后 143 个全部通过。CLI 的 tsc 构建、修改文件 ESLint 和 git diff --check 均通过。本轮未重跑 CLI 全量测试。

烦请复查。

if (!rootProperty) continue
if (rootProperty.value.type !== 'StringLiteral') return ConfigModificationState.Fail
if (rootProperty.value.value === subPackage && element?.type === 'ObjectExpression') {
targetSubPkgObject = element
break
}
}

if (!targetSubPkgObject) {
// 不存在 当前分包配置对象的情况
Expand Down
Loading