Skip to content

Commit ab71148

Browse files
Automated Resyntax fixes (#515)
* Fix 4 occurrences of `if-begin-to-cond` Using `cond` instead of `if` here makes `begin` unnecessary * Fix 2 occurrences of `or-hash-ref-set!-to-hash-ref!` This expression can be replaced with a simpler, equivalent `hash-ref!` expression. * Fix 2 occurrences of `if-let-to-cond` `cond` with internal definitions is preferred over `if` with `let`, to reduce nesting * Fix 2 occurrences of `map-to-for` This `map` operation can be replaced with a `for/list` loop. * Fix 1 occurrence of `ormap-to-for/or` This `ormap` operation can be replaced with a `for/or` loop. * Fix 1 occurrence of `when-expression-in-for-loop-to-when-keyword` Use the `#:when` keyword instead of `when` to reduce loop body indentation. * Fix 3 occurrences of `provide/contract-to-contract-out` The `provide/contract` form is a legacy form made obsolete by `contract-out`. * Fix 2 occurrences of `let-to-define` Internal definitions are recommended instead of `let` expressions, to reduce nesting. * Fix 2 occurrences of `quasiquote-to-list` This quasiquotation is equialent to a simple `list` call. * Fix 1 occurrence of `single-clause-match-to-match-define` This `match` expression can be simplified using `match-define`. --------- Co-authored-by: resyntax-ci[bot] <181813515+resyntax-ci[bot]@users.noreply.github.com>
1 parent 5abd0c2 commit ab71148

File tree

7 files changed

+140
-162
lines changed

7 files changed

+140
-162
lines changed

scribble-html-lib/scribble/html/html.rkt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,11 @@
186186
(define-values [attrs body] (attributes+body args))
187187
(make-element
188188
'script attrs
189-
`("\n" ,(set-prefix 0 (apply cdata #:line-prefix "//" body)) "\n")))
189+
(list "\n" (set-prefix 0 (apply cdata #:line-prefix "//" body)) "\n")))
190190
(provide style/inline)
191191
(define (style/inline . args)
192192
(define-values [attrs body] (attributes+body args))
193-
(make-element 'style attrs `("\n" ,body "\n")))
193+
(make-element 'style attrs (list "\n" body "\n")))
194194

195195
;; ----------------------------------------------------------------------------
196196
;; Entities

scribble-html-lib/scribble/html/resource.rkt

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,10 @@
5757
(set! cached-roots
5858
(cons roots
5959
(and (list? roots) (pair? roots)
60-
(map (lambda (root)
61-
(list* (regexp-match* #rx"[^/]+" (car root))
62-
(regexp-replace #rx"/$" (cadr root) "")
63-
(cddr root)))
64-
roots)))))
60+
(for/list ([root (in-list roots)])
61+
(list* (regexp-match* #rx"[^/]+" (car root))
62+
(regexp-replace #rx"/$" (cadr root) "")
63+
(cddr root)))))))
6564
(cdr cached-roots))
6665

6766
;; a utility for relative paths, taking the above `default-file' and
@@ -70,22 +69,23 @@
7069
(define file* (if (equal? file default-file) "" file))
7170
(define roots (current-url-roots))
7271
(define (find-root path mode)
73-
(ormap (lambda (root+url+flags)
74-
(let loop ([r (car root+url+flags)] [p path])
75-
(if (pair? r)
76-
(and (pair? p) (equal? (car p) (car r))
77-
(loop (cdr r) (cdr p)))
78-
(case mode
79-
[(get-path) `(,(cadr root+url+flags)
80-
,@p
81-
,(if (and (equal? file* "")
82-
(memq 'index (cddr root+url+flags)))
83-
default-file
84-
file*))]
85-
[(get-abs-or-true)
86-
(if (memq 'abs (cddr root+url+flags)) `("" ,@p) #t)]
87-
[else (error 'relativize "internal error: ~e" mode)]))))
88-
roots))
72+
(for/or ([root+url+flags (in-list roots)])
73+
(let loop ([r (car root+url+flags)]
74+
[p path])
75+
(if (pair? r)
76+
(and (pair? p) (equal? (car p) (car r)) (loop (cdr r) (cdr p)))
77+
(case mode
78+
[(get-path)
79+
`(,(cadr root+url+flags) ,@p
80+
,(if (and (equal? file* "")
81+
(memq 'index (cddr root+url+flags)))
82+
default-file
83+
file*))]
84+
[(get-abs-or-true)
85+
(if (memq 'abs (cddr root+url+flags))
86+
`("" ,@p)
87+
#t)]
88+
[else (error 'relativize "internal error: ~e" mode)])))))
8989
(define result
9090
(let loop ([t tgtdir] [c curdir] [pfx '()])
9191
(cond
@@ -165,9 +165,11 @@
165165
(define t (make-hash))
166166
(define-syntax-rule (S body) (call-with-semaphore s (lambda () body)))
167167
(values (lambda (path renderer)
168-
(S (if (hash-ref t path #f)
169-
(error 'resource "path used for two resources: ~e" path)
170-
(begin (hash-set! t path #t) (set! l (cons renderer l))))))
168+
(S (cond
169+
[(hash-ref t path #f) (error 'resource "path used for two resources: ~e" path)]
170+
[else
171+
(hash-set! t path #t)
172+
(set! l (cons renderer l))])))
171173
(lambda () (S (begin0 (reverse l) (set! l '())))))))
172174

173175
;; `#:exists' determines what happens when the render destination exists, it
@@ -180,32 +182,33 @@
180182
(define (resource path0 renderer #:exists [exists 'delete-file])
181183
(define (bad reason) (error 'resource "bad path, ~a: ~e" reason path0))
182184
(unless (string? path0) (bad "must be a string"))
183-
(for ([x (in-list '([#rx"^/" "must be relative"]
184-
[#rx"//" "must not have empty elements"]
185-
[#rx"(?:^|/)[.][.]?(?:/|$)"
186-
"must not contain `.' or `..'"]))])
187-
(when (regexp-match? (car x) path0) (bad (cadr x))))
185+
(for ([x (in-list '([#rx"^/" "must be relative"] [#rx"//" "must not have empty elements"]
186+
[#rx"(?:^|/)[.][.]?(?:/|$)"
187+
"must not contain `.' or `..'"]))]
188+
#:when (regexp-match? (car x) path0))
189+
(bad (cadr x)))
188190
(define path (regexp-replace #rx"(?<=^|/)$" path0 default-file))
189191
(define-values [dirpathlist filename]
190192
(let-values ([(l r) (split-at-right (regexp-split #rx"/" path) 1)])
191193
(values l (car r))))
192194
(define (render)
193195
(let loop ([ps dirpathlist])
194-
(if (pair? ps)
195-
(begin (unless (directory-exists? (car ps))
196-
(if (or (file-exists? (car ps)) (link-exists? (car ps)))
197-
(bad "exists as a file/link")
198-
(make-directory (car ps))))
199-
(parameterize ([current-directory (car ps)])
200-
(loop (cdr ps))))
201-
(begin (cond [(not exists)] ; do nothing
202-
[(or (file-exists? filename) (link-exists? filename))
203-
(delete-file filename)]
204-
[(directory-exists? filename)
205-
(bad "exists as directory")])
206-
(parameterize ([rendered-dirpath dirpathlist])
207-
(printf " ~a\n" path)
208-
(renderer filename))))))
196+
(cond
197+
[(pair? ps)
198+
(unless (directory-exists? (car ps))
199+
(if (or (file-exists? (car ps)) (link-exists? (car ps)))
200+
(bad "exists as a file/link")
201+
(make-directory (car ps))))
202+
(parameterize ([current-directory (car ps)])
203+
(loop (cdr ps)))]
204+
[else
205+
(cond
206+
[(not exists)] ; do nothing
207+
[(or (file-exists? filename) (link-exists? filename)) (delete-file filename)]
208+
[(directory-exists? filename) (bad "exists as directory")])
209+
(parameterize ([rendered-dirpath dirpathlist])
210+
(printf " ~a\n" path)
211+
(renderer filename))])))
209212
(define absolute-url
210213
(lazy (define url (relativize filename dirpathlist '()))
211214
(if (url-roots)

scribble-html-lib/scribble/html/xml.rkt

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,14 @@
106106
;; null body means a lone tag, tags that should always have a closer will
107107
;; have a '(#f) as their body (see below)
108108
(list (with-writer #f "<" tag)
109-
(map (lambda (attr)
110-
(define name (car attr))
111-
(define val (cdr attr))
112-
(cond [(not val) #f]
113-
;; #t means just mention the attribute
114-
[(eq? #t val) (with-writer #f (list " " name))]
115-
[else (list (with-writer #f (list " " name "=\""))
116-
val
117-
(with-writer #f "\""))]))
118-
attrs)
109+
(for/list ([attr (in-list attrs)])
110+
(define name (car attr))
111+
(define val (cdr attr))
112+
(cond
113+
[(not val) #f]
114+
;; #t means just mention the attribute
115+
[(eq? #t val) (with-writer #f (list " " name))]
116+
[else (list (with-writer #f (list " " name "=\"")) val (with-writer #f "\""))]))
119117
(if (null? body)
120118
(with-writer #f " />")
121119
(list (with-writer #f ">")

scribble-lib/scribble/base.rkt

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,22 @@
2929
#:rest (listof pre-content?)
3030
part-start?))
3131

32-
(provide/contract
33-
[title (->* ()
34-
(#:tag (or/c #f string? (listof string?))
35-
#:tag-prefix (or/c #f string? module-path? hash?)
36-
#:style (or/c style? string? symbol? (listof symbol?) #f)
37-
#:version (or/c string? #f)
38-
#:date (or/c string? #f)
39-
#:index-extras desc-extras/c)
40-
#:rest (listof pre-content?)
41-
title-decl?)]
42-
[section (title-like-contract)]
43-
[subsection (title-like-contract)]
44-
[subsubsection (title-like-contract)]
45-
[subsubsub*section (->* ()
46-
(#:tag (or/c #f string? (listof string?)))
47-
#:rest (listof pre-content?)
48-
block?)])
32+
(provide (contract-out
33+
[title
34+
(->* ()
35+
(#:tag (or/c #f string? (listof string?))
36+
#:tag-prefix (or/c #f string? module-path? hash?)
37+
#:style (or/c style? string? symbol? (listof symbol?) #f)
38+
#:version (or/c string? #f)
39+
#:date (or/c string? #f)
40+
#:index-extras desc-extras/c)
41+
#:rest (listof pre-content?)
42+
title-decl?)]
43+
[section (title-like-contract)]
44+
[subsection (title-like-contract)]
45+
[subsubsection (title-like-contract)]
46+
[subsubsub*section
47+
(->* () (#:tag (or/c #f string? (listof string?))) #:rest (listof pre-content?) block?)]))
4948
(provide include-section)
5049

5150
(define (title #:tag [tag #f] #:tag-prefix [prefix #f] #:style [style plain]
@@ -131,9 +130,8 @@
131130

132131
;; ----------------------------------------
133132

134-
(provide/contract
135-
[author (->* (content?) () #:rest (listof content?) block?)]
136-
[author+email (->* (content? string?) (#:obfuscate? any/c) element?)])
133+
(provide (contract-out [author (->* (content?) () #:rest (listof content?) block?)]
134+
[author+email (->* (content? string?) (#:obfuscate? any/c) element?)]))
137135

138136
(define (author . auths)
139137
(make-paragraph
@@ -142,10 +140,9 @@
142140
(case (length auths)
143141
[(1) auths]
144142
[(2) (list (car auths) nl "and " (cadr auths))]
145-
[else (let ([r (reverse auths)])
146-
(append (add-between (reverse (cdr r))
147-
(make-element #f (list "," nl)))
148-
(list "," nl "and " (car r))))]))))
143+
[else (define r (reverse auths))
144+
(append (add-between (reverse (cdr r)) (make-element #f (list "," nl)))
145+
(list "," nl "and " (car r)))]))))
149146

150147
(define (author+email name email #:obfuscate? [obfuscate? #f])
151148
(make-element #f
@@ -173,15 +170,10 @@
173170

174171
(provide items/c)
175172

176-
(provide/contract
177-
[itemlist (->* ()
178-
(#:style (or/c style? string? symbol? #f))
179-
#:rest (listof items/c)
180-
itemization?)]
181-
[item (->* ()
182-
()
183-
#:rest (listof pre-flow?)
184-
item?)])
173+
(provide (contract-out
174+
[itemlist
175+
(->* () (#:style (or/c style? string? symbol? #f)) #:rest (listof items/c) itemization?)]
176+
[item (->* () () #:rest (listof pre-flow?) item?)]))
185177
(provide/contract
186178
[item? (any/c . -> . boolean?)])
187179

scribble-lib/scribble/tag.rkt

Lines changed: 35 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -48,58 +48,45 @@
4848
(let ([v (if (list? v)
4949
(map intern-taglet v)
5050
(datum-intern-literal v))])
51-
(if (or (string? v)
52-
(bytes? v)
53-
(list? v))
54-
(let ([b (hash-ref interned v #f)])
55-
(if b
56-
(or (weak-box-value b)
57-
;; just in case the value is GCed before we extract it:
58-
(intern-taglet v))
59-
(begin
60-
(hash-set! interned v (make-weak-box v))
61-
v)))
62-
v)))
51+
(cond
52+
[(or (string? v) (bytes? v) (list? v))
53+
(define b (hash-ref interned v #f))
54+
(if b
55+
(or (weak-box-value b)
56+
;; just in case the value is GCed before we extract it:
57+
(intern-taglet v))
58+
(begin
59+
(hash-set! interned v (make-weak-box v))
60+
v))]
61+
[else v])))
6362

6463
(define (do-module-path-index->taglet mod)
6564
;; Derive the name from the module path:
66-
(let ([p (collapse-module-path-index
67-
mod
68-
(lambda () (build-path (current-directory) "dummy")))])
69-
(if (path? p)
70-
;; If we got a path back anyway, then it's best to use the resolved
71-
;; name; if the current directory has changed since we
72-
;; the path-index was resolved, then p might not be right. Also,
73-
;; the resolved path might be a symbol instead of a path.
74-
(let ([rp (resolved-module-path-name
75-
(module-path-index-resolve mod))])
76-
(if (path? rp)
77-
(intern-taglet
78-
(path->collects-relative rp))
79-
rp))
80-
(let ([p (if (and (pair? p)
81-
(eq? (car p) 'planet))
82-
;; Normalize planet verion number based on current
83-
;; linking:
84-
(let-values ([(path pkg)
85-
(get-planet-module-path/pkg p #f #f)])
86-
(list* 'planet
87-
(cadr p)
88-
(list (car (caddr p))
89-
(cadr (caddr p))
90-
(pkg-maj pkg)
91-
(pkg-min pkg))
92-
(cdddr p)))
93-
;; Otherwise the path is fully normalized:
94-
p)])
95-
(intern-taglet p)))))
65+
(define p (collapse-module-path-index mod (lambda () (build-path (current-directory) "dummy"))))
66+
(if (path? p)
67+
;; If we got a path back anyway, then it's best to use the resolved
68+
;; name; if the current directory has changed since we
69+
;; the path-index was resolved, then p might not be right. Also,
70+
;; the resolved path might be a symbol instead of a path.
71+
(let ([rp (resolved-module-path-name (module-path-index-resolve mod))])
72+
(if (path? rp)
73+
(intern-taglet (path->collects-relative rp))
74+
rp))
75+
(let ([p (if (and (pair? p) (eq? (car p) 'planet))
76+
;; Normalize planet verion number based on current
77+
;; linking:
78+
(let-values ([(path pkg) (get-planet-module-path/pkg p #f #f)])
79+
(list* 'planet
80+
(cadr p)
81+
(list (car (caddr p)) (cadr (caddr p)) (pkg-maj pkg) (pkg-min pkg))
82+
(cdddr p)))
83+
;; Otherwise the path is fully normalized:
84+
p)])
85+
(intern-taglet p))))
9686

9787
(define collapsed (make-weak-hasheq))
9888
(define (module-path-index->taglet mod)
99-
(or (hash-ref collapsed mod #f)
100-
(let ([v (do-module-path-index->taglet mod)])
101-
(hash-set! collapsed mod v)
102-
v)))
89+
(hash-ref! collapsed mod (λ () (do-module-path-index->taglet mod))))
10390

10491
(define (module-path-prefix->string p)
10592
(datum-intern-literal
@@ -123,9 +110,8 @@
123110
(define (definition-tag->class/interface-tag t) (cons 'class/intf (cdr t)))
124111
(define (class/interface-tag->constructor-tag t) (cons 'constructor (cdr t)))
125112
(define (get-class/interface-and-method meth-tag)
126-
(match meth-tag
127-
[`(meth ((,_ ,class/interface) ,method))
128-
(values class/interface method)]))
113+
(match-define `(meth ((,_ ,class/interface) ,method)) meth-tag)
114+
(values class/interface method))
129115
(define (definition-tag? x) (and (tag? x) (equal? (car x) 'def)))
130116
(define (class/interface-tag? x) (and (tag? x) (equal? (car x) 'class/intf)))
131117
(define (method-tag? x) (and (tag? x) (equal? (car x) 'meth)))

scribble-text-lib/scribble/text/output.rkt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,12 @@
112112
(cond
113113
[(pair? nls)
114114
(define nl (car nls))
115-
(if (regexp-match? #rx"^ *$" x start (car nl))
116-
(newline p) ; only spaces before the end of the line
117-
(begin
118-
(output-pfx col pfx lpfx)
119-
(write x p start (cdr nl))))
115+
(cond
116+
[(regexp-match? #rx"^ *$" x start (car nl))
117+
(newline p)] ; only spaces before the end of the line
118+
[else
119+
(output-pfx col pfx lpfx)
120+
(write x p start (cdr nl))])
120121
(loop (cdr nl) (cdr nls) 0 0)]
121122
;; last substring from here (always set lpfx state when done)
122123
[(start . = . len) (set-mcdr! pfxs lpfx)]
@@ -279,10 +280,7 @@
279280
[(eq? p (car last)) (cdr last)]
280281
[else
281282
(define s
282-
(or (hash-ref t p #f)
283-
(let ([s (mcons 0 0)])
284-
(hash-set! t p s)
285-
s)))
283+
(hash-ref! t p (λ () (mcons 0 0))))
286284
(set! last (cons p s))
287285
s]))))
288286

0 commit comments

Comments
 (0)