Skip to content

Commit

Permalink
Add make-temporary-directory-migration rule
Browse files Browse the repository at this point in the history
Closes #221.
  • Loading branch information
jackfirth committed Sep 12, 2024
1 parent 05e5d19 commit 170d369
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
3 changes: 3 additions & 0 deletions default-recommendations.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
31 changes: 31 additions & 0 deletions default-recommendations/file-io-suggestions-test.rkt
Original file line number Diff line number Diff line change
@@ -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)
71 changes: 71 additions & 0 deletions default-recommendations/file-io-suggestions.rkt
Original file line number Diff line number Diff line change
@@ -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)))

0 comments on commit 170d369

Please sign in to comment.