From 170d369411290efdd3797296ffa0e483b7bfb17e Mon Sep 17 00:00:00 2001 From: Jack Firth Date: Wed, 11 Sep 2024 20:33:08 -0700 Subject: [PATCH] Add `make-temporary-directory-migration` rule Closes #221. --- default-recommendations.rkt | 3 + .../file-io-suggestions-test.rkt | 31 ++++++++ .../file-io-suggestions.rkt | 71 +++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 default-recommendations/file-io-suggestions-test.rkt create mode 100644 default-recommendations/file-io-suggestions.rkt diff --git a/default-recommendations.rkt b/default-recommendations.rkt index 6255186..95dc5d5 100644 --- a/default-recommendations.rkt +++ b/default-recommendations.rkt @@ -10,6 +10,7 @@ resyntax/default-recommendations/conditional-shortcuts resyntax/default-recommendations/contract-shortcuts resyntax/default-recommendations/definition-shortcuts + resyntax/default-recommendations/file-io-suggestions resyntax/default-recommendations/for-loop-shortcuts resyntax/default-recommendations/function-definition-shortcuts resyntax/default-recommendations/function-shortcuts @@ -37,6 +38,7 @@ resyntax/default-recommendations/conditional-shortcuts resyntax/default-recommendations/contract-shortcuts resyntax/default-recommendations/definition-shortcuts + resyntax/default-recommendations/file-io-suggestions resyntax/default-recommendations/for-loop-shortcuts resyntax/default-recommendations/function-definition-shortcuts resyntax/default-recommendations/function-shortcuts @@ -69,6 +71,7 @@ (refactoring-suite-rules conditional-shortcuts) (refactoring-suite-rules contract-shortcuts) (refactoring-suite-rules definition-shortcuts) + (refactoring-suite-rules file-io-suggestions) (refactoring-suite-rules for-loop-shortcuts) (refactoring-suite-rules function-definition-shortcuts) (refactoring-suite-rules function-shortcuts) diff --git a/default-recommendations/file-io-suggestions-test.rkt b/default-recommendations/file-io-suggestions-test.rkt new file mode 100644 index 0000000..4b30d69 --- /dev/null +++ b/default-recommendations/file-io-suggestions-test.rkt @@ -0,0 +1,31 @@ +#lang resyntax/testing/refactoring-test + + +require: resyntax/default-recommendations file-io-suggestions + + +header: +---------------------------------------- +#lang racket/base +(require racket/file) +---------------------------------------- + + +test: "should migrate make-temporary-file with 'directory to make-temporary-directory" +- (void (make-temporary-file #:copy-from 'directory)) +- (void (make-temporary-directory)) + + +test: "should migrate make-temporary-file with template and 'directory to make-temporary-directory" +- (void (make-temporary-file "footmp~a" #:copy-from 'directory)) +- (void (make-temporary-file "footmp~a" 'directory)) +- (void (make-temporary-directory "footmp~a")) + + +test: "should migrate make-temporary-file with base-dir and 'directory to make-temporary-directory" +- (void (make-temporary-file #:base-dir #false #:copy-from 'directory)) +- (void (make-temporary-directory #:base-dir #false)) + + +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 new file mode 100644 index 0000000..0116c91 --- /dev/null +++ b/default-recommendations/file-io-suggestions.rkt @@ -0,0 +1,71 @@ +#lang racket/base + + +(require racket/contract/base) + + +(provide + (contract-out + [file-io-suggestions refactoring-suite?])) + + +(require racket/file + racket/list + rebellion/private/static-name + resyntax/refactoring-rule + resyntax/refactoring-suite + syntax/parse) + + +;@---------------------------------------------------------------------------------------------------- + + +(define-splicing-syntax-class function-call-argument + #:attributes (keyword expr) + (pattern (~seq expr:expr) #:attr keyword #false) + (pattern (~seq keyword:keyword expr:expr))) + + +(define-splicing-syntax-class function-call-arguments + #:attributes ([positional 1] keyword) + (pattern (~seq arg:function-call-argument ...) + #:cut + #:with (positional ...) + (for/list ([expr (in-list (attribute arg.expr))] + [keyword (in-list (attribute arg.keyword))] + #:unless keyword) + expr) + + #:attr keyword + (for/hasheq ([expr (in-list (attribute arg.expr))] + [keyword (in-list (attribute arg.keyword))] + #:when keyword) + (values (syntax-e keyword) expr)))) + + +(define-refactoring-rule make-temporary-directory-migration + #:description "Use `make-temporary-directory` to make directories instead of `make-temporary-file`." + #:literals (make-temporary-file) + #:datum-literals (quote directory) + + (make-temporary-file args:function-call-arguments) + #:with 'directory + (or (and (>= (length (attribute args.positional)) 2) + (second (attribute args.positional))) + (hash-ref (attribute args.keyword) '#:copy-from #false)) + #:cut + #:attr template-arg + (and (not (empty? (attribute args.positional))) + (first (attribute args.positional))) + #:attr base-dir-arg + (or (and (>= (length (attribute args.positional)) 3) + (third (attribute args.positional))) + (hash-ref (attribute args.keyword) '#:base-dir #false)) + + (make-temporary-directory (~? template-arg) (~? (~@ #:base-dir base-dir-arg)))) + + +(define file-io-suggestions + (refactoring-suite + #:name (name file-io-suggestions) + #:rules (list make-temporary-directory-migration)))