Skip to content

Commit

Permalink
Merge pull request #190 from stepstone-tech/feature-excludeparamsnames
Browse files Browse the repository at this point in the history
Allow to skip analyse for specific variable names
  • Loading branch information
ryaneberly authored Aug 5, 2016
2 parents 111ec82 + 17d1193 commit 3d9aeda
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion src/main/java/com/cflint/plugins/core/VariableNameChecker.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.cflint.plugins.core;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.cflint.BugList;
import com.cflint.plugins.CFLintScannerAdapter;
import com.cflint.plugins.Context;
Expand All @@ -13,8 +17,41 @@
@Extension
public class VariableNameChecker extends CFLintScannerAdapter {
public static final String VARIABLE = "Variable ";

private static final List<String> DEFAULT_EXCLUSIONS = Collections.singletonList("rc");
private static final String PARAM_EXCLUSION_LIST = "ExclusionList";

private final List<String> exclusions = new ArrayList<>();

final String severity = "INFO";


@Override
public void setParameter(String name, String value) {
super.setParameter(name, value);

populateExclusions();
}

private void populateExclusions() {
exclusions.clear();

final String exclusionList = getParameter(PARAM_EXCLUSION_LIST);

if (exclusionList == null) {
exclusions.addAll(DEFAULT_EXCLUSIONS);
return;
}

final String[] split = exclusionList.split(",");

for (String name : split) {
final String normalizedValue = normalize(name);
if (normalizedValue != null) {
exclusions.add(normalizedValue);
}
}
}

public void expression(final CFExpression expression, final Context context, final BugList bugs) {
if (expression instanceof CFVarDeclExpression) {
final CFVarDeclExpression cfVarDeclExpression = (CFVarDeclExpression)expression;
Expand All @@ -36,6 +73,10 @@ else if (expression instanceof CFIdentifier) {
}

public void checkNameForBugs(final Context context, String variable, String filename, String functionName, int line, BugList bugs) {
if (excludeFromAnalyse(variable)) {
return;
}

int minVarLength = ValidName.MIN_VAR_LENGTH;
int maxVarLength = ValidName.MAX_VAR_LENGTH;
int maxVarWords = ValidName.MAX_VAR_WORDS;
Expand Down Expand Up @@ -89,4 +130,16 @@ public void checkNameForBugs(final Context context, String variable, String file
context.addMessage("VAR_HAS_PREFIX_OR_POSTFIX", variable);
}
}

protected boolean excludeFromAnalyse(String variable) {
return exclusions.contains(normalize(variable));
}

protected String normalize(String name) {
if (name == null || name.trim().isEmpty()) {
return null;
}

return name.trim().toLowerCase();
}
}

0 comments on commit 3d9aeda

Please sign in to comment.