Skip to content

Architecture

This page explains some of Biome’s internals and how the project uses them. It is for contributors, and for anyone curious about how Biome processes code.

Biome has a scanner that crawls the file system to extract important metadata about projects. Biome uses the scanner in three ways:

  • To discover nested biome.json/biome.jsonc files in monorepos.
  • To discover nested .gitignore files if the vcs.useIgnoreFile setting is enabled.
  • To index package.json manifests as well as source files in a project if any rules from the project domain are enabled.

If project rules are not enabled, the scanner automatically targets only the folders that are relevant for a given session.

For example, if you have a large monorepo and you run biome check from inside the packages/foo/ folder, the scanner targets that folder. It scans the following folders for nested configuration files and nested ignore files:

  • The root folder of the repository.
  • The packages/ folder.
  • The packages/foo/ folder.
  • Any folders under packages/foo/, except node_modules/ and those that your configuration excludes (see below).

The scanner skips other folders next to packages/ or packages/foo/.

Similarly, if you run biome format packages/bar/src/index.ts from the root of the repository, the scanner targets the packages/bar/src/ folder.

If project rules are enabled, these optimizations do not apply.

The scanner can be configured through the files.includes setting.

The architecture of the parser is backed by an internal fork of rowan, a library that implements the Green and Red tree pattern.

The CST (Concrete Syntax Tree) is a data structure very similar to an AST (Abstract Syntax Tree) that keeps track of all the information of a program, trivia included.

Trivia is all the information that is not important for a program to run:

  • spaces
  • tabs
  • comments

Trivia is attached to a node. A node can have leading trivia and trailing trivia. If you read code from left to right, leading trivia appears before a keyword, and trailing trivia appears after a keyword.

Biome categorizes leading trivia and trailing trivia as follows:

  • Everything up to the token or keyword, including line breaks, is leading trivia.
  • Everything until the next line break, but not including it, is trailing trivia.

In the following JavaScript snippet, // comment 1 is trailing trivia of the token ;, and // comment 2 is leading trivia of the keyword const. After the snippet is a minimized version of the CST that Biome builds for it:

const a = "foo"; // comment 1
// comment 2
const b = "bar";
0: JS_MODULE@0..55
...
1: SEMICOLON@15..27 ";" [] [Whitespace(" "), Comments("// comment 1")]
1: JS_VARIABLE_STATEMENT@27..55
...
1: CONST_KW@27..45 "const" [Newline("\n"), Comments("// comment 2"), Newline("\n")] [Whitespace(" ")]
3: EOF@55..55 "" [] []

By design, the CST is never directly accessible. Developers read its information through the Red tree, using APIs that are generated from the grammar of the language.

To build a CST, a parser needs to be error-resilient and recoverable:

  • Resilient: the parser can resume parsing after it encounters syntax errors that belong to the language.
  • Recoverable: the parser can understand where an error occurred, and resume parsing by creating correct information.

The recoverable part of the parser is not a science, and no rules are set in stone. Depending on what the parser was parsing and where an error occurred, the parser might be able to recover in an expected way.

The parser also uses bogus nodes to protect consumers from consuming incorrect syntax. These nodes decorate the broken code caused by a syntax error.

In the following example, the parentheses of the while loop are missing, but the parser recovers well and represents the code with a decent CST. It marks the parentheses and the condition of the loop as missing, and parses the code block correctly:

while {}
JsModule {
interpreter_token: missing (optional),
directives: JsDirectiveList [],
items: JsModuleItemList [
JsWhileStatement {
while_token: WHILE_KW@0..6 "while" [] [Whitespace(" ")],
l_paren_token: missing (required),
test: missing (required),
r_paren_token: missing (required),
body: JsBlockStatement {
l_curly_token: L_CURLY@6..7 "{" [] [],
statements: JsStatementList [],
r_curly_token: R_CURLY@7..8 "}" [] [],
},
},
],
eof_token: EOF@8..8 "" [] [],
}

The parser emits this error:

main.tsx:1:7 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ expected `(` but instead found `{`
> 1 │ while {}
│ ^
ℹ Remove {

The parser cannot recover as well from the following snippet. It cannot properly understand the syntax during the recovery phase, so it relies on bogus nodes to mark some syntax as erroneous. Notice the JsBogusStatement:

function}
JsModule {
interpreter_token: missing (optional),
directives: JsDirectiveList [],
items: JsModuleItemList [
TsDeclareFunctionDeclaration {
async_token: missing (optional),
function_token: FUNCTION_KW@0..8 "function" [] [],
id: missing (required),
type_parameters: missing (optional),
parameters: missing (required),
return_type_annotation: missing (optional),
semicolon_token: missing (optional),
},
JsBogusStatement {
items: [
R_CURLY@8..9 "}" [] [],
],
},
],
eof_token: EOF@9..9 "" [] [],
}

The parser emits this error:

main.tsx:1:9 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ expected a name for the function in a function declaration, but found none
> 1 │ function}
│ ^