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