Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check for cfinclude inside cfcs #116

Merged
merged 4 commits into from
Oct 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/main/java/com/cflint/plugins/core/CFXTagChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ public class CFXTagChecker extends CFLintScannerAdapter {
public void element(final Element element, final Context context, final BugList bugs) {
final String tagName = element.getName();
final String cfmlTagCheck = getParameter("tagName");
final String scope = getParameter("scope");

if (tagName.matches(cfmlTagCheck)) {
context.addMessage("AVOID_USING_" + tagName.toUpperCase() + "_TAG", tagName);
if (scope == null || scope.equals("component") && context.isInComponent()) {
context.addMessage("AVOID_USING_" + tagName.toUpperCase() + "_TAG", tagName);
}
}
}
}
21 changes: 21 additions & 0 deletions src/main/resources/cflint.definition.json
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,27 @@
}
]
},
{
"name": "CFIncludeChecker",
"className": "CFXTagChecker",
"message": [
{
"code": "AVOID_USING_CFINCUDE_TAG",
"messageText": "Avoid using <${tagName}> tags. Use components instead.",
"severity": "WARNING"
}
],
"parameter": [
{
"name": "tagName",
"value": "cfinclude"
},
{
"name": "scope",
"value": "component"
}
]
},
{
"name": "ComponentHintChecker",
"className": "ComponentHintChecker",
Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/cflint.definition.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@
</message>
<parameter name="tagName" value="cfupdate"/>
</ruleImpl>
<ruleImpl name="CFIncludeChecker" className="CFXTagChecker">
<message code="AVOID_USING_CFINCLUDE_TAG">
<messageText>Avoid using &lt;${tagName}&gt; tags. Use components instead.</messageText>
<severity>WARNING</severity>
</message>
<parameter name="tagName" value="cfinclude"/>
<parameter name="scope" value="component"/>
</ruleImpl>
<ruleImpl name="ComponentHintChecker" className="ComponentHintChecker">
<message code="COMPONENT_HINT_MISSING">
<severity>WARNING</severity>
Expand Down
67 changes: 67 additions & 0 deletions src/test/java/com/cflint/TestCFIncludeChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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.CFXTagChecker;

public class TestCFIncludeChecker {

private CFLint cfBugs;

@Before
public void setUp() {
final ConfigRuntime conf = new ConfigRuntime();
final PluginInfoRule pluginRule = new PluginInfoRule();
pluginRule.setName("CFXTagChecker");
pluginRule.addParameter("tagName", "cfinclude");
pluginRule.addParameter("scope", "component");
conf.getRules().add(pluginRule);
final PluginMessage pluginMessage = new PluginMessage("AVOID_USING_CFINCLUDE_TAG");
pluginMessage.setSeverity("WARNING");
pluginRule.getMessages().add(pluginMessage);
final CFXTagChecker checker = new CFXTagChecker();
checker.setParameter("tagName", "cfinclude");
checker.setParameter("scope", "component");
cfBugs = new CFLint(conf, checker);
}

@Test
public void testCfIncludeIncomponent() throws ParseException, IOException {

final String scriptSrc = "<cfcomponent>\r\n"
+ "<cffunction>\r\n"
+ "<cfinclude template=\"functions.cfm\">\r\n"
+ "</cffunction>\r\n"
+ "</cfcomponent>\r\n";

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

@Test
public void testLonelyCfInclude() throws ParseException, IOException {

final String scriptSrc = "<cffunction>\r\n"
+ "<cfinclude template=\"functions.cfm\">\r\n"
+ "</cffunction>\r\n";

cfBugs.process(scriptSrc, "test");
final Map<String, List<BugInfo>> result = cfBugs.getBugs().getBugList();
assertEquals(0, result.size());
}

}