有办法对 handlebars 进行 lint 吗?

我一直在寻找一种在 Discourse 中 lint Handlebars 文件的方法。我确实找到了一个看似能运行但没有任何更改或列出问题的命令。

yarn ember-template-lint --fix path/to/files

我不确定它目前是否应该能正常工作。

yarn ember-template-lint <dir> 应该是正确的命令。

谢谢。但似乎没有返回任何信息。我尝试在一个损坏的文件上运行它,但它既没有显示错误,也没有修复它们。

@cvx 你刚刚在我们的插件工作流中实现了这个功能——我们需要做什么特殊操作才能使用它吗?

不需要魔法 :slightly_smiling_face:,但请确保在 Discourse 的根目录下运行该命令。我之前因此踩过几次坑。

对比一下:

# 在 [discourse]/app/assets/javascripts 目录下
$ yarn ember-template-lint .
✨ 完成,耗时 2.57 秒。
# 在 [discourse] 目录下
$ yarn ember-template-lint app/assets/javascripts
app/assets/javascripts/discourse/app/templates/tags.hbs
  4:0  error  意外使用了 {{debugger}}。  no-debugger
  4:0  error  使用三重大括号是不安全的  no-triple-curlies
  3:15  error  `d-section`(起始于 L1:C0)的缩进不正确。期望 `{{/d-section}}`(结束于 L3:C15)的缩进为 0,但实际为 1。  block-indentation
  2:4  error  `{{outlet}}`(起始于 L2:C4)的缩进不正确。期望 `{{outlet}}` 的缩进为 2,但实际为 4。  block-indentation
  1:23  error  模板中必须使用双引号  quotes

✖ 5 个问题(5 个错误,0 个警告)
error 命令执行失败,退出代码为 1。

另外请注意,该命令会使用我们的配置文件(.template-lintrc.js),因此可能不会报告所有预期的问题,因为某些规则当前已被禁用。


@justin 我刚刚意识到,我们的插件 GitHub Actions 工作流目前并未共享此配置。我们迟早应该对此进行调整。:smiley:

这能在 plugins/ 目录下运行吗?我猜 @fzngagan 感兴趣的就是在这里使用它。

如果插件位于 plugins 目录下,当然可以!:smiley:

$ yarn ember-template-lint plugins
plugins/poll/assets/javascripts/discourse/templates/modal/poll-breakdown.hbs
  1:22  error  you must use double quotes in templates  quotes

我相当确定我做了同样的事情,但我会再试一次并告知你。

Ember 模板 lint 无法修复所有问题,就像 eslint 一样;事实上,与 eslint 相比,Ember 模板 lint 能自动修复的问题更少。

它主要会报告错误,但你需要手动修复它们。

这是我尝试过的内容:

我尝试通过违反 discourse 自身的规则。有效

我尝试通过违反位于 discourse 目录中(作为符号链接)的 discourse-custom-wizard 插件的规则。无效

我对作为 discourse 仓库一部分的 poll 插件进行了同样的尝试。我也尝试了其他随 discourse 一起发布的插件。有效

我再次尝试通过违反 discourse-custom-wizard 插件的规则,这次将其复制到 discourse 的插件文件夹中。无效

我尝试通过违反 discourse-assign 插件的规则,将文件夹放置在 plugins 目录中。无效

对我来说:

有效

yarn ember-template-lint plugins/discourse-pfaffmanager/assets/javascripts/discourse/templates/user/servers/show.hbs 

无效

yarn ember-template-lint plugins/discourse-pfaffmanager/assets/javascripts/discourse/templates/user/servers/

我的 assets 目录应该放在 app 里面吗?

啊,没错。问题在于 ember-template-lint 会自动考虑 .gitignore 文件,而非捆绑的插件会被忽略。

因此,要对所有插件运行模板检查:

yarn ember-template-lint --no-ignore-pattern plugins

# 或者任何其他目录,例如单个插件:
yarn ember-template-lint --no-ignore-pattern plugins/discourse-calendar

谢谢,效果非常好。

谢谢!另外,有没有办法让它自动修复问题?我试过添加 --fix 但没用。

它可以工作,但:

  1. 只有少数规则支持自动修复(参见:GitHub - ember-template-lint/ember-template-lint: Linter for Ember or Handlebars templates · GitHub
  2. 在这些规则中,只有两条包含在“推荐”规则集中(link-rel-noopenerrequire-button-type