]> gitweb.factorcode.org Git - factor.git/blob - basis/help/lint/checks/checks.factor
help.lint.checks: Ignore linux-monitors as not real leaks.
[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-rendering ( element -- )
148     [ print-content ] with-string-writer drop ;
149
150 : check-strings ( str -- )
151     [
152         "\n\t" intersects? [
153             "Paragraph text should not contain \\n or \\t"
154             simple-lint-error
155         ] when
156     ] [
157         "  " swap subseq? [
158             "Paragraph text should not contain double spaces"
159             simple-lint-error
160         ] when
161     ] bi ;
162
163 : check-whitespace ( str1 str2 -- )
164     [ " " tail? ] [ " " head? ] bi* or
165     [ "Missing whitespace between strings" simple-lint-error ] unless ;
166
167 : check-bogus-nl ( element -- )
168     { { $nl } { { $nl } } } [ head? ] with any? [
169         "Simple element should not begin with a paragraph break"
170         simple-lint-error
171     ] when ;
172
173 : extract-slots ( elements -- seq )
174     [ dup pair? [ first \ $slot = ] [ drop f ] if ] deep-filter
175     [ second ] map ;
176
177 : check-class-description ( word element -- )
178     \ $class-description swap elements over class? [
179         [
180             dup struct-class? [ struct-slots ] [ all-slots ] if
181             [ name>> ] map
182         ] [ extract-slots ] bi*
183         [ swap member? ] with reject [
184             ", " join "Described $slot does not exist: " prepend
185             simple-lint-error
186         ] unless-empty
187     ] [
188         nip empty? not [
189             "A word that is not a class has a $class-description"
190             simple-lint-error
191         ] when
192     ] if ;
193
194 : check-article-title ( article -- )
195     article-title first LETTER?
196     [ "Article title must begin with a capital letter" simple-lint-error ] unless ;
197
198 : check-elements ( element -- )
199     {
200         [ check-bogus-nl ]
201         [ [ string? ] filter [ check-strings ] each ]
202         [ [ simple-element? ] filter [ check-elements ] each ]
203         [ 2 <clumps> [ [ string? ] all? ] filter [ first2 check-whitespace ] each ]
204     } cleave ;
205
206 : check-descriptions ( element -- )
207     { $description $class-description $var-description }
208     swap '[
209         _ elements [
210             rest { { } { "" } } member?
211             [ "Empty $description" simple-lint-error ] when
212         ] each
213     ] each ;
214
215 : check-markup ( element -- )
216     {
217         [ check-elements ]
218         [ check-rendering ]
219         [ check-examples ]
220         [ check-modules ]
221         [ check-descriptions ]
222     } cleave ;
223
224 : files>vocabs ( -- assoc )
225     loaded-vocab-names
226     [ [ [ vocab-docs-path ] keep ] H{ } map>assoc ]
227     [ [ [ vocab-source-path ] keep ] H{ } map>assoc ]
228     bi assoc-union ;
229
230 : group-articles ( -- assoc )
231     articles get keys
232     files>vocabs
233     H{ } clone [
234         '[
235             dup >link where dup
236             [ first _ at _ push-at ] [ 2drop ] if
237         ] each
238     ] keep ;
239
240 : all-word-help ( words -- seq )
241     [ word-help ] filter ;