Skip to content

Commit ba33193

Browse files
committed
scala style as a project
1 parent 007a733 commit ba33193

File tree

4 files changed

+101
-1
lines changed

4 files changed

+101
-1
lines changed

project/SparkBuild.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import sbt._
1919
import sbt.Classpaths.publishTask
20-
import Keys._
20+
import sbt.Keys._
2121
import sbtassembly.Plugin._
2222
import AssemblyKeys._
2323
import scala.util.Properties
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
import sbt._
19+
import sbt.Keys._
20+
21+
object SparkPluginDef extends Build {
22+
lazy val root = Project("plugins", file(".")) dependsOn(sparkStyle)
23+
lazy val sparkStyle = Project("spark-style", file("spark-style"), settings = styleSettings)
24+
val sparkVersion = "1.0.0-SNAPSHOT"
25+
// There is actually no need to publish this artifact.
26+
def styleSettings = Defaults.defaultSettings ++ Seq (
27+
name := "spark-style",
28+
organization := "org.apache.spark",
29+
version := sparkVersion,
30+
scalaVersion := "2.10.3",
31+
scalacOptions := Seq("-unchecked", "-deprecation"),
32+
libraryDependencies ++= Dependencies.scalaStyle,
33+
sbtPlugin := true
34+
)
35+
36+
object Dependencies {
37+
val scalaStyle = Seq("org.scalastyle" %% "scalastyle" % "0.4.0")
38+
}
39+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
}

scalastyle-config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,5 @@
140140
<!-- <check level="error" class="org.scalastyle.scalariform.PublicMethodsHaveTypeChecker" enabled="true"></check> -->
141141
<check level="error" class="org.scalastyle.file.NewLineAtEofChecker" enabled="true"></check>
142142
<check level="error" class="org.scalastyle.file.NoNewLineAtEofChecker" enabled="false"></check>
143+
<check level="error" class="org.apache.spark.scalastyle.SparkSpaceAfterCommentStartChecker" enabled="true"></check>
143144
</scalastyle>

0 commit comments

Comments
 (0)