generated from jackfirth/racket-package-template
-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.rkt
388 lines (324 loc) · 16.1 KB
/
main.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#lang racket/base
(require racket/contract/base)
(provide
(contract-out
[resyntax-analysis? (-> any/c boolean?)]
[resyntax-analysis-all-results
(-> resyntax-analysis?
(listof (hash/c source? refactoring-result-set? #:flat? #true #:immutable #true)))]
[resyntax-analysis-final-sources (-> resyntax-analysis? (listof modified-source?))]
[resyntax-analysis-total-fixes (-> resyntax-analysis? exact-nonnegative-integer?)]
[resyntax-analysis-total-sources-modified (-> resyntax-analysis? exact-nonnegative-integer?)]
[resyntax-analysis-rules-applied (-> resyntax-analysis? multiset?)]
[resyntax-analysis-write-file-changes! (-> resyntax-analysis? void?)]
[resyntax-analysis-commit-fixes! (-> resyntax-analysis? void?)]
[resyntax-analyze
(->* (source?) (#:suite refactoring-suite? #:lines range-set?) refactoring-result-set?)]
[resyntax-analyze-all
(->* ((hash/c source? range-set? #:flat? #true))
(#:suite refactoring-suite?
#:max-fixes (or/c exact-nonnegative-integer? +inf.0)
#:max-passes exact-nonnegative-integer?
#:max-modified-sources (or/c exact-nonnegative-integer? +inf.0)
#:max-modified-lines (or/c exact-nonnegative-integer? +inf.0))
resyntax-analysis?)]
[refactor! (-> (sequence/c refactoring-result?) void?)]))
(require fancy-app
guard
racket/file
racket/match
racket/sequence
rebellion/base/comparator
rebellion/base/option
rebellion/base/range
rebellion/collection/entry
rebellion/collection/hash
rebellion/collection/list
rebellion/collection/multiset
resyntax/private/commit
rebellion/collection/range-set
rebellion/streaming/reducer
rebellion/streaming/transducer
rebellion/type/record
resyntax/base
resyntax/default-recommendations
resyntax/private/comment-reader
resyntax/private/git
resyntax/private/limiting
resyntax/private/line-replacement
resyntax/private/logger
resyntax/private/refactoring-result
resyntax/private/source
resyntax/private/string-indent
resyntax/private/string-replacement
resyntax/private/syntax-range
resyntax/private/syntax-replacement
(except-in racket/list range)
(submod resyntax/base private))
(module+ test
(require racket/list
rackunit
(submod "..")))
;@----------------------------------------------------------------------------------------------------
(define-record-type resyntax-analysis (all-results) #:omit-root-binding)
(define (resyntax-analysis #:all-results all-results)
(constructor:resyntax-analysis #:all-results (sequence->list all-results)))
(define (resyntax-analysis-final-sources analysis)
(transduce (resyntax-analysis-all-results analysis)
(append-mapping in-hash-values)
(mapping refactoring-result-set-updated-source)
(indexing modified-source-original)
(grouping nonempty-into-last)
(mapping entry-value)
#:into into-list))
(define (resyntax-analysis-total-fixes analysis)
(for*/sum ([pass-results (in-list (resyntax-analysis-all-results analysis))]
[result-set (in-hash-values pass-results)])
(length (refactoring-result-set-results result-set))))
(define/guard (resyntax-analysis-total-sources-modified analysis)
(define all-results (resyntax-analysis-all-results analysis))
(guard (not (empty? all-results)) #:else 0)
(hash-count (first all-results)))
(define (resyntax-analysis-rules-applied analysis)
(for*/multiset ([pass-results (in-list (resyntax-analysis-all-results analysis))]
[result-set (in-hash-values pass-results)]
[result (in-list (refactoring-result-set-results result-set))])
(refactoring-result-rule-name result)))
(define (analysis-pass-fix-commits pass-results)
(append-map refactoring-result-map-commits pass-results))
(define (resyntax-analysis-fix-commits analysis)
(append-map refactoring-result-map-commits (resyntax-analysis-all-results analysis)))
(define (resyntax-analysis-write-file-changes! analysis)
(define sources (resyntax-analysis-final-sources analysis))
(unless (empty? sources)
(log-resyntax-info "--- fixing code ---"))
(for ([source (in-list sources)]
#:when (source-path source))
(log-resyntax-info "fixing ~a" (source-path source))
(display-to-file (modified-source-contents source) (source-path source)
#:mode 'text #:exists 'replace)))
(define (resyntax-analysis-commit-fixes! analysis)
(define commits (resyntax-analysis-fix-commits analysis))
(unless (empty? commits)
(log-resyntax-info "--- fixing code ---"))
(for ([commit (in-list commits)]
[i (in-naturals 1)])
(log-resyntax-info "--- commit ~a ---" i)
(match-define (resyntax-commit message changes) commit)
(for ([(path new-contents) (in-hash changes)])
(log-resyntax-info "fixing ~a" path)
(display-to-file new-contents path #:mode 'text #:exists 'replace))
(log-resyntax-info "commiting pass fixes")
(git-commit! message)))
(define (resyntax-analyze source
#:suite [suite default-recommendations]
#:lines [lines (range-set (unbounded-range #:comparator natural<=>))])
(define comments (with-input-from-source source read-comment-locations))
(define full-source (source->string source))
(log-resyntax-info "analyzing ~a" (or (source-path source) "string source"))
(for ([comment (in-range-set comments)])
(log-resyntax-debug "parsed comment: ~a: ~v" comment (substring-by-range full-source comment)))
(define (skip e)
(log-resyntax-error
"skipping ~a\n encountered an error during macro expansion\n error:\n~a"
(or (source-path source) "string source")
(string-indent (exn-message e) #:amount 3))
empty-list)
(define results
(with-handlers ([exn:fail:syntax? skip]
[exn:fail:filesystem:missing-module? skip]
[exn:fail:contract:variable? skip])
(define analysis
(parameterize ([current-namespace (make-base-namespace)])
(source-analyze source #:lines lines)))
(refactor-visited-forms #:analysis analysis #:suite suite #:comments comments #:lines lines)))
(refactoring-result-set #:base-source source #:results results))
(define (resyntax-analyze-all sources
#:suite [suite default-recommendations]
#:max-fixes [max-fixes +inf.0]
#:max-passes [max-passes 10]
#:max-modified-sources [max-modified-sources +inf.0]
#:max-modified-lines [max-modified-lines +inf.0])
(log-resyntax-info "--- analyzing code ---")
(for/fold ([pass-result-lists '()]
[sources sources]
[max-fixes max-fixes]
#:result (resyntax-analysis #:all-results (reverse pass-result-lists)))
([pass-index (in-range max-passes)]
#:do [(unless (zero? pass-index)
(log-resyntax-info "--- pass ~a ---" (add1 pass-index)))
(define pass-results
(resyntax-analyze-all-once sources
#:suite suite
#:max-fixes max-fixes
#:max-modified-sources max-modified-sources
#:max-modified-lines max-modified-lines))
(define pass-fix-count (count-total-results pass-results))
(define new-max-fixes (- max-fixes pass-fix-count))]
#:break (hash-empty? pass-results)
#:final (zero? new-max-fixes))
(define modified-sources (build-modified-source-map pass-results))
(values (cons pass-results pass-result-lists) modified-sources new-max-fixes)))
(define (count-total-results pass-results)
(for/sum ([(_ result-set) (in-hash pass-results)])
(length (refactoring-result-set-results result-set))))
(define (build-modified-source-map pass-results)
(transduce (in-hash-values pass-results)
(bisecting refactoring-result-set-updated-source refactoring-result-set-modified-lines)
#:into into-hash))
(define (resyntax-analyze-all-once sources
#:suite suite
#:max-fixes max-fixes
#:max-modified-sources max-modified-sources
#:max-modified-lines max-modified-lines)
(transduce (in-hash-entries sources) ; entries with source keys and line range set values
;; The following steps perform a kind of layered shuffle: the files to refactor are
;; shuffled such that files in the same directory remain together. When combined with
;; the #:max-modified-sources argument, this makes Resyntax prefer to refactor closely
;; related files instead of selecting arbitrary unrelated files from across an entire
;; codebase. This limits potential for merge conflicts and makes changes easier to
;; review, since it's more likely the refactored files will have shared context.
; key by directory
(indexing (λ (e) (source-directory (entry-key e))))
; group by key and shuffle within each group
(grouping (into-transduced (shuffling) #:into into-list))
; shuffle groups
(shuffling)
; ungroup and throw away directory
(append-mapping entry-value)
;; Now the stream contains exactly what it did before the above steps, but shuffled in
;; a convenient manner.
(append-mapping
(λ (e)
(match-define (entry source lines) e)
(define result-set (resyntax-analyze source #:suite suite #:lines lines))
(refactoring-result-set-results result-set)))
(limiting max-modified-lines
#:by (λ (result)
(define replacement (refactoring-result-line-replacement result))
(add1 (- (line-replacement-original-end-line replacement)
(line-replacement-start-line replacement)))))
(if (equal? max-fixes +inf.0) (transducer-pipe) (taking max-fixes))
(if (equal? max-modified-sources +inf.0)
(transducer-pipe)
(transducer-pipe
(indexing
(λ (result)
(syntax-replacement-source (refactoring-result-syntax-replacement result))))
(grouping into-list)
(taking max-modified-sources)
(append-mapping entry-value)))
(indexing refactoring-result-source)
(grouping into-list)
(mapping
(λ (e) (refactoring-result-set #:base-source (entry-key e) #:results (entry-value e))))
(indexing refactoring-result-set-base-source)
#:into into-hash))
(define (refactoring-rules-refactor rules syntax #:comments comments #:analysis analysis)
(define (refactor rule)
(with-handlers
([exn:fail?
(λ (e)
(log-resyntax-error "~a: refactoring attempt failed\n syntax:\n ~a\n cause:\n~a"
(object-name rule)
syntax
(string-indent (exn-message e) #:amount 3))
absent)])
(guarded-block
(guard-match (present replacement)
(refactoring-rule-refactor rule syntax (source-code-analysis-code analysis))
#:else absent)
(guard (syntax-replacement-introduces-incorrect-bindings? replacement) #:else
(log-resyntax-warning
(string-append
"~a: suggestion discarded because it introduces identifiers with incorrect bindings\n"
" incorrect identifiers: ~a")
(object-name rule)
(syntax-replacement-introduced-incorrect-identifiers replacement))
absent)
(guard (syntax-replacement-preserves-comments? replacement comments) #:else
(log-resyntax-warning
(string-append "~a: suggestion discarded because it does not preserve all comments\n"
" dropped comment locations: ~v\n"
" original syntax:\n"
" ~v\n"
" replacement syntax:\n"
" ~v")
(object-name rule)
(syntax-replacement-dropped-comment-locations replacement comments)
(syntax-replacement-original-syntax replacement)
(syntax-replacement-new-syntax replacement))
absent)
(present
(refactoring-result
#:rule-name (object-name rule)
#:message (refactoring-rule-description rule)
#:syntax-replacement replacement)))))
(falsey->option
(for*/first ([rule (in-list rules)]
[result (in-option (refactor rule))])
result)))
(define (refactor-visited-forms #:analysis analysis #:suite suite #:comments comments #:lines lines)
(define rule-list (refactoring-suite-rules suite))
(for*/fold ([results '()]
[modified-positions (range-set #:comparator natural<=>)]
#:result (reverse results))
([stx (in-list (source-code-analysis-visited-forms analysis))]
#:unless (range-set-intersects? modified-positions (syntax-source-range stx))
[result
(in-option
(refactoring-rules-refactor rule-list stx #:comments comments #:analysis analysis))]
#:when (check-lines-enclose-refactoring-result lines result))
(values (cons result results)
(range-set-add modified-positions (refactoring-result-modified-range result)))))
(define (check-lines-enclose-refactoring-result lines result)
(define modified-lines (refactoring-result-modified-line-range result))
(define enclosed? (range-set-encloses? lines modified-lines))
(unless enclosed?
(log-resyntax-info
(string-append "~a: suggestion discarded because it's outside the analyzed line range\n"
" analyzed lines: ~a\n"
" lines modified by result: ~a")
(refactoring-result-rule-name result)
lines
modified-lines))
enclosed?)
(define (refactor! results)
(define results-by-path
(transduce results
(bisecting
(λ (result)
(file-source-path
(syntax-replacement-source (refactoring-result-syntax-replacement result))))
refactoring-result-string-replacement)
(grouping union-into-string-replacement)
#:into into-hash))
(for ([(path replacement) (in-hash results-by-path)])
(file-apply-string-replacement! path replacement)))
(define (substring-by-range str rng)
(define lower-bound (range-lower-bound rng))
(define start
(cond
[(equal? lower-bound unbounded) 0]
[(equal? (range-bound-type lower-bound) inclusive)
(range-bound-endpoint lower-bound)]
[else
(max 0 (sub1 (range-bound-endpoint lower-bound)))]))
(define upper-bound (range-upper-bound rng))
(define end
(cond
[(equal? upper-bound unbounded) (string-length str)]
[(equal? (range-bound-type upper-bound) inclusive)
(min (string-length str) (+ (range-bound-endpoint upper-bound) 1))]
[else (range-bound-endpoint upper-bound)]))
(substring str start end))
(module+ test
(test-case "resyntax-analyze"
(define results
(refactoring-result-set-results
(resyntax-analyze (string-source "#lang racket (or 1 (or 2 3))"))))
(check-equal? (length results) 1)
(check-equal? (refactoring-result-string-replacement (first results))
(string-replacement #:start 13
#:end 28
#:contents (list (inserted-string "(or 1 2 3)"))))))