Skip to content

Commit 3a3e50d

Browse files
Copilotjackfirth
andcommitted
Add printf-without-specifiers refactoring rules
Co-authored-by: jackfirth <[email protected]>
1 parent a8e8b47 commit 3a3e50d

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

default-recommendations/console-io-suggestions-test.rkt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,27 @@ test: "should suggest 'any linemode with read-line when linemode and port not sp
2626
(define (foo)
2727
(read-line (current-input-port) 'any))
2828
----------------------------------------
29+
30+
31+
test: "printf to display"
32+
----------------------------------------
33+
(printf "foo")
34+
========================================
35+
(display "foo")
36+
----------------------------------------
37+
38+
39+
test: "printf to displayln"
40+
----------------------------------------
41+
(printf "foo\nbar\n")
42+
========================================
43+
(displayln "foo\nbar")
44+
----------------------------------------
45+
46+
47+
test: "printf to newline"
48+
----------------------------------------
49+
(printf "\n")
50+
========================================
51+
(newline)
52+
----------------------------------------

default-recommendations/console-io-suggestions.rkt

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
(require racket/file
1313
racket/list
14+
racket/string
1415
rebellion/private/static-name
1516
resyntax/base
1617
syntax/parse)
@@ -28,5 +29,34 @@
2829
(read-line (~? port (current-input-port)) 'any))
2930

3031

32+
(define-refactoring-rule printf-newline-only
33+
#:description "The `newline` function can be used to print a single newline character."
34+
#:literals (printf)
35+
(printf "\n")
36+
(newline))
37+
38+
39+
(define-refactoring-rule printf-ending-with-newline
40+
#:description
41+
"When `printf` is used with a single string argument ending in a newline, use `displayln`."
42+
#:literals (printf)
43+
(printf s:str)
44+
#:when (string-suffix? (syntax-e #'s) "\n")
45+
#:when (not (equal? (syntax-e #'s) "\n"))
46+
#:with stripped (substring (syntax-e #'s) 0 (- (string-length (syntax-e #'s)) 1))
47+
(displayln stripped))
48+
49+
50+
(define-refactoring-rule printf-without-newline
51+
#:description "When `printf` is used with a single string argument, use `display`."
52+
#:literals (printf)
53+
(printf s:str)
54+
#:when (not (string-suffix? (syntax-e #'s) "\n"))
55+
(display s))
56+
57+
3158
(define-refactoring-suite console-io-suggestions
32-
#:rules (read-line-any))
59+
#:rules (printf-newline-only
60+
printf-ending-with-newline
61+
printf-without-newline
62+
read-line-any))

0 commit comments

Comments
 (0)