0
# Linting
1
2
Rome's linter provides static analysis and code quality checks that replace ESLint, with extensive rules for correctness, performance, accessibility, security, and style. It includes automatic fixing capabilities for many issues.
3
4
## Capabilities
5
6
### Check Command
7
8
The primary linting command that analyzes files for issues and optionally applies fixes.
9
10
```bash { .api }
11
/**
12
* Run the linter on a set of files
13
*
14
* Usage: rome check [OPTIONS] <INPUTS...>
15
*
16
* INPUTS can be one or more filesystem paths, each pointing to a single file
17
* or an entire directory to be searched recursively for supported files
18
*/
19
rome check <INPUTS...>
20
```
21
22
**Core Options:**
23
24
```bash { .api }
25
--apply # Apply safe fixes automatically
26
--apply-suggested # Apply safe and suggested fixes automatically
27
--max-diagnostics <number> # Cap the amount of diagnostics displayed (default: 20)
28
--verbose # Print additional verbose advice on diagnostics
29
```
30
31
**Usage Examples:**
32
33
```bash
34
# Check files without fixing
35
rome check src/
36
37
# Apply safe fixes only
38
rome check src/ --apply
39
40
# Apply both safe and suggested fixes
41
rome check src/ --apply-suggested
42
43
# Limit diagnostic output
44
rome check src/ --max-diagnostics=10
45
46
# Verbose diagnostic information
47
rome check src/ --verbose
48
```
49
50
### Fix Application Types
51
52
Rome categorizes fixes into different safety levels:
53
54
```bash { .api }
55
# Safe fixes: Low-risk changes that preserve code behavior
56
--apply
57
58
# Suggested fixes: Higher-risk changes that may alter behavior
59
--apply-suggested
60
```
61
62
## Rule Categories
63
64
### Accessibility Rules (A11y)
65
66
Rules that enforce web accessibility best practices.
67
68
```json { .api }
69
{
70
"linter": {
71
"rules": {
72
"a11y": {
73
"recommended": true,
74
"noAutofocus": "error", // Avoid autoFocus attribute
75
"noBlankTarget": "error", // Disallow target="_blank" without rel="noreferrer"
76
"noPositiveTabindex": "error", // Prevent positive tabIndex values
77
"useAltText": "error", // Require alt text for images
78
"useAnchorContent": "error", // Enforce anchor element content
79
"useButtonType": "error", // Enforce button type attribute
80
"useKeyWithClickEvents": "error", // Require keyboard events with click events
81
"useKeyWithMouseEvents": "error" // Require keyboard events with mouse events
82
}
83
}
84
}
85
}
86
```
87
88
### Complexity Rules
89
90
Rules that enforce code complexity limits and maintainability.
91
92
```json { .api }
93
{
94
"linter": {
95
"rules": {
96
"complexity": {
97
"recommended": true,
98
"noBannedTypes": "error", // Disallow certain built-in types
99
"noExtraBooleanCast": "error", // Avoid unnecessary boolean casts
100
"noMultipleSpacesInRegularExpressionLiterals": "error",
101
"noStaticOnlyClass": "error", // Disallow classes with only static members
102
"noUselessCatch": "error", // Disallow unnecessary catch clauses
103
"noUselessConstructor": "error", // Disallow unnecessary constructors
104
"noUselessEmptyExport": "error", // Disallow empty export statements
105
"noUselessFragments": "error", // Disallow unnecessary React fragments
106
"noUselessLabel": "error", // Disallow unused labels
107
"noUselessRename": "error", // Disallow renaming import/export to same name
108
"noUselessSwitchCase": "error", // Disallow useless switch cases
109
"noWith": "error", // Disallow with statements
110
"useLiteralKeys": "error", // Enforce literal keys in objects
111
"useSimplifiedLogicExpression": "error" // Simplify boolean expressions
112
}
113
}
114
}
115
}
116
```
117
118
### Correctness Rules
119
120
Rules that catch potential bugs and logical errors.
121
122
```json { .api }
123
{
124
"linter": {
125
"rules": {
126
"correctness": {
127
"recommended": true,
128
"noChildrenProp": "error", // Disallow children prop in React
129
"noConstAssign": "error", // Disallow reassigning const variables
130
"noConstantCondition": "error", // Disallow constant conditions
131
"noEmptyCharacterClassInRegex": "error", // Disallow empty character classes
132
"noEmptyPattern": "error", // Disallow empty destructuring patterns
133
"noGlobalObjectCalls": "error", // Disallow calling global objects as functions
134
"noInnerDeclarations": "error", // Disallow variable/function declarations in nested blocks
135
"noInvalidConstructorSuper": "error", // Disallow invalid super() calls
136
"noNewSymbol": "error", // Disallow new Symbol()
137
"noPrecisionLoss": "error", // Disallow numeric literals that lose precision
138
"noSelfAssign": "error", // Disallow self assignment
139
"noSetterReturn": "error", // Disallow return in setter
140
"noSwitchDeclarations": "error", // Disallow lexical declarations in switch clauses
141
"noUndeclaredVariables": "error", // Disallow undeclared variables
142
"noUnreachable": "error", // Disallow unreachable code
143
"noUnreachableSuper": "error", // Disallow unreachable super() calls
144
"noUnsafeFinally": "error", // Disallow unsafe control flow in finally blocks
145
"noUnusedLabels": "error", // Disallow unused labels
146
"noUnusedVariables": "error", // Disallow unused variables
147
"useIsNan": "error", // Require isNaN() when checking for NaN
148
"useValidForDirection": "error", // Enforce correct for loop directions
149
"useYield": "error" // Require yield in generator functions
150
}
151
}
152
}
153
}
154
```
155
156
### Performance Rules
157
158
Rules that identify potential performance issues.
159
160
```json { .api }
161
{
162
"linter": {
163
"rules": {
164
"performance": {
165
"recommended": true,
166
"noDelete": "error" // Disallow delete operator
167
}
168
}
169
}
170
}
171
```
172
173
### Security Rules
174
175
Rules that help prevent security vulnerabilities.
176
177
```json { .api }
178
{
179
"linter": {
180
"rules": {
181
"security": {
182
"recommended": true,
183
"noDangerouslySetInnerHtml": "error", // Disallow dangerouslySetInnerHTML
184
"noGlobalEval": "error" // Disallow eval() in global scope
185
}
186
}
187
}
188
}
189
```
190
191
### Style Rules
192
193
Rules that enforce consistent code style.
194
195
```json { .api }
196
{
197
"linter": {
198
"rules": {
199
"style": {
200
"recommended": true,
201
"noArguments": "error", // Disallow arguments object
202
"noCommaOperator": "error", // Disallow comma operator
203
"noImplicitBoolean": "error", // Disallow implicit boolean conversion
204
"noInferrableTypes": "error", // Disallow obvious type annotations
205
"noNamespace": "error", // Disallow TypeScript namespaces
206
"noNonNullAssertion": "error", // Disallow non-null assertions
207
"noParameterAssign": "error", // Disallow reassigning function parameters
208
"noParameterProperties": "error", // Disallow parameter properties
209
"noRestrictedGlobals": "error", // Disallow specific global variables
210
"noShoutyConstants": "error", // Disallow SHOUTY_CASE constants
211
"noUnusedTemplateLiteral": "error", // Disallow unused template literals
212
"noVar": "error", // Disallow var declarations
213
"useBlockStatements": "error", // Enforce block statements
214
"useCollapsedElseIf": "error", // Enforce collapsed else-if
215
"useConst": "error", // Enforce const for never-modified variables
216
"useDefaultParameterLast": "error", // Enforce default parameters last
217
"useEnumInitializers": "error", // Enforce enum initializers
218
"useExponentiationOperator": "error", // Enforce ** over Math.pow()
219
"useFragmentSyntax": "error", // Enforce React fragment syntax
220
"useNumericLiterals": "error", // Enforce numeric literals
221
"useSelfClosingElements": "error", // Enforce self-closing elements
222
"useShorthandArrayType": "error", // Enforce T[] over Array<T>
223
"useSingleCaseStatement": "error", // Enforce single case per switch statement
224
"useSingleVarDeclarator": "error", // Enforce single variable declarator
225
"useTemplate": "error", // Enforce template literals
226
"useWhile": "error" // Enforce while over for loops where appropriate
227
}
228
}
229
}
230
}
231
```
232
233
### Suspicious Rules
234
235
Rules that flag potentially problematic code patterns.
236
237
```json { .api }
238
{
239
"linter": {
240
"rules": {
241
"suspicious": {
242
"recommended": true,
243
"noArrayIndexKey": "error", // Disallow array index as key
244
"noAssignInExpressions": "error", // Disallow assignment in expressions
245
"noAsyncPromiseExecutor": "error", // Disallow async Promise executor
246
"noCatchAssign": "error", // Disallow reassigning catch clause parameter
247
"noClassAssign": "error", // Disallow reassigning class declarations
248
"noCommentText": "error", // Disallow comments in JSX text
249
"noCompareNegZero": "error", // Disallow comparing against -0
250
"noControlCharactersInRegex": "error", // Disallow control characters in regex
251
"noDebugger": "error", // Disallow debugger statements
252
"noDoubleEquals": "error", // Disallow == and !=
253
"noDuplicateCase": "error", // Disallow duplicate case labels
254
"noDuplicateClassMembers": "error", // Disallow duplicate class members
255
"noDuplicateJsxProps": "error", // Disallow duplicate JSX props
256
"noDuplicateObjectKeys": "error", // Disallow duplicate object keys
257
"noDuplicateParameters": "error", // Disallow duplicate parameters
258
"noEmptyBlockStatements": "error", // Disallow empty block statements
259
"noExplicitAny": "error", // Disallow explicit any type
260
"noExtraNonNullAssertion": "error", // Disallow extra non-null assertions
261
"noFallthroughSwitchClause": "error", // Disallow fallthrough switch cases
262
"noFunctionAssign": "error", // Disallow reassigning function declarations
263
"noGlobalAssign": "error", // Disallow assignments to native objects
264
"noImportAssign": "error", // Disallow assigning to imports
265
"noLabelVar": "error", // Disallow labels with variable names
266
"noMisleadingCharacterClass": "error", // Disallow misleading character class
267
"noPrototypeBuiltins": "error", // Disallow prototype builtins
268
"noRedeclare": "error", // Disallow variable redeclaration
269
"noRedundantUseStrict": "error", // Disallow redundant 'use strict'
270
"noSelfCompare": "error", // Disallow self comparison
271
"noShadowRestrictedNames": "error", // Disallow shadowing restricted names
272
"noSparseArray": "error", // Disallow sparse arrays
273
"noUnsafeNegation": "error", // Disallow unsafe negation
274
"useDefaultSwitchClauseLast": "error", // Enforce default clause last
275
"useGetterReturn": "error" // Enforce return in getters
276
}
277
}
278
}
279
}
280
```
281
282
## Configuration
283
284
### Rule Configuration Levels
285
286
Each rule can be configured with different severity levels:
287
288
```json { .api }
289
{
290
"linter": {
291
"rules": {
292
"correctness": {
293
"noUnusedVariables": "error", // Error level (breaks build)
294
"noConstAssign": "warn", // Warning level (reports but continues)
295
"noEmptyPattern": "off" // Disabled
296
}
297
}
298
}
299
}
300
```
301
302
### Rule Groups
303
304
Enable or disable entire rule groups:
305
306
```json { .api }
307
{
308
"linter": {
309
"enabled": true,
310
"rules": {
311
"recommended": true, // Enable all recommended rules
312
"a11y": {
313
"recommended": true // Enable recommended accessibility rules
314
},
315
"performance": {
316
"recommended": false, // Disable performance rules
317
"noDelete": "error" // But enable specific rule
318
}
319
}
320
}
321
}
322
```
323
324
### File Ignoring
325
326
Configure files and patterns to ignore:
327
328
```json { .api }
329
{
330
"linter": {
331
"ignore": [
332
"dist/**",
333
"node_modules/**",
334
"*.min.js",
335
"build/**/*.js"
336
]
337
}
338
}
339
```
340
341
## Usage Examples
342
343
### Basic Linting
344
345
```bash
346
# Check all files in src directory
347
rome check src/
348
349
# Check specific files
350
rome check src/index.js src/utils.ts
351
352
# Check with custom file size limit
353
rome check src/ --files-max-size=2097152
354
```
355
356
### Auto-fixing
357
358
```bash
359
# Apply only safe fixes
360
rome check src/ --apply
361
362
# Apply all available fixes
363
rome check src/ --apply-suggested
364
365
# Check what would be fixed without applying
366
rome check src/ --verbose
367
```
368
369
### CI Integration
370
371
```bash
372
# Exit with error code if issues found
373
rome check src/
374
375
# Limit output for CI logs
376
rome check src/ --max-diagnostics=50
377
378
# JSON output for tooling integration
379
rome check src/ --json
380
```
381
382
## Error Handling
383
384
### Diagnostic Output
385
386
Rome provides detailed diagnostic information:
387
388
- **File location**: Exact line and column numbers
389
- **Rule name**: Which rule was violated
390
- **Severity level**: Error, warning, or info
391
- **Fix availability**: Whether automatic fixes are available
392
- **Code context**: Surrounding code for context
393
394
### Exit Codes
395
396
- `0`: No issues found
397
- `1`: Issues found (errors or warnings based on configuration)
398
- `2`: Configuration or runtime error