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