diff --git a/src/main/java/com/cflint/plugins/core/VariableNameChecker.java b/src/main/java/com/cflint/plugins/core/VariableNameChecker.java index 6a4c4239f..7e1872f03 100644 --- a/src/main/java/com/cflint/plugins/core/VariableNameChecker.java +++ b/src/main/java/com/cflint/plugins/core/VariableNameChecker.java @@ -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; @@ -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) { @@ -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) { diff --git a/src/test/java/com/cflint/TestCFBugs_VariableNames.java b/src/test/java/com/cflint/TestCFBugs_VariableNames.java index 13352c468..67d2780cc 100644 --- a/src/test/java/com/cflint/TestCFBugs_VariableNames.java +++ b/src/test/java/com/cflint/TestCFBugs_VariableNames.java @@ -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); @@ -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 @@ -202,7 +206,7 @@ public void nameIsTemporyTag() throws ParseException, IOException { + "\r\n" + ""; cfBugs.process(tagSrc, "test"); - final List result = cfBugs.getBugs().getBugList().values().iterator().next(); + final List 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()); @@ -403,7 +407,7 @@ public void nameIsTemporyScript() throws ParseException, IOException { + "}\r\n" + "}"; cfBugs.process(scriptSrc, "test"); - final List result = cfBugs.getBugs().getBugList().values().iterator().next(); + final List 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());