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