@@ -29,6 +29,7 @@ import _root_.scalariform.lexer.Token
29
29
import _root_ .scalariform .lexer .TokenType
30
30
import _root_ .scalariform .lexer .Tokens .CLASS
31
31
import _root_ .scalariform .parser .AccessModifier
32
+ import _root_ .scalariform .parser .AstNode
32
33
import _root_ .scalariform .parser .FullDefOrDcl
33
34
import _root_ .scalariform .parser .FunDefOrDcl
34
35
import _root_ .scalariform .parser .ParamClauses
@@ -54,6 +55,7 @@ class ScalaDocChecker extends CombinedChecker {
54
55
protected val errorKey : String = " scaladoc"
55
56
56
57
val DefaultIgnoreRegex = " ^$"
58
+ val DefaultIgnoreTokenTypes = " "
57
59
58
60
val skipPrivate = true
59
61
val skipQualifiedPrivate = false
@@ -62,7 +64,11 @@ class ScalaDocChecker extends CombinedChecker {
62
64
63
65
override def verify (ast : CombinedAst ): List [ScalastyleError ] = {
64
66
val tokens = ast.compilationUnit.tokens
67
+
65
68
val ignoreRegex = getString(" ignoreRegex" , DefaultIgnoreRegex )
69
+ val tokensToIgnore = getString(" ignoreTokenTypes" , DefaultIgnoreTokenTypes ).split(" ," ).filterNot(_.isEmpty).toSet
70
+
71
+ assertTokensToIgnore(tokensToIgnore)
66
72
67
73
def trimToTokenOfType (list : List [Token ], tokenType : TokenType ): List [Token ] = {
68
74
if (list.isEmpty) {
@@ -79,7 +85,7 @@ class ScalaDocChecker extends CombinedChecker {
79
85
val ignore = ts.nonEmpty && ts(1 ).text.matches(ignoreRegex)
80
86
ignore match {
81
87
case true => Nil
82
- case false => localVisit(skip = false , HiddenTokens (Nil ), ast.lines)(ast.compilationUnit.immediateChildren.head)
88
+ case false => localVisit(skip = false , HiddenTokens (Nil ), ast.lines, tokensToIgnore )(ast.compilationUnit.immediateChildren.head)
83
89
}
84
90
}
85
91
@@ -179,97 +185,114 @@ class ScalaDocChecker extends CombinedChecker {
179
185
*
180
186
* we do not bother descending down any further
181
187
*/
182
- private def localVisit (skip : Boolean , fallback : HiddenTokens , lines : Lines )(ast : Any ): List [ScalastyleError ] = ast match {
183
- case t : FullDefOrDcl =>
184
- // private, private[xxx];
185
- // protected, protected[xxx];
186
-
187
- // check if we are going to include or skip depending on access modifier
188
- val accessModifier = t.modifiers.find {
189
- case AccessModifier (_, _) => true
190
- case _ => false
191
- }
192
- val skip = accessModifier.exists {
193
- case AccessModifier (pop, Some (_)) =>
194
- if (pop.text == " private" ) skipQualifiedPrivate else skipQualifiedProtected
195
- case AccessModifier (pop, None ) =>
196
- if (pop.text == " private" ) skipPrivate else skipProtected
197
- case _ =>
198
- false
199
- }
188
+ private def localVisit (skip : Boolean , fallback : HiddenTokens , lines : Lines , tokensToIgnore : Set [String ])(ast : Any ): List [ScalastyleError ] = {
200
189
201
- // pick the ScalaDoc "attached" to the modifier, which actually means
202
- // ScalaDoc of the following member
203
- val scalaDocs = for {
204
- token <- t.tokens
205
- comment <- token.associatedWhitespaceAndComments
206
- if comment.token.isScalaDocComment
207
- } yield comment
208
-
209
- // descend
210
- visit(t, localVisit(skip, HiddenTokens (fallback.tokens ++ scalaDocs), lines))
211
- case t : TmplDef =>
212
- // trait Foo, trait Foo[A];
213
- // class Foo, class Foo[A](a: A);
214
- // case class Foo(), case class Foo[A](a: A);
215
- // object Foo;
216
- val (_, line) = lines.findLineAndIndex(t.firstToken.offset).get
217
-
218
- // we are checking parameters and type parameters
219
- val errors = if (skip) Nil else findScalaDoc(t.firstToken, fallback).
220
- map { scalaDoc =>
221
- paramErrors(line, t.paramClausesOpt)(scalaDoc) ++
222
- tparamErrors(line, t.typeParamClauseOpt)(scalaDoc)
223
- }.getOrElse(List (LineError (line, List (Missing ))))
224
-
225
- // and we descend, because we're interested in seeing members of the types
226
- errors ++ visit(t, localVisit(skip, NoHiddenTokens , lines))
227
- case t : FunDefOrDcl =>
228
- // def foo[A, B](a: Int): B = ...
229
- val (_, line) = lines.findLineAndIndex(t.firstToken.offset).get
230
-
231
- // we are checking parameters, type parameters and returns
232
- val errors = if (skip) Nil else findScalaDoc(t.firstToken, fallback).
233
- map { scalaDoc =>
234
- paramErrors(line, Some (t.paramClauses))(scalaDoc) ++
235
- tparamErrors(line, t.typeParamClauseOpt)(scalaDoc) ++
236
- returnErrors(line, t.returnTypeOpt)(scalaDoc)
237
- }.
238
- getOrElse(List (LineError (line, List (Missing ))))
239
-
240
- // we don't descend any further
241
- errors
242
- case t : TypeDefOrDcl =>
243
- // type Foo = ...
244
- val (_, line) = lines.findLineAndIndex(t.firstToken.offset).get
245
-
246
- // error is non-existence
247
- val errors = if (skip) Nil else findScalaDoc(t.firstToken, fallback).
248
- map(_ => Nil ).
249
- getOrElse(List (LineError (line, List (Missing ))))
250
-
251
- // we don't descend any further
252
- errors
253
-
254
- case t : PatDefOrDcl =>
255
- // val a = ...
256
- // var a = ...
257
- val (_, line) = lines.findLineAndIndex(t.valOrVarToken.offset).get
258
- val errors = if (skip) Nil else findScalaDoc(t.firstToken, fallback).
259
- map(_ => Nil ).
260
- getOrElse(List (LineError (line, List (Missing ))))
261
- // we don't descend any further
262
- errors
263
-
264
- case t : StatSeq =>
265
- localVisit(skip, fallback, lines)(t.firstStatOpt) ++ (
266
- for (statOpt <- t.otherStats)
267
- yield localVisit(skip, statOpt._1.associatedWhitespaceAndComments, lines)(statOpt._2)
268
- ).flatten
269
-
270
- case t : Any =>
271
- // anything else, we descend (unless we stopped above)
272
- visit(t, localVisit(skip, fallback, lines))
190
+ def shouldSkip (node : AstNode ) = skip || tokensToIgnore.contains(node.getClass.getSimpleName)
191
+
192
+ ast match {
193
+ case t : FullDefOrDcl =>
194
+ // private, private[xxx];
195
+ // protected, protected[xxx];
196
+
197
+ // check if we are going to include or skip depending on access modifier
198
+ val accessModifier = t.modifiers.find {
199
+ case AccessModifier (_, _) => true
200
+ case _ => false
201
+ }
202
+ val skip = accessModifier.exists {
203
+ case AccessModifier (pop, Some (_)) =>
204
+ if (pop.text == " private" ) skipQualifiedPrivate else skipQualifiedProtected
205
+ case AccessModifier (pop, None ) =>
206
+ if (pop.text == " private" ) skipPrivate else skipProtected
207
+ case _ =>
208
+ false
209
+ }
210
+
211
+ // pick the ScalaDoc "attached" to the modifier, which actually means
212
+ // ScalaDoc of the following member
213
+ val scalaDocs = for {
214
+ token <- t.tokens
215
+ comment <- token.associatedWhitespaceAndComments
216
+ if comment.token.isScalaDocComment
217
+ } yield comment
218
+
219
+ // descend
220
+ visit(t, localVisit(skip, HiddenTokens (fallback.tokens ++ scalaDocs), lines, tokensToIgnore))
221
+ case t : TmplDef =>
222
+ // trait Foo, trait Foo[A];
223
+ // class Foo, class Foo[A](a: A);
224
+ // case class Foo(), case class Foo[A](a: A);
225
+ // object Foo;
226
+ val (_, line) = lines.findLineAndIndex(t.firstToken.offset).get
227
+
228
+ // we are checking parameters and type parameters
229
+ val errors = if (shouldSkip(t)) Nil
230
+ else findScalaDoc(t.firstToken, fallback).
231
+ map { scalaDoc =>
232
+ paramErrors(line, t.paramClausesOpt)(scalaDoc) ++
233
+ tparamErrors(line, t.typeParamClauseOpt)(scalaDoc)
234
+ }.getOrElse(List (LineError (line, List (Missing ))))
235
+
236
+ // and we descend, because we're interested in seeing members of the types
237
+ errors ++ visit(t, localVisit(skip, NoHiddenTokens , lines, tokensToIgnore))
238
+ case t : FunDefOrDcl =>
239
+ // def foo[A, B](a: Int): B = ...
240
+ val (_, line) = lines.findLineAndIndex(t.firstToken.offset).get
241
+
242
+ // we are checking parameters, type parameters and returns
243
+ val errors = if (shouldSkip(t)) Nil
244
+ else findScalaDoc(t.firstToken, fallback).
245
+ map { scalaDoc =>
246
+ paramErrors(line, Some (t.paramClauses))(scalaDoc) ++
247
+ tparamErrors(line, t.typeParamClauseOpt)(scalaDoc) ++
248
+ returnErrors(line, t.returnTypeOpt)(scalaDoc)
249
+ }.
250
+ getOrElse(List (LineError (line, List (Missing ))))
251
+
252
+ // we don't descend any further
253
+ errors
254
+ case t : TypeDefOrDcl =>
255
+ // type Foo = ...
256
+ val (_, line) = lines.findLineAndIndex(t.firstToken.offset).get
257
+
258
+ // error is non-existence
259
+ val errors = if (shouldSkip(t)) Nil
260
+ else findScalaDoc(t.firstToken, fallback).
261
+ map(_ => Nil ).
262
+ getOrElse(List (LineError (line, List (Missing ))))
263
+
264
+ // we don't descend any further
265
+ errors
266
+
267
+ case t : PatDefOrDcl =>
268
+ // val a = ...
269
+ // var a = ...
270
+ val (_, line) = lines.findLineAndIndex(t.valOrVarToken.offset).get
271
+ val errors = if (shouldSkip(t)) Nil
272
+ else findScalaDoc(t.firstToken, fallback).
273
+ map(_ => Nil ).
274
+ getOrElse(List (LineError (line, List (Missing ))))
275
+ // we don't descend any further
276
+ errors
277
+
278
+ case t : StatSeq =>
279
+ localVisit(skip, fallback, lines, tokensToIgnore)(t.firstStatOpt) ++ (
280
+ for (statOpt <- t.otherStats)
281
+ yield localVisit(skip, statOpt._1.associatedWhitespaceAndComments, lines, tokensToIgnore)(statOpt._2)
282
+ ).flatten
283
+
284
+ case t : Any =>
285
+ // anything else, we descend (unless we stopped above)
286
+ visit(t, localVisit(skip, fallback, lines, tokensToIgnore))
287
+ }
288
+ }
289
+
290
+ private def assertTokensToIgnore (tokensToIgnore : Iterable [String ]): Unit = {
291
+ val wrongTokensToIgnore = tokensToIgnore.filterNot(availableTokensToIgnore.contains)
292
+ if (wrongTokensToIgnore.nonEmpty) {
293
+ throw new IllegalArgumentException (s " ignoreTokenTypes contained wrong types: $wrongTokensToIgnore, " +
294
+ s " available types are $availableTokensToIgnore" )
295
+ }
273
296
}
274
297
275
298
}
@@ -278,6 +301,13 @@ class ScalaDocChecker extends CombinedChecker {
278
301
* Contains the ScalaDoc model with trivial parsers
279
302
*/
280
303
object ScalaDocChecker {
304
+
305
+ private val availableTokensToIgnore = Set (classOf [PatDefOrDcl ],
306
+ classOf [TypeDefOrDcl ],
307
+ classOf [FunDefOrDcl ],
308
+ classOf [TmplDef ])
309
+ .map(_.getSimpleName)
310
+
281
311
val Missing = " Missing"
282
312
def missingParam (name : String ): String = " Missing @param " + name
283
313
def extraParam (name : String ): String = " Extra @param " + name
0 commit comments