Skip to content

Commit 9571452

Browse files
Copilotjackfirth
andauthored
Fix error-to-raise-arguments-error to check ~a context (#674)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: jackfirth <[email protected]>
1 parent e5cc4ff commit 9571452

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

default-recommendations/exception-suggestions-test.rkt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,18 @@ test: "error with ~a at end of string"
153153
(define (end-tilde x)
154154
(raise-arguments-error 'end-tilde "value is" "x" x))
155155
--------------------
156+
157+
158+
no-change-test: "error with ~a in quotes should not be refactored"
159+
--------------------
160+
(define (find-output-radio-box label)
161+
(error 'find-output-radio-box "could not find `~a' radio box"
162+
label))
163+
--------------------
164+
165+
166+
no-change-test: "error with ~a in parentheses should not be refactored"
167+
--------------------
168+
(define (f x y)
169+
(error 'f "bad point (~a, ~a)" x y))
170+
--------------------

default-recommendations/exception-suggestions.rkt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,18 @@ conventions."
4343
(error sym:expr message:str arg:id ...+)
4444

4545
#:do [(define message-str (syntax-e (attribute message)))
46-
(define args-list (attribute arg))]
47-
#:when (= (length (regexp-match* #rx"~a" message-str)) (length args-list))
46+
(define args-list (attribute arg))
47+
(define tilde-a-matches (regexp-match-positions* #rx"~a" message-str))]
48+
#:when (= (length tilde-a-matches) (length args-list))
49+
;; Check that all ~a occurrences are surrounded by spaces or at string boundaries
50+
#:when (for/and ([match (in-list tilde-a-matches)])
51+
(define start (car match))
52+
(define end (cdr match))
53+
(define before-ok? (or (= start 0)
54+
(char-whitespace? (string-ref message-str (- start 1)))))
55+
(define after-ok? (or (= end (string-length message-str))
56+
(char-whitespace? (string-ref message-str end))))
57+
(and before-ok? after-ok?))
4858
#:do [(define cleaned-message (string-replace message-str "~a" ""))
4959
;; Clean up extra spaces and trailing punctuation from placeholder removal
5060
(define cleaned-message-normalized

0 commit comments

Comments
 (0)