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