Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions default-recommendations/file-io-suggestions-test.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,73 @@ header:
----------------------------------------


test: "original code should be refactorable to new code"
--------------------
(define (f path s)
(call-with-output-file path
(lambda (out)
(displayln s out))))
====================
(define (f path s)
(display-lines-to-file (list s) path))
--------------------


test: "call-with-output-file with display should refactor to display-to-file"
--------------------
(define (f path s)
(call-with-output-file path
(lambda (out)
(display s out))))
====================
(define (f path s)
(display-to-file s path))
--------------------


test: "call-with-output-file with displayln and λ should refactor"
--------------------
(define (f path s)
(call-with-output-file path
(λ (out)
(displayln s out))))
====================
(define (f path s)
(display-lines-to-file (list s) path))
--------------------


test: "call-with-output-file with display and λ should refactor"
--------------------
(define (f path s)
(call-with-output-file path
(λ (out)
(display s out))))
====================
(define (f path s)
(display-to-file s path))
--------------------


no-change-test: "should not refactor when port parameter has different name"
--------------------
(define (f path s different-port)
(call-with-output-file path
(lambda (out)
(displayln s different-port))))
--------------------


no-change-test: "should not refactor when there are multiple expressions in lambda body"
--------------------
(define (f path s1 s2)
(call-with-output-file path
(lambda (out)
(displayln s1 out)
(displayln s2 out))))
--------------------


no-change-test:
"should not migrate make-temporary-file without 'directory to make-temporary-directory"
- (make-temporary-file #:copy-from #false)
32 changes: 29 additions & 3 deletions default-recommendations/file-io-suggestions.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,38 @@
[file-io-suggestions refactoring-suite?]))


(require rebellion/private/static-name
resyntax/base)
(require racket/file
rebellion/private/static-name
resyntax/base
resyntax/default-recommendations/private/lambda-by-any-name
syntax/parse)


;@----------------------------------------------------------------------------------------------------


(define-refactoring-rule manual-display-to-file
#:description
"This `call-with-output-file` expression can be replaced with `display-to-file`."
#:literals (call-with-output-file display display-to-file)
(call-with-output-file path:expr
(_:lambda-by-any-name (out:id)
(display content:expr out-ref:id)))
#:when (free-identifier=? #'out #'out-ref)
(display-to-file content path))


(define-refactoring-rule manual-displayln-to-file
#:description
"This `call-with-output-file` expression can be replaced with `display-lines-to-file`."
#:literals (call-with-output-file displayln display-lines-to-file list)
(call-with-output-file path:expr
(_:lambda-by-any-name (out:id)
(displayln content:expr out-ref:id)))
#:when (free-identifier=? #'out #'out-ref)
(display-lines-to-file (list content) path))


(define-refactoring-suite file-io-suggestions
#:rules ())
#:rules (manual-display-to-file
manual-displayln-to-file))