Rules and severities
Set the severity of each issue type in the rules config, change it for some files with overrides, and see the defaults.
In the demo project, fallow dead-code finds an unused file, three unused exports and two dependency problems, and it exits with code 1. Your team can agree with each finding and still want to remove the exports next month, without a red CI job until then. The rules object of the config file sets the severity of each issue type, so you decide which findings fail a run. Findings defines rules, severities and gates.
The rules object
Each key is a rule name, and each value is a severity:
| Severity | Effect |
|---|---|
error | Fallow reports the finding, and the run exits with code 1. |
warn | Fallow reports the finding, and the run exits with code 0. |
off | Fallow does not look for the issue type. |
// .fallowrc.json
{
"rules": {
"unused-files": "error",
"unused-exports": "warn",
"unused-types": "off",
"private-type-leaks": "warn"
}
}
# fallow.toml
[rules]
unused-files = "error"
unused-exports = "warn"
unused-types = "off"
private-type-leaks = "warn"
A rule that you do not set keeps its default. An unknown rule name gives a warning on stderr, and fallow ignores the entry.
Set each of the four rules of the demo findings to warn, and fallow dead-code still lists each finding. The summary line starts with ⚠ instead of ✗, and the command exits with code 0:
⚠ 1 file · 3 exports · 1 unused dependency · 1 unlisted dependency (0.02s)
Rule names
To set a rule, you need its name. The rule name is the plural form of the issue type in most cases, for example unused-exports for the issue type unused-export. Most rules also accept the other form as an alias, for example circular-dependency for circular-dependencies. The page of each issue type gives its key in the row "Config key in rules". Issue types lists each issue type with its default severity. The rules reference lists each key.
Defaults
Before you change a rule, know where it starts. A rule defaults to error unless it is in one of these groups:
off: the checks that you turn on yourself. Examples areprivate-type-leaks,deprecated-exports-in-use,unused-protected-class-members,absent-component-props,constant-parameters,prop-drilling,thin-wrapper,duplicate-prop-shape,coverage-gaps,feature-flags,require-suppression-reason,security-sink,security-client-server-leak,diverged-clone-siblingandadded-dependency.warn: these groups of rules.unused-dev-dependencies,unused-optional-dependencies,type-only-dependencies,test-only-dependenciesanddev-dependencies-in-production.- The framework rules, for example
unused-component-propsandunused-store-members, and the Effect rules. - The client and server directive rules, and the styling rules (
css-*). unused-catalog-entries,empty-catalog-groupsandunused-dependency-overrides.- The change checks of
fallow auditthat are notoff:weakened-tests,added-suppression,test-only-branchandintroduced-clone-group. unlisted-binaries,unused-namespace-members,duplicate-export-aliases,re-export-cycle,package-cycle,stale-suppressionsandpolicy-violation.
To see the severity of each rule for your project, run fallow config. The rules object of its output holds the result after extends and the defaults.
Overrides
A severity in rules applies to the whole project, but one part of the code can need another one. overrides sets other severities for the files that its globs match. The globs follow the glob rules.
files takes globs of project files, not package names or binary names. The glob must match the file that has the finding. For unresolved-import, that is the file with the import, not the import target. To hide one import, add its specifier to ignoreUnresolvedImports.
These rules read only the top-level severity, so an override does not change them:
unused-dependencies,unused-dev-dependenciesandunused-optional-dependencies: a package inpackage.jsonthat no file uses.unlisted-dependencies: a package that a file imports, but thatpackage.jsondoes not list.unlisted-binaries: a binary that apackage.jsonscript or a process call runs, but that no installed package provides.type-only-dependencies: a production dependency that the code uses only for types.test-only-dependencies: a production dependency that only test files use.dev-dependencies-in-production: a development dependency that production code imports.duplicate-exports: an export name that more than one file exports.re-export-cycle: files that re-export each other in a loop.unused-catalog-entries: a catalog entry that no workspace package uses.
To hide one package, add it to ignoreDependencies. To hide one binary, add it to ignoreBinaries.
In this config for the demo project, unused exports only warn. The rule is off in src/lib, but src/lib/format.ts must stay clean:
// .fallowrc.json
{
"rules": { "unused-exports": "warn" },
"overrides": [
{ "files": ["src/lib/**"], "rules": { "unused-exports": "off" } },
{ "files": ["src/lib/format.ts"], "rules": { "unused-exports": "error" } }
]
}
npx fallow dead-code --unused-exports
● Unused exports (2)
src/lib/format.ts
:3 formatPercent
src/services/orders.ts
:18 cancelOrder
Exported symbols with no known consumers
https://fallow.tools/docs/explanations/dead-code/#unused-exports
✗ 2 exports (0.03s)
The re-export in src/lib/index.ts is gone, because the first entry turns the rule off in src/lib. formatPercent stays, at error, so the run fails. cancelOrder keeps the top-level warn.
- Fallow applies the entries in list order. When two entries match a file and set the same rule, the later entry wins.
- An override cannot start a check that the top-level
rulesturns off. Set the rule on at the top level, then set it toofffor the other files.
Fallow reports an override entry that has no effect as an override rule with no effect. The overrides reference has the details.
Production mode
The same config can give other results in production mode, because a development dependency does not ship. Production mode changes some dependency rules:
unused-dev-dependenciesandunused-optional-dependenciesareoff.- Fallow reports
type-only-dependencies(defaultwarn) only in production mode. - Fallow reports
test-only-dependenciesonly outside production mode.
Complexity rules
Complexity findings use two settings with different jobs. The health limits, for example health.maxCyclomatic, decide if a function has a complexity finding. The rules complexity-cyclomatic, complexity-cognitive, complexity-crap and complexity-file-lines decide if that finding fails the run.
-
A rule at
warnkeeps the finding in the report.fallow healththen exits with code 0, and the verdict offallow auditiswarn. -
A rule at
offremoves the finding from the report. -
A function above more than one limit takes the most severe rule of those limits.
-
In JSON output,
effective_severityholds the rule severity.severityholds the band:moderate,highorcritical. -
The rule applies before
fallow health --min-severity. So a finding atwarndoes not fail the run, also when its band is at or above the--min-severitylevel. -
--fail-on-issues, and--ci, make awarncomplexity finding failfallow healthand the barefallowrun. -
complexity-crap: "off"removes only the CRAP findings.health.maxCrap: 0also removes the CRAP signals from the file scores.
After you set a complexity rule to off, save the health baseline again. Its entries for the removed findings no longer match.
Change the result for one run
A CI job or a local check can need a stricter or a narrower run than the config file sets. The config file stays the same for these options:
--fail-on-issuesmakes awarnfinding fail the run too.--ciincludes it.fallow auditignores it and keeps its own verdict.- An issue type filter, such as
--unused-deps, does not turn on a rule that isoff. Fallow then adds a requested rule that is off warning. - When the config sets their rule to
off, these filters offallow dead-codeturn it on atwarn:--private-type-leaks,--deprecated-exports-in-use,--unused-protected-class-members,--absent-component-propsand--constant-parameters.
Exit codes lists each code.
Levels in CI formats
A severity also decides how a finding looks in a pull request. The CI formats take the level of a finding from its rule severity, also from an overrides entry. A complexity finding takes its level from its complexity-* rule, not from its band.
| Rule severity | github-annotations | SARIF | CodeClimate |
|---|---|---|---|
error | ::error | error | major |
warn | ::warning | warning | minor |
With the default rules, a moderate complexity finding is an ::error annotation and a SARIF error, because the finding fails the run. Each finding in the JSON output has the effective_severity field, and fallow report --from reads it. So a saved report gives the same levels as the run that made it.
Related
- Next: Suppression comments. Use a comment when you keep one finding and want the rule to stay on.
- Findings: rules, severities and gates.
- Issue types: each issue type with its default severity.