]> gitweb.factorcode.org Git - factor.git/blob - basis/help/lint/checks/checks.factor
0133e3b7c8cef4bf55cd57ad5798f92347b90e16
[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.categories vocabs
9 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: 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     } 1|| ;
82
83 : skip-check-values? ( word element -- ? )
84     [ don't-check-word? ] [ contains-funky-elements? ] bi* or ;
85
86 : check-values ( word element -- )
87     2dup skip-check-values? [ 2drop ] [
88         [ effect-values ] [ extract-values ] bi* 2dup
89         sequence= [ 2drop ] [
90             "$values don't match stack effect; expected %u, got %u" sprintf
91             simple-lint-error
92         ] if
93     ] if ;
94
95 : check-value-effects ( word element -- )
96     [ effect-effects ] [ extract-value-effects ] bi*
97     [ 2dup and [ = ] [ 2drop t ] if ] 2all? [
98         "$quotation stack effects in $values don't match"
99         simple-lint-error
100     ] unless ;
101
102 : check-nulls ( element -- )
103     \ $values swap elements
104     null swap deep-member?
105     [ "$values should not contain null" simple-lint-error ] when ;
106
107 : check-see-also ( element -- )
108     \ $see-also swap elements [ rest all-unique? ] all?
109     [ "$see-also are not unique" simple-lint-error ] unless ;
110
111 : vocab-exists? ( name -- ? )
112     [ lookup-vocab ] [ all-vocabs-list get member? ] bi or ;
113
114 : check-modules ( element -- )
115     \ $vocab-link swap elements [
116         second
117         vocab-exists? [
118             "$vocab-link to non-existent vocabulary"
119             simple-lint-error
120         ] unless
121     ] each ;
122
123 : check-rendering ( element -- )
124     [ print-content ] with-string-writer drop ;
125
126 : check-strings ( str -- )
127     [
128         "\n\t" intersects? [
129             "Paragraph text should not contain \\n or \\t"
130             simple-lint-error
131         ] when
132     ] [
133         "  " swap subseq? [
134             "Paragraph text should not contain double spaces"
135             simple-lint-error
136         ] when
137     ] bi ;
138
139 : check-whitespace ( str1 str2 -- )
140     [ " " tail? ] [ " " head? ] bi* or
141     [ "Missing whitespace between strings" simple-lint-error ] unless ;
142
143 : check-bogus-nl ( element -- )
144     { { $nl } { { $nl } } } [ head? ] with any? [
145         "Simple element should not begin with a paragraph break"
146         simple-lint-error
147     ] when ;
148
149 : extract-slots ( elements -- seq )
150     [ dup pair? [ first \ $slot = ] [ drop f ] if ] deep-filter
151     [ second ] map ;
152
153 : check-class-description ( word element -- )
154     \ $class-description swap elements over class? [
155         [
156             dup struct-class? [ struct-slots ] [ all-slots ] if
157             [ name>> ] map
158         ] [ extract-slots ] bi*
159         [ swap member? ] with reject [
160             ", " join "Described $slot does not exist: " prepend
161             simple-lint-error
162         ] unless-empty
163     ] [
164         nip empty? not [
165             "A word that is not a class has a $class-description"
166             simple-lint-error
167         ] when
168     ] if ;
169
170 : check-article-title ( article -- )
171     article-title first LETTER?
172     [ "Article title must begin with a capital letter" simple-lint-error ] unless ;
173
174 : check-elements ( element -- )
175     {
176         [ check-bogus-nl ]
177         [ [ string? ] filter [ check-strings ] each ]
178         [ [ simple-element? ] filter [ check-elements ] each ]
179         [ 2 <clumps> [ [ string? ] all? ] filter [ first2 check-whitespace ] each ]
180     } cleave ;
181
182 : check-descriptions ( element -- )
183     { $description $class-description $var-description }
184     swap '[
185         _ elements [
186             rest { { } { "" } } member?
187             [ "Empty $description" simple-lint-error ] when
188         ] each
189     ] each ;
190
191 : check-markup ( element -- )
192     {
193         [ check-elements ]
194         [ check-rendering ]
195         [ check-examples ]
196         [ check-modules ]
197         [ check-descriptions ]
198     } cleave ;
199
200 : files>vocabs ( -- assoc )
201     loaded-vocab-names
202     [ [ [ vocab-docs-path ] keep ] H{ } map>assoc ]
203     [ [ [ vocab-source-path ] keep ] H{ } map>assoc ]
204     bi assoc-union ;
205
206 : group-articles ( -- assoc )
207     articles get keys
208     files>vocabs
209     H{ } clone [
210         '[
211             dup >link where dup
212             [ first _ at _ push-at ] [ 2drop ] if
213         ] each
214     ] keep ;
215
216 : all-word-help ( words -- seq )
217     [ word-help ] filter ;