-
Notifications
You must be signed in to change notification settings - Fork 4.9k
My feauture compon #17933
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?
My feauture compon #17933
Conversation
""" Walkthrough本次变更为 Taro 框架引入了对 ASCF 平台的全新支持,涵盖 CLI、平台插件、组件适配、模板、运行时、类型声明等多个层面。新增了平台插件包 Changes
Sequence Diagram(s)sequenceDiagram
participant CLI
participant Kernel
participant Plugin-Platform-ASCF
participant AscfApp
participant Template
participant Webpack
CLI->>Kernel: 解析命令与平台参数
Kernel->>Plugin-Platform-ASCF: 加载 ASCF 平台插件
Plugin-Platform-ASCF->>Kernel: 注册 ascf 平台(configName: mini)
Kernel->>AscfApp: 实例化并启动 AscfApp
AscfApp->>Template: 初始化模板与组件适配
AscfApp->>Webpack: 修改 Webpack 配置(sourcemap 路径修正)
AscfApp-->>Kernel: 平台编译流程继续
sequenceDiagram
participant ReactComponent
participant withAscfAdapter
participant ASCF_MAPPINGS
participant ENV
ReactComponent->>withAscfAdapter: 被包裹
withAscfAdapter->>ENV: 检查 TARO_ENV
alt TARO_ENV == 'ascf'
withAscfAdapter->>ASCF_MAPPINGS: 查询属性映射
withAscfAdapter->>ReactComponent: 传递适配后的 props
else
withAscfAdapter->>ReactComponent: 传递原始 props
end
Possibly related PRs
Suggested reviewers
Poem
""" ✨ Finishing Touches
🧪 Generate Unit Tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 5
🧹 Nitpick comments (8)
package.json (1)
160-160
: 依赖排序调整无实际收益仅移动
"typescript"
行的位置,会在 Git 历史里造成多余噪音。若非出于工具排序或冲突解决需求,建议避免此类纯格式改动,以保持提交简洁。packages/taro-platform-ascf/README.md (1)
1-4
: README 过于简略,缺少关键使用信息建议补充以下内容:
- 安装与启用方式(CLI 命令、配置示例)。
- 支持的运行时/编译差异及已知限制。
- 与其他平台差异的 API/组件映射说明。
完善文档可降低新用户上手成本。
packages/taro-platform-ascf/tsconfig.json (1)
1-13
: tsconfig 设置基本完备,但可补充 rootDir建议显式添加:
"compilerOptions": { ... + "rootDir": "./src",
并在
package.json
指定types
字段指向types/index.d.ts
,可避免发布后声明文件路径错位。packages/taro-components/src/common/ascf-adapter.tsx (1)
20-32
: 建议改进类型安全性当前使用
any
类型降低了类型安全性,建议考虑使用更具体的类型定义来提高代码质量。- const ascfProps: any = {} + const ascfProps: Partial<T> = {}同时可以考虑为映射配置添加类型定义:
interface PropMapping { ascfProp: string valueMap?: Record<string, any> default?: any } interface ComponentMappings { [componentName: string]: { [propName: string]: PropMapping } }packages/taro-platform-ascf/src/runtime-utils.ts (1)
19-33
: 改善代码可读性和维护性该方法存在以下问题:
data.nn
的含义不清晰,需要更好的命名或注释- 使用了魔法数字
'8'
作为默认值建议添加注释说明数据结构,并使用具名常量替代魔法数字。
+ const DEFAULT_TEXT_NODE_NUM = '8' + /** + * 处理元素的水合数据传输 + * @param data - 包含节点信息的数据对象,其中 nn 为节点名称 + * @param element - 传输元素 + * @param componentsAlias - 组件别名映射 + */ transferHydrateData (data, element, componentsAlias) { if (element.isTransferElement) { const pages = getCurrentPages() const page = pages[pages.length - 1] data[Shortcuts.NodeName] = element.dataName page.setData({ [toCamelCase(data.nn)]: data }) return { sid: element.sid, [Shortcuts.Text]: '', - [Shortcuts.NodeName]: componentsAlias['#text']?._num || '8' + [Shortcuts.NodeName]: componentsAlias['#text']?._num || DEFAULT_TEXT_NODE_NUM } } },packages/taro-platform-ascf/rollup.config.mjs (1)
11-19
: 建议为 TypeScript 插件添加显式配置当前 TypeScript 插件使用默认配置,建议添加显式配置以确保构建的一致性和正确性。
const base = { plugins: [ nodeResolve(), externals({ peerDeps: true, }), - typescript() + typescript({ + tsconfig: './tsconfig.json', + declarationDir: './dist', + declaration: true + }) ] }packages/taro-platform-ascf/src/template.ts (2)
57-73
: 提高函数模板的可维护性当前实现通过字符串拼接生成 JavaScript 函数,这种方式难以维护和测试。建议:
- 将组件别名提取为常量
- 考虑使用模板字符串提高可读性
buildXSTepFocus (nn: string) { if (this.pluginOptions.enablekeyboardAccessory) { const textarea = this.componentsAlias.textarea._num const input = this.componentsAlias.input._num const ka = this.componentsAlias['keyboard-accessory']._num - return `function(i, prefix) { - var s = i.focus !== undefined ? 'focus' : 'blur' - var r = prefix + i.${nn} + '_' + s - if ((i.nn === '${textarea}' || i.nn === '${input}') && i.cn[0] && i.cn[0].nn === '${ka}') { - r = r + '_ka' - } - return r - }` + return `function(i, prefix) { + var s = i.focus !== undefined ? 'focus' : 'blur' + var r = prefix + i.${nn} + '_' + s + if ((i.nn === '${textarea}' || i.nn === '${input}') && i.cn[0] && i.cn[0].nn === '${ka}') { + r = r + '_ka' + } + return r + }` } else { return super.buildXSTepFocus(nn) } }
107-132
: 页面模板构建逻辑清晰 👍
buildPageTemplate
方法结构良好,将组件属性生成逻辑抽取到getComponentAttrs
内部函数中,提高了代码的可读性。建议在模板字符串中保持一致的缩进格式:
pageMetaTemplate = ` -<hjs module="xs" src="${baseTempPath.replace('base.hxml', 'utils.hjs')}" /> -<page-meta data-sid="{{pageMeta.sid}}" ${pageMetaAttrs}> - <navigation-bar ${navigationBarAttrs}/> -</page-meta>` + <hjs module="xs" src="${baseTempPath.replace('base.hxml', 'utils.hjs')}" /> + <page-meta data-sid="{{pageMeta.sid}}" ${pageMetaAttrs}> + <navigation-bar ${navigationBarAttrs}/> + </page-meta>`
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
pnpm-lock.yaml
is excluded by!**/pnpm-lock.yaml
📒 Files selected for processing (24)
package.json
(1 hunks)packages/shared/src/constants.ts
(2 hunks)packages/taro-api/src/env.ts
(2 hunks)packages/taro-cli/src/cli.ts
(1 hunks)packages/taro-cli/src/presets/platforms/plugin.ts
(1 hunks)packages/taro-components/ascf-mappings/core.json
(1 hunks)packages/taro-components/src/common/ascf-adapter.tsx
(1 hunks)packages/taro-helper/src/constants.ts
(2 hunks)packages/taro-platform-ascf/README.md
(1 hunks)packages/taro-platform-ascf/index.js
(1 hunks)packages/taro-platform-ascf/package.json
(1 hunks)packages/taro-platform-ascf/rollup.config.mjs
(1 hunks)packages/taro-platform-ascf/src/apis-list.ts
(1 hunks)packages/taro-platform-ascf/src/apis.ts
(1 hunks)packages/taro-platform-ascf/src/components-react.ts
(1 hunks)packages/taro-platform-ascf/src/components.ts
(1 hunks)packages/taro-platform-ascf/src/index.ts
(1 hunks)packages/taro-platform-ascf/src/program.ts
(1 hunks)packages/taro-platform-ascf/src/runtime-utils.ts
(1 hunks)packages/taro-platform-ascf/src/runtime.ts
(1 hunks)packages/taro-platform-ascf/src/template.ts
(1 hunks)packages/taro-platform-ascf/tsconfig.json
(1 hunks)packages/taro/types/api/taro.extend.d.ts
(1 hunks)packages/taro/types/global.d.ts
(1 hunks)
🧰 Additional context used
🧠 Learnings (13)
📓 Common learnings
Learnt from: ianzone
PR: NervJS/taro#17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
packages/taro-platform-ascf/README.md (1)
Learnt from: ianzone
PR: NervJS/taro#17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
package.json (1)
Learnt from: ianzone
PR: NervJS/taro#17842
File: packages/jest-helper/tsconfig.json:9-9
Timestamp: 2025-06-23T00:09:31.233Z
Learning: 在 jest-helper 包中,src 目录下没有 __tests__ 测试目录,只包含 resolver.ts、sequencer.ts 和 snapshot 目录。不要假设包中存在测试目录结构。
packages/taro-platform-ascf/src/runtime.ts (1)
Learnt from: ianzone
PR: NervJS/taro#17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
packages/taro-platform-ascf/tsconfig.json (1)
Learnt from: ianzone
PR: NervJS/taro#17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
packages/taro-platform-ascf/package.json (1)
Learnt from: ianzone
PR: NervJS/taro#17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
packages/taro-platform-ascf/src/runtime-utils.ts (1)
Learnt from: ianzone
PR: NervJS/taro#17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
packages/taro-platform-ascf/src/apis.ts (1)
Learnt from: ianzone
PR: NervJS/taro#17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
packages/taro-platform-ascf/src/components.ts (1)
Learnt from: ianzone
PR: NervJS/taro#17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
packages/taro-platform-ascf/src/program.ts (1)
Learnt from: ianzone
PR: NervJS/taro#17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
packages/taro-platform-ascf/src/components-react.ts (1)
Learnt from: ianzone
PR: NervJS/taro#17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
packages/taro-helper/src/constants.ts (1)
Learnt from: ianzone
PR: NervJS/taro#17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
packages/taro-platform-ascf/rollup.config.mjs (1)
Learnt from: ianzone
PR: NervJS/taro#17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
🪛 Biome (1.9.4)
packages/taro-platform-ascf/src/template.ts
[error] 38-38: Avoid the delete operator which can impact performance.
Unsafe fix: Use an undefined assignment instead.
(lint/performance/noDelete)
[error] 39-39: Avoid the delete operator which can impact performance.
Unsafe fix: Use an undefined assignment instead.
(lint/performance/noDelete)
🔇 Additional comments (32)
packages/taro/types/global.d.ts (1)
412-425
: ENV_TYPE 增补 ASCF ✔️类型扩展与平台新增保持一致,声明无误。
packages/taro/types/api/taro.extend.d.ts (1)
51-52
: TARO_ENV_TYPE 映射同步 ✔️已正确加入
ASC F
映射,保持与全局枚举一致,类型安全 OK。packages/taro-cli/src/cli.ts (1)
121-121
: LGTM - 平台集成符合现有模式ASCF 平台的集成遵循了现有的平台插件加载模式,与其他小程序平台保持一致。
packages/taro-platform-ascf/src/runtime.ts (1)
1-6
: 批准:ASCF 平台运行时初始化已验证
runtime-utils
正确导出components
与hostConfig
,并且
mergeReconciler(hostConfig)
与mergeInternalComponents(components)
调用符合预期。无需进一步修改,代码可合并。packages/taro-api/src/env.ts (2)
2-2
: LGTM - 环境类型定义正确ASCF 环境类型的添加遵循了现有的命名约定。
39-40
: LGTM - 环境检测逻辑完整ASCF 平台的环境检测逻辑与其他平台保持一致,实现正确。
packages/shared/src/constants.ts (2)
3-3
: LGTM - 平台类型枚举添加正确ASCF 平台类型的添加遵循了现有的命名约定和值格式。
21-23
: LGTM - 平台配置映射完整ASCF 平台的配置映射正确地引用了对应的平台类型,保持了与其他平台的一致性。
packages/taro-platform-ascf/src/apis-list.ts (1)
1-33
: 实现清晰合理,API列表完整这个常量导出了需要Promise处理的API列表,结构清晰,API命名规范,涵盖了文件操作、用户认证、图像编辑、直播频道、聊天功能等各个方面,符合小程序平台的API特征。
packages/taro-cli/src/presets/platforms/plugin.ts (3)
19-19
: 正确添加了ASCF平台常量的导入从PLATFORMS中解构ASCF常量,与其他平台保持一致的导入方式。
21-21
: 正确添加了ASCF平台的类型映射在typeMap中为ASCF平台添加了对应的显示名称,保持了代码的一致性。
27-28
: 正确更新了平台验证逻辑和错误消息验证逻辑正确地包含了ASCF平台,错误消息也相应更新,与现有平台支持保持一致。
packages/taro-platform-ascf/package.json (1)
1-40
: 包配置符合Taro平台插件标准这个package.json文件遵循了Taro平台插件的标准配置:
- 使用workspace依赖引用,确保版本一致性
- 正确配置了peerDependencies
- 使用Rollup构建工具与其他包保持一致
- Node.js版本要求合理(>=18)
- 包含了必要的元数据和构建脚本
packages/taro-platform-ascf/src/index.ts (3)
5-6
: 正确导出AscfApp类供其他平台继承通过export导出AscfApp类,允许其他平台插件继承此平台的功能,体现了良好的可扩展性设计。
8-10
: 接口定义清晰简洁IOptions接口定义了可选的键盘配件选项,命名清晰,类型定义准确。
12-21
: 平台注册实现标准规范遵循了Taro平台插件的标准模式:
- 使用'ascf'作为平台名称
- 使用'mini'配置名称与小程序平台保持一致
- 正确初始化并启动AscfApp实例
packages/taro-components/src/common/ascf-adapter.tsx (4)
6-8
: 映射对象结构合理从核心映射文件导入配置并合并为ASCF_MAPPINGS对象,结构清晰易于维护。
10-12
: 高阶组件设计合理使用高阶组件模式进行平台适配是正确的设计选择,通过组件名获取映射配置的方式简洁有效。
16-18
: 环境检测逻辑正确通过检测TARO_ENV环境变量来判断是否需要进行ASCF平台适配,非ASCF环境直接返回原组件,避免了不必要的处理开销。
34-39
: 默认值应用逻辑正确正确处理了映射属性的默认值应用,确保所有必要的默认值都能被正确设置。逻辑清晰,处理完整。
packages/taro-components/ascf-mappings/core.json (1)
1-32
: 验证外部文档链接有效性JSON 配置结构清晰,已包含必要的属性转换、默认值和类型信息。但在自动检查时,以下文档链接均未返回有效响应,请手动确认它们是否可访问:
packages/taro-helper/src/constants.ts (2)
106-106
: 正确添加了 ASCF 平台的模板文件扩展名支持在正则表达式中添加
.hxml
文件扩展名是合理的,符合 ASCF 平台的文件类型要求。
180-180
: ASCF 平台插件包正确添加到更新列表中在
@tarojs/plugin-platform-weapp
之后添加@tarojs/plugin-platform-ascf
的位置是合理的,保持了平台插件的组织结构。packages/taro-platform-ascf/src/apis.ts (1)
7-34
: API 初始化实现规范,逻辑正确函数实现符合 Taro 平台 API 初始化的标准模式:
- 正确处理了已知问题(删除
lanDebug
API)- 合理转换了
showShareMenu
的参数映射- 添加的辅助方法使用了适当的空值检查和可选链操作符
- 类型安全得到了良好维护
packages/taro-platform-ascf/src/components.ts (3)
1-6
: 使用常量定义提高代码可维护性通过定义
_true
、_false
、_empty
等常量来统一管理通用值是很好的实践,提高了代码的可维护性和一致性。
277-281
: 注释说明了临时禁用属性的原因对于
picture-in-picture-show-progress
属性的注释很有价值,清楚说明了由于平台限制而临时禁用的原因,并提供了相关文档链接。
7-517
: 组件定义全面且结构清晰组件定义覆盖了从基础到高级的所有 UI 组件,属性配置合理,事件绑定完整。代码组织良好,分类明确。
packages/taro-platform-ascf/src/program.ts (4)
10-23
: 平台类定义符合 Taro 架构规范类继承和属性定义遵循了 Taro 平台的标准模式:
- 正确定义了平台特定的文件类型扩展名
- 运行时和组件路径配置合理
- 全局对象命名符合 ASCF 平台要求
33-42
: 构造函数和事务包装器实现正确构造函数正确使用了事务包装器模式,在适当的生命周期阶段调用模板和 Webpack 配置修改方法。
47-56
: 模板修改逻辑合理模板修改方法正确合并了组件定义,并根据插件选项动态调整了 void 元素集合,体现了良好的可配置性。
61-69
: Webpack 配置修改解决了已知问题通过修改
devtoolModuleFilenameTemplate
来解决微信小程序 sourcemap 映射失败的问题,这是一个实用的修复方案。packages/taro-platform-ascf/src/components-react.ts (1)
1-38
: 组件定义清晰规范 👍React 组件类型标识符定义规范,命名采用统一的 kebab-case 格式,结构清晰。
module.exports = require('./dist/index.js').default | ||
|
||
module.exports.default = module.exports | ||
module.exports.AscfApp = require('./dist/index.js').AscfApp |
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.
💡 Verification agent
❓ Verification inconclusive
模块导出结构符合约定
导出文件遵循了 Taro 平台包的标准模式,确保了 CommonJS 和 ES 模块的兼容性。
建议验证编译后的分发文件是否正确导出了预期的内容:
🏁 Script executed:
#!/bin/bash
# 检查 dist 目录和导出文件是否存在
fd "dist" --type d packages/taro-platform-ascf/ --exec ls -la {}/index.js \;
Length of output: 79
脚本未返回任何结果,可能是目录不存在或命令路径有误。请使用更直接的方式验证 packages/taro-platform-ascf/dist/index.js
是否存在并导出预期内容:
🏁 Script executed:
#!/bin/bash
# 查找并列出 dist 目录下的 index.js
find packages/taro-platform-ascf/dist -maxdepth 1 -type f -name index.js -exec ls -la {} \;
# 如存在,则检查默认和 AscfApp 导出
if [ -f packages/taro-platform-ascf/dist/index.js ]; then
grep -E "export default|export (\\{[^}]*\\}|AscfApp)" -n packages/taro-platform-ascf/dist/index.js || echo "未找到预期的导出"
fi
Length of output: 219
缺少 dist 构建产物
在 packages/taro-platform-ascf
中未能找到 dist/index.js
,请确认已生成并提交该构建产物,或将导出路径调整为实际存在的文件:
– 确认 packages/taro-platform-ascf/dist/index.js
已被生成并纳入版本控制
– 若 dist 目录由 CI/构建流程动态生成,请在发布包时确保包含该目录或改用正确的入口文件
– 如路径不同,请在 packages/taro-platform-ascf/index.js
中更新 require('./dist/index.js')
为有效路径
🤖 Prompt for AI Agents
In packages/taro-platform-ascf/index.js lines 1 to 4, the require statement
points to './dist/index.js' which is missing. Ensure that the dist/index.js file
is generated and included in version control if it is a build artifact. If the
dist directory is created dynamically during CI or build, update the package
publishing process to include it or change the require path in index.js to point
to an existing file that can serve as the module entry point.
|
||
import { initNativeApi } from './apis' | ||
|
||
declare const getCurrentPages: any |
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.
🛠️ Refactor suggestion
避免使用 any
类型,提供明确的类型定义
应该为 getCurrentPages
提供明确的类型定义,以提高代码的类型安全性和可维护性。
-declare const getCurrentPages: any
+interface Page {
+ setData(data: Record<string, any>): void
+}
+declare const getCurrentPages: () => Page[]
🤖 Prompt for AI Agents
In packages/taro-platform-ascf/src/runtime-utils.ts at line 5, the declaration
of getCurrentPages uses the any type, which reduces type safety. Replace the any
type with a more specific type definition that accurately represents the return
type and usage of getCurrentPages to improve type safety and maintainability.
getMiniLifecycle (config) { | ||
const methods = config.page[5] | ||
if (methods.indexOf('onSaveExitState') === -1) { | ||
methods.push('onSaveExitState') | ||
} | ||
return config | ||
}, |
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.
🛠️ Refactor suggestion
避免使用魔法数字,提高代码可读性
config.page[5]
使用了魔法数字 5
,这使得代码难以理解和维护。建议使用具名常量或更清晰的数据结构。
+ const PAGE_LIFECYCLE_METHODS_INDEX = 5
getMiniLifecycle (config) {
- const methods = config.page[5]
+ const methods = config.page[PAGE_LIFECYCLE_METHODS_INDEX]
if (methods.indexOf('onSaveExitState') === -1) {
methods.push('onSaveExitState')
}
return config
},
Committable suggestion skipped: line range outside the PR's diff.
🤖 Prompt for AI Agents
In packages/taro-platform-ascf/src/runtime-utils.ts around lines 12 to 18, the
code uses the magic number 5 to access config.page[5], which reduces readability
and maintainability. Replace the numeric index 5 with a named constant that
clearly describes what this element represents, or refactor config.page to use a
more descriptive data structure such as an object with named properties. This
will make the code easier to understand and maintain.
this.transferComponents['page-meta'] = result['page-meta'] | ||
this.transferComponents['navigation-bar'] = result['navigation-bar'] | ||
delete result['page-meta'] | ||
delete result['navigation-bar'] | ||
|
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.
避免使用 delete 操作符以提高性能
使用 delete
操作符会影响性能,建议使用 undefined
赋值替代。
// PageMeta & NavigationBar
this.transferComponents['page-meta'] = result['page-meta']
this.transferComponents['navigation-bar'] = result['navigation-bar']
- delete result['page-meta']
- delete result['navigation-bar']
+ result['page-meta'] = undefined
+ result['navigation-bar'] = undefined
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
this.transferComponents['page-meta'] = result['page-meta'] | |
this.transferComponents['navigation-bar'] = result['navigation-bar'] | |
delete result['page-meta'] | |
delete result['navigation-bar'] | |
// PageMeta & NavigationBar | |
this.transferComponents['page-meta'] = result['page-meta'] | |
this.transferComponents['navigation-bar'] = result['navigation-bar'] | |
result['page-meta'] = undefined | |
result['navigation-bar'] = undefined |
🧰 Tools
🪛 Biome (1.9.4)
[error] 38-38: Avoid the delete operator which can impact performance.
Unsafe fix: Use an undefined assignment instead.
(lint/performance/noDelete)
[error] 39-39: Avoid the delete operator which can impact performance.
Unsafe fix: Use an undefined assignment instead.
(lint/performance/noDelete)
🤖 Prompt for AI Agents
In packages/taro-platform-ascf/src/template.ts between lines 36 and 40, replace
the use of the delete operator on result['page-meta'] and
result['navigation-bar'] with assigning undefined to these properties to improve
performance. Change the lines deleting these keys to setting result['page-meta']
= undefined and result['navigation-bar'] = undefined instead.
modifyTemplateResult = (res: string, nodeName: string, _, children) => { | ||
if (nodeName === 'keyboard-accessory') return '' | ||
|
||
if ((nodeName === 'textarea' || nodeName === 'input') && this.pluginOptions.enablekeyboardAccessory) { | ||
const list = res.split('</template>') | ||
const componentAlias = this.componentsAlias[nodeName] | ||
const nodeNameAlias = componentAlias._num | ||
const xs = `xs.a(c, item.${Shortcuts.NodeName}, l)` | ||
|
||
const target = ` | ||
<keyboard-accessory style="{{i.cn[0].st}}" class="{{i.cn[0].cl}}" bindtap="eh" id="{{i.cn[0].uid||i.cn[0].sid}}" data-sid="{{i.cn[0].sid}}"> | ||
<block has:for="{{i.cn[0].cn}}" has:key="sid"> | ||
<template is="{{${xs}}}" data="{{i:item,c:c+1,l:xs.f(l,item.${Shortcuts.NodeName})}}" /> | ||
</block> | ||
</keyboard-accessory> | ||
` | ||
|
||
const templateFocus = list[1] | ||
.replace(children, target) | ||
.replace(`_${nodeNameAlias}_focus`, `_${nodeNameAlias}_focus_ka`) | ||
|
||
const templateBlur = list[2] | ||
.replace(children, target) | ||
.replace(`_${nodeNameAlias}_blur`, `_${nodeNameAlias}_blur_ka`) | ||
|
||
list.splice(3, 0, templateFocus, templateBlur) | ||
return list.join('</template>') | ||
} | ||
|
||
return res | ||
} |
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.
🛠️ Refactor suggestion
改善模板修改逻辑的健壮性
当前实现通过字符串分割和拼接修改模板,这种方式容易出错。建议添加错误处理和验证逻辑。
在修改模板之前,建议验证分割结果的有效性:
modifyTemplateResult = (res: string, nodeName: string, _, children) => {
if (nodeName === 'keyboard-accessory') return ''
if ((nodeName === 'textarea' || nodeName === 'input') && this.pluginOptions.enablekeyboardAccessory) {
const list = res.split('</template>')
+ // 验证分割结果
+ if (list.length < 3) {
+ console.warn(`Template split resulted in unexpected parts: ${list.length}`)
+ return res
+ }
🤖 Prompt for AI Agents
In packages/taro-platform-ascf/src/template.ts between lines 75 and 105, the
current method modifies templates by splitting strings without validating the
split result, which can cause errors. To fix this, add checks to ensure the
split array has the expected length before accessing its elements, and include
error handling to manage unexpected cases gracefully. This will improve the
robustness of the template modification logic.
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.
Actionable comments posted: 3
🧹 Nitpick comments (1)
packages/taro-components/types/Button.d.ts (1)
444-448
: 命名空间嵌套结构不符合 TypeScript 最佳实践在 ButtonProps 命名空间内部定义 TaroGeneral 命名空间可能导致类型访问混乱。建议将 TaroGeneral 移到文件顶层或使用更清晰的命名结构。
- declare namespace TaroGeneral { - interface ENV_TYPE { - ascf: boolean - } - } +} + +declare namespace TaroGeneral { + interface ENV_TYPE { + ascf: boolean + }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
packages/taro-components/src/components/button/button.tsx
(2 hunks)packages/taro-components/types/Button.d.ts
(1 hunks)
✅ Files skipped from review due to trivial changes (1)
- packages/taro-components/src/components/button/button.tsx
🧰 Additional context used
🧠 Learnings (1)
📓 Common learnings
Learnt from: ianzone
PR: NervJS/taro#17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
|
||
type TaroButtonProps = | ||
TaroGeneral.ENV_TYPE['ascf'] extends true | ||
? ASCFAdaptedProps<ButtonProps, typeof coreMappings.Button> |
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.
缺少 coreMappings 导入
代码引用了 coreMappings.Button
但未导入该模块。根据 AI 摘要,此映射定义在 packages/taro-components/ascf-mappings/core.json
中。
在文件顶部添加导入语句:
import coreMappings from '../ascf-mappings/core.json'
🤖 Prompt for AI Agents
In packages/taro-components/types/Button.d.ts at line 467, the code references
coreMappings.Button but coreMappings is not imported. Fix this by adding the
import statement "import coreMappings from '../ascf-mappings/core.json'" at the
top of the file to properly import the coreMappings module.
interface ButtonProps { | ||
size?: 'mini' | 'small' | 'default' | 'large' | ||
loading?: boolean | ||
} |
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.
接口重复声明导致编译错误
ButtonProps
接口已在第4行声明,此处重复声明会导致 TypeScript 编译错误。应使用不同的名称,如 SimplifiedButtonProps
或 ASCFButtonProps
。
- interface ButtonProps {
+ interface SimplifiedButtonProps {
size?: 'mini' | 'small' | 'default' | 'large'
loading?: boolean
}
同时更新第467行的引用:
- ? ASCFAdaptedProps<ButtonProps, typeof coreMappings.Button>
- : ButtonProps;
+ ? ASCFAdaptedProps<SimplifiedButtonProps, typeof coreMappings.Button>
+ : SimplifiedButtonProps;
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
interface ButtonProps { | |
size?: 'mini' | 'small' | 'default' | 'large' | |
loading?: boolean | |
} | |
// Rename to avoid duplicate declaration | |
interface SimplifiedButtonProps { | |
size?: 'mini' | 'small' | 'default' | 'large' | |
loading?: boolean | |
} | |
// …later in the same file, update the conditional type reference… | |
? ASCFAdaptedProps<SimplifiedButtonProps, typeof coreMappings.Button> | |
: SimplifiedButtonProps; |
🤖 Prompt for AI Agents
In packages/taro-components/types/Button.d.ts around lines 460 to 463, the
ButtonProps interface is being redeclared, causing a TypeScript compilation
error. Rename this interface to a unique name such as SimplifiedButtonProps or
ASCFButtonProps to avoid duplication, and update all references to this
interface accordingly, including the one at line 467.
type TaroButtonProps = | ||
TaroGeneral.ENV_TYPE['ascf'] extends true | ||
? ASCFAdaptedProps<ButtonProps, typeof coreMappings.Button> | ||
: ButtonProps; | ||
|
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.
💡 Verification agent
🧩 Analysis chain
条件类型实现可能存在问题
当前条件类型检查 TaroGeneral.ENV_TYPE['ascf'] extends true
可能无法正常工作。TypeScript 中的条件类型通常不应直接检查布尔值属性。
建议重新考虑类型设计,可能需要使用不同的方法来实现平台特定的类型切换。请验证当前实现是否能正确工作:
🏁 Script executed:
#!/bin/bash
# 检查其他文件中是否有类似的条件类型实现模式
ast-grep --pattern 'type $_ = $_["$_"] extends true ? $_ : $_'
# 查找 ENV_TYPE 的使用方式
rg -A 3 "ENV_TYPE\['ascf'\]"
Length of output: 408
🏁 Script executed:
#!/bin/bash
# 查找 TaroGeneral 接口定义及 ENV_TYPE 的声明
rg -n "interface TaroGeneral" -C 5 .
rg -n "type TaroGeneral" -C 5 .
rg -n "ENV_TYPE" -C 5 .
Length of output: 23498
修复 Button.d.ts 中 TaroGeneral.ENV_TYPE['ascf']
条件类型判断
当前实现
// packages/taro-components/types/Button.d.ts:445-447
declare namespace TaroGeneral {
interface ENV_TYPE {
ascf: boolean
}
}
// packages/taro-components/types/Button.d.ts:465-469
type TaroButtonProps =
TaroGeneral.ENV_TYPE['ascf'] extends true
? ASCFAdaptedProps<ButtonProps, typeof coreMappings.Button>
: ButtonProps;
问题
TaroGeneral.ENV_TYPE['ascf'] 的类型是 boolean
,在 TypeScript 中 boolean extends true
永远为 false
,导致条件类型始终走到 : ButtonProps
分支。
请在以下位置进行修正:
- packages/taro-components/types/Button.d.ts
- 修改
ENV_TYPE['ascf']
的声明为字面量类型(true
/false
) - 或者将条件类型改为接收类型参数,再在不同环境下传入具体的布尔值
- 或者重新设计为映射类型,根据环境字符串(如
ENV_TYPE.ASCF
)来分发不同 Props
- 修改
示例(仅供参考):
- type TaroButtonProps =
- TaroGeneral.ENV_TYPE['ascf'] extends true
- ? ASCFAdaptedProps<ButtonProps, typeof coreMappings.Button>
- : ButtonProps;
+ // 方式一:字面量化 ENV_TYPE 定义
+ declare namespace TaroGeneral {
+ interface ENV_TYPE {
+ ascf: true
+ // ...其他平台字段同理
+ }
+ }
+ type TaroButtonProps =
+ TaroGeneral.ENV_TYPE['ascf'] extends true
+ ? ASCFAdaptedProps<ButtonProps, typeof coreMappings.Button>
+ : ButtonProps;
+
+ // 或 方式二:使用类型参数
+ type TaroButtonProps<IsASCF extends boolean> =
+ IsASCF extends true
+ ? ASCFAdaptedProps<ButtonProps, typeof coreMappings.Button>
+ : ButtonProps;
+ // 使用处:TaroButtonProps<typeof TaroGeneral.ENV_TYPE['ascf']>
请根据项目实际需求选择合适方案并修复,确保 ASCF 环境下能正确生成 ASCFAdaptedProps
。
🤖 Prompt for AI Agents
In packages/taro-components/types/Button.d.ts around lines 465 to 469, the
conditional type uses TaroGeneral.ENV_TYPE['ascf'] which is declared as boolean,
causing the condition to always evaluate to false. To fix this, change the
declaration of ENV_TYPE['ascf'] from boolean to a literal type true or false, or
refactor the conditional type to accept a type parameter representing the
environment boolean value, ensuring the condition can correctly discriminate and
select ASCFAdaptedProps when appropriate.
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.
Actionable comments posted: 4
🧹 Nitpick comments (1)
packages/taro-api/src/ascf-bridge.ts (1)
7-15
: 改进 transformers 对象的类型定义建议为 transformers 对象添加更具体的类型定义以提高类型安全性。
+type TransformerFunction = (value: any) => any + -const transformers = { +const transformers: Record<string, TransformerFunction> = { normalizeHeaders: (headers: Record<string, any>) => { const normalized: Record<string, any> = {} Object.keys(headers).forEach(key => { normalized[key.toLowerCase()] = headers[key] }) return normalized } }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
packages/taro-api/src/ascf-bridge.ts
(1 hunks)packages/taro-api/src/ascf-mappings/network.json
(1 hunks)
✅ Files skipped from review due to trivial changes (1)
- packages/taro-api/src/ascf-mappings/network.json
🧰 Additional context used
🧠 Learnings (1)
📓 Common learnings
Learnt from: ianzone
PR: NervJS/taro#17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
@@ -0,0 +1,64 @@ | |||
import * as ASCF from '@ascf/native-api' | |||
|
|||
import Taro from '../../../../../taroo/taro/packages/taro/types' |
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.
修复导入路径
这个导入路径看起来不正确,包含了 taroo
而不是 taro
,并且相对路径层级过深。请确认正确的导入路径。
建议修复为:
-import Taro from '../../../../../taroo/taro/packages/taro/types'
+import Taro from '@tarojs/taro'
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
import Taro from '../../../../../taroo/taro/packages/taro/types' | |
import Taro from '@tarojs/taro' |
🤖 Prompt for AI Agents
In packages/taro-api/src/ascf-bridge.ts at line 3, the import path for Taro is
incorrect because it uses 'taroo' instead of 'taro' and has an overly deep
relative path. Correct the import path to use the proper 'taro' directory and
simplify the relative path to accurately point to the Taro types module.
export function ascRequest(options: Taro.request.Option): Promise<Taro.request.SuccessCallbackResult> { | ||
const mapping = networkMappings.request |
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.
💡 Verification agent
🧩 Analysis chain
验证映射配置结构
建议验证 networkMappings.request 的结构,确保映射配置包含必要的字段。
运行以下脚本来验证映射配置文件的存在和结构:
🏁 Script executed:
#!/bin/bash
# 描述:验证 ASCF 网络映射配置文件的存在和结构
# 预期:文件存在且包含 request 字段
# 检查映射文件是否存在
if [ -f "packages/taro-api/src/ascf-mappings/network.json" ]; then
echo "✓ 映射文件存在"
# 检查文件内容结构
cat packages/taro-api/src/ascf-mappings/network.json | jq '.request'
else
echo "✗ 映射文件不存在"
fi
Length of output: 712
验证网络映射配置结构并添加运行时校验
经过验证,packages/taro-api/src/ascf-mappings/network.json
文件存在且其 .request
段包含以下字段:
url
(ascfParam、type、required、docLink)method
(ascfParam、valueMap、default、docLink)header
(ascfParam、type、transform、docLink)
为提升健壮性,建议在 packages/taro-api/src/ascf-bridge.ts
的 ascRequest
函数(第 17–18 行)中引入对映射结构的运行时校验,例如:
- 检查
mapping.url
、mapping.method
、mapping.header
等必填节点是否存在且类型符合预期 - 对可选字段(如
default
、transform
)提供安全默认值或在缺失时抛出明确异常 - 考虑使用
zod
、ajv
等 schema 验证库统一管理校验逻辑
这样可确保未来对映射文件的修改不会导致调用异常。
🤖 Prompt for AI Agents
In packages/taro-api/src/ascf-bridge.ts around lines 17 to 18, add runtime
validation for the networkMappings.request structure by checking that required
fields like url, method, and header exist and have the correct types. Implement
default values or throw clear errors for optional fields such as default and
transform if missing. Use a schema validation library like zod or ajv to define
and enforce this validation to prevent runtime errors from future changes to the
mapping file.
return ASCF.Network.request(ascfParams).then(res => { | ||
return { | ||
data: res.body, | ||
statusCode: res.status, | ||
header: res.headers, | ||
errMsg: 'request:ok' | ||
} | ||
}) | ||
} |
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.
🛠️ Refactor suggestion
添加错误处理和响应验证
当前实现缺少对 ASCF.Network.request 调用的错误处理,并且假设响应结构固定。建议添加错误处理和响应验证。
- return ASCF.Network.request(ascfParams).then(res => {
- return {
- data: res.body,
- statusCode: res.status,
- header: res.headers,
- errMsg: 'request:ok'
- }
- })
+ return ASCF.Network.request(ascfParams).then(res => {
+ if (!res) {
+ throw new Error('[ASCF] Invalid response from ASCF.Network.request')
+ }
+ return {
+ data: res.body,
+ statusCode: res.status || 200,
+ header: res.headers || {},
+ errMsg: 'request:ok'
+ }
+ }).catch(error => {
+ throw new Error(`[ASCF] Network request failed: ${error.message || error}`)
+ })
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
return ASCF.Network.request(ascfParams).then(res => { | |
return { | |
data: res.body, | |
statusCode: res.status, | |
header: res.headers, | |
errMsg: 'request:ok' | |
} | |
}) | |
} | |
return ASCF.Network.request(ascfParams) | |
.then(res => { | |
if (!res) { | |
throw new Error('[ASCF] Invalid response from ASCF.Network.request') | |
} | |
return { | |
data: res.body, | |
statusCode: res.status || 200, | |
header: res.headers || {}, | |
errMsg: 'request:ok' | |
} | |
}) | |
.catch(error => { | |
throw new Error(`[ASCF] Network request failed: ${error.message || error}`) | |
}) | |
} |
🤖 Prompt for AI Agents
In packages/taro-api/src/ascf-bridge.ts around lines 46 to 54, the current code
lacks error handling and assumes a fixed response structure from
ASCF.Network.request. Modify the code to add a catch block to handle any errors
from the request call, returning a proper error object or message. Also,
validate the response structure before accessing properties like body, status,
and headers to avoid runtime errors.
export const ascfApis = { | ||
connectDevice: (options: { deviceId: string, timeout?: number }) => { | ||
return ASCF.Device.connect({ | ||
deviceId: options.deviceId, | ||
timeout: options.timeout || 5000 | ||
}) | ||
} | ||
} |
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.
🛠️ Refactor suggestion
添加参数验证和错误处理
建议为 connectDevice 函数添加参数验证和错误处理,以提高代码健壮性。
export const ascfApis = {
- connectDevice: (options: { deviceId: string, timeout?: number }) => {
+ connectDevice: (options: { deviceId: string, timeout?: number }) => {
+ if (!options || !options.deviceId) {
+ throw new Error('[ASCF] deviceId is required for connectDevice')
+ }
+ if (options.timeout && (options.timeout < 0 || options.timeout > 60000)) {
+ throw new Error('[ASCF] timeout must be between 0 and 60000ms')
+ }
return ASCF.Device.connect({
deviceId: options.deviceId,
timeout: options.timeout || 5000
- })
+ }).catch(error => {
+ throw new Error(`[ASCF] Device connection failed: ${error.message || error}`)
+ })
}
}
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
export const ascfApis = { | |
connectDevice: (options: { deviceId: string, timeout?: number }) => { | |
return ASCF.Device.connect({ | |
deviceId: options.deviceId, | |
timeout: options.timeout || 5000 | |
}) | |
} | |
} | |
export const ascfApis = { | |
connectDevice: (options: { deviceId: string, timeout?: number }) => { | |
if (!options || !options.deviceId) { | |
throw new Error('[ASCF] deviceId is required for connectDevice') | |
} | |
if (options.timeout && (options.timeout < 0 || options.timeout > 60000)) { | |
throw new Error('[ASCF] timeout must be between 0 and 60000ms') | |
} | |
return ASCF.Device.connect({ | |
deviceId: options.deviceId, | |
timeout: options.timeout || 5000 | |
}).catch(error => { | |
throw new Error(`[ASCF] Device connection failed: ${error.message || error}`) | |
}) | |
} | |
} |
🤖 Prompt for AI Agents
In packages/taro-api/src/ascf-bridge.ts around lines 57 to 64, the connectDevice
function lacks parameter validation and error handling. Add checks to ensure the
deviceId parameter is provided and valid before calling ASCF.Device.connect.
Wrap the connection call in a try-catch block to handle potential errors
gracefully, returning or throwing meaningful error messages to improve
robustness.
Taro组件属性和ascf文档对应
这个 PR 是什么类型? (至少选择一个)
这个 PR 涉及以下平台:
Summary by CodeRabbit
新功能
@tarojs/plugin-platform-ascf
,支持相关构建与开发流程。文档
类型声明
其他