on linter and formatter Language support: JS, TS, JSON and JSONC Biome is the fork of Rome by community The members of Rome are laid off Rome isn’t maintained anymore
the function on every variable declarator VariableDeclarator(node) { // Check if a `const` variable declaration if (node.parent.kind === "const") { // Check if variable name is `foo` if (node.id.type === "Identifier" && node.id.name === "foo") { // Check if value of variable is "bar" if (node.init && node.init.type === "Literal" && node.init.value !== "bar") { // report error context.report({ ... }); } } } } } }
AST node which visitor enter type Query = Ast<JsVariableDeclarator>; type State = (); // The main logic for linter // - return Some(()) when to report error // - return None **when not** to report error fn run(ctx: &RuleContext<Self>) -> Option<Self::State> { let node = ctx.query(); let parent = node .parent::<JsVariableDeclaratorList>()? .parent::<JsVariableDeclaration>()?; ... } }
type Query = Ast<JsVariableDeclarator>; impl Rule for EnforceFooBar { type State = (); // The main logic for linter // - return Some(()) when to report error // - return None **when not** to report error fn run(ctx: &RuleContext<Self>) -> Option<Self::State> { let node = ctx.query(); let parent = node .parent::<JsVariableDeclaratorList>()? .parent::<JsVariableDeclaration>()?; ... } }
return Some(()) when to report error // - return None **when not** to report error fn run(ctx: &RuleContext<Self>) -> Option<Self::State> { let node = ctx.query(); let parent = node .parent::<JsVariableDeclaratorList>()? .parent::<JsVariableDeclaration>()?; ... } } impl Rule for EnforceFooBar { // define the AST node which visitor enter type Query = Ast<JsVariableDeclarator>; type State = ();
AST node which visitor enter type Query = Ast<JsVariableDeclarator>; type State = (); // The main logic for linter // - return Some(()) when to report error // - return None **when not** to report error fn run(ctx: &RuleContext<Self>) -> Option<Self::State> { let node = ctx.query(); let parent = node .parent::<JsVariableDeclaratorList>()? .parent::<JsVariableDeclaration>()?; ... } }
check if a `const` variable declaration if parent.is_const() { // Check if variable name is `foo` if node.id().ok()?.text() == "foo" { // Check if value of variable is "bar" let init_exp = node.initializer()?.expression().ok()?; let literal_exp = init_exp.as_any_js_literal_expression()?; if literal_exp.as_static_value()?.text() != "bar" { // Report error to Biome // The details of error message is implemented by "diagnostic" method return Some(()); } } } None }
parent.is_const() { // Check if variable name is `foo` if node.id().ok()?.text() == "foo" { fn run(ctx: &RuleContext<Self>) -> Option<Self::State> { ... // Check if value of variable is "bar" let init_exp = node.initializer()?.expression().ok()?; let literal_exp = init_exp.as_any_js_literal_expression()?; if literal_exp.as_static_value()?.text() != "bar" { // Report error to Biome // The details of error message is implemented by "diagnostic" method return Some(()); } } } None }
let init_exp = node.initializer()?.expression().ok()?; let literal_exp = init_exp.as_any_js_literal_expression()?; if literal_exp.as_static_value()?.text() != "bar" { // Report error to Biome // The details of error message is implemented by "diagnostic" method return Some(()); } fn run(ctx: &RuleContext<Self>) -> Option<Self::State> { ... // check if a `const` variable declaration if parent.is_const() { // Check if variable name is `foo` if node.id().ok()?.text() == "foo" { } } None }
check if a `const` variable declaration if parent.is_const() { // Check if variable name is `foo` if node.id().ok()?.text() == "foo" { // Check if value of variable is "bar" let init_exp = node.initializer()?.expression().ok()?; let literal_exp = init_exp.as_any_js_literal_expression()?; if literal_exp.as_static_value()?.text() != "bar" { // Report error to Biome // The details of error message is implemented by "diagnostic" method return Some(()); } } } None }