From 5709ca0cbd3eb9a5d03df3a4b32cd9b01fae4db1 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 15 Dec 2025 17:05:32 +0000 Subject: [PATCH 1/3] Initial plan From 3cc4a3daf67d61a58e1b9fcbe291afdb034a6eb8 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 15 Dec 2025 17:26:41 +0000 Subject: [PATCH 2/3] Add flatten-apply-append-syntax-template refactoring rule Co-authored-by: jackfirth <8175575+jackfirth@users.noreply.github.com> --- .../syntax-shortcuts-test.rkt | 15 +++++++++++++ default-recommendations/syntax-shortcuts.rkt | 22 ++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/default-recommendations/syntax-shortcuts-test.rkt b/default-recommendations/syntax-shortcuts-test.rkt index 147ffdd3..3cc1ced9 100644 --- a/default-recommendations/syntax-shortcuts-test.rkt +++ b/default-recommendations/syntax-shortcuts-test.rkt @@ -53,3 +53,18 @@ test: "making a symbol with format from a keyword can be simplified to format-sy test: "making a symbol with format from a keyword syntax object can be simplified to format-symbol" - (string->symbol (format "make-~a" (keyword->string (syntax-e #'#:foo)))) - (format-symbol "make-~a" #'#:foo) + + +test: "flattening nested syntax templates with apply append can be simplified" +-------------------- +(require racket/syntax) +(define (f stx) + (with-syntax ([((a ...) ...) stx]) + (apply append + (map syntax->list (syntax->list #'((a ...) ...)))))) +==================== +(require racket/syntax) +(define (f stx) + (with-syntax ([((a ...) ...) stx]) + (syntax->list (syntax (a ... ...))))) +-------------------- diff --git a/default-recommendations/syntax-shortcuts.rkt b/default-recommendations/syntax-shortcuts.rkt index 2ff8ce1e..21d118e2 100644 --- a/default-recommendations/syntax-shortcuts.rkt +++ b/default-recommendations/syntax-shortcuts.rkt @@ -11,6 +11,7 @@ (require racket/string racket/syntax + racket/list rebellion/private/static-name resyntax/base syntax/parse) @@ -103,6 +104,25 @@ (format-symbol template (~replacement arg.simplified #:original arg) ...)) +(define-refactoring-rule flatten-apply-append-syntax-template + #:description + "Flattening nested syntax templates with `apply append` and `map syntax->list` can be simplified\ + by using a single `syntax->list` call on a flattened template." + #:literals (apply append map syntax->list syntax ...) + + (apply append (map syntax->list (syntax->list (syntax ((inner ...) ...))))) + + #:with flattened-template + (let* ([inner-attrs (attribute inner)] + ; Wrap in list if it's not already a list + [inner-list (if (list? inner-attrs) inner-attrs (list inner-attrs))] + [ellipsis-sym (datum->syntax #'here '...)]) + (datum->syntax #'here (append inner-list (list ellipsis-sym ellipsis-sym)))) + + (syntax->list #'flattened-template)) + + (define-refactoring-suite syntax-shortcuts - #:rules (format-string-to-format-symbol + #:rules (flatten-apply-append-syntax-template + format-string-to-format-symbol syntax-e-in-format-id-unnecessary)) From 8dfbc9a94b9e45fe5277bf850064d4b99630e405 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 15 Dec 2025 17:31:46 +0000 Subject: [PATCH 3/3] Improve code clarity in flatten-apply-append-syntax-template rule Co-authored-by: jackfirth <8175575+jackfirth@users.noreply.github.com> --- default-recommendations/syntax-shortcuts.rkt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/default-recommendations/syntax-shortcuts.rkt b/default-recommendations/syntax-shortcuts.rkt index 21d118e2..7b4e29a5 100644 --- a/default-recommendations/syntax-shortcuts.rkt +++ b/default-recommendations/syntax-shortcuts.rkt @@ -114,10 +114,13 @@ #:with flattened-template (let* ([inner-attrs (attribute inner)] - ; Wrap in list if it's not already a list + ; When matching ((inner ...) ...), the inner attribute contains the elements from + ; within each nested list. If there's only one element, inner-attrs will be a single + ; syntax object, otherwise it's a list. We need a list for appending the ellipses. [inner-list (if (list? inner-attrs) inner-attrs (list inner-attrs))] [ellipsis-sym (datum->syntax #'here '...)]) - (datum->syntax #'here (append inner-list (list ellipsis-sym ellipsis-sym)))) + ; Construct (inner-elements ... ...) by appending two ellipsis symbols + (datum->syntax #'here (append inner-list (make-list 2 ellipsis-sym)))) (syntax->list #'flattened-template))