Skip to content

Commit 5f8061c

Browse files
Copilotjackfirth
andcommitted
Add manual-display-to-file refactoring rule
Co-authored-by: jackfirth <[email protected]>
1 parent 360608a commit 5f8061c

File tree

2 files changed

+96
-3
lines changed

2 files changed

+96
-3
lines changed

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,73 @@ header:
1111
----------------------------------------
1212

1313

14+
test: "original code should be refactorable to new code"
15+
--------------------
16+
(define (f path s)
17+
(call-with-output-file path
18+
(lambda (out)
19+
(displayln s out))))
20+
====================
21+
(define (f path s)
22+
(display-lines-to-file (list s) path))
23+
--------------------
24+
25+
26+
test: "call-with-output-file with display should refactor to display-to-file"
27+
--------------------
28+
(define (f path s)
29+
(call-with-output-file path
30+
(lambda (out)
31+
(display s out))))
32+
====================
33+
(define (f path s)
34+
(display-to-file s path))
35+
--------------------
36+
37+
38+
test: "call-with-output-file with displayln and λ should refactor"
39+
--------------------
40+
(define (f path s)
41+
(call-with-output-file path
42+
(λ (out)
43+
(displayln s out))))
44+
====================
45+
(define (f path s)
46+
(display-lines-to-file (list s) path))
47+
--------------------
48+
49+
50+
test: "call-with-output-file with display and λ should refactor"
51+
--------------------
52+
(define (f path s)
53+
(call-with-output-file path
54+
(λ (out)
55+
(display s out))))
56+
====================
57+
(define (f path s)
58+
(display-to-file s path))
59+
--------------------
60+
61+
62+
no-change-test: "should not refactor when port parameter has different name"
63+
--------------------
64+
(define (f path s different-port)
65+
(call-with-output-file path
66+
(lambda (out)
67+
(displayln s different-port))))
68+
--------------------
69+
70+
71+
no-change-test: "should not refactor when there are multiple expressions in lambda body"
72+
--------------------
73+
(define (f path s1 s2)
74+
(call-with-output-file path
75+
(lambda (out)
76+
(displayln s1 out)
77+
(displayln s2 out))))
78+
--------------------
79+
80+
1481
no-change-test:
1582
"should not migrate make-temporary-file without 'directory to make-temporary-directory"
1683
- (make-temporary-file #:copy-from #false)

default-recommendations/file-io-suggestions.rkt

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,38 @@
99
[file-io-suggestions refactoring-suite?]))
1010

1111

12-
(require rebellion/private/static-name
13-
resyntax/base)
12+
(require racket/file
13+
rebellion/private/static-name
14+
resyntax/base
15+
resyntax/default-recommendations/private/lambda-by-any-name
16+
syntax/parse)
1417

1518

1619
;@----------------------------------------------------------------------------------------------------
1720

1821

22+
(define-refactoring-rule manual-display-to-file
23+
#:description
24+
"This `call-with-output-file` expression can be replaced with `display-to-file`."
25+
#:literals (call-with-output-file display display-to-file)
26+
(call-with-output-file path:expr
27+
(_:lambda-by-any-name (out:id)
28+
(display content:expr out-ref:id)))
29+
#:when (free-identifier=? #'out #'out-ref)
30+
(display-to-file content path))
31+
32+
33+
(define-refactoring-rule manual-displayln-to-file
34+
#:description
35+
"This `call-with-output-file` expression can be replaced with `display-lines-to-file`."
36+
#:literals (call-with-output-file displayln display-lines-to-file list)
37+
(call-with-output-file path:expr
38+
(_:lambda-by-any-name (out:id)
39+
(displayln content:expr out-ref:id)))
40+
#:when (free-identifier=? #'out #'out-ref)
41+
(display-lines-to-file (list content) path))
42+
43+
1944
(define-refactoring-suite file-io-suggestions
20-
#:rules ())
45+
#:rules (manual-display-to-file
46+
manual-displayln-to-file))

0 commit comments

Comments
 (0)