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