Add flatten-apply-append-syntax-template refactoring rule
Nested syntax template flattening operations can be simplified. The pattern (apply append (map syntax->list (syntax->list #'((a ...) ...)))) does the same work as (syntax->list #'(a ... ...)) but with unnecessary intermediate steps.
Changes
-
New refactoring rule
flatten-apply-append-syntax-templateinsyntax-shortcuts.rkt- Detects the
apply append (map syntax->list ...)pattern on nested syntax templates - Replaces with direct
syntax->liston flattened template - Uses
datum->syntaxto construct the flattened pattern, avoiding template ellipsis depth restrictions
- Detects the
-
Test coverage in
syntax-shortcuts-test.rktvalidates the transformation
Example
;; Before
(define (f stx)
(with-syntax ([((a ...) ...) stx])
(apply append
(map syntax->list (syntax->list #'((a ...) ...))))))
;; After
(define (f stx)
(with-syntax ([((a ...) ...) stx])
(syntax->list (syntax (a ... ...)))))
Note: Output uses (syntax ...) form rather than #' reader syntax, as Resyntax operates on expanded syntax forms.
Original prompt
This section details on the original issue you should resolve
<issue_title>New rule:
flatten-apply-append-syntax-template</issue_title> <issue_description>### Rule summaryThe code
(apply append (map syntax->list (syntax->list #'((id ...) ...))))is more directly expressed as(syntax->list #'(id ... ...)).Test case
#lang resyntax/test test: "original code should be refactorable to new code" -------------------- #lang racket (define (f stx) (with-syntax ([((a ...) ...) stx]) (apply append (map syntax->list (syntax->list #'((a ...) ...)))))) ==================== #lang racket (define (f stx) (with-syntax ([((a ...) ...) stx]) (syntax->list #'(a ... ...)))) --------------------Additional context
Saw this here in racket/drracket#783.</issue_description>
Comments on the Issue (you are @copilot in this section)
- Fixes jackfirth/resyntax#769
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.