]> gitweb.factorcode.org Git - factor.git/blob - basis/help/lint/checks/checks.factor
help.lint.checks: ignore words without defined word-help.
[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: all-vocabs-list
20 SYMBOL: vocab-articles
21
22 : check-example ( element -- )
23     [
24         '[
25             _ rest [
26                 but-last "\n" join
27                 [ (eval>string) ] call( code -- output )
28                 "\n" ?tail drop
29             ] keep
30             last assert=
31         ] vocabs-quot get call( quot -- )
32     ] leaks members length [
33         "%d disposable(s) leaked in example" sprintf simple-lint-error
34     ] unless-zero ;
35
36 : check-examples ( element -- )
37     \ $example swap elements [ check-example ] each ;
38
39 : extract-values ( element -- seq )
40     \ $values swap elements
41     [ f ] [ first rest keys ] if-empty ;
42
43 : extract-value-effects ( element -- seq )
44     \ $values swap elements [ f ] [
45         first rest [
46             \ $quotation swap elements [ f ] [
47                 first second dup effect? [ effect>string ] when
48             ] if-empty
49         ] map
50     ] if-empty ;
51
52 : effect-values ( word -- seq )
53     stack-effect
54     [ in>> ] [ out>> ] bi append
55     [ dup pair? [ first ] when effect>string ] map members ;
56
57 : effect-effects ( word -- seq )
58     stack-effect in>> [
59         dup pair?
60         [ second dup effect? [ effect>string ] [ drop f ] if ]
61         [ drop f ] if
62     ] map ;
63
64 : contains-funky-elements? ( element -- ? )
65     {
66         $shuffle
67         $complex-shuffle
68         $values-x/y
69         $predicate
70         $class-description
71         $error-description
72     } swap '[ _ elements empty? not ] any? ;
73
74 : don't-check-word? ( word -- ? )
75     {
76         [ macro? ]
77         [ symbol? ]
78         [ parsing-word? ]
79         [ "declared-effect" word-prop not ]
80         [ constant? ]
81         [ "word-help" word-prop not ]
82     } 1|| ;
83
84 : skip-check-values? ( word element -- ? )
85     [ don't-check-word? ] [ contains-funky-elements? ] bi* or ;
86
87 : check-values ( word element -- )
88     2dup skip-check-values? [ 2drop ] [
89         [ effect-values ] [ extract-values ] bi* 2dup
90         sequence= [ 2drop ] [
91             "$values don't match stack effect; expected %u, got %u" sprintf
92             simple-lint-error
93         ] if
94     ] if ;
95
96 : check-value-effects ( word element -- )
97     [ effect-effects ] [ extract-value-effects ] bi*
98     [ 2dup and [ = ] [ 2drop t ] if ] 2all? [
99         "$quotation stack effects in $values don't match"
100         simple-lint-error
101     ] unless ;
102
103 : check-nulls ( element -- )
104     \ $values swap elements
105     null swap deep-member?
106     [ "$values should not contain null" simple-lint-error ] when ;
107
108 : check-see-also ( element -- )
109     \ $see-also swap elements [ rest all-unique? ] all?
110     [ "$see-also are not unique" simple-lint-error ] unless ;
111
112 : vocab-exists? ( name -- ? )
113     [ lookup-vocab ] [ all-vocabs-list get member? ] bi or ;
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 ;