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

Import Control checker (port from Checkstyle) #244

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions import-control.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<import-control pkg="org.scalastyle" regex="false">
<allow pkg="org.scalastyle" exact-match="false" local-only="false" regex="false" />
<allow pkg="java" exact-match="false" local-only="false" />
<allow pkg="scala" exact-match="false" local-only="false" />
<allow pkg="(?:_root_\.)?scalariform" exact-match="false" local-only="false" />
<allow pkg="com.typesafe.config" exact-match="false" regex="false" />
<subpackage name="file">
<disallow pkg="(?:_root_\.)?org.scalariform" exact-match="false" />
</subpackage>
<subpackage name="util">
<allow pkg="com.typesafe.config" exact-match="false" regex="false" />
</subpackage>
</import-control>
5 changes: 5 additions & 0 deletions src/main/resources/default_config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,9 @@
</check>
<check class="org.scalastyle.scalariform.CurliesImportChecker" level="warning" enabled="false" />
<check class="org.scalastyle.scalariform.ThrowChecker" level="warning" enabled="true" />
<check level="warning" class="org.scalastyle.scalariform.ImportControlChecker" enabled="true">
<parameters>
<parameter name="file"><![CDATA[import-control.xml]]></parameter>
</parameters>
</check>
</scalastyle>
107 changes: 107 additions & 0 deletions src/main/resources/import_control_1_4.dtd
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<!-- Add the following to any file that is to be validated against this DTD:

<!DOCTYPE import-control PUBLIC
"-//Checkstyle//DTD ImportControl Configuration 1.4//EN"
"https://checkstyle.org/dtds/import_control_1_4.dtd">
-->

<!--
The root element of the configuration file.
-->
<!ELEMENT import-control ((allow|disallow)*,(subpackage|file)*)>

<!--
pkg - The root package to be checked. For example "com.puppycrawl".

regex - Root package name has to be interpreted as
regular expression.

strategyOnMismatch - Strategy in a case if matching allow/disallow rule
was not found. Possible values: allowed, disallowed.
If not defined explicitly, has "disallowed" value by default.
-->
<!ATTLIST import-control
pkg CDATA #REQUIRED
strategyOnMismatch (allowed|disallowed) #IMPLIED
regex (true) #IMPLIED>

<!--
Represents a subpackage of the parent element.
-->
<!ELEMENT subpackage ((allow|disallow)*,(subpackage|file)*)>

<!--
name - The name of the subpackage. For example if the name is "tools"
and the parent is "com.puppycrawl", then it corresponds to the
package "com.puppycrawl.tools". If the regex attribute is "true" the
name is interpreted as a regular expression.

regex - Subpackage name has to be interpreted as
regular expression.

strategyOnMismatch - Strategy in a case if matching allow/disallow rule
was not found. Possible values: allowed, disallowed, delegateToParent.
If not defined explicitly, has "delegateToParent" value by default.
-->
<!ATTLIST subpackage
name CDATA #REQUIRED
strategyOnMismatch (delegateToParent|allowed|disallowed) #IMPLIED
regex (true) #IMPLIED>

<!--
Represents a file of the parent element.
-->
<!ELEMENT file (allow|disallow)*>

<!--
name - The name of the file.
If the regex attribute is "true" the name is interpreted as a regular expression.

regex - File name has to be interpreted as regular expression.
-->
<!ATTLIST file
name CDATA #REQUIRED
regex (true) #IMPLIED>

<!--
Represents attributes for an import rule which can either allow or
disallow access.

pkg - The fully qualified name of the package to allow/disallow.
Cannot be specified in conjunction with "class".

class - The fully qualified name of the class to allow/disallow.
Cannot be specified in conjunction with "pkg".

exact-match - Only valid with "pkg". Specifies whether the package
name matching should be exact. For example, the pkg
"com.puppycrawl.tools" will match the import
"com.puppycrawl.tools.checkstyle.api.*" when the option is not set,
but will not match if the option is set.

local-only - Indicates that the rule is to apply only to the current
package and not to subpackages.

regex - Indicates that the class or package name has to be interpreted as
regular expression.
-->
<!ENTITY % attlist.importrule "
pkg CDATA #IMPLIED
exact-match (true) #IMPLIED
class CDATA #IMPLIED
local-only (true) #IMPLIED
regex (true) #IMPLIED">

<!--
Represents an import rule that will allow access.
-->
<!ELEMENT allow EMPTY>
<!ATTLIST allow
%attlist.importrule;>

<!--
Represents an import rule that will disallow access.
-->
<!ELEMENT disallow EMPTY>
<!ATTLIST disallow
%attlist.importrule;>
6 changes: 6 additions & 0 deletions src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,9 @@ disallow.case.brace.description = "Checks that braces aren't used in case clause
throw.message = "Avoid using throw statements."
throw.label = "No throw statements."
throw.description = "Checks that throw is not used."

import.control.message = "Import not allowed: ''{0}'' by ''{1}'' in package {2}"
import.control.label = "Control imports per file or package"
import.control.description = "Control imports per file or package"
import.control.file.label = "File configuring imports per file or package"
import.control.file.description = "File configuring imports per file or package"
5 changes: 5 additions & 0 deletions src/main/resources/scalastyle_definition.xml
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,9 @@
</checker>
<checker class="org.scalastyle.scalariform.WhileBraceChecker" id="while.brace" defaultLevel="warning"/>
<checker class="org.scalastyle.scalariform.CaseBraceChecker" id="disallow.case.brace" defaultLevel="warning"/>
<checker class="org.scalastyle.scalariform.ImportControlChecker" id="import.control" defaultLevel="warning">
<parameters>
<parameter name="file" type="string" />
</parameters>
</checker>
</scalastyle-definition>
15 changes: 15 additions & 0 deletions src/main/resources/scalastyle_documentation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -905,4 +905,19 @@ To fix it, replace the (unicode operator) `⇒` with `=>`.
]]>
</example-configuration>
</check>
<check id="import.control">
<justification>
Allow fine-grained control over imports in files and packages.
</justification>
<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>
<example-configuration>
<![CDATA[
<check level="warning" class="org.scalastyle.scalariform.ImportControlChecker" enabled="true">
<parameters>
<parameter name="file">import-control.xml</parameter>
</parameters>
</check>
]]>
</example-configuration>
</check>
</scalastyle-documentation>
Loading