Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
eberlyrh authored and eberlyrh committed Dec 10, 2016
1 parent ea56a3f commit 14c900f
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/main/java/com/cflint/CFLint.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)) {
Expand All @@ -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);
Expand Down
41 changes: 40 additions & 1 deletion src/main/java/com/cflint/plugins/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -137,7 +146,18 @@ public void setInComponent(final boolean inComponent) {
public List<ContextMessage> 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));
}
Expand Down Expand Up @@ -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<String> ignores) {
this.ignores.addAll(ignores);
Expand All @@ -289,4 +321,11 @@ public void setFunctionInfo(CFFuncDeclStatement functionInfo) {
}
}

public ContextType getContextType() {
return contextType;
}

public void setContextType(ContextType contextType) {
this.contextType = contextType;
}
}
17 changes: 9 additions & 8 deletions src/main/java/com/cflint/plugins/core/VariableNameChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/cflint/tools/ObjectEquals.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
10 changes: 10 additions & 0 deletions src/test/resources/com/cflint/tests/Naming/.cflintrc.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<config>
<includes code="VAR_INVALID_NAME"/>
<includes code="VAR_ALLCAPS_NAME"/>
<includes code="SCOPE_ALLCAPS_NAME"/>
<includes code="VAR_TOO_SHORT"/>
<includes code="VAR_TOO_LONG"/>
<includes code="VAR_TOO_WORDY"/>
<includes code="VAR_IS_TEMPORARY"/>
<includes code="VAR_HAS_PREFIX_OR_POSTFIX"/>
</config>
7 changes: 7 additions & 0 deletions src/test/resources/com/cflint/tests/Naming/tempVar.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
component {
function foo() {
var tempStruct = {};
doSomething(tempStruct);
tempStruct.foo = "bar";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ ]

0 comments on commit 14c900f

Please sign in to comment.