generated from jackfirth/racket-package-template
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
make-temporary-directory-migration
rule
Closes #221.
- Loading branch information
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |