Programmatically Run Lint

While most people prefer to run SmallLint (Smalltalk Code Critics) from within OmniBrowser, the question arises from time to time on how to do the same from within a workspace script:

1. Select one or more rules to run from the class hierarchy below RBLintRule. For example, the following expression would instantiate a single rule that searches for questionable message sends:

rule := RBBadMessageRule new

The following expression could be used to search for both classes that implement #= but not #hash and methods that do float equality comparisons:

rule := RBCompositeLintRule rules: (Array
with: RBDefinesEqualNotHashRule new
with: RBFloatEqualityComparisonRule new)

2. Next select the scope to run the rules in. In the most simple case this is the complete image:

environment := BrowserEnvironment new

You can however restrict the scope further, for example to the collection hierarchy:

environment := BrowserEnvironment new
forClasses: Collection withAllSubclasses

If you have the OmniBrowser refactoring tools installed you can easily browse a restricted environment by evaluating:

environment open

3. Finally you perform the actual search by evaluating the following expression:

SmalllintChecker runRule: rule onEnvironment: environment

Note that this might take a while depending on the size of the environment and the number of rules you run.

4. If you have the OmniBrowser refactoring tools installed you can have a look at the result by evaluating the following expression:

rule open

If you have an image without OmniBrowser loaded open an Inspector on the result to see the matching code. If you are running a transformation rule, use the following script to perform the change in your system:

change := CompositeRefactoryChange new.
change changes: rule changes.
change execute
Posted by Lukas Renggli at 19 November 2009, 6:04 pm with tags lint, refactoring, smalltalk, tutorial link


Thanks Lukas, this should come in handy!

Posted by Peter Hugosson-Miller at 19 November 2009, 6:25 pm link