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