From 59ae0fb84c97d80ef042b6447c92206fee6c9c5d Mon Sep 17 00:00:00 2001 From: Justin Mclean Date: Mon, 28 Sep 2015 19:22:45 +1000 Subject: [PATCH 1/3] Rule to check for missing types and any types in function arguments. --- .../cflint/plugins/core/ArgTypeChecker.java | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/main/java/com/cflint/plugins/core/ArgTypeChecker.java diff --git a/src/main/java/com/cflint/plugins/core/ArgTypeChecker.java b/src/main/java/com/cflint/plugins/core/ArgTypeChecker.java new file mode 100644 index 000000000..2a62abb0f --- /dev/null +++ b/src/main/java/com/cflint/plugins/core/ArgTypeChecker.java @@ -0,0 +1,69 @@ +package com.cflint.plugins.core; + +import net.htmlparser.jericho.Element; +import cfml.parsing.cfscript.script.CFFuncDeclStatement; +import cfml.parsing.cfscript.script.CFFunctionParameter; +import cfml.parsing.cfscript.script.CFScriptStatement; + +import com.cflint.BugInfo; +import com.cflint.BugList; +import com.cflint.plugins.CFLintScannerAdapter; +import com.cflint.plugins.Context; +import com.cflint.tools.CFTool; + +public class ArgTypeChecker extends CFLintScannerAdapter { + final String severity = "WARNING"; + + @Override + public void expression(final CFScriptStatement expression, final Context context, final BugList bugs) { + if (expression instanceof CFFuncDeclStatement) { + final CFFuncDeclStatement function = (CFFuncDeclStatement) expression; + final int begLine = function.getLine(); + + for (final CFFunctionParameter argument : function.getFormals()) { + final String name = argument.getName(); + final String variableType = argument.getType(); + + System.out.println("Function!"); + + if (variableType == null) { + System.out.println("Missing type"); + bugs.add(new BugInfo.BugInfoBuilder().setLine(begLine).setMessageCode("ARG_TYPE_MISSING") + .setSeverity(severity).setFilename(context.getFilename()) + .setMessage("Argument " + name + " is missing a type.") + .build()); + } + else if (variableType.equals("any")) { + System.out.println("Type is any"); + bugs.add(new BugInfo.BugInfoBuilder().setLine(begLine).setMessageCode("ARG_TYPE_ANY") + .setSeverity(severity).setFilename(context.getFilename()) + .setMessage("Argument " + name + " is any. Please change to be the correct type.") + .build()); + } + } + } + } + + @Override + public void element(final Element element, final Context context, final BugList bugs) { + if (element.getName().equals("cfargument")) { + final String name = element.getAttributeValue("name"); + final String variableType = element.getAttributeValue("type"); + int begLine = element.getSource().getRow(element.getBegin()); + + if (variableType == null) { + bugs.add(new BugInfo.BugInfoBuilder().setLine(begLine).setMessageCode("ARG_TYPE_MISSING") + .setSeverity(severity).setFilename(context.getFilename()) + .setMessage("Argument " + name + " is missing a type.") + .build()); + } + else if (variableType.equals("any")) { + bugs.add(new BugInfo.BugInfoBuilder().setLine(begLine).setMessageCode("ARG_TYPE_ANY") + .setSeverity(severity).setFilename(context.getFilename()) + .setMessage("Argument " + name + " is any. Please change to be the correct type.") + .build()); + } + } + } + +} From aa0f8845340de1930a7f5f2ea011bcaae00d104e Mon Sep 17 00:00:00 2001 From: Justin Mclean Date: Mon, 28 Sep 2015 19:23:12 +1000 Subject: [PATCH 2/3] Tests for argument checker. --- .../java/com/cflint/TestCFBugs_ArgsType.java | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 src/test/java/com/cflint/TestCFBugs_ArgsType.java diff --git a/src/test/java/com/cflint/TestCFBugs_ArgsType.java b/src/test/java/com/cflint/TestCFBugs_ArgsType.java new file mode 100644 index 000000000..0b8530d82 --- /dev/null +++ b/src/test/java/com/cflint/TestCFBugs_ArgsType.java @@ -0,0 +1,95 @@ +package com.cflint; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; + +import cfml.parsing.reporting.ParseException; + +import com.cflint.config.CFLintPluginInfo.PluginInfoRule; +import com.cflint.config.CFLintPluginInfo.PluginInfoRule.PluginMessage; +import com.cflint.config.ConfigRuntime; +import com.cflint.plugins.core.ArgTypeChecker; + +public class TestCFBugs_ArgsType { + + private CFLint cfBugs; + + @Before + public void setUp() { + final ConfigRuntime conf = new ConfigRuntime(); + final PluginInfoRule pluginRule = new PluginInfoRule(); + pluginRule.setName("ArgTypeChecker"); + conf.getRules().add(pluginRule); + final PluginMessage pluginMessageMissing = new PluginMessage("ARG_TYPE_MISSING"); + pluginMessageMissing.setSeverity("WARNING"); + pluginMessageMissing.setMessageText("Argument ${variable} is missing a hint."); + pluginRule.getMessages().add(pluginMessageMissing); + final PluginMessage pluginMessageAny = new PluginMessage("ARG_TYPE_ANY"); + pluginMessageAny.setSeverity("INFO"); + pluginMessageAny.setMessageText("Argument ${variable} is any, please use correct type."); + pluginRule.getMessages().add(pluginMessageAny); + + cfBugs = new CFLint(conf, new ArgTypeChecker()); + } + + @Test + public void testMissingType() throws ParseException, IOException { + final String cfcSrc = "\r\n" + + "\r\n" + + " \r\n" + + "\r\n" + + "\r\n"; + cfBugs.process(cfcSrc, "test"); + final List result = cfBugs.getBugs().getBugList().values().iterator().next(); + assertEquals(1, result.size()); + assertEquals("ARG_TYPE_MISSING", result.get(0).getMessageCode()); + assertEquals(3, result.get(0).getLine()); + } + + @Test + public void testMissingTypeNoTags() throws ParseException, IOException { + final String cfcSrc = "component {\r\n" + + "public void function test(arg1) {\r\n" + + "}\r\n" + + "}\r\n"; + cfBugs.process(cfcSrc, "test"); + final List result = cfBugs.getBugs().getBugList().values().iterator().next(); + assertEquals(1, result.size()); + assertEquals("ARG_TYPE_MISSING", result.get(0).getMessageCode()); + assertEquals(2, result.get(0).getLine()); + } + + @Test + public void testTypeAny() throws ParseException, IOException { + final String cfcSrc = "\r\n" + + "\r\n" + + " \r\n" + + "\r\n" + + "\r\n"; + cfBugs.process(cfcSrc, "test"); + final List result = cfBugs.getBugs().getBugList().values().iterator().next(); + assertEquals(1, result.size()); + assertEquals("ARG_TYPE_ANY", result.get(0).getMessageCode()); + assertEquals(3, result.get(0).getLine()); + } + + @Test + public void testTypeAnyNoTags() throws ParseException, IOException { + final String cfcSrc = "component {\r\n" + + "public void function test(any arg1, numeric arg2) {\r\n" + + "}\r\n" + + "}\r\n"; + cfBugs.process(cfcSrc, "test"); + final List result = cfBugs.getBugs().getBugList().values().iterator().next(); + assertEquals(1, result.size()); + assertEquals("ARG_TYPE_ANY", result.get(0).getMessageCode()); + assertEquals(2, result.get(0).getLine()); + } + +} From 9a2a420041e0bf0a9508f7fc8dda7be4d93230fa Mon Sep 17 00:00:00 2001 From: Justin Mclean Date: Mon, 28 Sep 2015 20:15:00 +1000 Subject: [PATCH 3/3] Remove debug statements --- src/main/java/com/cflint/plugins/core/ArgTypeChecker.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/com/cflint/plugins/core/ArgTypeChecker.java b/src/main/java/com/cflint/plugins/core/ArgTypeChecker.java index 2a62abb0f..5594abb9d 100644 --- a/src/main/java/com/cflint/plugins/core/ArgTypeChecker.java +++ b/src/main/java/com/cflint/plugins/core/ArgTypeChecker.java @@ -24,17 +24,13 @@ public void expression(final CFScriptStatement expression, final Context context final String name = argument.getName(); final String variableType = argument.getType(); - System.out.println("Function!"); - if (variableType == null) { - System.out.println("Missing type"); bugs.add(new BugInfo.BugInfoBuilder().setLine(begLine).setMessageCode("ARG_TYPE_MISSING") .setSeverity(severity).setFilename(context.getFilename()) .setMessage("Argument " + name + " is missing a type.") .build()); } else if (variableType.equals("any")) { - System.out.println("Type is any"); bugs.add(new BugInfo.BugInfoBuilder().setLine(begLine).setMessageCode("ARG_TYPE_ANY") .setSeverity(severity).setFilename(context.getFilename()) .setMessage("Argument " + name + " is any. Please change to be the correct type.")