Skip to content

Commit

Permalink
#405 implemented. (#460)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryaneberly authored Aug 21, 2017
1 parent 0b9befa commit c0e66aa
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 6 deletions.
13 changes: 12 additions & 1 deletion src/main/java/com/cflint/plugins/core/StructKeyChecker.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.cflint.plugins.core;

import java.util.List;

import com.cflint.BugList;
import com.cflint.plugins.CFLintScannerAdapter;
import com.cflint.plugins.Context;

import cfml.parsing.cfscript.CFExpression;
import cfml.parsing.cfscript.CFFullVarExpression;
import cfml.parsing.cfscript.CFIdentifier;
import cfml.parsing.cfscript.CFStructElementExpression;
import cfml.parsing.cfscript.CFStructExpression;

Expand All @@ -13,7 +17,14 @@ public class StructKeyChecker extends CFLintScannerAdapter {
@Override
public void expression(final CFExpression expression, final Context context, final BugList bugs) {

if (expression instanceof CFStructExpression) {
if (context.isInAssignmentExpression() && expression instanceof CFFullVarExpression) {
final List<CFExpression> subExpressions = expression.decomposeExpression();
if (subExpressions != null && subExpressions.size() > 1
&& subExpressions.get(subExpressions.size() - 1) instanceof CFIdentifier) {
context.addMessage("STRUCT_ARRAY_NOTATION", subExpressions.get(subExpressions.size() - 1).Decompile(0));
}
}
else if (expression instanceof CFStructExpression) {
CFStructExpression structExpression = (CFStructExpression) expression;
for (Object element : structExpression.getElements()) {
CFStructElementExpression structKeyExpression = (CFStructElementExpression) element;
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/cflint.definition.json
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,11 @@
"code": "UNQUOTED_STRUCT_KEY",
"messageText": "Unquoted struct key ${variable} is not case-sensitive. Quoting it is recommended.",
"severity": "WARNING"
},
{
"code": "STRUCT_ARRAY_NOTATION",
"messageText": "Unquoted struct key ${variable} is not case-sensitive. Using array notation is recommended.",
"severity": "WARNING"
}
],
"parameter": []
Expand Down Expand Up @@ -1003,6 +1008,7 @@
"UNUSED_LOCAL_VARIABLE",
"UNUSED_METHOD_ARGUMENT",
"UNQUOTED_STRUCT_KEY",
"STRUCT_ARRAY_NOTATION",
"USE_DISPLAY_NAME"
]
}, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
"output" : [ ],
"rule" : [ ],
"excludes" : [ ],
"includes" : [ {
"code" : "UNQUOTED_STRUCT_KEY",
"messageText" : null,
"severity" : null
} ],
"includes" : [ {"code" : "UNQUOTED_STRUCT_KEY"},
{"code" : "STRUCT_ARRAY_NOTATION"}
],
"inheritParent" : false,
"inheritPlugins" : true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// not compliant
component{
function x(){
sampleStruct.yetAnotherKey = "yetAnotherValue";
//repeat
sampleStruct.yetAnotherKey = "yetAnotherValue";
sampleStruct.foo["abc"] = "yetAnotherValue";
sampleStruct.foo().xyzzy["abc"] = "yetAnotherValue";
sampleStruct.foo().bar = "yetAnotherValue";
sampleStruct.foo() = "yetAnotherValue";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"version" : "",
"timestamp" : 0,
"issues" : [ {
"severity" : "WARNING",
"id" : "STRUCT_ARRAY_NOTATION",
"message" : "STRUCT_ARRAY_NOTATION",
"category" : "CFLINT",
"abbrev" : "SA",
"locations" : [ {
"file" : "src/test/resources/com/cflint/tests/TooManyFunctionsChecker/StructKeyQuoted/dotNotation_405.cfc",
"fileName" : "dotNotation_405.cfc",
"function" : "x",
"column" : 3,
"line" : 4,
"message" : "Unquoted struct key yetAnotherKey is not case-sensitive. Using array notation is recommended.",
"variable" : "yetAnotherKey",
"expression" : "sampleStruct.yetAnotherKey"
} ]
}, {
"severity" : "WARNING",
"id" : "STRUCT_ARRAY_NOTATION",
"message" : "STRUCT_ARRAY_NOTATION",
"category" : "CFLINT",
"abbrev" : "SA",
"locations" : [ {
"file" : "src/test/resources/com/cflint/tests/TooManyFunctionsChecker/StructKeyQuoted/dotNotation_405.cfc",
"fileName" : "dotNotation_405.cfc",
"function" : "x",
"column" : 3,
"line" : 6,
"message" : "Unquoted struct key yetAnotherKey is not case-sensitive. Using array notation is recommended.",
"variable" : "yetAnotherKey",
"expression" : "sampleStruct.yetAnotherKey"
} ]
}, {
"severity" : "WARNING",
"id" : "STRUCT_ARRAY_NOTATION",
"message" : "STRUCT_ARRAY_NOTATION",
"category" : "CFLINT",
"abbrev" : "SA",
"locations" : [ {
"file" : "src/test/resources/com/cflint/tests/TooManyFunctionsChecker/StructKeyQuoted/dotNotation_405.cfc",
"fileName" : "dotNotation_405.cfc",
"function" : "x",
"column" : 3,
"line" : 9,
"message" : "Unquoted struct key bar is not case-sensitive. Using array notation is recommended.",
"variable" : "bar",
"expression" : "sampleStruct.foo().bar"
} ]
} ],
"counts" : {
"totalFiles" : 0,
"totalLines" : 0,
"countByCode" : [ {
"code" : "STRUCT_ARRAY_NOTATION",
"count" : 3
} ],
"countBySeverity" : [ {
"severity" : "WARNING",
"count" : 3
} ]
}
}

0 comments on commit c0e66aa

Please sign in to comment.