From 75adc54bdebae600cea94442347f69fb5bf0398f Mon Sep 17 00:00:00 2001 From: Justin Mclean Date: Sun, 11 Oct 2015 23:55:22 +1100 Subject: [PATCH 1/3] Added rule for ArrayNewChecker --- src/main/resources/cflint.definition.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/resources/cflint.definition.xml b/src/main/resources/cflint.definition.xml index befa1ab8a..220b9ceaf 100644 --- a/src/main/resources/cflint.definition.xml +++ b/src/main/resources/cflint.definition.xml @@ -179,4 +179,9 @@ INFO + + + INFO + + From 7ddd4b705df1294fb6d6bf435e21fe343fe957db Mon Sep 17 00:00:00 2001 From: Justin Mclean Date: Sun, 11 Oct 2015 23:56:00 +1100 Subject: [PATCH 2/3] Rule to check for ArrayNew and suggest you use = [] instead --- .../cflint/plugins/core/ArrayNewChecker.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/main/java/com/cflint/plugins/core/ArrayNewChecker.java diff --git a/src/main/java/com/cflint/plugins/core/ArrayNewChecker.java b/src/main/java/com/cflint/plugins/core/ArrayNewChecker.java new file mode 100644 index 000000000..d93a40a99 --- /dev/null +++ b/src/main/java/com/cflint/plugins/core/ArrayNewChecker.java @@ -0,0 +1,49 @@ +package com.cflint.plugins.core; + +import ro.fortsoft.pf4j.Extension; +import net.htmlparser.jericho.Element; + +import cfml.parsing.cfscript.CFExpression; +import cfml.parsing.cfscript.script.CFExpressionStatement; +import cfml.parsing.cfscript.script.CFScriptStatement; + +import com.cflint.BugInfo; +import com.cflint.BugList; +import com.cflint.plugins.CFLintScannerAdapter; +import com.cflint.plugins.Context; + +@Extension +public class ArrayNewChecker extends CFLintScannerAdapter { + final String severity = "INFO"; + + @Override + public void expression(final CFScriptStatement expression, final Context context, final BugList bugs) { + if (expression instanceof CFExpressionStatement) { + String code = ((CFExpressionStatement) expression).getExpression().Decompile(0); + int lineNo = ((CFExpressionStatement) expression).getLine() + context.startLine() - 1; + + if (code.toLowerCase().contains("arraynew(1)")) { + arrayNew(lineNo, context, bugs); + } + } + } + + @Override + public void element(final Element element, final Context context, final BugList bugs) { + if (element.getName().equals("cfset")) { + String content = element.getStartTag().getTagContent().toString(); + int lineNo = element.getSource().getRow(element.getBegin()); + + if (content.toLowerCase().contains("arraynew(1)")) { + arrayNew(lineNo, context, bugs); + } + } + } + + protected void arrayNew(final int lineNo, final Context context, final BugList bugs) { + bugs.add(new BugInfo.BugInfoBuilder().setLine(lineNo).setMessageCode("AVOID_USING_ARRAYNEW") + .setSeverity(severity).setFilename(context.getFilename()) + .setMessage("ArrayNew statement at line " + lineNo + ". Use implict array construction instead (= []).") + .build()); + } +} \ No newline at end of file From 3a7c19dbe033275869d64efe15435afebc5f5a8c Mon Sep 17 00:00:00 2001 From: Justin Mclean Date: Sun, 11 Oct 2015 23:56:51 +1100 Subject: [PATCH 3/3] Tests for arrayNew checker --- .../java/com/cflint/TestArrayNewChecker.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/test/java/com/cflint/TestArrayNewChecker.java diff --git a/src/test/java/com/cflint/TestArrayNewChecker.java b/src/test/java/com/cflint/TestArrayNewChecker.java new file mode 100644 index 000000000..cfbadabb8 --- /dev/null +++ b/src/test/java/com/cflint/TestArrayNewChecker.java @@ -0,0 +1,70 @@ +package com.cflint; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.util.List; + +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.ArrayNewChecker; + +public class TestArrayNewChecker { + + private CFLint cfBugs; + + @Before + public void setUp() { + final ConfigRuntime conf = new ConfigRuntime(); + final PluginInfoRule pluginRule = new PluginInfoRule(); + pluginRule.setName("ArrayNewChecker"); + conf.getRules().add(pluginRule); + final PluginMessage pluginMessage = new PluginMessage("AVOID_USING_ARRAYNEW"); + pluginMessage.setSeverity("INFO"); + cfBugs = new CFLint(conf, new ArrayNewChecker()); + } + + @Test + public void testArrayNewInScript() throws ParseException, IOException { + final String scriptSrc = "\r\n" + + "var a = 23;\r\n" + + "var b = arrayNew(1);\r\n" + + ""; + + cfBugs.process(scriptSrc, "test"); + final List result = cfBugs.getBugs().getBugList().values().iterator().next(); + assertEquals(1, result.size()); + assertEquals("AVOID_USING_ARRAYNEW", result.get(0).getMessageCode()); + assertEquals(3, result.get(0).getLine()); + } + + @Test + public void testArrayNewMultiDimentionInScript() throws ParseException, IOException { + final String scriptSrc = "\r\n" + + "var a = 23;\r\n" + + "var b = arrayNew(3);\r\n" + + ""; + + cfBugs.process(scriptSrc, "test"); + assertEquals(0, cfBugs.getBugs().getBugList().size()); + } + + @Test + public void testArrayNewInTag() throws ParseException, IOException { + final String tagSrc = "\r\n" + + ""; + + cfBugs.process(tagSrc, "test"); + final List result = cfBugs.getBugs().getBugList().values().iterator().next(); + assertEquals(1, result.size()); + assertEquals("AVOID_USING_ARRAYNEW", result.get(0).getMessageCode()); + assertEquals(2, result.get(0).getLine()); + } + +}