CommitFence
Esc
↑ ↓ to moveEnter to open
Rules / Writing a Rule

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:

For if statement in C#, where statements in body is 1 and body fits on one line is true, require braces is false.

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:

commitfence.json
{    "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:

Terminal
$ 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#

Terminal
$ 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
Terminal
$ 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.

CommandWhat it doesIt prints
commitfence rules disable TEAM012Switches a rule offTEAM012 is now off.
commitfence rules enable TEAM012Switches it onTEAM012 is now on.
commitfence rules severity TEAM012 warnChooses 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 awayChanged rule TEAM012.
commitfence rules copy TEAM012 TEAM014Copies a rule right after the original, titled Copy of its titleCopied rule TEAM012 as TEAM014.
commitfence rules remove TEAM014Removes a ruleRemoved 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.

Terminal
$ 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