Search in sources :

Example 1 with CFAssignmentExpression

use of cfml.parsing.cfscript.CFAssignmentExpression in project CFLint by cflint.

the class CFLint method process.

/**
 * @param expression
 *            CF expression
 * @param elem
 *            Jericho HTML element
 * @param oldcontext
 *            The previous context
 */
private void process(final CFExpression expression, final Element elem, final Context oldcontext) {
    if (expression != null) {
        final Context context = oldcontext.subContext(elem);
        for (final CFLintScanner plugin : extensions) {
            try {
                plugin.expression(expression, context, bugs);
                for (final ContextMessage message : context.getMessages()) {
                    reportRule(elem, expression, context, plugin, message);
                }
                context.getMessages().clear();
            } catch (final Exception e) {
                printException(e);
                reportRule(elem, expression, context, plugin, PLUGIN_ERROR);
            }
        }
        // Handle a few expression types in a special fashion.
        if (expression instanceof CFVarDeclExpression) {
            handler.addVariable(((CFVarDeclExpression) expression).getName());
        }
        // CFIdentifier should not decompose
        if (expression.getClass().equals(CFIdentifier.class)) {
            final String name = ((CFIdentifier) expression).getName();
            handler.checkVariable(name);
        }
        if (expression instanceof CFStructElementExpression) {
            final Context assignmentContext = context.subContext(elem);
            assignmentContext.setInStructKeyExpression(true);
            handler.push(CF.STRUCT);
            process(((CFStructElementExpression) expression).getKey(), elem, assignmentContext);
            handler.pop();
            process(((CFStructElementExpression) expression).getValue(), elem, context);
        } else if (expression instanceof CFVarDeclExpression) {
            final Context assignmentContext = context.subContext(elem);
            assignmentContext.setInAssignmentExpression(true);
            process(((CFVarDeclExpression) expression).getVar(), elem, assignmentContext);
            // Right hand side is handled below. Left hand side gets a
            // special context.
            process(((CFVarDeclExpression) expression).getInit(), elem, context);
        // Only process function call expressions
        } else if (expression instanceof CFAssignmentExpression && !(expression instanceof CFTernaryExpression)) {
            final Context assignmentContext = context.subContext(elem);
            assignmentContext.setInAssignmentExpression(true);
            process(((CFAssignmentExpression) expression).getLeft(), elem, assignmentContext);
            // Right hand side is handled below. Left hand side gets a
            // special context.
            process(((CFAssignmentExpression) expression).getRight(), elem, context);
        // Only process function call expressions
        } else if (expression instanceof CFFullVarExpression) {
            final CFFullVarExpression fullVarExpression = (CFFullVarExpression) expression;
            if (context.isInAssignmentExpression() && new CFScopes().isScoped(fullVarExpression, "local") && fullVarExpression.getExpressions().size() > 1) {
                handler.addVariable(fullVarExpression.getExpressions().get(1).Decompile(0));
            }
            final Context subContext = context.subContext(context.getElement());
            subContext.setInAssignmentExpression(false);
            for (final CFExpression expr : fullVarExpression.getExpressions()) {
                if (expr instanceof CFFunctionExpression) {
                    process(expr, elem, subContext);
                } else if (expr instanceof CFMember) {
                    process(((CFMember) expr).getExpression(), elem, subContext);
                } else if (expr instanceof CFArrayExpression) {
                    final CFArrayExpression aryExpr = (CFArrayExpression) expr;
                    if (!aryExpr.getElements().isEmpty()) {
                        process(aryExpr.getElements().get(0), elem, subContext);
                    }
                }
            }
        } else if (expression instanceof CFFunctionExpression && tagInfo.isTag(((CFFunctionExpression) expression).getFunctionName())) {
            final CFFunctionExpression functionExpr = (CFFunctionExpression) expression;
            final StringBuilder sb = new StringBuilder();
            sb.append("<").append(functionExpr.getFunctionName()).append(" ");
            for (final CFExpression expr : functionExpr.getArgs()) {
                if (expr instanceof CFAssignmentExpression) {
                    final CFAssignmentExpression assignExpr = (CFAssignmentExpression) expr;
                    sb.append(assignExpr.getLeft().Decompile(0)).append("=").append(assignExpr.getRight().Decompile(0)).append(" ");
                }
            }
            sb.append("/>");
            final CFMLSource source = new CFMLSource(sb.toString());
            try {
                processStack(source.getChildElements(), " ", context.subContextCFML(source.getChildElements().size() > 0 ? source.getChildElements().get(0) : null, expression));
            } catch (CFLintScanException e) {
            }
            if (functionExpr.getBody() != null) {
                process(functionExpr.getBody(), context);
            }
        } else if (!(expression instanceof CFNewExpression)) {
            // EXCEPT CFNewExpressions.
            for (final CFExpression child : expression.decomposeExpression()) {
                process(child, elem, context.subContextInAssignment(false));
            }
        } else {
            // Process only the right hand side of new expressions
            final CFNewExpression newExpr = (CFNewExpression) expression;
            for (final CFExpression child : (List<CFExpression>) newExpr.getArgs()) {
                if (child instanceof CFAssignmentExpression) {
                    process(((CFAssignmentExpression) child).getRight(), elem, context.subContextInAssignment(false));
                }
            }
        }
    }
}
Also used : Context(com.cflint.plugins.Context) CFStructElementExpression(cfml.parsing.cfscript.CFStructElementExpression) CFMLSource(cfml.parsing.CFMLSource) CFNewExpression(cfml.parsing.cfscript.CFNewExpression) CFFunctionExpression(cfml.parsing.cfscript.CFFunctionExpression) CFLintScanException(com.cflint.exception.CFLintScanException) CFLintScanner(com.cflint.plugins.CFLintScanner) CFAssignmentExpression(cfml.parsing.cfscript.CFAssignmentExpression) CFIdentifier(cfml.parsing.cfscript.CFIdentifier) ParseException(cfml.parsing.reporting.ParseException) IOException(java.io.IOException) CFLintScanException(com.cflint.exception.CFLintScanException) CFScopes(com.cflint.plugins.core.CFScopes) CFArrayExpression(cfml.parsing.cfscript.CFArrayExpression) CFExpression(cfml.parsing.cfscript.CFExpression) ContextMessage(com.cflint.plugins.Context.ContextMessage) CFMember(cfml.parsing.cfscript.CFMember) CFVarDeclExpression(cfml.parsing.cfscript.CFVarDeclExpression) CFTernaryExpression(cfml.parsing.cfscript.CFTernaryExpression) CFFullVarExpression(cfml.parsing.cfscript.CFFullVarExpression)

Aggregations

CFMLSource (cfml.parsing.CFMLSource)1 CFArrayExpression (cfml.parsing.cfscript.CFArrayExpression)1 CFAssignmentExpression (cfml.parsing.cfscript.CFAssignmentExpression)1 CFExpression (cfml.parsing.cfscript.CFExpression)1 CFFullVarExpression (cfml.parsing.cfscript.CFFullVarExpression)1 CFFunctionExpression (cfml.parsing.cfscript.CFFunctionExpression)1 CFIdentifier (cfml.parsing.cfscript.CFIdentifier)1 CFMember (cfml.parsing.cfscript.CFMember)1 CFNewExpression (cfml.parsing.cfscript.CFNewExpression)1 CFStructElementExpression (cfml.parsing.cfscript.CFStructElementExpression)1 CFTernaryExpression (cfml.parsing.cfscript.CFTernaryExpression)1 CFVarDeclExpression (cfml.parsing.cfscript.CFVarDeclExpression)1 ParseException (cfml.parsing.reporting.ParseException)1 CFLintScanException (com.cflint.exception.CFLintScanException)1 CFLintScanner (com.cflint.plugins.CFLintScanner)1 Context (com.cflint.plugins.Context)1 ContextMessage (com.cflint.plugins.Context.ContextMessage)1 CFScopes (com.cflint.plugins.core.CFScopes)1 IOException (java.io.IOException)1