Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
9 changes: 9 additions & 0 deletions packages/taro-cli/src/create/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export interface IProjectConf {
hideDefaultTemplate?: boolean
framework: FrameworkType
compiler?: CompilerType
ask?: (config: {}) => Promise<void> | void
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

改进类型定义以提供更好的类型安全性

当前使用 {} 作为类型定义存在问题,{} 实际上表示"任何非空值"而不是空对象。根据之前的讨论,这个 ask 函数需要接收配置参数,建议使用更具体的类型定义。

-  ask?: (config: {}) => Promise<void> | void
+  ask?: (config: Omit<IProjectConf, 'ask'> & { templatePath: string }) => Promise<void> | void

这样可以:

  1. 明确指定函数参数的结构(配置对象排除 ask 属性本身,并包含 templatePath)
  2. 提供更好的类型提示和编译时检查
  3. 避免使用不安全的 {} 类型
📝 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.

Suggested change
ask?: (config: {}) => Promise<void> | void
ask?: (config: Omit<IProjectConf, 'ask'> & { templatePath: string }) => Promise<void> | void
🧰 Tools
🪛 Biome (1.9.4)

[error] 46-46: Don't use '{}' as a type.

Prefer explicitly define the object shape. '{}' means "any non-nullable value".

(lint/complexity/noBannedTypes)

🪛 ESLint

[error] 46-46: Don't use {} as a type. {} actually means "any non-nullish value".

  • If you want a type meaning "any object", you probably want object instead.
  • If you want a type meaning "any value", you probably want unknown instead.
  • If you want a type meaning "empty object", you probably want Record<string, never> instead.
  • If you really want a type meaning "any non-nullish value", you probably want NonNullable<unknown> instead.

(@typescript-eslint/ban-types)

🤖 Prompt for AI Agents
In packages/taro-cli/src/create/project.ts at line 46, the type definition for
the ask function parameter is currently {}, which is too broad and unsafe.
Replace {} with a more specific type that excludes the ask property itself and
includes the templatePath property to clearly define the expected structure of
the config object. This will improve type safety, provide better type hints, and
enable stricter compile-time checks.

}

type CustomPartial<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
Expand Down Expand Up @@ -126,6 +127,14 @@ export default class Project extends Creator {
await this.askTemplate(conf, prompts, templates)
const templateChoiceAnswer = await inquirer.prompt<IProjectConf>(prompts)

// 导航步骤扩展
try {
if (typeof conf.ask === 'function') {
const { ask, ...other } = conf
await conf.ask({ ...other, templatePath: this.templatePath(templateChoiceAnswer.template) })
}
} catch (e) { }

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

改进错误处理并修复未使用变量的问题

当前实现存在以下问题:

  1. 静默忽略所有错误,不利于调试和问题排查
  2. 解构出的 ask 变量未被使用,触发 ESLint 警告
  3. 应该提供适当的错误信息以帮助开发者排查问题
// 导航步骤扩展
try {
  if (typeof conf.ask === 'function') {
-    const { ask, ...other } = conf
-    await conf.ask({ ...other, templatePath: this.templatePath(templateChoiceAnswer.template) })
+    const { ask: _, ...other } = conf
+    await conf.ask({ ...other, templatePath: this.templatePath(templateChoiceAnswer.template) })
  }
-} catch (e) { }
+} catch (e) {
+  console.log(chalk.yellow('执行导航步骤扩展时发生错误: '), e)
+}

改进说明:

  • 使用 ask: _ 来避免未使用变量警告
  • 添加错误日志输出,方便调试和问题定位
  • 保持了原有的容错机制,不会因扩展失败而中断整个流程
📝 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.

Suggested change
// 导航步骤扩展
try {
if (typeof conf.ask === 'function') {
const { ask, ...other } = conf
await conf.ask({ ...other, templatePath: this.templatePath(templateChoiceAnswer.template) })
}
} catch (e) { }
// 导航步骤扩展
try {
if (typeof conf.ask === 'function') {
const { ask: _, ...other } = conf
await conf.ask({ ...other, templatePath: this.templatePath(templateChoiceAnswer.template) })
}
} catch (e) {
console.log(chalk.yellow('执行导航步骤扩展时发生错误: '), e)
}
🧰 Tools
🪛 ESLint

[error] 133-133: 'ask' is assigned a value but never used.

(@typescript-eslint/no-unused-vars)

🤖 Prompt for AI Agents
In packages/taro-cli/src/create/project.ts around lines 130 to 137, the current
code silently catches all errors without logging, and the destructured variable
'ask' is unused, causing ESLint warnings. To fix this, rename 'ask' to 'ask: _'
to avoid the unused variable warning, and add a console.error or logger.error
statement inside the catch block to output the caught error for debugging, while
preserving the try-catch to prevent the process from stopping on errors.

return {
...answers,
...compilerAndTemplateSourceAnswer,
Expand Down
4 changes: 3 additions & 1 deletion packages/taro-cli/src/presets/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default (ctx: IPluginContext) => {
hideDefaultTemplate,
sourceRoot,
autoInstall,
ask
} = opts.options

const Project = require('../../create/project').default
Expand All @@ -52,7 +53,8 @@ export default (ctx: IPluginContext) => {
compiler,
hideDefaultTemplate,
autoInstall,
css
css,
ask
})

project.create()
Expand Down