Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add support for @CFLintIgnore to cfcomponent and cffunction tags.

cleanup bugs - all tests passing.
  • Loading branch information
ryaneberly committed Oct 22, 2016
1 parent 2f0fd4b commit 36d67a4
Show file tree
Hide file tree
Showing 13 changed files with 342 additions and 187 deletions.
344 changes: 178 additions & 166 deletions src/main/java/com/cflint/CFLint.java

Large diffs are not rendered by default.

43 changes: 40 additions & 3 deletions src/main/java/com/cflint/plugins/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@

import cfml.parsing.cfscript.CFIdentifier;
import net.htmlparser.jericho.Element;
import static com.cflint.tools.CFTool.*;

public class Context {

String filename;
String componentName;
final Element element;
List<Element> siblingElements;
String functionName;
final boolean inAssignmentExpression;
boolean inAssignmentExpression;
public void setInAssignmentExpression(boolean inAssignmentExpression) {
this.inAssignmentExpression = inAssignmentExpression;
}

boolean inComponent;
final StackHandler callStack;
final CommonTokenStream tokens;
Expand Down Expand Up @@ -93,6 +99,14 @@ public boolean isInAssignmentExpression() {
return inAssignmentExpression;
}

public List<Element> getSiblingElements() {
return siblingElements;
}

public void setSiblingElements(List<Element> siblingElements) {
this.siblingElements = siblingElements;
}

public StackHandler getCallStack() {
return callStack;
}
Expand Down Expand Up @@ -162,8 +176,19 @@ public Integer getLine() {
}

public Context subContext(final Element elem) {
final Context context2 = new Context(getFilename(), elem, getFunctionName(), isInAssignmentExpression(),
if(elem == this.element || (siblingElements != null && siblingElements.contains(elem))){
return subContext(elem, siblingElements);
}
return subContext(elem, null);
}
public Context subContext(final Element elem, final List<Element> siblingElements) {
final Context context2 = new Context(getFilename(), elem == null? this.element:elem,
getFunctionName(), isInAssignmentExpression(),
callStack,tokens);
context2.siblingElements=siblingElements;
if(siblingElements == null && elem==this.element){
context2.siblingElements=this.siblingElements;
}
context2.setInComponent(isInComponent());
context2.parent = this;
return context2;
Expand Down Expand Up @@ -228,7 +253,7 @@ public ContextTokensIterator(Token token, int direction){
@Override
public boolean hasNext() {
if(direction <0)
return tokens != null && tokenIndex >= 0;
return tokens != null && tokenIndex > 0;
else
return tokens != null && tokenIndex < tokens.getTokens().size();
}
Expand Down Expand Up @@ -261,4 +286,16 @@ public boolean isSuppressed(BugInfo bugInfo){
return ignores.contains(bugInfo.getMessageCode())
|| (parent != null && parent.isSuppressed(bugInfo));
}

public Element getPreviousSiblingElement(){
if(element.getParentElement() != null){
return getElementBefore(element,element.getParentElement().getChildElements());
}
if(siblingElements != null){
return getElementBefore(element,siblingElements);
}
return null;
}


}
24 changes: 8 additions & 16 deletions src/main/java/com/cflint/tools/CFTool.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
package com.cflint.tools;

import java.lang.reflect.Field;
import java.util.List;

import cfml.parsing.cfscript.CFExpression;
import cfml.parsing.cfscript.CFUnaryExpression;
import net.htmlparser.jericho.Element;

public class CFTool {

public static CFExpression sub(final CFUnaryExpression expression) {
Field f;
try {
f = CFUnaryExpression.class.getDeclaredField("sub");
f.setAccessible(true);
return (CFExpression) (f.get(expression));
} catch (final Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

public static Element getNamedParent(Element elem, final String tagName) {
elem = elem.getParentElement();
while (elem != null && !elem.getName().equals(tagName)) {
Expand All @@ -35,4 +20,11 @@ public static boolean toBoolean(final String value) {
}
return value.trim().equalsIgnoreCase("yes") || value.trim().equalsIgnoreCase("true");
}

public static Element getElementBefore(Element element, List<Element> elements) {
if(element != null && elements != null && elements.indexOf(element) >0){
return elements.get(elements.indexOf(element) - 1);
}
return null;
}
}
1 change: 1 addition & 0 deletions src/test/java/com/cflint/TestCFBugs_MethodNames.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public void testValidNamesTag() throws ParseException, IOException {
+ "</cfcomponent>";
cfBugs.process(tagSrc, "test");
Collection<List<BugInfo>> result = cfBugs.getBugs().getBugList().values();
System.out.println(result);
assertEquals(0, result.size());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!---
Test Comment
--->
<cfcomponent>

<cffunction name="testFunction" displayname="" access="public" output="false" returntype="void" hint="">
<cfscript>
someVar = '';
</cfscript>
</cffunction>

</cfcomponent>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[ {
"severity" : "ERROR",
"id" : "MISSING_VAR",
"message" : "MISSING_VAR",
"category" : "CFLINT",
"abbrev" : "MV",
"locations" : [ {
"file" : "src\\test\\resources\\com\\cflint\\tests\\Ignores\\ignoreCFMLComponent.cfc",
"fileName" : "ignoreCFMLComponent.cfc",
"function" : "testFunction",
"column" : "8",
"line" : "8",
"message" : "Variable someVar is not declared with a var statement.",
"variable" : "someVar",
"expression" : "someVar"
} ]
} ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!---
Test Comment
@CFLintIgnore SOMETHINGELSE,MISSING_VAR,ANOTHERTHINGTOIGNORE
--->
<cfcomponent>

<cffunction name="testFunction" displayname="" access="public" output="false" returntype="void" hint="">
<cfscript>
someVar = '';
</cfscript>
</cffunction>

</cfcomponent>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
component {

/*
Test!
Test!
*/
public void function function1() {
someVar = '';
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
[ ]
[ {
"severity" : "ERROR",
"id" : "MISSING_VAR",
"message" : "MISSING_VAR",
"category" : "CFLINT",
"abbrev" : "MV",
"locations" : [ {
"file" : "src\\test\\resources\\com\\cflint\\tests\\Ignores\\ignoreMultiLine.cfc",
"fileName" : "ignoreMultiLine.cfc",
"function" : "function1",
"column" : "6",
"line" : "7",
"message" : "Variable someVar is not declared with a var statement.",
"variable" : "someVar",
"expression" : "someVar"
} ]
} ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
Test!
*/
component {

public void function function1() {
someVar = '';
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[ {
"severity" : "ERROR",
"id" : "MISSING_VAR",
"message" : "MISSING_VAR",
"category" : "CFLINT",
"abbrev" : "MV",
"locations" : [ {
"file" : "src\\test\\resources\\com\\cflint\\tests\\Ignores\\ignoreMultiLineComponent.cfc",
"fileName" : "ignoreMultiLineComponent.cfc",
"function" : "function1",
"column" : "6",
"line" : "7",
"message" : "Variable someVar is not declared with a var statement.",
"variable" : "someVar",
"expression" : "someVar"
} ]
} ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
Test!
@CFLintIgnore SOMETHINGELSE,MISSING_VAR,ANOTHERTHINGTOIGNORE
*/
component {

public void function function1() {
someVar = '';
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[ {
"severity" : "ERROR",
"id" : "MISSING_VAR",
"message" : "MISSING_VAR",
"category" : "CFLINT",
"abbrev" : "MV",
"locations" : [ {
"file" : "src\\test\\resources\\com\\cflint\\tests\\Ignores\\ignoreMultiLineComponent2.cfc",
"fileName" : "ignoreMultiLineComponent2.cfc",
"function" : "function1",
"column" : "6",
"line" : "8",
"message" : "Variable someVar is not declared with a var statement.",
"variable" : "someVar",
"expression" : "someVar"
} ]
} ]

0 comments on commit 36d67a4

Please sign in to comment.