代码检查插件
Biome Linter 支持 GritQL 插件。目前,这些插件允许你匹配特定的代码模式并为其注册自定义诊断消息。
¥Biome Linter supports GritQL plugins. Currently, these plugins allow you to match specific code patterns and register customized diagnostic messages for them.
以下是一个插件示例,用于报告所有 Object.assign() 的使用情况:
¥Here is an example of a plugin that reports on all usages of Object.assign():
`$fn($args)` where { $fn <: `Object.assign`, register_diagnostic( span = $fn, message = "Prefer object spread instead of `Object.assign()`" )}你可以将 GritQL 代码片段放在项目中的任何文件中,但请注意使用 .grit 扩展名。然后,你只需使用以下配置将其启用为插件:
¥You can put a GritQL snippet in a file anywhere in your project, but be mindful
you use the .grit extension. Then, you can simply enable it as a plugin with
the following configuration:
{ "plugins": ["./path-to-plugin.grit"]}现在,该插件将在所有支持 linter 运行的文件上启用。运行 biome lint 或 biome check 后,你可以查看其结果。例如:
¥The plugin will now be enabled on all supported files the linter runs on. You
can see its results when running biome lint or biome check. For example:
$ biome lint/packages/tailwindcss-config-analyzer/src/introspect.ts:12:17 plugin ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Prefer object spread instead of `Object.assign()`
10 │ function createContextFromConfig(config: Partial<Config>) { 11 │ return createContext( > 12 │ resolveConfig(Object.assign({}, DEFAULT_CONFIG, config)), │ ^^^^^^^^^^^^^ 13 │ ); 14 │ }¥Target Languages
GritQL 代码片段始终尝试匹配给定的目标语言。如果未指定目标语言,则假定为 JavaScript 或其上级语言之一。
¥A GritQL snippet always attempts to match against a given target language. If no target language is specified, JavaScript or one of its super languages is assumed.
如果你想使用不同的目标语言,则必须显式指定。例如,以下是一个 CSS 插件,用于报告任何设置颜色的 .color-* 类之外的颜色选择器:
¥If you want to use a different target language, you must specify it explicitly.
For example, here is a CSS plugin to report any selector that sets a color
outside the allowed .color-* classes:
language css;
`$selector { $props }` where { $props <: contains `color: $color` as $rule, not $selector <: r"\.color-.*", register_diagnostic( span = $rule, message = "Don't set explicit colors. Use `.color-*` classes instead." )}我们目前仅支持 JavaScript 和 CSS 这两种目标语言。
¥We currently do not support other target languages than JavaScript and CSS.
插件 API
Section titled “插件 API”¥Plugin API
除了 Grit 的 内置函数 之外,Biome 目前还支持一个额外的功能:
¥In addition to Grit’s built-in functions, Biome currently supports one extra function:
register_diagnostic()
Section titled “register_diagnostic()”注册一个诊断信息,以便在模式匹配时报告。
¥Registers a diagnostic to be reported whenever the pattern matches.
支持三个参数:
¥Supports three arguments:
-
span(必需):用于附加诊断信息的语法节点。这通常是你在代码片段中匹配到的变量。¥
span(required): The syntax node to attach the diagnostic to. This is typically a variable that you matched within a code snippet. -
message(必需):诊断信息中显示的消息。¥
message(required): The message to show with the diagnostic. -
severity:诊断的严重程度。允许的值:hint、info、warn和error。默认情况下,使用error。¥
severity: The severity of the diagnostic. Allowed values are:hint,info,warn, anderror. By default,erroris used.
Biome v2.1 中文网 - 粤ICP备13048890号