Skip to content

Commit 5808677

Browse files
committed
feat: 添加对 tabBar 页面未声明的错误提示
1 parent 1d5f65c commit 5808677

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

packages/uni-cli-shared/src/json/utils.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export function checkPagesJson(jsonStr: string, inputDir: string) {
2929
}
3030
const errors: ParseError[] = []
3131
const root = parseTree(jsonStr, errors)
32+
let allPages: string[] = [] // 收集全部页面,包含分包页面
33+
let tabBarPages: string[] = [] // 收集 tabBar 页面
3234
if (!root) {
3335
if (errors.length) {
3436
for (const error of errors) {
@@ -88,6 +90,41 @@ export function checkPagesJson(jsonStr: string, inputDir: string) {
8890
}
8991
})
9092

93+
allPages.push(...pagePathNodes.map((node) => node.value as string))
94+
95+
const tabBarNode = root.children?.find(
96+
(child) =>
97+
child.type === 'property' &&
98+
child.children?.length === 2 &&
99+
child.children[0].value === 'tabBar'
100+
)
101+
if (tabBarNode) {
102+
findRootNode(tabBarNode.children![1], ['list']).forEach((node) => {
103+
const pagePathNode =
104+
node.type === 'object' &&
105+
node.children?.find(
106+
(child) =>
107+
child.type === 'property' &&
108+
child.children?.length === 2 &&
109+
child.children[0].value === 'pagePath'
110+
)
111+
if (pagePathNode) {
112+
const pagePathValueNode = pagePathNode.children![1]
113+
const pagePath = pagePathValueNode.value as string
114+
tabBarPages.push(pagePath)
115+
if (
116+
!allPages.includes(pagePath) &&
117+
!allPages.includes(pagePath.substring(1))
118+
) {
119+
throwCompilerError(
120+
jsonStr,
121+
pagePathValueNode,
122+
M['pages.json.tabbar.page.notfound'].replace('{pagePath}', pagePath)
123+
)
124+
}
125+
}
126+
})
127+
}
91128
for (const node of pagePathNodes) {
92129
const pagePath: string = node.value ?? ''
93130
if (!pageExistsWithCaseSync(path.join(inputDir, pagePath))) {

packages/uni-cli-shared/src/messages/en.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,6 @@ export default {
6464
'uni_modules.import': 'Plug-in [{0}] only supports @/uni_modules/{1}.',
6565
'pages.json.page.notfound': 'The page "{pagePath}" does not exist.',
6666
'pages.json.page.slash': 'The Path "{pagePath}" cannot start with "/"',
67+
'pages.json.tabbar.page.notfound':
68+
'The tabBar page "{pagePath}" is not declared in "pages".',
6769
} as const

packages/uni-cli-shared/src/messages/zh_CN.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,6 @@ export default {
6767
'pages.json.page.notfound':
6868
'页面"{pagePath}"不存在,请确保填写的页面路径不包含文件后缀,且必须与真实的文件路径大小写保持一致。',
6969
'pages.json.page.slash': '路径 "{pagePath}" 不能以 "/" 开头',
70+
'pages.json.tabbar.page.notfound':
71+
'tabBar 中配置的页面 "{pagePath}" 未在 pages 中注册。',
7072
} as const

0 commit comments

Comments
 (0)