Linter
Rome’s linter statically analyzes your code to catch common errors and to help writing idiomatic code.
Use the linter via CLI
You can start by running the CLI with the --help
flag:
- npm
- yarn
- pnpm
npx rome check --help
yarn rome check --help
pnpm rome check --help
Which will show you the options available at the moment:
Rome Check: Run the linter on a set of files
USAGE:
rome check <INPUTS...>
INPUTS can be one or more filesystem path, each pointing to a single file or an entire directory to be searched recursively for supported files
OPTIONS:
--apply Apply safe fixes
--apply-suggested Apply safe and suggested fixes
--max-diagnostics Cap the amount of diagnostics displayed - default 20
Code fixes
Lint rules may provide automatic code fixes. Rome distinguishes between two types of fixes:
- safe fixes
- suggested fixes
Safe fixes are guaranteed to not change the semantic of your code. They can be applied without explicit review.
Suggested fixes may change the semantic of your program. Therefore, it’s advised to manually review the changes.
Ignoring Code
There are times when a developer wants to ignore a lint rule for a specific line of the code. You can achieve this by adding a suppression comment above the line that emits the lint diagnostic.
Suppression comments have the following format:
// rome-ignore lint: <explanation>
// rome-ignore lint/suspicious/noDebugger: <explanation>
Where
rome-ignore
is the start of a suppression comment;lint
suppresses the linter;/suspicious/noDebugger
: optional, group and name of the rule you want to suppress;<explanation>
explanation why the rule is disabled
Here’s an example:
// rome-ignore lint: reason
debugger;
// rome-ignore lint/suspicious/noDebugger: reason
debugger;
Configuration
Enable a lint rule
Recommended rules are enabled by default and emit diagnostics with the error severity. Rules that are not recommended are disabled by default, but they can be enabled via configuration. The diagnostics emitted by these rules are displayed with the warning severity in the documentation.
To enable rules, you need to change their diagnostic severity based on your needs:
{
"linter": {
"enabled": true,
"rules": {
"style": {
"useBlockStatements": "error",
"useShorthandArrayType": "error",
"noShoutyConstants": "warn"
}
}
}
}
Disable a lint rule
Just add "off"
as value inside its configuration. For example:
{
"linter": {
"enabled": true,
"rules": {
"suspicious": {
"noCommentText": "off"
},
"style": {
"noUnusedTemplateLiteral": "off"
}
}
}
}
Change the diagnostic severity
Most of Rome’s rules will emit an error, but you are free to change their severity. Just add "warn"
as value of the rule. Example:
{
"linter": {
"enabled": true,
"rules": {
"suspicious": {
"noCommentText": "warn"
}
}
}
}
This is useful in cases there’s being a refactor going on and there’s need to make the CI passing.
Rule options
A few rules have options. When they do accept some, you can pass them by shaping the value of the rule differently.
{
"linter": {
"enabled": true,
"rules": {
"correctness": {
"noCommentText": {
"level": "warn",
"options": {}
}
}
}
}
}
level
will indicate the severity of the diagnostic, valid values are:"off"
,"warn"
and"error"
;options
will change based on the rule.