pharo-wiki
pharo-wiki copied to clipboard
Add script to replace usage of `isNil ifTrue:`
Gabriel Cotelli did this script to improve quality of Pharo:
| env rewriter result methods changes |
env := RBBrowserEnvironment default
forPackages: (RPackageOrganizer default packages select: [:p | p name beginsWith: 'Spec2' ]).
rewriter := RBParseTreeRewriter new.
rewriter
replace:'`@obj isNil ifFalse: `@block' with: '`@obj ifNotNil: `@block';
replace: '`@obj isNil ifTrue: `@block' with: '`@obj ifNil: `@block';
replace: '`@obj isNil ifTrue: `@tb ifFalse: `@block' with: '`@obj ifNil: `@tb ifNotNil: `@block'.
changes := Array with: (RBSmalllintChecker runRule: (RBTransformationRule new
rewriteRule: rewriter;
yourself)
onEnvironment: env ) builder.
(ChangesBrowser changes: changes) open.
We should add it to the cool scripts page
And we can advice people on trying to not using nil
in the first place :). There's almost always a better option.
There's now a PR in Pharo including a critics rule with auto-fix funcionality, but could be a good example on how to run the rewrite engine scripted.