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

Rule to check if .cfm file starts with an upper case letter #121

Merged
merged 7 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
70 changes: 50 additions & 20 deletions src/main/java/com/cflint/CFLint.java
Original file line number Diff line number Diff line change
Expand Up @@ -383,14 +383,20 @@ else if (elem.getName().equalsIgnoreCase("cffunction")) {

for (final CFLintStructureListener structurePlugin : getStructureListeners(extensions)) {
try{
structurePlugin.startFunction(context);
}catch(Exception e){}
structurePlugin.startFunction(context, bugs);
}catch(Exception e){
e.printStackTrace();
bugs = new BugList(null);
}
}
processStack(elem.getChildElements(), space + " ", context);
for (final CFLintStructureListener structurePlugin : getStructureListeners(extensions)) {
try{
structurePlugin.endFunction(context);
}catch(Exception e){}
structurePlugin.endFunction(context, bugs);
}catch(Exception e){
e.printStackTrace();
bugs = new BugList(null);
}
}
inFunction = false;
handler.pop();
Expand All @@ -399,15 +405,21 @@ else if (elem.getName().equalsIgnoreCase("cffunction")) {
handler.push("component");
for (final CFLintStructureListener structurePlugin : getStructureListeners(extensions)) {
try{
structurePlugin.startComponent(context);
}catch(Exception e){}
structurePlugin.startComponent(context, bugs);
}catch(Exception e){
e.printStackTrace();
bugs = new BugList(null);
}
}
context.setInComponent(true);
processStack(elem.getChildElements(), space + " ", context);
for (final CFLintStructureListener structurePlugin : getStructureListeners(extensions)) {
try{
structurePlugin.endComponent(context);
}catch(Exception e){}
structurePlugin.endComponent(context, bugs);
}catch(Exception e){
e.printStackTrace();
bugs = new BugList(null);
}
}

inComponent = false;
Expand Down Expand Up @@ -499,16 +511,22 @@ private void process(final CFScriptStatement expression, final String filename,
//do startComponent notifications
for (final CFLintStructureListener structurePlugin : getStructureListeners(extensions)) {
try{
structurePlugin.startComponent(context);
}catch(Exception e){}
structurePlugin.startComponent(context, bugs);
}catch(Exception e){
e.printStackTrace();
bugs = new BugList(null);
}
}
//process the component declaration
process(((CFCompDeclStatement) expression).getBody(), filename, elem, functionName);
//do endComponent notifications
for (final CFLintStructureListener structurePlugin : getStructureListeners(extensions)) {
try{
structurePlugin.endComponent(context);
}catch(Exception e){}
structurePlugin.endComponent(context, bugs);
}catch(Exception e){
e.printStackTrace();
bugs = new BugList(null);
}
}

inComponent = false;
Expand Down Expand Up @@ -556,15 +574,21 @@ private void process(final CFScriptStatement expression, final String filename,
}
for (final CFLintStructureListener structurePlugin : getStructureListeners(extensions)) {
try{
structurePlugin.startFunction(context);
}catch(Exception e){}
structurePlugin.startFunction(context, bugs);
}catch(Exception e){
e.printStackTrace();
bugs = new BugList(null);
}
}

process(function.getBody(), filename, elem, function.getName());
for (final CFLintStructureListener structurePlugin : getStructureListeners(extensions)) {
try{
structurePlugin.endFunction(context);
}catch(Exception e){}
structurePlugin.endFunction(context, bugs);
}catch(Exception e){
e.printStackTrace();
bugs = new BugList(null);
}
}
inFunction = false;
handler.pop();
Expand Down Expand Up @@ -855,8 +879,11 @@ protected void fireStartedProcessing(final String srcidentifier) {
currentFile = srcidentifier;
for (final CFLintStructureListener structurePlugin : getStructureListeners(extensions)) {
try{
structurePlugin.startFile(srcidentifier);
}catch(Exception e){}
structurePlugin.startFile(srcidentifier, bugs);
}catch(Exception e){
e.printStackTrace();
bugs = new BugList(null);
}
}
for (final ScanProgressListener p : scanProgressListeners) {
p.startedProcessing(srcidentifier);
Expand All @@ -866,8 +893,11 @@ protected void fireStartedProcessing(final String srcidentifier) {
protected void fireFinishedProcessing(final String srcidentifier) {
for (final CFLintStructureListener structurePlugin : getStructureListeners(extensions)) {
try{
structurePlugin.endFile(srcidentifier);
}catch(Exception e){}
structurePlugin.endFile(srcidentifier, bugs);
}catch(Exception e){
e.printStackTrace();
bugs = new BugList(null);
}
}
for (final ScanProgressListener p : scanProgressListeners) {
p.finishedProcessing(srcidentifier);
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/cflint/plugins/CFLintScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,17 @@ public interface CFLintScanner {
void expression(CFScriptStatement expression, Context context, BugList bugs);

void setParameter(String name, String value);

void startFile(String fileName, BugList bugs);

void endFile(String fileName, BugList bugs);

void startComponent(Context context, BugList bugs);

void endComponent(Context context, BugList bugs);

void startFunction(Context context, BugList bugs);

void endFunction(Context context, BugList bugs);

}
12 changes: 6 additions & 6 deletions src/main/java/com/cflint/plugins/CFLintScannerAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,35 +55,35 @@ public Map<String, String> getParams() {
* Default implementation does nothing
*/
@Override
public void startFile(String fileName) {}
public void startFile(String fileName, BugList bugs) {}

/**
* Default implementation does nothing
*/
@Override
public void endFile(String fileName) {}
public void endFile(String fileName, BugList bugs) {}

/**
* Default implementation does nothing
*/
@Override
public void startComponent(Context context) {}
public void startComponent(Context context, BugList bugs) {}

/**
* Default implementation does nothing
*/
@Override
public void endComponent(Context context) {}
public void endComponent(Context context, BugList bugs) {}

/**
* Default implementation does nothing
*/
@Override
public void startFunction(Context context) {}
public void startFunction(Context context, BugList bugs) {}

/**
* Default implementation does nothing
*/
@Override
public void endFunction(Context context) {}
public void endFunction(Context context, BugList bugs) {}
}
14 changes: 8 additions & 6 deletions src/main/java/com/cflint/plugins/CFLintStructureListener.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.cflint.plugins;

import com.cflint.BugList;

/**
* This interface supports plugins recieving structure notifications (file start/end, component start/end, function start/end)
*
Expand All @@ -13,33 +15,33 @@ public interface CFLintStructureListener {
* Called when processing of a new file has started
* @param context
*/
public void startFile(String fileName);
public void startFile(String fileName, BugList bugs);
/**
* Called when processing of current file has ended
* @param context
*/
public void endFile(String fileName);
public void endFile(String fileName, BugList bugs);

/**
* Called when processing a new component has started
* @param context
*/
public void startComponent(Context context);
public void startComponent(Context context, BugList bugs);
/**
* Called when processing of current component has ended
* @param context
*/
public void endComponent(Context context);
public void endComponent(Context context, BugList bugs);

/**
* Called when processing of a new function has started
* @param context
*/
public void startFunction(Context context);
public void startFunction(Context context, BugList bugs);
/**
* Called when processing of current function has ended
* @param context
*/
public void endFunction(Context context);
public void endFunction(Context context, BugList bugs);

}
34 changes: 34 additions & 0 deletions src/main/java/com/cflint/plugins/core/FileCaseChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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.CFScriptStatement;

import com.cflint.BugInfo;
import com.cflint.BugList;
import com.cflint.plugins.CFLintScannerAdapter;

@Extension
public class FileCaseChecker extends CFLintScannerAdapter {
final String severity = "INFO";

public void startFile(String fileName, BugList bugs) {
String actualFileName = fileName;
String separator = System.getProperty("file.separator");
int seperatorPosition = fileName.lastIndexOf(separator);

if (seperatorPosition >= 0) {
actualFileName = fileName.substring(seperatorPosition + 1);
}

if (actualFileName.contains(".cfm")) {
if (Character.isUpperCase(actualFileName.charAt(0))) {
bugs.add(new BugInfo.BugInfoBuilder().setLine(1).setMessageCode("FILE_SHOULD_START_WITH_LOWERCASE")
.setSeverity(severity).setFilename(fileName)
.setMessage("File " + actualFileName + " starts with a upper case letter. Only components (.cfc files) should start with an upper case letter.")
.build());
}
}
}
}
13 changes: 13 additions & 0 deletions src/main/resources/cflint.definition.json
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,19 @@
"value": "3"
}
]
},
{
"name": "FileCaseChecker",
"className": "FileCaseChecker",
"message": [
{
"code": "FILE_SHOULD_START_WITH_LOWERCASE",
"severity": "INFO"
}
],
"parameter": [

]
}
]
}
5 changes: 5 additions & 0 deletions src/main/resources/cflint.definition.xml
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,9 @@
<severity>INFO</severity>
</message>
</ruleImpl>
<ruleImpl name="FileCaseChecker" className="FileCaseChecker">
<message code="FILE_SHOULD_START_WITH_LOWERCASE">
<severity>INFO</severity>
</message>
</ruleImpl>
</CFLint-Plugin>
71 changes: 71 additions & 0 deletions src/test/java/com/cflint/TestFileCaseChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.cflint;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.util.Collection;
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.FileCaseChecker;

public class TestFileCaseChecker {

private CFLint cfBugs;

@Before
public void setUp() {
final ConfigRuntime conf = new ConfigRuntime();
final PluginInfoRule pluginRule = new PluginInfoRule();
pluginRule.setName("FileCaseChecker");
conf.getRules().add(pluginRule);
PluginMessage pluginMessage = new PluginMessage("FILE_SHOULD_START_WITH_LOWERCASE");
pluginMessage.setSeverity("INFO");
pluginRule.getMessages().add(pluginMessage);

cfBugs = new CFLint(conf, new FileCaseChecker());
}

@Test
public void testLowerCaseCFMName() throws ParseException, IOException {
final String cfmSrc = "<cfset a = 23>";
cfBugs.process(cfmSrc, "test.cfm");
Collection<List<BugInfo>> result = cfBugs.getBugs().getBugList().values();
assertEquals(0, result.size());
}

@Test
public void testLowerCaseCFCName() throws ParseException, IOException {
final String cfcSrc = "<cfcomponent></cfcomponent>";
cfBugs.process(cfcSrc, "test.cfc");
Collection<List<BugInfo>> result = cfBugs.getBugs().getBugList().values();
assertEquals(0, result.size());
}

@Test
public void testUppeCaseCFMName() throws ParseException, IOException {
final String cfmSrc = "<cfset a = 23>";
cfBugs.process(cfmSrc, "Test.cfm");
final List<BugInfo> result = cfBugs.getBugs().getBugList().values().iterator().next();
assertEquals(1, result.size());
assertEquals("FILE_SHOULD_START_WITH_LOWERCASE", result.get(0).getMessageCode());
assertEquals(1, result.get(0).getLine());
}

@Test
public void testUppperCaseCFCName() throws ParseException, IOException {
final String cfcSrc = "<cfcomponent></cfcomponent>";
cfBugs.process(cfcSrc, "Test.cfc");
Collection<List<BugInfo>> result = cfBugs.getBugs().getBugList().values();
assertEquals(0, result.size());
}

}