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