resyntax icon indicating copy to clipboard operation
resyntax copied to clipboard

Add flatten-apply-append-syntax-template refactoring rule

Open Copilot opened this issue 2 months ago • 0 comments

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-template in syntax-shortcuts.rkt

    • Detects the apply append (map syntax->list ...) pattern on nested syntax templates
    • Replaces with direct syntax->list on flattened template
    • Uses datum->syntax to construct the flattened pattern, avoiding template ellipsis depth restrictions
  • Test coverage in syntax-shortcuts-test.rkt validates 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 summary

The 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.

Copilot avatar Dec 15 '25 17:12 Copilot