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