Skip to content

Commit 0f321f7

Browse files
Copilotjackfirth
andcommitted
Update CLI and add tests for warning-only rules
Co-authored-by: jackfirth <[email protected]>
1 parent 2e84755 commit 0f321f7

File tree

3 files changed

+91
-24
lines changed

3 files changed

+91
-24
lines changed

cli.rkt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -300,18 +300,20 @@ For help on these, use 'analyze --help' or 'fix --help'."
300300
(match (resyntax-analyze-options-output-format options)
301301
[(== plain-text)
302302
(for ([result (in-list results)])
303-
(define path
304-
(file-source-path
305-
(syntax-replacement-source (refactoring-result-syntax-replacement result))))
303+
(define source (refactoring-result-source result))
304+
(define path (file-source-path source))
306305
(define line (refactoring-result-original-line result))
307306
(define column (refactoring-result-original-column result))
308307
(printf "resyntax: ~a:~a:~a [~a]\n" path line column (refactoring-result-rule-name result))
309308
(printf "\n\n~a\n" (string-indent (refactoring-result-message result) #:amount 2))
310309
(define old-code (refactoring-result-original-code result))
311310
(define new-code (refactoring-result-new-code result))
312-
(printf "\n\n~a\n\n\n~a\n\n\n"
313-
(string-indent (~a old-code) #:amount 2)
314-
(string-indent (~a new-code) #:amount 2)))]
311+
(if new-code
312+
(printf "\n\n~a\n\n\n~a\n\n\n"
313+
(string-indent (~a old-code) #:amount 2)
314+
(string-indent (~a new-code) #:amount 2))
315+
(printf "\n\n~a\n\n\n"
316+
(string-indent (~a old-code) #:amount 2))))]
315317
[(== github-pull-request-review)
316318
(define req (refactoring-results->github-review results #:file-count (hash-count sources)))
317319
(write-json (github-review-request-jsexpr req))]))

private/github.rkt

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,14 @@
9393

9494

9595
(define (refactoring-result->github-review-comment result)
96-
(define path
97-
(file-source-path (syntax-replacement-source (refactoring-result-syntax-replacement result))))
98-
(define replacement (refactoring-result-line-replacement result))
99-
(define body
100-
(format #<<EOS
96+
(cond
97+
[(refactoring-result-has-fix? result)
98+
;; For results with fixes, generate a suggestion comment
99+
(define path
100+
(file-source-path (syntax-replacement-source (refactoring-result-syntax-replacement result))))
101+
(define replacement (refactoring-result-line-replacement result))
102+
(define body
103+
(format #<<EOS
101104
**`~a`:** ~a
102105

103106
```suggestion
@@ -124,19 +127,35 @@
124127
</details>
125128
</details>
126129
EOS
127-
(refactoring-result-rule-name result)
128-
(refactoring-result-message result)
129-
(line-replacement-new-text replacement)
130-
(string-indent (pretty-format replacement) #:amount 2)
131-
(string-indent (pretty-format (refactoring-result-syntax-replacement result))
132-
#:amount 2)))
133-
(github-review-comment
134-
#:path (first (git-path path))
135-
#:body body
136-
#:start-line (line-replacement-start-line replacement)
137-
#:end-line (line-replacement-original-end-line replacement)
138-
#:start-side "RIGHT"
139-
#:end-side "RIGHT"))
130+
(refactoring-result-rule-name result)
131+
(refactoring-result-message result)
132+
(line-replacement-new-text replacement)
133+
(string-indent (pretty-format replacement) #:amount 2)
134+
(string-indent (pretty-format (refactoring-result-syntax-replacement result))
135+
#:amount 2)))
136+
(github-review-comment
137+
#:path (first (git-path path))
138+
#:body body
139+
#:start-line (line-replacement-start-line replacement)
140+
#:end-line (line-replacement-original-end-line replacement)
141+
#:start-side "RIGHT"
142+
#:end-side "RIGHT")]
143+
[else
144+
;; For warning-only results, generate a comment without a suggestion
145+
(define source (refactoring-result-source result))
146+
(define path (file-source-path source))
147+
(define line (refactoring-result-original-line result))
148+
(define body
149+
(format "**`~a`:** ~a"
150+
(refactoring-result-rule-name result)
151+
(refactoring-result-message result)))
152+
(github-review-comment
153+
#:path (first (git-path path))
154+
#:body body
155+
#:start-line line
156+
#:end-line line
157+
#:start-side "RIGHT"
158+
#:end-side "RIGHT")]))
140159

141160

142161
(define branch-ref (getenv "GITHUB_REF"))

private/warning-rule-test.rkt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#lang racket/base
2+
3+
(require racket/list
4+
resyntax/base
5+
resyntax
6+
resyntax/private/refactoring-result
7+
resyntax/private/source
8+
rackunit)
9+
10+
;; Define a warning-only rule that matches any (equal? x y)
11+
(define-refactoring-rule test-warning-rule
12+
#:description "This is a test warning rule for equal?"
13+
#:suggested-fixes 'none
14+
#:literals (equal?)
15+
(equal? x y)
16+
(void))
17+
18+
;; Test that the rule works
19+
(define test-suite (refactoring-suite #:rules (list test-warning-rule)))
20+
21+
(define test-source (string-source "#lang racket/base\n(define a 5)\n(equal? a a)\n"))
22+
23+
(define result-set (resyntax-analyze test-source #:suite test-suite))
24+
25+
(define results (refactoring-result-set-results result-set))
26+
27+
(test-case "warning-only rule produces a result"
28+
(check-equal? (length results) 1 "Should have one result"))
29+
30+
(test-case "warning-only result has no fix"
31+
(define result (first results))
32+
(check-false (refactoring-result-has-fix? result) "Should not have a fix")
33+
(check-false (refactoring-result-syntax-replacement result) "Should have no syntax replacement")
34+
(check-false (refactoring-result-new-code result) "Should have no new code"))
35+
36+
( test-case "warning-only result has message and location"
37+
(define result (first results))
38+
(check-equal? (refactoring-result-message result) "This is a test warning rule for equal?")
39+
(check-equal? (refactoring-result-rule-name result) 'test-warning-rule)
40+
(check-true (positive? (refactoring-result-original-line result))))
41+
42+
(test-case "warning-only result doesn't modify source"
43+
(define updated (refactoring-result-set-updated-source result-set))
44+
(define updated-contents (modified-source-contents updated))
45+
(check-equal? updated-contents (source->string test-source)
46+
"Source should not be modified"))

0 commit comments

Comments
 (0)