Skip to content

Commit

Permalink
used more of the built into component, less decompile and string
Browse files Browse the repository at this point in the history
manipulation.

added sample parmeter test
  • Loading branch information
ryaneberly committed Oct 16, 2015
1 parent 030b9a4 commit e86dff3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
33 changes: 15 additions & 18 deletions src/main/java/com/cflint/plugins/core/VariableNameChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ro.fortsoft.pf4j.Extension;
import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.Attributes;
import cfml.parsing.cfscript.CFAssignmentExpression;
import cfml.parsing.cfscript.CFExpression;
import cfml.parsing.cfscript.CFFullVarExpression;
import cfml.parsing.cfscript.CFIdentifier;
Expand All @@ -26,25 +27,14 @@ public class VariableNameChecker extends CFLintScannerAdapter {

public void expression(final CFExpression expression, final Context context, final BugList bugs) {
if (expression instanceof CFVarDeclExpression) {
String content = ((CFVarDeclExpression) expression).Decompile(0);
String parts[] = content.split(" ");
String varName = parts[1]; // var name = expression
int lineNo = ((CFVarDeclExpression) expression).getLine() + context.startLine() - 1;

checkNameForBugs(varName, context.getFilename(), lineNo, bugs);
final CFVarDeclExpression cfVarDeclExpression = (CFVarDeclExpression)expression;
int lineNo = expression.getLine() + context.startLine() - 1;
checkNameForBugs(cfVarDeclExpression.getName(), context.getFilename(), lineNo, bugs);
}
else if (expression instanceof CFFullVarExpression) {
String content = ((CFFullVarExpression) expression).Decompile(0);
content = content.replace(".", " ");
content = content.replace("[", " ");
content = content.replace("]", " ");
String parts[] = content.split(" ");
int lineNo = ((CFFullVarExpression) expression).getLine() + context.startLine() - 1;

for (int i = 0; i < parts.length; i++) {
if (!parts[i].matches("\\d+")) {
checkNameForBugs(parts[i], context.getFilename(), lineNo, bugs);
}
final CFFullVarExpression cfFullVarExpression = (CFFullVarExpression)expression;
for(final CFExpression subexpression : cfFullVarExpression.getExpressions()){
expression(subexpression,context,bugs);
}
}
else if (expression instanceof CFIdentifier) {
Expand Down Expand Up @@ -138,7 +128,14 @@ protected boolean endsInNumber(String variable) {
}

protected boolean tooShort(String variable) {
return variable.length() < MIN_VAR_LENGTH;

int minVarLength = MIN_VAR_LENGTH;
if(getParameter("MinLength") !=null){
try {
minVarLength= Integer.parseInt(getParameter("MinLength"));
}catch(Exception e){}
}
return variable.length() < minVarLength;
}

protected boolean tooLong(String variable) {
Expand Down
10 changes: 7 additions & 3 deletions src/test/java/com/cflint/TestCFBugs_VariableNames.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public void setUp() {
pluginMessage = new PluginMessage("VAR_TOO_SHORT");
pluginMessage.setSeverity("INFO");
pluginRule.getMessages().add(pluginMessage);
pluginRule.addParameter("MinLength", "3");

pluginMessage = new PluginMessage("VAR_TOO_LONG");
pluginMessage.setSeverity("INFO");
pluginRule.getMessages().add(pluginMessage);
Expand All @@ -49,7 +51,9 @@ public void setUp() {
pluginMessage.setSeverity("INFO");
pluginRule.getMessages().add(pluginMessage);

cfBugs = new CFLint(conf, new VariableNameChecker());
final VariableNameChecker checker = new VariableNameChecker();
checker.setParameter("MinLength", "3");
cfBugs = new CFLint(conf, checker);
}

@Test
Expand Down Expand Up @@ -202,7 +206,7 @@ public void nameIsTemporyTag() throws ParseException, IOException {
+ "</cffunction>\r\n"
+ "</cfcomponent>";
cfBugs.process(tagSrc, "test");
final List<BugInfo> result = cfBugs.getBugs().getBugList().values().iterator().next();
final List<BugInfo> result = cfBugs.getBugs().getBugList().get("VAR_IS_TEMPORARY");
assertEquals(7, result.size());
assertEquals("VAR_IS_TEMPORARY", result.get(0).getMessageCode());
assertEquals(3, result.get(0).getLine());
Expand Down Expand Up @@ -403,7 +407,7 @@ public void nameIsTemporyScript() throws ParseException, IOException {
+ "}\r\n"
+ "}";
cfBugs.process(scriptSrc, "test");
final List<BugInfo> result = cfBugs.getBugs().getBugList().values().iterator().next();
final List<BugInfo> result = cfBugs.getBugs().getBugList().get("VAR_IS_TEMPORARY");
assertEquals(7, result.size());
assertEquals("VAR_IS_TEMPORARY", result.get(0).getMessageCode());
assertEquals(3, result.get(0).getLine());
Expand Down

1 comment on commit e86dff3

@justinmclean
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for that looks a lot better, I'll fill in the other parameters tomorrow.

Please sign in to comment.