-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FileWithoutPackageStatement check
- Loading branch information
1 parent
022985c
commit 232c351
Showing
7 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...rc/main/java/nl/ramsolutions/sw/magik/checks/checks/FileWithoutPackageStatementCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package nl.ramsolutions.sw.magik.checks.checks; | ||
|
||
import com.sonar.sslr.api.AstNode; | ||
import nl.ramsolutions.sw.magik.api.MagikGrammar; | ||
import nl.ramsolutions.sw.magik.checks.MagikCheck; | ||
import org.sonar.check.Rule; | ||
|
||
/** Check if file contains a _package-statement. */ | ||
@Rule(key = FileWithoutPackageStatementCheck.CHECK_KEY) | ||
public class FileWithoutPackageStatementCheck extends MagikCheck { | ||
|
||
@SuppressWarnings("checkstyle:JavadocVariable") | ||
public static final String CHECK_KEY = "FileWithoutPackageStatement"; | ||
|
||
private static final String MESSAGE = "File has no package-statement."; | ||
|
||
@Override | ||
protected void walkPostMagik(final AstNode node) { | ||
if (!hasPackageStatement(node)) { | ||
this.addFileIssue(MESSAGE); | ||
} | ||
} | ||
|
||
private boolean hasPackageStatement(final AstNode node) { | ||
return node.getChildren(MagikGrammar.PACKAGE_SPECIFICATION).stream().findAny().isPresent(); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...main/resources/nl/ramsolutions/sw/sonar/l10n/magik/rules/FileWithoutPackageStatement.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<p>File should have at least one <pre>_package</pre>-statement. This ensures the code is loaded into the correct package.</p> |
16 changes: 16 additions & 0 deletions
16
...main/resources/nl/ramsolutions/sw/sonar/l10n/magik/rules/FileWithoutPackageStatement.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"title": "File has no package-statement", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "5min" | ||
}, | ||
"tags": [ | ||
"bad-practice", | ||
"confusing" | ||
], | ||
"defaultSeverity": "Minor", | ||
"ruleSpecification": "FileWithoutPackageStatement", | ||
"sqKey": "file-without-package-statement" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...est/java/nl/ramsolutions/sw/magik/checks/checks/FileWithoutPackageStatementCheckTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package nl.ramsolutions.sw.magik.checks.checks; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.List; | ||
import nl.ramsolutions.sw.magik.checks.MagikIssue; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** Test FileWithoutPackageStatementCheck. */ | ||
class FileWithoutPackageStatementCheckTest extends MagikCheckTestBase { | ||
|
||
@Test | ||
void testNoPackageStatement() { | ||
final String code = | ||
""" | ||
_method a.m1 _endmethod | ||
"""; | ||
final FileWithoutPackageStatementCheck check = new FileWithoutPackageStatementCheck(); | ||
final List<MagikIssue> issues = this.runCheck(code, check); | ||
assertThat(issues).hasSize(1); | ||
} | ||
|
||
@Test | ||
void testPackageStatement() { | ||
final String code = | ||
""" | ||
_package user | ||
_method a.m1 _endmethod | ||
"""; | ||
final FileWithoutPackageStatementCheck check = new FileWithoutPackageStatementCheck(); | ||
final List<MagikIssue> issues = this.runCheck(code, check); | ||
assertThat(issues).isEmpty(); | ||
} | ||
} |