From ea56a3f43000fbf916b09441e777935b2113bc38 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sun, 4 Dec 2016 22:43:14 -0500 Subject: [PATCH 1/2] version 0.10.0 --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 7bac30573..aef5daaf2 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 com.github.cflint CFLint - 0.10.0-SNAPSHOT + 0.10.0 CFLint @@ -59,7 +59,7 @@ UTF-8 - 2.2.11-SNAPSHOT + 2.2.11 From 14c900fbcf1bd4f3292567b3d68ea1acaa78dad7 Mon Sep 17 00:00:00 2001 From: eberlyrh Date: Fri, 9 Dec 2016 22:08:40 -0500 Subject: [PATCH 2/2] #223 --- src/main/java/com/cflint/CFLint.java | 6 +++ src/main/java/com/cflint/plugins/Context.java | 41 ++++++++++++++++++- .../plugins/core/VariableNameChecker.java | 17 ++++---- .../java/com/cflint/tools/ObjectEquals.java | 12 ++++++ .../com/cflint/tests/Naming/.cflintrc.xml | 10 +++++ .../com/cflint/tests/Naming/tempVar.cfc | 7 ++++ .../cflint/tests/Naming/tempVar.expected.txt | 1 + 7 files changed, 85 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/cflint/tools/ObjectEquals.java create mode 100644 src/test/resources/com/cflint/tests/Naming/.cflintrc.xml create mode 100644 src/test/resources/com/cflint/tests/Naming/tempVar.cfc create mode 100644 src/test/resources/com/cflint/tests/Naming/tempVar.expected.txt diff --git a/src/main/java/com/cflint/CFLint.java b/src/main/java/com/cflint/CFLint.java index 7be4a8238..d045007a3 100644 --- a/src/main/java/com/cflint/CFLint.java +++ b/src/main/java/com/cflint/CFLint.java @@ -31,6 +31,7 @@ import com.cflint.plugins.CFLintStructureListener; import com.cflint.plugins.Context; import com.cflint.plugins.Context.ContextMessage; +import com.cflint.plugins.Context.ContextType; import com.cflint.plugins.exceptions.CFLintExceptionListener; import com.cflint.plugins.exceptions.DefaultCFLintExceptionListener; import com.cflint.tools.AllowedExtensionsLoader; @@ -251,11 +252,13 @@ private void process(final Element elem, final String space, final Context conte final Context componentContext = context.subContext(elem); componentContext.setInComponent(true); componentContext.setComponentName(elem.getAttributeValue("displayname")); + componentContext.setContextType(ContextType.Component); handler.push("component"); doStructureStart(elem, componentContext, CFCompDeclStatement.class); } else if (elem.getName().equalsIgnoreCase("cffunction")) { final Context functionContext = context.subContext(elem); functionContext.setFunctionName(elem.getAttributeValue("name")); + functionContext.setContextType(ContextType.Function); handler.push("function"); doStructureStart(elem, functionContext, CFFuncDeclStatement.class); } @@ -300,6 +303,7 @@ private void process(final Element elem, final String space, final Context conte } else if (elem.getName().equalsIgnoreCase("cffunction")) { final Context functionContext = context.subContext(elem); functionContext.setFunctionName(elem.getAttributeValue("name")); + functionContext.setContextType(ContextType.Function); scanElement(elem, functionContext); processStack(elem.getChildElements(), space + " ", functionContext); for (final CFLintStructureListener structurePlugin : getStructureListeners(extensions)) { @@ -319,6 +323,8 @@ private void process(final Element elem, final String space, final Context conte final Context componentContext = context.subContext(elem); componentContext.setInComponent(true); componentContext.setComponentName(elem.getAttributeValue("displayname")); + componentContext.setContextType(ContextType.Component); + scanElement(elem, componentContext); processStack(elem.getChildElements(), space + " ", componentContext); diff --git a/src/main/java/com/cflint/plugins/Context.java b/src/main/java/com/cflint/plugins/Context.java index 2a5a62ffb..f168c3a61 100644 --- a/src/main/java/com/cflint/plugins/Context.java +++ b/src/main/java/com/cflint/plugins/Context.java @@ -10,17 +10,26 @@ import com.cflint.BugInfo; import com.cflint.StackHandler; +import com.cflint.tools.ObjectEquals; import cfml.parsing.cfscript.CFIdentifier; import cfml.parsing.cfscript.script.CFFuncDeclStatement; import net.htmlparser.jericho.Element; public class Context { + + public static enum ContextType{ + Component, + Function, + Other + } String filename; String componentName; final Element element; CFFuncDeclStatement functionInfo; + ContextType contextType; + String functionName; boolean inAssignmentExpression; @@ -137,7 +146,18 @@ public void setInComponent(final boolean inComponent) { public List getMessages() { return messages; } - + + public void addUniqueMessage(final String messageCode, final String variable) { + if(messageCode != null){ + for(ContextMessage msg: messages){ + if(ObjectEquals.equals(msg.getMessageCode(), messageCode) && ObjectEquals.equals(variable, msg.getVariable())){ + return; + } + } + } + addMessage(messageCode, variable); + } + public void addMessage(final String messageCode, final String variable) { messages.add(new ContextMessage(messageCode, variable)); } @@ -268,6 +288,18 @@ public void remove(){ public Context getParent() { return parent; } + /** + * + * @param type + * @return the parent context of the given type OR the root context if none matches + */ + public Context getParent(ContextType type) { + Context p = this; + while(p.parent != null && p.contextType != type){ + p = p.parent; + } + return p; + } public void ignore(List ignores) { this.ignores.addAll(ignores); @@ -289,4 +321,11 @@ public void setFunctionInfo(CFFuncDeclStatement functionInfo) { } } + public ContextType getContextType() { + return contextType; + } + + public void setContextType(ContextType contextType) { + this.contextType = contextType; + } } diff --git a/src/main/java/com/cflint/plugins/core/VariableNameChecker.java b/src/main/java/com/cflint/plugins/core/VariableNameChecker.java index 7cbb5de43..573ef9814 100644 --- a/src/main/java/com/cflint/plugins/core/VariableNameChecker.java +++ b/src/main/java/com/cflint/plugins/core/VariableNameChecker.java @@ -7,6 +7,7 @@ import com.cflint.BugList; import com.cflint.plugins.CFLintScannerAdapter; import com.cflint.plugins.Context; +import com.cflint.plugins.Context.ContextType; import cfml.parsing.cfscript.CFExpression; import cfml.parsing.cfscript.CFFullVarExpression; @@ -133,29 +134,29 @@ public void checkNameForBugs(final Context context, final String variable, final final ValidName name = new ValidName(minVarLength, maxVarLength, maxVarWords); if (name.isInvalid(variable)) { - context.addMessage("VAR_INVALID_NAME", variable); + context.getParent(ContextType.Function).addUniqueMessage("VAR_INVALID_NAME", variable); } if (!scope.isCFScoped(variable) && name.isUpperCase(variable)) { - context.addMessage("VAR_ALLCAPS_NAME", variable); + context.getParent(ContextType.Function).addUniqueMessage("VAR_ALLCAPS_NAME", variable); } if (scope.isCFScoped(variable) && name.isUpperCase(variable) && (getParameter("IgnoreUpperCaseScopes") == null || !getParameter("IgnoreUpperCaseScopes").contains(variable))) { - context.addMessage("SCOPE_ALLCAPS_NAME", variable); + context.getParent(ContextType.Function).addUniqueMessage("SCOPE_ALLCAPS_NAME", variable); } if (name.tooShort(variable)) { - context.addMessage("VAR_TOO_SHORT", variable); + context.getParent(ContextType.Function).addUniqueMessage("VAR_TOO_SHORT", variable); } if (name.tooLong(variable)) { - context.addMessage("VAR_TOO_LONG", variable); + context.getParent(ContextType.Function).addUniqueMessage("VAR_TOO_LONG", variable); } if (!name.isUpperCase(variable) && name.tooWordy(variable)) { - context.addMessage("VAR_TOO_WORDY", variable); + context.getParent(ContextType.Function).addUniqueMessage("VAR_TOO_WORDY", variable); } if (name.isTemporary(variable)) { - context.addMessage("VAR_IS_TEMPORARY", variable); + context.getParent(ContextType.Function).addUniqueMessage("VAR_IS_TEMPORARY", variable); } if (name.hasPrefixOrPostfix(variable)) { - context.addMessage("VAR_HAS_PREFIX_OR_POSTFIX", variable); + context.getParent(ContextType.Function).addUniqueMessage("VAR_HAS_PREFIX_OR_POSTFIX", variable); } } diff --git a/src/main/java/com/cflint/tools/ObjectEquals.java b/src/main/java/com/cflint/tools/ObjectEquals.java new file mode 100644 index 000000000..b8c42fb25 --- /dev/null +++ b/src/main/java/com/cflint/tools/ObjectEquals.java @@ -0,0 +1,12 @@ +package com.cflint.tools; + +public class ObjectEquals { + + public static boolean equals(Object a, Object b){ + if(a==null){ + return b==null; + }else{ + return a.equals(b); + } + } +} diff --git a/src/test/resources/com/cflint/tests/Naming/.cflintrc.xml b/src/test/resources/com/cflint/tests/Naming/.cflintrc.xml new file mode 100644 index 000000000..41cb9d3ea --- /dev/null +++ b/src/test/resources/com/cflint/tests/Naming/.cflintrc.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/com/cflint/tests/Naming/tempVar.cfc b/src/test/resources/com/cflint/tests/Naming/tempVar.cfc new file mode 100644 index 000000000..6dfaa2a14 --- /dev/null +++ b/src/test/resources/com/cflint/tests/Naming/tempVar.cfc @@ -0,0 +1,7 @@ +component { + function foo() { + var tempStruct = {}; + doSomething(tempStruct); + tempStruct.foo = "bar"; + } +} \ No newline at end of file diff --git a/src/test/resources/com/cflint/tests/Naming/tempVar.expected.txt b/src/test/resources/com/cflint/tests/Naming/tempVar.expected.txt new file mode 100644 index 000000000..8878e547a --- /dev/null +++ b/src/test/resources/com/cflint/tests/Naming/tempVar.expected.txt @@ -0,0 +1 @@ +[ ] \ No newline at end of file