]> gitweb.factorcode.org Git - factor.git/blob - basis/help/lint/checks/checks.factor
help.lint.checks: remove leaks check for now due to bootstrap issues.
[factor.git] / basis / help / lint / checks / checks.factor
1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs classes classes.tuple combinators
4 combinators.short-circuit debugger definitions effects eval
5 formatting fry grouping help help.markup help.topics io
6 io.streams.string kernel macros namespaces sequences
7 sequences.deep sets splitting strings summary unicode.categories
8 vocabs vocabs.loader words words.constant words.symbol ;
9 FROM: sets => members ;
10 IN: help.lint.checks
11
12 ERROR: simple-lint-error message ;
13
14 M: simple-lint-error summary message>> ;
15
16 M: simple-lint-error error. summary print ;
17
18 SYMBOL: vocabs-quot
19 SYMBOL: all-vocabs
20 SYMBOL: vocab-articles
21
22 : check-example ( element -- )
23     ! [
24         '[
25             _ rest [
26                 but-last "\n" join
27                 [ (eval>string) ] call( code -- output )
28                 "\n" ?tail drop
29             ] keep
30             last assert=
31         ] vocabs-quot get call( quot -- ) ;
32     ! ] leaks members length [
33     !     "%d disposable(s) leaked in example" sprintf simple-lint-error
34     ! ] unless-zero ;
35
36 : check-examples ( element -- )
37     \ $example swap elements [ check-example ] each ;
38
39 : extract-values ( element -- seq )
40     \ $values swap elements dup empty? [
41         first rest keys
42     ] unless ;
43
44 : extract-value-effects ( element -- seq )
45     \ $values swap elements dup empty? [
46         first rest [
47             \ $quotation swap elements dup empty? [ drop f ] [
48                 first second
49             ] if
50         ] map
51     ] unless ;
52
53 : effect-values ( word -- seq )
54     stack-effect
55     [ in>> ] [ out>> ] bi append
56     [ dup pair? [ first ] when effect>string ] map members ;
57
58 : effect-effects ( word -- seq )
59     stack-effect in>> [
60         dup pair?
61         [ second dup effect? [ effect>string ] [ drop f ] if ]
62         [ drop f ] if
63     ] map ;
64
65 : contains-funky-elements? ( element -- ? )
66     {
67         $shuffle
68         $complex-shuffle
69         $values-x/y
70         $predicate
71         $class-description
72         $error-description
73     } swap '[ _ elements empty? not ] any? ;
74
75 : don't-check-word? ( word -- ? )
76     {
77         [ macro? ]
78         [ symbol? ]
79         [ parsing-word? ]
80         [ "declared-effect" word-prop not ]
81         [ constant? ]
82     } 1|| ;
83
84 : skip-check-values? ( word element -- ? )
85     [ don't-check-word? ] [ contains-funky-elements? ] bi* or ;
86
87 : check-values ( word element -- )
88     2dup skip-check-values? [ 2drop ] [
89         [ effect-values ] [ extract-values ] bi* 2dup
90         sequence= [ 2drop ] [
91             "$values don't match stack effect; expected %u, got %u" sprintf
92             simple-lint-error
93         ] if
94     ] if ;
95
96 : check-value-effects ( word element -- )
97     [ effect-effects ]
98     [ extract-value-effects ]
99     bi* [ 2dup and [ = ] [ 2drop t ] if ] 2all?
100     [ "$quotation documentation in $values don't match stack effect" simple-lint-error ]
101     unless ;
102
103 : check-nulls ( element -- )
104     \ $values swap elements
105     null swap deep-member?
106     [ "$values should not contain null" simple-lint-error ] when ;
107
108 : check-see-also ( element -- )
109     \ $see-also swap elements [
110         rest all-unique? t assert=
111     ] each ;
112
113 : vocab-exists? ( name -- ? )
114     [ lookup-vocab ] [ all-vocabs get member? ] bi or ;
115
116 : check-modules ( element -- )
117     \ $vocab-link swap elements [
118         second
119         vocab-exists? [
120             "$vocab-link to non-existent vocabulary"
121             simple-lint-error
122         ] unless
123     ] each ;
124
125 : check-rendering ( element -- )
126     [ print-content ] with-string-writer drop ;
127
128 : check-strings ( str -- )
129     [
130         "\n\t" intersects? [
131             "Paragraph text should not contain \\n or \\t"
132             simple-lint-error
133         ] when
134     ] [
135         "  " swap subseq? [
136             "Paragraph text should not contain double spaces"
137             simple-lint-error
138         ] when
139     ] bi ;
140
141 : check-whitespace ( str1 str2 -- )
142     [ " " tail? ] [ " " head? ] bi* or
143     [ "Missing whitespace between strings" simple-lint-error ] unless ;
144
145 : check-bogus-nl ( element -- )
146     { { $nl } { { $nl } } } [ head? ] with any? [
147         "Simple element should not begin with a paragraph break"
148         simple-lint-error
149     ] when ;
150
151 : extract-slots ( elements -- seq )
152     [ dup pair? [ first \ $slot = ] [ drop f ] if ] deep-filter
153     [ second ] map ;
154
155 : check-class-description ( word element -- )
156     \ $class-description swap elements over class? [
157         [ all-slots [ name>> ] map ] [ extract-slots ] bi*
158         [ swap member? not ] with filter [
159             ", " join "Described $slot does not exist: " prepend
160             simple-lint-error
161         ] unless-empty
162     ] [
163         nip empty? not [
164             "A word that is not a class has a $class-description"
165             simple-lint-error
166         ] when
167     ] if ;
168
169 : check-article-title ( article -- )
170     article-title first LETTER?
171     [ "Article title must begin with a capital letter" simple-lint-error ] unless ;
172
173 : check-elements ( element -- )
174     {
175         [ check-bogus-nl ]
176         [ [ string? ] filter [ check-strings ] each ]
177         [ [ simple-element? ] filter [ check-elements ] each ]
178         [ 2 <clumps> [ [ string? ] all? ] filter [ first2 check-whitespace ] each ]
179     } cleave ;
180
181 : check-descriptions ( element -- )
182     { $description $class-description $var-description }
183     swap '[
184         _ elements [
185             rest { { } { "" } } member?
186             [ "Empty $description" simple-lint-error ] when
187         ] each
188     ] each ;
189
190 : check-markup ( element -- )
191     {
192         [ check-elements ]
193         [ check-rendering ]
194         [ check-examples ]
195         [ check-modules ]
196         [ check-descriptions ]
197     } cleave ;
198
199 : files>vocabs ( -- assoc )
200     vocabs
201     [ [ [ vocab-docs-path ] keep ] H{ } map>assoc ]
202     [ [ [ vocab-source-path ] keep ] H{ } map>assoc ]
203     bi assoc-union ;
204
205 : group-articles ( -- assoc )
206     articles get keys
207     files>vocabs
208     H{ } clone [
209         '[
210             dup >link where dup
211             [ first _ at _ push-at ] [ 2drop ] if
212         ] each
213     ] keep ;
214
215 : all-word-help ( words -- seq )
216     [ word-help ] filter ;