Skip to content

Commit

Permalink
Add FileWithoutPackageStatement check
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastiaanspeck committed Nov 17, 2024
1 parent 022985c commit 232c351
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- Add `.sys!perform()` and `.sys!slot()` to ForbiddenCallCheck.
- Do not check abstract method parameters in UnusedVariableCheck if `check-parameters`.
- Fix sslr-magik-toolkit's "Evaluate XPath"-button.
- Add FileWithoutPackageStatement check to test if a file has a package statement.
- Several fixes.

## Breaking changes (reiterated from above)
Expand All @@ -32,6 +33,7 @@
- Add `.sys!perform()` and `.sys!slot()` to ForbiddenCallCheck. This might result in more issues.
- Add SimplifyIf check to Sonar way profile. This might result in more issues.
- Add UnsafeEvaluateInvocation check to Sonar way profile. This might result in more issues.
- Add FileWithoutPackageStatement check to test if a file has a package statement. This might result in more issues.

0.10.1 (2024-08-14)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import nl.ramsolutions.sw.magik.checks.checks.ExemplarSlotCountCheck;
import nl.ramsolutions.sw.magik.checks.checks.FileMethodCountCheck;
import nl.ramsolutions.sw.magik.checks.checks.FileNotInLoadListCheck;
import nl.ramsolutions.sw.magik.checks.checks.FileWithoutPackageStatementCheck;
import nl.ramsolutions.sw.magik.checks.checks.ForbiddenCallCheck;
import nl.ramsolutions.sw.magik.checks.checks.ForbiddenGlobalUsageCheck;
import nl.ramsolutions.sw.magik.checks.checks.ForbiddenInheritanceCheck;
Expand Down Expand Up @@ -68,6 +69,7 @@ public static List<Class<? extends MagikCheck>> getChecks() {
ExemplarSlotCountCheck.class,
FileMethodCountCheck.class,
FileNotInLoadListCheck.class,
FileWithoutPackageStatementCheck.class,
ForbiddenCallCheck.class,
ForbiddenGlobalUsageCheck.class,
ForbiddenInheritanceCheck.class,
Expand Down
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();
}
}
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>
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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"EmptyBlock",
"ExemplarSlotCount",
"FileNotInLoadList",
"FileWithoutPackageStatement",
"ForbiddenCall",
"ForbiddenGlobalUsage",
"HidesVariable",
Expand Down
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();
}
}

0 comments on commit 232c351

Please sign in to comment.