jackfirth / jackfirth/resyntax

Suggest `match` with `regexp` instead of `regexp-match`

Open
#541 1 comment 0 reactions 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

In this code, the use of cond and regexp-match can be expressed more cleanly using match with the regexp pattern. This probably shouldn't be rewritten unless 1) every non-else branch of the cond is using the lambda-based form and 2) all of the match patterns use define to name the extracted groups, if there are multiple groups. So this test case ought to pass:

#lang resyntax/test
require: resyntax/default-recommendations default-recommendations
header:
- #lang racket

test: "original code should be refactorable to new code"
--------------------
(define re #rx"^[ \t\n]*([0-9]+)[ \t\n]*,(.*)$")
(define (f str)
  (cond
    [(regexp-match re str)
     =>
     (lambda (m)
       (define a (cadr m))
       (define b (caddr m))
       (list a b))]
    [else 'else]))
====================
(define re #rx"^[ \t\n]*([0-9]+)[ \t\n]*,(.*)$")
(define (f str)
  (match str
    [(regexp re (list _ a b)) (list a b)]
    [else 'else]))
--------------------

There are some similar cases in DrRacket's codebase but refactoring all of them is questionable. The cases where there's multiple capture groups and none of them have named variables associated with them ought to be left alone.

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 with the linked drracket/browser/private/html.rkt examples and the supplied resyntax/test case, then find the recommendation implementation and existing tests for regexp-match and match patterns. Done means the shown test passes while cases with unnamed multiple capture groups or non-lambda branches remain unrecommended.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.