Rust rules
A Rust rule is a shared library lintrs loads in-process: it queries the same
checker as typed JS rules and skips the JS rule runtime. It is enabled and
configured exactly like a JS rule. Rust rules run on TypeScript project
files only — never on .graphql.
Use lintrs --require-native to verify that every enabled rule in the selected
files runs in Rust. It rejects JavaScript rules, including rules enabled by
inline comments, before linting. With --quiet, only error-level rules are
required to be native, matching the rules that command executes. This is a
coverage gate for migrations; it does not compare the behavior of rule ports.
The same rule, in Rust
This is call-returns-union from JavaScript rules, as a
cdylib crate:
RuleContext
A target is a node (P<Node>, its token range) or a { pos, end } range in
tsrs UTF-8 byte offsets. lintrs converts to ESLint's UTF-16 locations.
initialize runs once per file before traversal; listens_to picks node
kinds, visit runs before its children, and the optional exit callback runs
after its children. Use exit for rules that need the same ordering as
ESLint's :exit listeners.
For expensive graph analysis, use context.cache::<MyCache>() rather than a
global map keyed by raw program pointers. The cache is scoped to the snapshot,
project, checker, library, rule id and options. A checker replacement gets a
fresh cache, and releasing the snapshot releases its caches. Rule instances
themselves are still created per file.
Exporting
lintrs::export_rules! maps names to constructors
(fn(&Value) -> Result<Box<dyn Rule>, String>) and emits the lintrs_rules_v0
entry point with the ABI fingerprint.
lintrs::native::simple::<R>: constructor forDefaultrules without options.- With options: write a constructor;
Errfails the lint. Example:rename-valueinfixtures/native/demo-rules/src/lib.rs.
Dependencies
lintrs builds crates with
cargo rustc --extern lintrs=<liblintrs> -L dependency=<sdk/deps> so rule and
host share one copy of lintrs and tsrs. So:
- Don't depend on
lintrsor crates it contains (serde_json, tsrs crates, …); the build fails. - Use the re-exports:
lintrs::native::serde_json,lintrs::native::Value,lintrs::ts::{ast, checker, compiler, core, scanner}. - Other crates are fine.
fixtures/native/casing-rulesusescamino.
See also: Packaging, run-time requirements, Native rules API.
