Skip to content

Commit 1e74922

Browse files
committed
Centralize .rktd document policy
Handle `.rktd` file policy logic in doc-lang module
1 parent 1714b43 commit 1e74922

4 files changed

Lines changed: 36 additions & 8 deletions

File tree

doclib/check-syntax.rkt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
racket/port
99
"editor.rkt"
1010
"doc-trace.rkt"
11+
"doc-lang.rkt"
1112
"../common/path-util.rkt"
1213
"internal-types.rkt")
1314

@@ -55,19 +56,18 @@
5556
(define (check-syntax uri doc-text)
5657
(define path (uri->path uri))
5758
(define text (send doc-text get-text))
58-
; rktd <-> rkt is just like JSON <-> js
59-
(define data-file? (equal? (path-get-extension path) #".rktd"))
59+
(define expand? (requires-expansion? path))
6060
(define new-trace (new build-trace% [src path] [doc-text doc-text]))
6161

6262
(define in (open-input-string text))
63-
(define er (expand-source path in new-trace #:expand? (not data-file?)))
63+
(define er (expand-source path in new-trace #:expand? expand?))
6464

6565
(send new-trace walk-stx er)
6666
(send new-trace walk-log (ExpandResult-logs er))
6767
(CSResult new-trace text
68-
(if data-file?
69-
(and (ExpandResult-pre-syntax er) #t)
70-
(ExpandResult-all-succeed? er))))
68+
(if expand?
69+
(ExpandResult-all-succeed? er)
70+
(and (ExpandResult-pre-syntax er) #t))))
7171

7272
(provide
7373
(struct-out CSResult)

doclib/doc-lang.rkt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,18 @@
263263
(and (regexp-match? (Known-Language-name-rx language) text)
264264
language)))
265265

266+
(define/contract (racket-data-file-path? path)
267+
(-> path-string? boolean?)
268+
(equal? (path-get-extension path) #".rktd"))
269+
270+
(define/contract (requires-expansion? path)
271+
(-> path-string? boolean?)
272+
(not (racket-data-file-path? path)))
273+
274+
(define/contract (requires-language-declaration? path)
275+
(-> path-string? boolean?)
276+
(not (racket-data-file-path? path)))
277+
266278
(define (uri->suffix uri)
267279
(define extension (path-get-extension (uri->path uri)))
268280
(bytes->string/utf-8 (subbytes extension 1)))
@@ -366,6 +378,9 @@
366378
Known-Language~kw
367379
known-languages
368380
find-language-by-text
381+
racket-data-file-path?
382+
requires-expansion?
383+
requires-language-declaration?
369384
parse-language-prefix
370385
parse-language
371386
guess-language-by-uri

doclib/service/diagnostic.rkt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
racket/class
55
racket/string
66
racket/set
7-
racket/path
87
racket/match
98
racket/list
109
setup/path-to-relative
@@ -44,7 +43,7 @@
4443
(define pre-exn (ExpandResult-pre-exn expand-result))
4544
(define post-exn (ExpandResult-post-exn expand-result))
4645
(define maybe-language-diag
47-
(and (not (equal? (path-get-extension src) #".rktd"))
46+
(and (requires-language-declaration? src)
4847
(language-diagnostic doc-text)))
4948
(when maybe-language-diag
5049
(add-diag! maybe-language-diag))

tests/lib/doc-lang-test.rkt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,20 @@
232232
'scribble)
233233
(check-false (guess-language-by-uri "file:///tmp/demo.rkt")))
234234

235+
(test-case
236+
"rktd files are Racket data, not expandable modules"
237+
(define path (string->path "/tmp/demo.rktd"))
238+
(check-true (racket-data-file-path? path))
239+
(check-false (requires-expansion? path))
240+
(check-false (requires-language-declaration? path)))
241+
242+
(test-case
243+
"rkt files still require module policy"
244+
(define path (string->path "/tmp/demo.rkt"))
245+
(check-false (racket-data-file-path? path))
246+
(check-true (requires-expansion? path))
247+
(check-true (requires-language-declaration? path)))
248+
235249
(test-case
236250
"sexp-language? is true only for known sexp families"
237251
(check-true (sexp-language? "#lang racket/base\n(define x 1)\n"))

0 commit comments

Comments
 (0)