Skip to content

Commit

Permalink
Merge pull request #218 from cflint/master
Browse files Browse the repository at this point in the history
update dev
  • Loading branch information
ryaneberly authored Nov 25, 2016
2 parents 1489694 + 2fa060b commit c67917a
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 50 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.cflint</groupId>
<artifactId>CFLint</artifactId>
<version>0.8.0</version>
<version>0.9.0</version>

<name>CFLint</name>
<description>
Expand Down
41 changes: 28 additions & 13 deletions src/main/java/com/cflint/CFLint.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -209,17 +208,36 @@ protected ParserTag getFirstTagQuietly(final CFMLSource cfmlSource) {

public void processStack(final List<Element> elements, final String space, final String filename,
final CFIdentifier functionName) throws ParseException, IOException {
Element commentElement = null;
for (final Element elem : elements) {
final Context context = new Context(filename, elem, functionName, false, handler);
context.setSiblingElements(elements);
process(elem, space, context);
if (elem.getName().equals("!---")){
commentElement=elem;
}else{
final Context context = new Context(filename, elem, functionName, false, handler);
if(commentElement != null){
applyRuleOverrides(context,commentElement);
commentElement=null;
}
process(elem, space, context);

}
}
}

public void processStack(final List<Element> elements, final String space, final Context context)
throws ParseException, IOException {
Element commentElement = null;
for (final Element elem : elements) {
process(elem, space, context.subContext(elem, elements));
if (elem.getName().equals("!---")){
commentElement=elem;
}else{
final Context subContext = context.subContext(elem);
if(commentElement != null){
applyRuleOverrides(subContext,commentElement);
commentElement=null;
}
process(elem, space, subContext);
}
}
}

Expand All @@ -230,13 +248,11 @@ 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"));
registerRuleOverrides(componentContext);
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"));
registerRuleOverrides(functionContext);
handler.push("function");
doStructureStart(elem, functionContext, CFFuncDeclStatement.class);
}
Expand Down Expand Up @@ -280,7 +296,6 @@ 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"));
registerRuleOverrides(functionContext);
scanElement(elem, functionContext);
processStack(elem.getChildElements(), space + " ", functionContext);
for (final CFLintStructureListener structurePlugin : getStructureListeners(extensions)) {
Expand All @@ -299,7 +314,6 @@ 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"));
registerRuleOverrides(componentContext);
scanElement(elem, componentContext);

processStack(elem.getChildElements(), space + " ", componentContext);
Expand Down Expand Up @@ -526,12 +540,12 @@ protected void registerRuleOverrides(Context context, final Token functionToken)
/**
* Register any overrides from comment elements before functions/components.
* @param context The current context.
* @param commentElement The CFML comment element
*/
protected void registerRuleOverrides(Context context) {
protected void applyRuleOverrides(Context context,Element commentElement) {

final Element priorElement = context == null ? null : context.getPreviousSiblingElement();
if (priorElement != null && "!---".equals(priorElement.getName())) {
String mlText = priorElement.toString();
if (commentElement != null && "!---".equals(commentElement.getName())) {
String mlText = commentElement.toString();
Pattern pattern = Pattern.compile(".*\\s*@CFLintIgnore\\s+([\\w,_]+)\\s*.*", Pattern.DOTALL);
Matcher matcher = pattern.matcher(mlText);
if (matcher.matches()) {
Expand Down Expand Up @@ -814,6 +828,7 @@ protected void fireStartedProcessing(final String srcidentifier) {
cfmlParser = new CFMLParser();
cfmlParser.setErrorReporter(this);
currentFile = srcidentifier;
currentElement = null;
for (final CFLintStructureListener structurePlugin : getStructureListeners(extensions)) {
try {
structurePlugin.startFile(srcidentifier, bugs);
Expand Down
31 changes: 1 addition & 30 deletions src/main/java/com/cflint/plugins/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public class Context {
String filename;
String componentName;
final Element element;
List<Element> siblingElements;
CFFuncDeclStatement functionInfo;

String functionName;
Expand Down Expand Up @@ -111,14 +110,6 @@ 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 @@ -187,21 +178,11 @@ public Integer getLine() {
}
}

public Context subContext(final Element elem) {
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) {
public Context subContext(final Element elem) {
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 @@ -300,16 +281,6 @@ public boolean isSuppressed(BugInfo bugInfo){
|| (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;
}

public CFFuncDeclStatement getFunctionInfo() {
return functionInfo;
}
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/com/cflint/TestCFLint2Files.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ public void testVarAndArgs_DisabledOther() throws ParseException, IOException {
assertEquals(1, result.size());
assertEquals("ARG_DEFAULT_MISSING", result.get(0).getMessageCode());

final String cfcSrc2 = "component {\r\n" + " public string function fooFunction() {\r\n"
+ "local.query = new Query();\r\n" + "local.query.setSql(\"\r\n"
+ " SELECT id from table where id = #arguments.id#\");" + "</cfscript>\r\n"
+ " }\r\n"
+ "}";
final String cfcSrc2 = "component {\n" +
" public string function fooFunction() {\n" +
" return foo = bar\n" +
" }\n" +
"}";
cfBugs.process(cfcSrc2, "test");
final List<BugInfo> result2 = cfBugs.getBugs().getBugList().get("QUERYPARAM_REQ");
final List<BugInfo> result2 = cfBugs.getBugs().getBugList().get("MISSING_SEMI");
assertEquals(1, result2.size());
System.out.println(result2);
assertEquals(4, result2.get(0).getLine());
Expand Down
12 changes: 12 additions & 0 deletions src/test/resources/com/cflint/tests/Ignores/ignoreCFMLAny1.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<cfcomponent>

<cffunction name="testFunction" displayname="" access="public" output="false" returntype="void" hint="">
<!---
@CFLintIgnore SOMETHINGELSE,MISSING_VAR,ANOTHERTHINGTOIGNORE
--->
<cfscript>
someVar = '';
</cfscript>
</cffunction>

</cfcomponent>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ ]
18 changes: 18 additions & 0 deletions src/test/resources/com/cflint/tests/Ignores/ignoreCFMLAny2.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!---
This shouldn't ignore the missing var int the 2nd cfscript;
--->
<cfcomponent>

<cffunction name="testFunction" displayname="" access="public" output="false" returntype="void" hint="">
<!---
@CFLintIgnore SOMETHINGELSE,MISSING_VAR,ANOTHERTHINGTOIGNORE
--->
<cfscript>
var test = '';
</cfscript>
<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\\ignoreCFMLAny2.cfc",
"fileName" : "ignoreCFMLAny2.cfc",
"function" : "testFunction",
"column" : "8",
"line" : "14",
"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,19 @@
<!---
This shouldn ignore the missing var int the nested cfscript;
--->
<cfcomponent>

<cffunction name="testFunction" displayname="" access="public" output="false" returntype="void" hint="">
<!---
@CFLintIgnore SOMETHINGELSE,MISSING_VAR,ANOTHERTHINGTOIGNORE
--->
<div>
<cfsavecontent>
<cfscript>
someVar = '';
</cfscript>
</cfsavecontent>
</div>
</cffunction>

</cfcomponent>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ ]
12 changes: 12 additions & 0 deletions src/test/resources/com/cflint/tests/Ignores/ignoreCFMLFunction.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<cfcomponent>

<!---
@CFLintIgnore SOMETHINGELSE,MISSING_VAR,ANOTHERTHINGTOIGNORE
--->
<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 @@
[ ]

0 comments on commit c67917a

Please sign in to comment.