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