]> gitweb.factorcode.org Git - factor.git/blob - basis/help/lint/checks/checks.factor
sequences: rename subsequence? words to subseq? again.
[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-nulls ( element -- )
124     \ $values swap elements
125     null swap deep-member?
126     [ "$values should not contain null" simple-lint-error ] when ;
127
128 : check-see-also ( element -- )
129     \ $see-also swap elements [ rest all-unique? ] all?
130     [ "$see-also are not unique" simple-lint-error ] unless ;
131
132 : check-modules ( element -- )
133     \ $vocab-link swap elements [
134         second
135         vocab-exists? [
136             "$vocab-link to non-existent vocabulary"
137             simple-lint-error
138         ] unless
139     ] each ;
140
141 : check-slots-tables ( element -- )
142     \ $slots swap elements [ rest [ length 2 = ] all?  ] all?
143     [ "$slots have too many values in at least one row" simple-lint-error ] unless ;
144
145 : check-rendering ( element -- )
146     [ print-content ] with-string-writer drop ;
147
148 : check-strings ( str -- )
149     [
150         "\n\t" intersects? [
151             "Paragraph text should not contain \\n or \\t"
152             simple-lint-error
153         ] when
154     ] [
155         "  " subseq-index? [
156             "Paragraph text should not contain double spaces"
157             simple-lint-error
158         ] when
159     ] bi ;
160
161 : check-whitespace ( str1 str2 -- )
162     [ " " tail? ] [ " " head? ] bi* or
163     [ "Missing whitespace between strings" simple-lint-error ] unless ;
164
165 : check-bogus-nl ( element -- )
166     { { $nl } { { $nl } } } [ head? ] with any? [
167         "Simple element should not begin with a paragraph break"
168         simple-lint-error
169     ] when ;
170
171 : extract-slots ( elements -- seq )
172     [ dup pair? [ first \ $slot = ] [ drop f ] if ] deep-filter
173     [ second ] map ;
174
175 : check-class-description ( word element -- )
176     \ $class-description swap elements over class? [
177         [
178             dup struct-class? [ struct-slots ] [ all-slots ] if
179             [ name>> ] map
180         ] [ extract-slots ] bi*
181         [ swap member? ] with reject [
182             ", " join "Described $slot does not exist: " prepend
183             simple-lint-error
184         ] unless-empty
185     ] [
186         nip empty? not [
187             "A word that is not a class has a $class-description"
188             simple-lint-error
189         ] when
190     ] if ;
191
192 : check-article-title ( article -- )
193     article-title first LETTER?
194     [ "Article title must begin with a capital letter" simple-lint-error ] unless ;
195
196 : check-elements ( element -- )
197     {
198         [ check-bogus-nl ]
199         [ [ string? ] filter [ check-strings ] each ]
200         [ [ simple-element? ] filter [ check-elements ] each ]
201         [ 2 <clumps> [ [ string? ] all? ] filter [ first2 check-whitespace ] each ]
202     } cleave ;
203
204 : check-descriptions ( element -- )
205     { $description $class-description $var-description }
206     swap '[
207         _ elements [
208             rest { { } { "" } } member?
209             [ "Empty $description" simple-lint-error ] when
210         ] each
211     ] each ;
212
213 : check-markup ( element -- )
214     {
215         [ check-elements ]
216         [ check-rendering ]
217         [ check-examples ]
218         [ check-modules ]
219         [ check-descriptions ]
220         [ check-slots-tables ]
221     } cleave ;
222
223 : files>vocabs ( -- assoc )
224     loaded-vocab-names
225     [ [ [ vocab-docs-path ] keep ] H{ } map>assoc ]
226     [ [ [ vocab-source-path ] keep ] H{ } map>assoc ]
227     bi assoc-union ;
228
229 : group-articles ( -- assoc )
230     articles get keys
231     files>vocabs
232     H{ } clone [
233         '[
234             dup >link where dup
235             [ first _ at _ push-at ] [ 2drop ] if
236         ] each
237     ] keep ;
238
239 : all-word-help ( words -- seq )
240     [ word-help ] filter ;