Skip to content

Commit

Permalink
Merge pull request #98 from cflint/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
ryaneberly committed Oct 12, 2015
2 parents e327a1e + ae7170a commit ee52037
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 2 deletions.
49 changes: 49 additions & 0 deletions src/main/java/com/cflint/plugins/core/ArrayNewChecker.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
35 changes: 35 additions & 0 deletions src/main/java/com/cflint/plugins/core/FunctionXChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.cflint.plugins.core;

import ro.fortsoft.pf4j.Extension;
import net.htmlparser.jericho.Element;
import cfml.parsing.cfscript.CFExpression;
import cfml.parsing.cfscript.CFFunctionExpression;
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 FunctionXChecker extends CFLintScannerAdapter {
final String severity = "INFO";

@Override
public void expression(CFExpression expression, Context context,
BugList bugs) {
if (expression instanceof CFFunctionExpression) {
final String cfmlFunctionCheck = getParameter("functionName");

CFFunctionExpression functionExpression = (CFFunctionExpression)expression;
if(functionExpression.getName().equalsIgnoreCase(cfmlFunctionCheck)){
int lineNo = expression.getLine() + context.startLine() - 1;
//structNew(lineNo, context, bugs);
context.addMessage("AVOID_USING_" + cfmlFunctionCheck.toUpperCase(), cfmlFunctionCheck);

}
}
}

}
16 changes: 15 additions & 1 deletion src/main/resources/cflint.definition.xml
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,22 @@
</message>
<parameter name="maximum" value="10" />
</ruleImpl>
<ruleImpl name="WriteDumpChecker" className="WriteDumpChecker">
<ruleImpl name="WriteDumpChecker" className="FunctionXChecker">
<message code="AVOID_USING_WRITEDUMP">
<messageText>Avoid using the ${functionName} function in production code.</messageText>
<severity>INFO</severity>
</message>
<parameter name="functionName" value="writeDump"/>
</ruleImpl>
<ruleImpl name="StructNewChecker" className="FunctionXChecker">
<message code="AVOID_USING_STRUCTNEW">
<messageText>Avoid using the ${functionName} function. Use implicit structure construction instead (= {}).</messageText>
<severity>INFO</severity>
</message>
<parameter name="functionName" value="structNew"/>
</ruleImpl>
<ruleImpl name="ArrayNewChecker" className="ArrayNewChecker">
<message code="AVOID_USING_ARRAYNEW">
<severity>INFO</severity>
</message>
</ruleImpl>
Expand Down
70 changes: 70 additions & 0 deletions src/test/java/com/cflint/TestArrayNewChecker.java
Original file line number Diff line number Diff line change
@@ -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 = "<cfscript>\r\n"
+ "var a = 23;\r\n"
+ "var b = arrayNew(1);\r\n"
+ "</cfscript>";

cfBugs.process(scriptSrc, "test");
final List<BugInfo> 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 = "<cfscript>\r\n"
+ "var a = 23;\r\n"
+ "var b = arrayNew(3);\r\n"
+ "</cfscript>";

cfBugs.process(scriptSrc, "test");
assertEquals(0, cfBugs.getBugs().getBugList().size());
}

@Test
public void testArrayNewInTag() throws ParseException, IOException {
final String tagSrc = "<cfset a = 23>\r\n"
+ "<cfset b = arrayNew(1)>";

cfBugs.process(tagSrc, "test");
final List<BugInfo> 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());
}

}
66 changes: 66 additions & 0 deletions src/test/java/com/cflint/TestStructNewChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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.FunctionXChecker;

public class TestStructNewChecker {

private CFLint cfBugs;

@Before
public void setUp() {
final ConfigRuntime conf = new ConfigRuntime();
final PluginInfoRule pluginRule = new PluginInfoRule();
pluginRule.setName("StructNewChecker");
pluginRule.setClassName("FunctionXChecker");
pluginRule.addParameter("functionName", "structnew");
conf.getRules().add(pluginRule);
final PluginMessage pluginMessage = new PluginMessage("AVOID_USING_STRUCTNEW");
pluginMessage.setSeverity("INFO");
pluginMessage
.setMessageText("Avoid using the ${functionName} function. Use implict structure construction instead (= {}).");
pluginRule.getMessages().add(pluginMessage);
FunctionXChecker checker = new FunctionXChecker();
checker.setParameter("functionName", "structnew");
cfBugs = new CFLint(conf, checker);
}

@Test
public void testStructNewInScript() throws ParseException, IOException {
final String scriptSrc = "<cfscript>\r\n"
+ "var a = 23;\r\n"
+ "var b = structNew();\r\n"
+ "</cfscript>";

cfBugs.process(scriptSrc, "test");
final List<BugInfo> result = cfBugs.getBugs().getBugList().values().iterator().next();
assertEquals(1, result.size());
assertEquals("AVOID_USING_STRUCTNEW", result.get(0).getMessageCode());
assertEquals(3, result.get(0).getLine());
}

@Test
public void testStructNewInTag() throws ParseException, IOException {
final String tagSrc = "<cfset a = 23>\r\n"
+ "<cfset b = structNew()>";

cfBugs.process(tagSrc, "test");
final List<BugInfo> result = cfBugs.getBugs().getBugList().values().iterator().next();
assertEquals(1, result.size());
assertEquals("AVOID_USING_STRUCTNEW", result.get(0).getMessageCode());
assertEquals(2, result.get(0).getLine());
}

}
11 changes: 10 additions & 1 deletion src/test/java/com/cflint/TestWriteDumpChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.cflint.config.CFLintPluginInfo.PluginInfoRule;
import com.cflint.config.CFLintPluginInfo.PluginInfoRule.PluginMessage;
import com.cflint.config.ConfigRuntime;
import com.cflint.plugins.core.FunctionXChecker;
import com.cflint.plugins.core.WriteDumpChecker;

public class TestWriteDumpChecker {
Expand All @@ -24,10 +25,18 @@ public void setUp() {
final ConfigRuntime conf = new ConfigRuntime();
final PluginInfoRule pluginRule = new PluginInfoRule();
pluginRule.setName("WriteDumpChecker");
pluginRule.setClassName("FunctionXChecker");
pluginRule.addParameter("functionName", "writedump");
conf.getRules().add(pluginRule);
final PluginMessage pluginMessage = new PluginMessage("AVOID_USING_WRITEDUMP");
pluginMessage.setSeverity("INFO");
cfBugs = new CFLint(conf, new WriteDumpChecker());
pluginMessage.setMessageText("Avoid using the ${functionName} function in production code.");
pluginRule.getMessages().add(pluginMessage);

FunctionXChecker checker = new FunctionXChecker();
checker.setParameter("functionName", "writedump");
cfBugs = new CFLint(conf, checker);

}

@Test
Expand Down

0 comments on commit ee52037

Please sign in to comment.