]> gitweb.factorcode.org Git - factor.git/blob - basis/help/lint/checks/checks.factor
remove values vocab
[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 combinators
4 combinators.short-circuit definitions effects eval fry grouping
5 help help.markup help.topics io.streams.string kernel macros
6 namespaces sequences sequences.deep sets sorting splitting
7 strings unicode.categories vocabs vocabs.loader words
8 words.symbol summary debugger io ;
9 FROM: sets => members ;
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: all-vocabs
20 SYMBOL: vocab-articles
21
22 : check-example ( element -- )
23     '[
24         _ rest [
25             but-last "\n" join
26             [ (eval>string) ] call( code -- output )
27             "\n" ?tail drop
28         ] keep
29         last assert=
30     ] vocabs-quot get call( quot -- ) ;
31
32 : check-examples ( element -- )
33     \ $example swap elements [ check-example ] each ;
34
35 : extract-values ( element -- seq )
36     \ $values swap elements dup empty? [
37         first rest keys
38     ] unless ;
39
40 : extract-value-effects ( element -- seq )
41     \ $values swap elements dup empty? [
42         first rest [ 
43             \ $quotation swap elements dup empty? [ drop f ] [
44                 first second
45             ] if
46         ] map
47     ] unless ;
48
49 : effect-values ( word -- seq )
50     stack-effect
51     [ in>> ] [ out>> ] bi append
52     [ dup pair? [ first ] when effect>string ] map members ;
53
54 : effect-effects ( word -- seq )
55     stack-effect in>> [
56         dup pair?
57         [ second dup effect? [ effect>string ] [ drop f ] if ]
58         [ drop f ] if
59     ] map ;
60
61 : contains-funky-elements? ( element -- ? )
62     {
63         $shuffle
64         $complex-shuffle
65         $values-x/y
66         $predicate
67         $class-description
68         $error-description
69     } swap '[ _ elements empty? not ] any? ;
70
71 : don't-check-word? ( word -- ? )
72     {
73         [ macro? ]
74         [ symbol? ]
75         [ parsing-word? ]
76         [ "declared-effect" word-prop not ]
77     } 1|| ;
78
79 : check-values ( word element -- )
80     {
81         [
82             [ don't-check-word? ]
83             [ contains-funky-elements? ]
84             bi* or
85         ] [
86             [ effect-values ]
87             [ extract-values ]
88             bi* sequence=
89         ] 
90     } 2|| [ "$values don't match stack effect" simple-lint-error ] unless ;
91
92 : check-value-effects ( word element -- )
93     [ effect-effects ]
94     [ extract-value-effects ]
95     bi* [ 2dup and [ = ] [ 2drop t ] if ] 2all?
96     [ "$quotation documentation in $values don't match stack effect" simple-lint-error ]
97     unless ;
98
99 : check-nulls ( element -- )
100     \ $values swap elements
101     null swap deep-member?
102     [ "$values should not contain null" simple-lint-error ] when ;
103
104 : check-see-also ( element -- )
105     \ $see-also swap elements [
106         rest all-unique? t assert=
107     ] each ;
108
109 : vocab-exists? ( name -- ? )
110     [ lookup-vocab ] [ all-vocabs get member? ] bi or ;
111
112 : check-modules ( element -- )
113     \ $vocab-link swap elements [
114         second
115         vocab-exists? [
116             "$vocab-link to non-existent vocabulary"
117             simple-lint-error
118         ] unless
119     ] each ;
120
121 : check-rendering ( element -- )
122     [ print-content ] with-string-writer drop ;
123
124 : check-strings ( str -- )
125     [
126         "\n\t" intersects? [
127             "Paragraph text should not contain \\n or \\t"
128             simple-lint-error
129         ] when
130     ] [
131         "  " swap subseq? [
132             "Paragraph text should not contain double spaces"
133             simple-lint-error
134         ] when
135     ] bi ;
136
137 : check-whitespace ( str1 str2 -- )
138     [ " " tail? ] [ " " head? ] bi* or
139     [ "Missing whitespace between strings" simple-lint-error ] unless ;
140
141 : check-bogus-nl ( element -- )
142     { { $nl } { { $nl } } } [ head? ] with any? [
143         "Simple element should not begin with a paragraph break"
144         simple-lint-error
145     ] when ;
146
147 : check-class-description ( word element -- )
148     [ class? not ]
149     [ { $class-description } swap elements empty? not ] bi* and
150     [ "A word that is not a class has a $class-description" simple-lint-error ] when ;
151
152 : check-article-title ( article -- )
153     article-title first LETTER?
154     [ "Article title must begin with a capital letter" simple-lint-error ] unless ;
155
156 : check-elements ( element -- )
157     {
158         [ check-bogus-nl ]
159         [ [ string? ] filter [ check-strings ] each ]
160         [ [ simple-element? ] filter [ check-elements ] each ]
161         [ 2 <clumps> [ [ string? ] all? ] filter [ first2 check-whitespace ] each ]
162     } cleave ;
163
164 : check-descriptions ( element -- )
165     { $description $class-description $var-description }
166     swap '[
167         _ elements [
168             rest { { } { "" } } member?
169             [ "Empty $description" simple-lint-error ] when
170         ] each
171     ] each ;
172
173 : check-markup ( element -- )
174     {
175         [ check-elements ]
176         [ check-rendering ]
177         [ check-examples ]
178         [ check-modules ]
179         [ check-descriptions ]
180     } cleave ;
181
182 : files>vocabs ( -- assoc )
183     vocabs
184     [ [ [ vocab-docs-path ] keep ] H{ } map>assoc ]
185     [ [ [ vocab-source-path ] keep ] H{ } map>assoc ]
186     bi assoc-union ;
187
188 : group-articles ( -- assoc )
189     articles get keys
190     files>vocabs
191     H{ } clone [
192         '[
193             dup >link where dup
194             [ first _ at _ push-at ] [ 2drop ] if
195         ] each
196     ] keep ;
197
198 : all-word-help ( words -- seq )
199     [ word-help ] filter ;