Skip to content
Merged
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
80 changes: 80 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,86 @@ header:
----------------------------------------


test: "call-with-output-file displaying a value refactorable to display-to-file"
----------------------------------------
(define (f path s)
(call-with-output-file path
(lambda (out)
(display s out))))
========================================
(define (f path s)
(call-with-output-file path
(λ (out)
(display s out))))
========================================
(define (f path s)
(display-to-file s path))
----------------------------------------


test: "call-with-output-file displaying a line refactorable to display-lines-to-file"
----------------------------------------
(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 displaying multiple lines refactorable to display-lines-to-file"
----------------------------------------
(define (f path a b)
(call-with-output-file path
(lambda (out)
(displayln a out)
(displayln b out))))
========================================
(define (f path a b)
(display-lines-to-file (list a b) path))
----------------------------------------


no-change-test: "call-with-output-file with keyword arguments not refactorable"
----------------------------------------
(define (f path s)
(call-with-output-file path
(lambda (out)
(displayln s out))
#:exists 'replace))
----------------------------------------


no-change-test: "call-with-output-file displaying to a different port not refactorable"
----------------------------------------
(define (f path s other-port)
(call-with-output-file path
(lambda (out)
(displayln s other-port))))
----------------------------------------


no-change-test: "call-with-output-file with displayed value depending on the port not refactorable"
----------------------------------------
(define (f path)
(call-with-output-file path
(lambda (out)
(displayln (object-name out) out))))
----------------------------------------


no-change-test: "call-with-output-file with non-display body forms not refactorable"
----------------------------------------
(define (f path s)
(call-with-output-file path
(lambda (out)
(displayln s out)
(flush-output out))))
----------------------------------------


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


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


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


(define-refactoring-rule call-with-output-file-to-display-to-file
#:description
"This use of `call-with-output-file` can be replaced with a simpler call to `display-to-file`."
#:literals (call-with-output-file display)
(call-with-output-file path:expr
(_:lambda-by-any-name (out:id) (display v:expr out-use:id)))
#:when (free-identifier=? (attribute out-use) (attribute out))
#:when (not (set-member? (syntax-free-identifiers (attribute v)) (attribute out)))
(display-to-file v path))


(define-refactoring-rule call-with-output-file-to-display-lines-to-file
#:description
"This use of `call-with-output-file` can be replaced with a simpler call to\
`display-lines-to-file`."
#:literals (call-with-output-file displayln)
(call-with-output-file path:expr
(_:lambda-by-any-name (out:id) (displayln v:expr out-use:id) ...+))
#:when (for/and ([out-use-id (in-list (attribute out-use))])
(free-identifier=? out-use-id (attribute out)))
#:when (not (set-member? (syntax-free-identifiers #'(v ...)) (attribute out)))
(display-lines-to-file (list v ...) path))


(define-refactoring-suite file-io-suggestions
#:rules ())
#:rules (call-with-output-file-to-display-lines-to-file
call-with-output-file-to-display-to-file))
Loading