forked from scalastyle/scalastyle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImportsChecker.scala
414 lines (353 loc) · 14.1 KB
/
ImportsChecker.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// Copyright (C) 2011-2012 the original author or authors.
// See the LICENCE.txt file distributed with this work for additional
// information regarding copyright ownership.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.scalastyle.scalariform
import java.util.regex.Pattern
import scala.Array.canBuildFrom
import scala.collection.mutable.ListBuffer
import org.scalastyle.PositionError
import org.scalastyle.ScalariformChecker
import org.scalastyle.ScalastyleError
import scalariform.lexer.MultiLineComment
import scalariform.lexer.Token
import scalariform.lexer.Whitespace
import scalariform.parser.AstNode
import scalariform.parser.BlockImportExpr
import scalariform.parser.CompilationUnit
import scalariform.parser.Expr
import scalariform.parser.ExprElement
import scalariform.parser.GeneralTokens
import scalariform.parser.ImportClause
import scalariform.parser.ImportSelectors
import VisitorHelper.visit
abstract class AbstractImportChecker extends ScalariformChecker {
case class ImportClauseVisit(t: ImportClause, importExpr: List[ImportClauseVisit], otherImportExprs: List[ImportClauseVisit]);
def verify(ast: CompilationUnit): List[ScalastyleError] = {
init()
val it = for {
t <- localvisit(ast.immediateChildren);
f <- traverse(t)
} yield {
PositionError(t.t.firstToken.offset)
}
it.toList
}
protected def init(): Unit = {}
private[this] def traverse(t: ImportClauseVisit): List[ImportClauseVisit] = {
val l = t.importExpr.map(traverse(_)).flatten ::: t.otherImportExprs.map(traverse(_)).flatten
if (matches(t)) t :: l else l
}
private[this] def imports(tokens: List[Token]): String = {
tokens.foldLeft("")((a, b) => a + b.text)
}
private[this] def imports(t: BlockImportExpr): List[String] = {
val is = t.importSelectors
val firsts = is.firstImportSelector.firstToken.text ::
is.otherImportSelectors.map(_._2).map(is => is.firstToken.text)
firsts.map(f => imports(t.prefixExpr.tokens) + f)
}
protected final def imports(t: ImportClauseVisit): List[String] = {
t.t.importExpr match {
case t: BlockImportExpr => imports(t)
case _ => List(imports(t.t.importExpr.tokens))
}
}
def matches(t: ImportClauseVisit): Boolean
private[this] def localvisit(ast: Any): List[ImportClauseVisit] = ast match {
case t: ImportClause => List(ImportClauseVisit(t, localvisit(t.importExpr), localvisit(t.otherImportExprs)))
case t: Any => visit(t, localvisit)
}
}
class IllegalImportsChecker extends AbstractImportChecker {
val errorKey = "illegal.imports"
val DefaultIllegalImports = "sun._"
var illegalImportsList: List[String] = _
var exemptImportsList: List[String] = _
// sun._ => sun\.
// sun.com.foobar => sun\.com\.foobar
private def toMatchList(s: String) = {
s.trim().split(" *, *").map(s => s.replaceAll("_$", "")).toList
}
override protected def init() = {
illegalImportsList = toMatchList(getString("illegalImports", DefaultIllegalImports))
exemptImportsList = toMatchList(getString("exemptImports", ""))
}
def matches(t: ImportClauseVisit): Boolean = {
val list = imports(t)
val revisedList = list diff exemptImportsList
illegalImportsList.exists(ill => revisedList.exists(_.startsWith(ill)))
}
}
class UnderscoreImportChecker extends AbstractImportChecker {
val errorKey = "underscore.import"
def matches(t: ImportClauseVisit): Boolean = imports(t).exists(_.endsWith("._"))
}
class ImportGroupingChecker extends ScalariformChecker {
val errorKey = "import.grouping"
def verify(ast: CompilationUnit): List[ScalastyleError] = {
val it = VisitorHelper.getAll[ImportClause](ast.immediateChildren)
if (it.size == 0) {
List()
} else {
val importTokens = it.map(ic => ic.tokens).flatten
val (min, max) = (importTokens.head.offset, importTokens.last.offset)
val s = ast.tokens.find(t => t.offset >= min && t.offset <= max && !t.isNewline && !(t.text == ";") && !importTokens.contains(t))
s match {
case Some(x) => it.dropWhile(ic => ic.firstToken.offset <= x.offset).map(ic => PositionError(ic.firstToken.offset))
case None => List()
}
}
}
}
/**
* Style checker that enforces import ordering. The following configuration parameters are
* available:
*
* - groups: a comma-separated list of group names to consider.
* - maxBlankLines: maximum number of blank lines to allow between groups. The default is "1".
* A value less than 1 disables the blank line limit.
* - group.[groupName]: a regular expression that matches imports that should be in the given
* group.
*
* For example, to check that "java" and "javax" imports are in a separate group at the top of the
* import list, you'd use this config:
*
* <parameter name="groups">java,others</parameter>
* <parameter name="group.java">javax?\..+</parameter>
* <parameter name="group.other">.+</parameter>
*
* Other non-configurable rules:
* - Within each group, import clauses are ordered alphabetically.
* - In multi-import statements, entries are ordered alphabetically, with method / packages
* (assumed to be any string starting with a lower case letter) coming before classes.
*
* Currently, this checker only looks at the top-level list of imports.
*/
class ImportOrderChecker extends ScalariformChecker {
val errorKey: String = "import.ordering"
private var groups: Seq[(String, Pattern)] = _
private var maxBlankLines: Int = _
private var ast: AstNode = _
private var lastImport: AstNode = _
private var currentGroup = 0
private var lastImportInGroup: String = _
override def setParameters(parameters: Map[String, String]): Unit = {
// Note that any exceptions thrown here are swallowed by CheckerUtils and ignored...
require(parameters.contains("groups"))
groups = parameters("groups").split(",").map { name =>
name -> Pattern.compile(parameters(s"group.${name}"))
}
maxBlankLines = parameters.getOrElse("maxBlankLines", "1").toInt
}
override def verify(ast: CompilationUnit): List[ScalastyleError] = {
this.ast = ast
val CompilationUnit(statements, _) = ast
statements.immediateChildren.flatMap { n =>
val result = n match {
case ImportClause(_, BlockImportExpr(prefix, selectors), _) =>
val text = exprToText(prefix.contents)
checkImport(text, n.firstToken.offset) ++ checkSelectors(selectors)
case ImportClause(_, Expr(contents), _) =>
val text = exprToText(contents)
checkImport(text, n.firstToken.offset)
case _ =>
Nil
}
lastImport = n
result
}
}
private def exprToText(contents: List[ExprElement]): String = {
contents.flatMap {
case GeneralTokens(toks) =>
toks.map(_.text)
case n =>
throw new IllegalStateException(s"FIXME: unexpected expr child node $n")
}.mkString("")
}
/**
* Check that the given import belongs to the current group and is ordered correctly within it.
*/
private def checkImport(str: String, offset: Int): Seq[ScalastyleError] = {
val errors = new ListBuffer[ScalastyleError]()
if (!groups(currentGroup)._2.matcher(str).matches()) {
// If a statement doesn't match the current group, there are two options:
// - It belongs to a previous group, in which case an error is flagged.
// - It belongs to a following group, in which case the group index moves forward.
for (i <- 0 to currentGroup - 1) {
if (groups(i)._2.matcher(str).matches()) {
return Seq(newError(offset, "wrongGroup", str, groups(i)._1, groups(currentGroup)._1))
}
}
var nextGroup = currentGroup + 1
while (nextGroup < groups.size && !groups(nextGroup)._2.matcher(str).matches()) {
nextGroup += 1
}
if (nextGroup == groups.size) {
throw new IllegalStateException(s"FIXME: import statement does not match any group: $str")
}
errors ++= checkGroupSeparation(currentGroup, nextGroup, offset)
currentGroup = nextGroup
lastImportInGroup = null
} else {
// If the statement is in the same group, make sure there is no empty line between it and
// the previous import.
errors ++= checkNoSeparator(offset)
}
// Ensure import is in alphabetical order.
if (lastImportInGroup != null && compareImports(lastImportInGroup, str) > 0) {
errors += newError(offset, "wrongOrderInGroup", str, lastImportInGroup)
}
lastImportInGroup = str
errors.toSeq
}
/**
* Check that the imports inside a multi-import block are ordered.
*/
private def checkSelectors(selectors: ImportSelectors): Seq[ScalastyleError] = {
val ImportSelectors(_, first, others, _) = selectors
val errors = new ListBuffer[ScalastyleError]()
val names = Seq(first.contents.head.tokens.head.text) ++
others.map( _._2.contents.head.tokens.head.text)
if (names.size > 1) {
names.sliding(2).foreach { case Seq(left, right) =>
if (compareNames(left, right, false) > 0) {
errors += newError(selectors.firstToken.offset, "wrongOrderInSelector", right, left)
}
}
}
errors.toSeq
}
/**
* When the current import group changes, checks that there is a single empty line between
* the last import statement in the previous group and the first statement in the new one.
*/
private def checkGroupSeparation(
lastGroup: Int,
nextGroup: Int,
nextGroupOffset: Int): Option[ScalastyleError] = {
if (lastGroup != nextGroup && lastImport != null) {
val start = lastImport.lastToken.offset + lastImport.lastToken.length
val separatorLines = countNewLines(start, nextGroupOffset) - 1
val last = groups(lastGroup)._1
val current = groups(nextGroup)._1
if (separatorLines == 0) {
return Some(newError(nextGroupOffset, "missingEmptyLine", last, current))
} else if (maxBlankLines > 0 && separatorLines > maxBlankLines) {
return Some(newError(nextGroupOffset, "tooManyEmptyLines", maxBlankLines, last, current))
}
}
None
}
/**
* Check that there are no empty lines between imports in the same group.
*/
private def checkNoSeparator(offset: Int): Option[ScalastyleError] = {
if (lastImportInGroup != null) {
val start = lastImport.lastToken.offset + lastImport.lastToken.length
if (countNewLines(start, offset) != 1) {
return Some(newError(offset, "noEmptyLine"))
}
}
None
}
/**
* Counts the number of new lines between the given offsets, adjusted for comments.
*/
private def countNewLines(start: Int, end: Int): Int = {
var count = 0
ast.tokens.filter { t => t.offset >= start && t.offset < end }.foreach { t =>
val commentsToken = t.associatedWhitespaceAndComments
if (commentsToken != null) {
var ignoreNext = false
commentsToken.tokens.foreach {
case c: MultiLineComment =>
// Do not count a new line after a multi-line comment.
ignoreNext = true
case w: Whitespace =>
if (!ignoreNext) {
// Assumes "\n" only used for new lines.
count += w.text.filter(_ == '\n').size
}
ignoreNext = true
case _ =>
// Nothing to do.
}
}
}
count
}
/**
* Compares two import statements, comparing each component of the import separately.
*
* The import statements can end with a dangling `.`, meaning they're the start of a
* multi-import block.
*/
private def compareImports(imp1: String, imp2: String): Int = {
val imp1Components = imp1.split("[.]")
val imp2Components = imp2.split("[.]")
val max = math.min(imp1Components.size, imp2Components.size)
for (i <- 0 to (max - 1)) {
val comp1 = imp1Components(i)
val comp2 = imp2Components(i)
val result = compareNames(comp1, comp2, true)
if (result != 0) {
return result
}
}
// At this point, there is still a special case: where one import is a multi-import block
// (and, thus, has no extra components) and another is a wildcard; the wildcard should come
// first.
val diff = imp1Components.size - imp2Components.size
if (diff == -1 && imp1.endsWith(".") && imp2Components.last == "_") {
1
} else if (diff == 1 && imp2.endsWith(".") && imp1Components.last == "_") {
-1
} else {
diff
}
}
/**
* Compares two strings that represent a single imported artifact; this considers lower-case
* names as being "lower" than upper case ones.
*
* @param name1 First name.
* @param name2 Second name.
* @param isImport If true, orders names according to the import statement rules:
* "_" should come before other names, and capital letters should come
* before lower case ones. Otherwise, do the opposite, which are the ordering
* rules for names within a multi-import block.
*/
private def compareNames(name1: String, name2: String, isImport: Boolean): Int = {
if (name1 != "_") {
if (name2 == "_") {
-1 * compareNames(name2, name1, isImport)
} else {
val isName1UpperCase = Character.isUpperCase(name1.codePointAt(0))
val isName2UpperCase = Character.isUpperCase(name2.codePointAt(0))
if (isName1UpperCase == isName2UpperCase) {
name1.compareToIgnoreCase(name2)
} else {
if (isName1UpperCase && !isImport) -1 else 1
}
}
} else {
if (isImport) -1 else 1
}
}
private def newError(offset: Int, errorKey: String, args: Any*): ScalastyleError = {
PositionError(offset, args.map(_.toString).toList, Some(this.errorKey + "." + errorKey))
}
}