jackfirth / jackfirth/resyntax

New rule: `Shared else-value for if/case`

Open
#784 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

new lint
Dominant language
Racket
Stars
70
Forks
11
PR merge metrics
No merged PRs in 30d

Description

Rule summary

From:

      (if (symbol? level)
          (case level
            [(warning)                                'warning]
            [(danger)                                 'danger]
            [(info success warning danger
                   primary secondary light dark)      level]
            [else                                     'info])
          'info)

To:

          (case level
            [(warning)                                'warning]
            [(danger)                                 'danger]
            [(info success warning danger
                   primary secondary light dark)      level]
            [else                                     'info])

The observation is that the value return by the else-arm of the if-expression,
is the same as the expression returned in the else-expression of the case.

Test case
#lang resyntax/test

test: "original code should be refactorable to new code"
--------------------
#lang racket
(define level 'foo)
(if (symbol? level)
    (case level
      [(warning) 'warning]
      [else      'info])
    'info)

====================
#lang racket
(define level 'foo)
(case level
      [(warning) 'warning]
      [else      'info])
--------------------
No-change test case

Additional context

Spotted here:

https://github.com/soegaard/webracket/blob/main/lib/web-easy/renderer.rkt#L892

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the existing refactoring-rule implementations and their test suite; the issue provides a positive Racket test case and points to renderer.rkt in the webracket repository as the observation site. Done means the shown if/case form is transformed into the case form while unrelated code remains unchanged.

Written by the indexing model from the issue text.

Assessment

Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.