Writing a Rule
A rule is a sentence: what it is for, where it applies and what the code must be. How to write one, change it and try it.
On this page
A rule is a sentence#
A rule says what it is about, where it applies, and what the code must be. The rule No braces around a single-line if reads:
It blocks the push. for names the artifact the rule is about; where narrows it to the places the rule applies to; require says what those places must be. A rule can instead forbid something altogether: the places where describes must not exist.
Add a rule#
With the command line:
$ commitfence rules add TEAM010 --for if-statement --title "No braces around a single-line if" --where statementsInBody=1 --where bodyFitsOnOneLine=true --require braces=false
In commitfence.json the same rule is:
{ "rules": [ { "id": "TEAM010", "language": "csharp", "for": "if-statement", "severity": "block", "title": "No braces around a single-line if", "where": { "statementsInBody": 1, "bodyFitsOnOneLine": true }, "require": { "braces": false } } ]}
The properties a rule can look at, and the artifacts it can be for, are in the rule catalog. A wrong property is refused, and the answer names the right ones:
$ commitfence rules add TEAM020 --for if-statement --title x --require nothing=1'nothing' is not a property of if-statement. Known properties: statementsInBody, bodyFitsOnOneLine, fitsOnOneLine, braces, isElseIf, hasElse, lines.exit code 3
A rule that forbids#
A rule for a usage forbids a use of an API. It can carry the reason it exists with --why:
$ commitfence rules add TEAM012 --for usage --title "Use IClock, not DateTime.Now" --where target=System.DateTime.Now --forbid --why "Code that reads the clock directly cannot be given another time in a check."
A usage is known only from the build, so a rule for it needs a review that builds: --push, --staged, --base or --all, not --files.
Look at the rules#
$ commitfence rules listTEAM010 block csharp if-statement No braces around a single-line ifTEAM012 block csharp usage Use IClock, not DateTime.Now.editorconfig: 2 rulesCA1305 on blocks Specify IFormatProvider line 5CA1822 on warns Mark members as static line 4exit code 0
$ commitfence rules show TEAM010TEAM010 No braces around a single-line if severity: block language: csharp for: if-statement where: statementsInBody is 1 bodyFitsOnOneLine is true require: braces is falseexit code 0
Change a rule#
Every one of these writes commitfence.json and prints one line.
| Command | What it does | It prints |
|---|---|---|
commitfence rules disable TEAM012 | Switches a rule off | TEAM012 is now off. |
commitfence rules enable TEAM012 | Switches it on | TEAM012 is now on. |
commitfence rules severity TEAM012 warn | Chooses whether it blocks the push (block) or only warns (warn) | TEAM012 now only warns. |
commitfence rules set TEAM012 --paths src/** | Changes the parts of a rule you name; --no-where, --no-paths, --no-message and --no-why take a part away | Changed rule TEAM012. |
commitfence rules copy TEAM012 TEAM014 | Copies a rule right after the original, titled Copy of its title | Copied rule TEAM012 as TEAM014. |
commitfence rules remove TEAM014 | Removes a rule | Removed rule TEAM014. |
Try a rule#
rules try runs one rule on a file, or on standard input, and says for each place whether it breaks the rule, follows it or is not covered.
$ commitfence rules try TEAM010 src/Shop/Discount.cssrc/Shop/Discount.cs(7,9): breaks it: braces is true, the rule requires false1 breaks it, 0 follows it, 0 not covered.exit code 0
Something wrong or unclear on this page? Tell us on GitHub