Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 15, 2025

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)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Add new rule: flatten-apply-append-syntax-template Add flatten-apply-append-syntax-template refactoring rule Dec 15, 2025
Copilot AI requested a review from jackfirth December 15, 2025 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

New rule: flatten-apply-append-syntax-template

2 participants