Add conditions to rules to precisely target them
Last time we introduced custom rules into Sourcery.
This time they’re becoming even more powerful as you can filter pattern matches with a condition.
Let’s say we want to stop global variables being declared. First let’s write down what a global variable is:
- It’s declared at the top level of the module
- It’s not a constant using uppercase like
DAYS_IN_WEEK = 7
- Let’s allow private globals starting with
_
Here’s a rule to identify global variables:
rules:
- id: no-global-variables
pattern: ${var} = ${value}
condition: |
var.in_module_scope()
and not var.is_upper_case()
and not var.starts_with("_")
description: Don't declare `${var}` as a global variable
Check out the conditions reference to see what conditions are available.
Added
- Custom rules can contain a
condition
field that means the rule only matches when the condition is satisfied - CLI: display a progress bar showing how many files have been reviewed
- Docs: Flag Dependencies to a Library with Sourcery custom rules
- Docs: Establish Rules for Dependencies Between Your Packages with Sourcery custom rules
use-file-iterator
refactoring
Changed
reintroduce-else
will now trigger more often- Refactorings
list-literal
anddict-literal
will now trigger in every instance oflist()
anddict()
, instead of only in assignments - CLI: display rule descriptions and explanations as Markdown
- CLI docs: present the
review
command instead ofrefactor
Fixed
dict-assign-update-to-union
doesn’t trigger for global variables- Files using tab indenting will be always be refactored with tabs
- Identical clone detection will now only trigger for functions if they have the same name
- Recover from PermissionErrors when rotating log files
- Sourcery now run refactorings on functions containing nested functions