-
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.
- Loading branch information
Showing
19 changed files
with
337 additions
and
9 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
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
63 changes: 63 additions & 0 deletions
63
magik-checks/src/main/java/org/stevenlooman/sw/magik/checks/ScopeCountCheck.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,63 @@ | ||
package org.stevenlooman.sw.magik.checks; | ||
|
||
import com.sonar.sslr.api.AstNode; | ||
import com.sonar.sslr.api.AstNodeType; | ||
import org.sonar.check.Rule; | ||
import org.sonar.check.RuleProperty; | ||
import org.stevenlooman.sw.magik.MagikCheck; | ||
import org.stevenlooman.sw.magik.analysis.scope.GlobalScope; | ||
import org.stevenlooman.sw.magik.analysis.scope.Scope; | ||
import org.stevenlooman.sw.magik.analysis.scope.ScopeEntry; | ||
import org.stevenlooman.sw.magik.api.MagikGrammar; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Rule(key = ScopeCountCheck.CHECK_KEY) | ||
public class ScopeCountCheck extends MagikCheck { | ||
|
||
public static final String CHECK_KEY = "ScopeCount"; | ||
private static final String MESSAGE = "Too many variables in scope."; | ||
private static final int DEFAULT_MAX_SCOPE_COUNT = 20; | ||
@RuleProperty( | ||
key = "max scope count", | ||
defaultValue = "" + DEFAULT_MAX_SCOPE_COUNT, | ||
description = "Maximum number of entries in scope") | ||
public int maxScopeCount = DEFAULT_MAX_SCOPE_COUNT; | ||
|
||
@Override | ||
public boolean isTemplatedCheck() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public List<AstNodeType> subscribedTo() { | ||
return Arrays.asList( | ||
MagikGrammar.METHOD_DEFINITION, | ||
MagikGrammar.PROC_DEFINITION); | ||
} | ||
|
||
/** | ||
* Test if there are too many entries in the method/procedure scope. | ||
* @param node Node to check. | ||
*/ | ||
@Override | ||
public void leaveNode(AstNode node) { | ||
GlobalScope globalScope = getContext().getGlobalScope(); | ||
if (globalScope == null) { | ||
return; | ||
} | ||
|
||
Scope procedureScope = globalScope.getScopeForNode(node); | ||
List<ScopeEntry> procedureScopeEntries = procedureScope.getSelfAndDescendantScopes().stream() | ||
.map(scope -> scope.getScopeEntries()) | ||
.flatMap(scopeEntries -> scopeEntries.stream()) | ||
.filter(scopeEntry -> scopeEntry.getType() != ScopeEntry.Type.IMPORT) | ||
.collect(Collectors.toList()); | ||
if (procedureScopeEntries.size() > maxScopeCount) { | ||
addIssue(MESSAGE, node); | ||
} | ||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
magik-checks/src/main/java/org/stevenlooman/sw/magik/checks/UndefinedVariableCheck.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,65 @@ | ||
package org.stevenlooman.sw.magik.checks; | ||
|
||
import com.sonar.sslr.api.AstNode; | ||
import com.sonar.sslr.api.AstNodeType; | ||
import org.sonar.check.Rule; | ||
import org.stevenlooman.sw.magik.MagikCheck; | ||
import org.stevenlooman.sw.magik.analysis.scope.GlobalScope; | ||
import org.stevenlooman.sw.magik.analysis.scope.Scope; | ||
import org.stevenlooman.sw.magik.analysis.scope.ScopeEntry; | ||
import org.stevenlooman.sw.magik.api.MagikGrammar; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
@Rule(key = UndefinedVariableCheck.CHECK_KEY) | ||
public class UndefinedVariableCheck extends MagikCheck { | ||
|
||
public static final String CHECK_KEY = "UndefinedVariable"; | ||
private static final String MESSAGE = | ||
"Variable %s is expected to be declared, but used as a global."; | ||
|
||
@Override | ||
public boolean isTemplatedCheck() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public List<AstNodeType> subscribedTo() { | ||
return Arrays.asList( | ||
MagikGrammar.METHOD_DEFINITION, | ||
MagikGrammar.PROC_DEFINITION); | ||
} | ||
|
||
/** | ||
* Test if any global variable in the method/procedure scope has a prefix. | ||
* @param node Node to check. | ||
*/ | ||
@Override | ||
public void leaveNode(AstNode node) { | ||
GlobalScope globalScope = getContext().getGlobalScope(); | ||
if (globalScope == null) { | ||
return; | ||
} | ||
|
||
Scope procedureScope = globalScope.getScopeForNode(node); | ||
procedureScope.getSelfAndDescendantScopes().stream() | ||
.map(scope -> scope.getScopeEntries()) | ||
.flatMap(scopeEntries -> scopeEntries.stream()) | ||
.filter(scopeEntry -> scopeEntry.getType() == ScopeEntry.Type.GLOBAL) | ||
.filter(scopeEntry -> { | ||
String identifier = scopeEntry.getIdentifier().toLowerCase(); | ||
return identifier.startsWith("l_") | ||
|| identifier.startsWith("i_") | ||
|| identifier.startsWith("p_") | ||
|| identifier.startsWith("c_"); | ||
}) | ||
.forEach(scopeEntry -> { | ||
AstNode scopeEntryNode = scopeEntry.getNode(); | ||
String identifier = scopeEntry.getIdentifier(); | ||
String message = String.format(MESSAGE, identifier); | ||
addIssue(message, scopeEntryNode); | ||
}); | ||
} | ||
|
||
} |
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
1 change: 1 addition & 0 deletions
1
magik-checks/src/main/resources/org/stevenlooman/sw/sonar/l10n/magik/rules/ScopeCount.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>The number of variables in scope is too high.</p> |
15 changes: 15 additions & 0 deletions
15
magik-checks/src/main/resources/org/stevenlooman/sw/sonar/l10n/magik/rules/ScopeCount.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,15 @@ | ||
{ | ||
"title": "Too many variables in scope", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "30min" | ||
}, | ||
"tags": [ | ||
"complexity" | ||
], | ||
"defaultSeverity": "Major", | ||
"ruleSpecification": "ScopeCount", | ||
"sqKey": "scope-count" | ||
} |
14 changes: 14 additions & 0 deletions
14
...ecks/src/main/resources/org/stevenlooman/sw/sonar/l10n/magik/rules/UndefinedVariable.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,14 @@ | ||
<p>The prefixed variable indicates it should have been declared in the local scope, but was used as a global.</p> | ||
<h2>Noncompliant Code Example</h2> | ||
<pre> | ||
_method object.do_something() | ||
write(l_a) | ||
_endmethod | ||
</pre> | ||
<h2>Compliant Solution</h2> | ||
<pre> | ||
_method object.do_something() | ||
_local l_a << 10 | ||
write(l_a) | ||
_endmethod | ||
</pre> |
15 changes: 15 additions & 0 deletions
15
...ecks/src/main/resources/org/stevenlooman/sw/sonar/l10n/magik/rules/UndefinedVariable.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,15 @@ | ||
{ | ||
"title": "Prefixed variable used as a global", | ||
"type": "BUG", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "15min" | ||
}, | ||
"tags": [ | ||
"error" | ||
], | ||
"defaultSeverity": "Major", | ||
"ruleSpecification": "UndefinedVariable", | ||
"sqKey": "undefined-variable" | ||
} |
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
49 changes: 49 additions & 0 deletions
49
magik-checks/src/test/java/org/stevenlooman/sw/magik/checks/ScopeCountCheckTest.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,49 @@ | ||
package org.stevenlooman.sw.magik.checks; | ||
|
||
import static org.fest.assertions.Assertions.assertThat; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.Test; | ||
import org.stevenlooman.sw.magik.MagikIssue; | ||
|
||
public class ScopeCountCheckTest extends MagikCheckTestBase { | ||
|
||
@Test | ||
public void testTooManyScopeEntries() { | ||
ScopeCountCheck check = new ScopeCountCheck(); | ||
check.maxScopeCount = 1; | ||
String code = | ||
"_method a.b\n" + | ||
"\t_local l_a, l_b\n" + | ||
"_endmethod"; | ||
List<MagikIssue> issues = runCheck(code, check); | ||
assertThat(issues).hasSize(1); | ||
} | ||
|
||
@Test | ||
public void testOk() { | ||
ScopeCountCheck check = new ScopeCountCheck(); | ||
check.maxScopeCount = 10; | ||
String code = | ||
"_method a.b\n" + | ||
"\t_local l_a, l_b\n" + | ||
"_endmethod"; | ||
List<MagikIssue> issues = runCheck(code, check); | ||
assertThat(issues).isEmpty(); | ||
} | ||
|
||
@Test | ||
public void testTooManyScopeEntriesGlobals() { | ||
ScopeCountCheck check = new ScopeCountCheck(); | ||
check.maxScopeCount = 1; | ||
String code = | ||
"_method a.b\n" + | ||
"\t_global a, b\n" + | ||
"_endmethod"; | ||
List<MagikIssue> issues = runCheck(code, check); | ||
assertThat(issues).hasSize(1); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.