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