Перейти к содержимому

GritQL syntax

Это содержимое пока не доступно на вашем языке.

GritQL is an open-source query language by Grit.io that searches code by its structure, so it ignores details such as whitespace or the type of quotes in strings.

Biome uses GritQL in plugins and in the biome search command. To write your first plugin, see Create a GritQL plugin.

GritQL in Biome supports the following target languages:

  • JavaScript/TypeScript: language js (with optional flavors: typescript, jsx)
  • CSS: language css
  • JSON: language json

In plugins, patterns can also include rewrites (=>) to provide fixable diagnostics. See Suggest a fix for safe and unsafe fixes and fix_kind.

GritQL queries work through patterns. The most common pattern is the code snippet, which looks like ordinary source code wrapped in backticks:

`console.log('Hello, world!')`

This pattern matches any call to console.log() that receives the string 'Hello, world!'. Because GritQL matches structure, formatting details do not matter. This also matches:

console.log (
'Hello, world!'
)

So does this, with different quotes:

console.log("Hello, world!")

GritQL queries can also have variables. The following pattern matches any call to console.log(), regardless of the message:

`console.log($message)`

This pattern also matches any of the methods on the console object:

`console.$method($message)`

The same variable name can occur multiple times in a single snippet:

`$fn && $fn()`

This pattern matches foo && foo() and foo.bar && foo.bar(), but not foo && bar().

Add conditions to patterns with the where operator. It is commonly used together with the match operator, <::

`console.$method($message)` where {
$method <: `log`
}

This query is identical to the console.log($message) pattern above. Other operators make conditions more expressive:

`console.$method($message)` where {
$method <: or { `log`, `info`, `warn`, `error` }
}

For more precise queries, match against Biome’s internal syntax nodes directly. Each node is identified by a unique PascalCase name.

For example, to find all JavaScript if statements, match the JsIfStatement node:

engine biome(1.0)
language js(typescript,jsx)
JsIfStatement() as $stmt where {
register_diagnostic(
span=$stmt,
message="Found an if statement"
)
}

You can also match specific parts of a node by name. For example, this pattern captures the first branch of a ternary expression using Biome’s node field names:

JsConditionalExpression(consequent = $cons) where {
register_diagnostic(
span=$cons,
message="Found the consequent branch of a conditional expression"
)
}

The engine biome(1.0) directive tells GritQL to use Biome’s syntax tree, and the language directive chooses which grammar to match against. Without it, JavaScript is assumed.

To find the name of a node, explore the syntax tree of your code in the Biome Playground:

  1. Paste the code you want to match.
  2. Switch to the Syntax tab in the output panel.
  3. Expand the nodes to see their names, children, and fields.
  4. Use the name in your pattern: NodeName() matches any instance, and NodeName(field = ...) matches specific children.

The .ungram files in the xtask/codegen directory of the Biome repository list every node and its fields.

These node names are frequently useful for JavaScript and TypeScript:

Node name Matches
JsIfStatement if (...) { ... }
JsConditionalExpression a ? b : c
JsForStatement for (...; ...; ...) { ... }
JsForOfStatement for (... of ...) { ... }
JsCallExpression fn(), obj.method()
JsNewExpression new Foo()
JsArrowFunctionExpression () => { ... }
JsFunctionDeclaration function foo() { ... }
JsCatchClause catch (e) { ... }
JsBlockStatement { ... } (block of statements)
JsFormalParameter A single function parameter
JsParameters The parameter list (a, b, c)
JsVariableDeclaration const x = 1, let y = 2
TsAnyType : any type annotation
TsTypeAlias type Foo = ...
TsInterfaceDeclaration interface Foo { ... }
JsxElement <div>...</div>
JsxSelfClosingElement <img />
JsxAttribute className="test", disabled

For CSS:

Node name Matches
CssDeclarationWithSemicolon property: value;
CssComplexSelector div > .class

Use the range() pattern to filter syntax nodes by their position in the source file. For example, this query matches calls contained entirely on line 2:

range(start_line=2, end_line=2) as $call where {
$call <: call_expression()
}

Given this source, it matches only second():

first();
second();
third();

range() accepts four optional named arguments:

Argument Constraint
start_line The node must start on or after this line.
start_column If the node starts on start_line, it must start at or after this column. Requires start_line.
end_line The node must end on or before this line.
end_column If the node ends on end_line, its end position must be at or before this column. Requires end_line.

Arguments must be unsigned integer literals from 0 to 4294967295. Source lines and columns start at 1. Columns count Unicode scalar values, so é counts as one column, and a tab also counts as one column.

Omitting start_line leaves the start unbounded; omitting end_line leaves the end unbounded. An omitted column imposes no column restriction on its boundary line. For example, range(start_line=2) matches nodes starting on line 2 or later, and range(end_line=2) matches nodes ending on line 2 or earlier. range() with no arguments matches any binding that has a source position.

The entire node must fit within the range, including every line of a multiline node. Leading and trailing whitespace and comments attached to the node do not count toward its range. The node’s end position is immediately after its last character, and it may equal the specified end bound.

For example, add column limits to select one call from a line with multiple calls:

call_expression() as $call where {
$call <: range(start_line=2, start_column=3, end_line=2, end_column=11)
}
first();
second(); third();
fourth(
fifth()
);

This matches only second(), which starts at column 3 and ends at column 11, immediately after ). The semicolon is outside the call expression. Setting end_column=10 would exclude the call because its end position exceeds the limit. Column limits apply only on their respective boundary lines.

You can use range() as a condition in searches and plugin patterns, including patterns with rewrites. It filters existing syntax matches; it does not extract arbitrary source lines or make comments independently matchable.

GritQL can match patterns in JSON files, which is useful for searching and transforming configuration files:

language json
`"foo": $value`

This pattern matches any JSON member with the key "foo".

For more precise matching, use Biome’s JSON syntax nodes directly:

language json
JsonMember(name = JsonMemberName(value = `"version"`))

JSON GritQL also supports TreeSitter-compatible node aliases for compatibility with existing Grit patterns:

Biome AST TreeSitter alias
JsonMember pair
JsonObjectValue object
JsonArrayValue array

Biome does not support every GritQL feature, and some supported features still have bugs. Issue #2582 tracks which features work. The plugin RFC describes the direction of Biome’s plugin support.

For the full GritQL syntax, see the GritQL language documentation.