diff --git a/default-recommendations/file-io-suggestions-test.rkt b/default-recommendations/file-io-suggestions-test.rkt index 0875350..7e93d6f 100644 --- a/default-recommendations/file-io-suggestions-test.rkt +++ b/default-recommendations/file-io-suggestions-test.rkt @@ -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) diff --git a/default-recommendations/file-io-suggestions.rkt b/default-recommendations/file-io-suggestions.rkt index 2e11c3a..b5e3476 100644 --- a/default-recommendations/file-io-suggestions.rkt +++ b/default-recommendations/file-io-suggestions.rkt @@ -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))