Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit 6b2425c

Browse files
committed
Import Control checker (port from Checkstyle)
1 parent cff4fde commit 6b2425c

File tree

8 files changed

+854
-28
lines changed

8 files changed

+854
-28
lines changed

src/main/resources/default_config.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,4 +309,9 @@
309309
</check>
310310
<check class="org.scalastyle.scalariform.CurliesImportChecker" level="warning" enabled="false" />
311311
<check class="org.scalastyle.scalariform.ThrowChecker" level="warning" enabled="true" />
312+
<check level="warning" class="org.scalastyle.scalariform.ImportControlChecker" enabled="true">
313+
<parameters>
314+
<parameter name="file"><![CDATA[import-control.xml]]></parameter>
315+
</parameters>
316+
</check>
312317
</scalastyle>
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<!-- Add the following to any file that is to be validated against this DTD:
2+
3+
<!DOCTYPE import-control PUBLIC
4+
"-//Checkstyle//DTD ImportControl Configuration 1.4//EN"
5+
"https://checkstyle.org/dtds/import_control_1_4.dtd">
6+
-->
7+
8+
<!--
9+
The root element of the configuration file.
10+
-->
11+
<!ELEMENT import-control ((allow|disallow)*,(subpackage|file)*)>
12+
13+
<!--
14+
pkg - The root package to be checked. For example "com.puppycrawl".
15+
16+
regex - Root package name has to be interpreted as
17+
regular expression.
18+
19+
strategyOnMismatch - Strategy in a case if matching allow/disallow rule
20+
was not found. Possible values: allowed, disallowed.
21+
If not defined explicitly, has "disallowed" value by default.
22+
-->
23+
<!ATTLIST import-control
24+
pkg CDATA #REQUIRED
25+
strategyOnMismatch (allowed|disallowed) #IMPLIED
26+
regex (true) #IMPLIED>
27+
28+
<!--
29+
Represents a subpackage of the parent element.
30+
-->
31+
<!ELEMENT subpackage ((allow|disallow)*,(subpackage|file)*)>
32+
33+
<!--
34+
name - The name of the subpackage. For example if the name is "tools"
35+
and the parent is "com.puppycrawl", then it corresponds to the
36+
package "com.puppycrawl.tools". If the regex attribute is "true" the
37+
name is interpreted as a regular expression.
38+
39+
regex - Subpackage name has to be interpreted as
40+
regular expression.
41+
42+
strategyOnMismatch - Strategy in a case if matching allow/disallow rule
43+
was not found. Possible values: allowed, disallowed, delegateToParent.
44+
If not defined explicitly, has "delegateToParent" value by default.
45+
-->
46+
<!ATTLIST subpackage
47+
name CDATA #REQUIRED
48+
strategyOnMismatch (delegateToParent|allowed|disallowed) #IMPLIED
49+
regex (true) #IMPLIED>
50+
51+
<!--
52+
Represents a file of the parent element.
53+
-->
54+
<!ELEMENT file (allow|disallow)*>
55+
56+
<!--
57+
name - The name of the file.
58+
If the regex attribute is "true" the name is interpreted as a regular expression.
59+
60+
regex - File name has to be interpreted as regular expression.
61+
-->
62+
<!ATTLIST file
63+
name CDATA #REQUIRED
64+
regex (true) #IMPLIED>
65+
66+
<!--
67+
Represents attributes for an import rule which can either allow or
68+
disallow access.
69+
70+
pkg - The fully qualified name of the package to allow/disallow.
71+
Cannot be specified in conjunction with "class".
72+
73+
class - The fully qualified name of the class to allow/disallow.
74+
Cannot be specified in conjunction with "pkg".
75+
76+
exact-match - Only valid with "pkg". Specifies whether the package
77+
name matching should be exact. For example, the pkg
78+
"com.puppycrawl.tools" will match the import
79+
"com.puppycrawl.tools.checkstyle.api.*" when the option is not set,
80+
but will not match if the option is set.
81+
82+
local-only - Indicates that the rule is to apply only to the current
83+
package and not to subpackages.
84+
85+
regex - Indicates that the class or package name has to be interpreted as
86+
regular expression.
87+
-->
88+
<!ENTITY % attlist.importrule "
89+
pkg CDATA #IMPLIED
90+
exact-match (true) #IMPLIED
91+
class CDATA #IMPLIED
92+
local-only (true) #IMPLIED
93+
regex (true) #IMPLIED">
94+
95+
<!--
96+
Represents an import rule that will allow access.
97+
-->
98+
<!ELEMENT allow EMPTY>
99+
<!ATTLIST allow
100+
%attlist.importrule;>
101+
102+
<!--
103+
Represents an import rule that will disallow access.
104+
-->
105+
<!ELEMENT disallow EMPTY>
106+
<!ATTLIST disallow
107+
%attlist.importrule;>

src/main/resources/reference.conf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,3 +411,9 @@ disallow.case.brace.description = "Checks that braces aren't used in case clause
411411
throw.message = "Avoid using throw statements."
412412
throw.label = "No throw statements."
413413
throw.description = "Checks that throw is not used."
414+
415+
import.control.message = "Import not allowed: ''{0}'' by ''{1}'' in package {2}"
416+
import.control.label = "Control imports per file or package"
417+
import.control.description = "Control imports per file or package"
418+
import.control.file.label = "File configuring imports per file or package"
419+
import.control.file.description = "File configuring imports per file or package"

src/main/resources/scalastyle_definition.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,9 @@
218218
</checker>
219219
<checker class="org.scalastyle.scalariform.WhileBraceChecker" id="while.brace" defaultLevel="warning"/>
220220
<checker class="org.scalastyle.scalariform.CaseBraceChecker" id="disallow.case.brace" defaultLevel="warning"/>
221+
<checker class="org.scalastyle.scalariform.ImportControlChecker" id="import.control" defaultLevel="warning">
222+
<parameters>
223+
<parameter name="file" type="string" />
224+
</parameters>
225+
</checker>
221226
</scalastyle-definition>

src/main/resources/scalastyle_documentation.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,4 +905,19 @@ To fix it, replace the (unicode operator) `⇒` with `=>`.
905905
]]>
906906
</example-configuration>
907907
</check>
908+
<check id="import.control">
909+
<justification>
910+
Allow fine-grained control over imports in files and packages.
911+
</justification>
912+
<extra-description>The import control specification is the exact same as Checkstyle's Import Control checker. See https://checkstyle.sourceforge.io/config_imports.html#ImportControl for more information.</extra-description>
913+
<example-configuration>
914+
<![CDATA[
915+
<check level="warning" class="org.scalastyle.scalariform.ImportControlChecker" enabled="true">
916+
<parameters>
917+
<parameter name="file">import-control.xml</parameter>
918+
</parameters>
919+
</check>
920+
]]>
921+
</example-configuration>
922+
</check>
908923
</scalastyle-documentation>

0 commit comments

Comments
 (0)