On this page:
refactoring-rule?
refactoring-suite?
define-refactoring-rule
define-refactoring-suite
3.1 Resyntax’s Default Rules
default-recommendations
3.2 What Makes a Good Refactoring Rule?

3 Refactoring Rules and Suites🔗ℹ

 (require resyntax/base) package: resyntax

Resyntax derives its suggestions from refactoring rules, which can be grouped into a refactoring suite. Resyntax ships with a default refactoring suite consisting of many rules that cover various scenarios related to Racket’s standard libraries. However, you may also define your own refactoring suite and rules using the forms below. Knowledge of Racket macros, and of syntax-parse in particular, is especially useful for understanding how to create effective refactoring rules.

procedure

(refactoring-rule? v)  boolean?

  v : any/c
A predicate that recognizes refactoring rules.

procedure

(refactoring-suite? v)  boolean?

  v : any/c
A predicate that recognizes refactoring suites.

syntax

(define-refactoring-rule id
  #:description description
  parse-option ...
  syntax-pattern
  pattern-directive ...
  template)
 
  description : string?
Defines a refactoring rule named id. Refactoring rules are defined in terms of syntax-parse. The rule matches syntax objects that match syntax-pattern, and template is a syntax template that defines what the matched code is refactored into. The message in description is presented to the user when Resyntax makes a suggestion based on the rule. Refactoring rules function roughly like macros defined with define-syntax-parse-rule. For example, here is a simple rule that flattens nested or expressions:

Example:
> (define-refactoring-rule nested-or-to-flat-or
    #:description "This nested `or` expression can be flattened."
    #:literals (or)
    (or a (or b c))
    (or a b c))

Like syntax-parse and define-syntax-parse-rule, pattern directives can be used to aid in defining rules. Here is a rule that uses the #:when directive to only refactor or expressions that have a duplicate condition:

Example:
> (define-refactoring-rule or-with-duplicate-subterm
    #:description "This `or` expression has a duplicate subterm."
    #:literals (or)
    (or before ... a:id between ... b:id after ...)
    #:when (free-identifier=? #'a #'b)
    (or before ... a between ... after ...))

syntax

(define-refactoring-suite id rules-list suites-list)

 
rules-list = 
  | #:rules (rule ...)
     
suites-list = 
  | #:suites (suite ...)
 
  rule : refactoring-rule?
  suite : refactoring-suite?
Defines a refactoring suite named id containing each listed rule. Additionally, each suite provided has its rules added to the newly defined suite.

Example:
> (define-refactoring-suite my-suite
    #:rules (rule1 rule2 rule3)
    #:suites (subsuite1 subsuite2))

3.1 Resyntax’s Default Rules🔗ℹ

 (require resyntax/default-recommendations)
  package: resyntax

The refactoring suite containing all of Resyntax’s default refactoring rules. These rules are further broken up into subsuites, with each subsuite corresponding to a module within the resyntax/default-recommendations collection. For example, all of Resyntax’s rules related to for loops are located in the resyntax/default-recommendations/for-loop-shortcuts module. See this directory for all of Resyntax’s default refactoring rules.

3.2 What Makes a Good Refactoring Rule?🔗ℹ

If you’d like to add a new refactoring rule to Resyntax, there are a few guidelines to keep in mind: