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