|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | + |
| 19 | +package org.apache.spark.scalastyle |
| 20 | + |
| 21 | +import java.util.regex.Pattern |
| 22 | +import org.scalastyle.PositionError |
| 23 | +import org.scalastyle.ScalariformChecker |
| 24 | +import org.scalastyle.ScalastyleError |
| 25 | +import scalariform.lexer.MultiLineComment |
| 26 | +import scalariform.lexer.ScalaDocComment |
| 27 | +import scalariform.lexer.SingleLineComment |
| 28 | +import scalariform.parser.CompilationUnit |
| 29 | +import scalariform.lexer.Token |
| 30 | + |
| 31 | +class SparkSpaceAfterCommentStartChecker extends ScalariformChecker { |
| 32 | + val errorKey: String = "space.after.comment.start" |
| 33 | + |
| 34 | + private def multiLineCommentRegex(comment: Token) = |
| 35 | + Pattern.compile( """/\*\S+.*""", Pattern.DOTALL).matcher(comment.text.trim).matches() |
| 36 | + |
| 37 | + private def scalaDocPatternRegex(comment: Token) = |
| 38 | + Pattern.compile( """/\*\*\S+.*""", Pattern.DOTALL).matcher(comment.text.trim).matches() |
| 39 | + |
| 40 | + private def singleLineCommentRegex(comment: Token): Boolean = |
| 41 | + comment.text.trim.matches( """//\S+.*""") && !comment.text.trim.matches( """///+""") |
| 42 | + |
| 43 | + override def verify(ast: CompilationUnit): List[ScalastyleError] = { |
| 44 | + ast.tokens |
| 45 | + .filter(hasComment) |
| 46 | + .map { |
| 47 | + _.associatedWhitespaceAndComments.comments.map { |
| 48 | + case x: SingleLineComment if singleLineCommentRegex(x.token) => Some(x.token.offset) |
| 49 | + case x: MultiLineComment if multiLineCommentRegex(x.token) => Some(x.token.offset) |
| 50 | + case x: ScalaDocComment if scalaDocPatternRegex(x.token) => Some(x.token.offset) |
| 51 | + case _ => None |
| 52 | + }.flatten |
| 53 | + }.flatten.map(PositionError(_)) |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + private def hasComment(x: Token) = |
| 58 | + x.associatedWhitespaceAndComments != null && !x.associatedWhitespaceAndComments.comments.isEmpty |
| 59 | + |
| 60 | +} |
0 commit comments