Automatic Deprecation Refactoring


I have dusted off and improved the automatic refactoring of deprecations.

- when the refactoring engine is not present, it falls back to a standard deprecation
- It does not introduce any dependencies (can just be in the Kernel, the only cost is one method of 10 lines).
- It sets the author so it does not require initials
- It refactors *only* the sending (offending, deprecated) send site. So a second selector that is identical (which might be a different method and not deprecated) will not be affected.

The way to use it is this:

ifNotNilDo: aBlock
        deprecated: 'Please use #ifNotNil: instead'
        transformWith: '`@receiver ifNotNilDo: `@statements' -> '`@receiver ifNotNil: `@statements'.
    ^ aBlock ifNotNil: aBlock

As we try to provide already Lint rules for deprecations (or at least should have..) the amount of additional work is not much.

This is the full implementation:

deprecated: anExplanationString transformWith: aRule
    | rewriteRule method context node |
    context := thisContext sender sender.
    (Smalltalk globals hasClassNamed: #RBParseTreeRewriter) 
        ifFalse: [ ^self deprecated: anExplanationString ].
    method := context method.
    node := context sourceNodeExecuted.
    rewriteRule := (Smalltalk globals classNamed: #RBParseTreeRewriter) new replace: aRule key with: aRule value.
    (rewriteRule executeTree: node)
        ifFalse: [ ^ self ].
    node replaceWith: rewriteRule tree. 
        useAuthor: 'AutoDeprecationRefactoring'
        during: [method methodClass compile: method ast formattedCode classified: method protocol]

This is *with intention* the absolute “simplest thing that can possibly work”. There is no interactive tool to preview refactoring in interactive
mode, or some way to opt in or out. 

The power of things like this comes from actually using them, not from being perfect...


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: