From e6ce7ad56e61d48e8db28dc6e1dfe813c2ee2f6c Mon Sep 17 00:00:00 2001 From: Justin Mclean Date: Sun, 25 Oct 2015 15:26:54 +1100 Subject: [PATCH 1/6] add new start/end file / component / function methods to interface --- src/main/java/com/cflint/plugins/CFLintScanner.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/java/com/cflint/plugins/CFLintScanner.java b/src/main/java/com/cflint/plugins/CFLintScanner.java index 59ff6cc25..f4a1d92d4 100644 --- a/src/main/java/com/cflint/plugins/CFLintScanner.java +++ b/src/main/java/com/cflint/plugins/CFLintScanner.java @@ -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); } From 77a88ce6f7825ad0c0d57ca0869a1107a6f6f8d0 Mon Sep 17 00:00:00 2001 From: Justin Mclean Date: Sun, 25 Oct 2015 15:27:44 +1100 Subject: [PATCH 2/6] add BugList as a parameter as we can add bugs --- .../com/cflint/plugins/CFLintScannerAdapter.java | 12 ++++++------ .../cflint/plugins/CFLintStructureListener.java | 14 ++++++++------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/cflint/plugins/CFLintScannerAdapter.java b/src/main/java/com/cflint/plugins/CFLintScannerAdapter.java index 580be1521..f20bb4f24 100644 --- a/src/main/java/com/cflint/plugins/CFLintScannerAdapter.java +++ b/src/main/java/com/cflint/plugins/CFLintScannerAdapter.java @@ -55,35 +55,35 @@ public Map 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) {} } diff --git a/src/main/java/com/cflint/plugins/CFLintStructureListener.java b/src/main/java/com/cflint/plugins/CFLintStructureListener.java index 4beb0377d..67c5d0ed7 100644 --- a/src/main/java/com/cflint/plugins/CFLintStructureListener.java +++ b/src/main/java/com/cflint/plugins/CFLintStructureListener.java @@ -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) * @@ -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); } From 0e2f90b7649e61905bdbf5925dc73390638ecdc0 Mon Sep 17 00:00:00 2001 From: Justin Mclean Date: Sun, 25 Oct 2015 15:28:48 +1100 Subject: [PATCH 3/6] Add bug list as a parameter where needed and print stack trace if an exception occurs --- src/main/java/com/cflint/CFLint.java | 70 ++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/cflint/CFLint.java b/src/main/java/com/cflint/CFLint.java index 1a34f02e4..063f3c5c4 100644 --- a/src/main/java/com/cflint/CFLint.java +++ b/src/main/java/com/cflint/CFLint.java @@ -380,14 +380,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(); @@ -396,15 +402,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; @@ -493,16 +505,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; @@ -550,15 +568,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(); @@ -825,8 +849,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); @@ -836,8 +863,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); From a7baa6452f217ab58f73c23847da1d8f6330f01a Mon Sep 17 00:00:00 2001 From: Justin Mclean Date: Sun, 25 Oct 2015 15:29:28 +1100 Subject: [PATCH 4/6] add file case checker to config --- src/main/resources/cflint.definition.json | 13 +++++++++++++ src/main/resources/cflint.definition.xml | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/src/main/resources/cflint.definition.json b/src/main/resources/cflint.definition.json index eb44e8d5d..a01c8981b 100644 --- a/src/main/resources/cflint.definition.json +++ b/src/main/resources/cflint.definition.json @@ -545,6 +545,19 @@ "value": "5" } ] + }, + { + "name": "FileCaseChecker", + "className": "FileCaseChecker", + "message": [ + { + "code": "FILE_SHOULD_START_WITH_LOWERCASE", + "severity": "INFO" + } + ], + "parameter": [ + + ] } ] } \ No newline at end of file diff --git a/src/main/resources/cflint.definition.xml b/src/main/resources/cflint.definition.xml index 2620ca36c..df8ecaf79 100644 --- a/src/main/resources/cflint.definition.xml +++ b/src/main/resources/cflint.definition.xml @@ -257,4 +257,9 @@ INFO + + + INFO + + From 47fba71d5504699f222898a1f289b8a53fb84dc5 Mon Sep 17 00:00:00 2001 From: Justin Mclean Date: Sun, 25 Oct 2015 15:29:53 +1100 Subject: [PATCH 5/6] Add rule to check if .cfm files start with an upper case letter --- .../cflint/plugins/core/FileCaseChecker.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/main/java/com/cflint/plugins/core/FileCaseChecker.java diff --git a/src/main/java/com/cflint/plugins/core/FileCaseChecker.java b/src/main/java/com/cflint/plugins/core/FileCaseChecker.java new file mode 100644 index 000000000..4aaa8faa0 --- /dev/null +++ b/src/main/java/com/cflint/plugins/core/FileCaseChecker.java @@ -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()); + } + } + } +} \ No newline at end of file From 41fefab0951a6a58523cb6339823926c416572d8 Mon Sep 17 00:00:00 2001 From: Justin Mclean Date: Sun, 25 Oct 2015 15:30:35 +1100 Subject: [PATCH 6/6] Add tests for file case checker rule --- .../java/com/cflint/TestFileCaseChecker.java | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/test/java/com/cflint/TestFileCaseChecker.java diff --git a/src/test/java/com/cflint/TestFileCaseChecker.java b/src/test/java/com/cflint/TestFileCaseChecker.java new file mode 100644 index 000000000..bcfd4eff0 --- /dev/null +++ b/src/test/java/com/cflint/TestFileCaseChecker.java @@ -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 = ""; + cfBugs.process(cfmSrc, "test.cfm"); + Collection> result = cfBugs.getBugs().getBugList().values(); + assertEquals(0, result.size()); + } + + @Test + public void testLowerCaseCFCName() throws ParseException, IOException { + final String cfcSrc = ""; + cfBugs.process(cfcSrc, "test.cfc"); + Collection> result = cfBugs.getBugs().getBugList().values(); + assertEquals(0, result.size()); + } + + @Test + public void testUppeCaseCFMName() throws ParseException, IOException { + final String cfmSrc = ""; + cfBugs.process(cfmSrc, "Test.cfm"); + final List 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 = ""; + cfBugs.process(cfcSrc, "Test.cfc"); + Collection> result = cfBugs.getBugs().getBugList().values(); + assertEquals(0, result.size()); + } + +}