Scalafmt 包名称检查器

Scalafmt package name checker

我在 scalariform 中发现了一个很好的规则来检查包名称,如下所示:

<check enabled="true" class="org.scalastyle.scalariform.PackageNamesChecker" level="warning">
 <parameters>
  <parameter name="regex">^[a-z][A-Za-z]*$</parameter>
 </parameters>
</check>

但是,我在文档中找不到对应的scalafmt规则。有这样的吗?

据我所知,Scalafmt 只是一个代码格式化程序,这意味着它不会标记文体错误,而是根据您设置的预定义规则重新格式化您的代码(例如,添加一个新的括号后的行,对齐 case 语句等)。它的目标只是格式化代码,以便它在开发人员之间看起来一致。

另一方面,Scalastyle(使用 Scalariform)定义为:

Scalastyle examines your Scala code and indicates potential problems with it.

所以它定义了"check" rules, like the one you show in your question, to warn you about the fact that your code does not respect these rules. But it won't rewrite/reformat your code accordingly to these rules. It's more like a linting tool than a code formatter. Moreover, its scope is wider, as it provides more rules than just formatting : for example, it can check if you are using null with NullChecker.

请注意,只要您没有定义相互矛盾的规则,您就可以同时使用两者(这通常是一种很好的做法)!