Java Javascript Mozilla Library isVariable(Name name)

Here you can find the source of isVariable(Name name)

Description

Check if the Name (AstNode) is a variable.

License

Apache License

Parameter

Parameter Description
node a parameter

Declaration

public static boolean isVariable(Name name) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import org.mozilla.javascript.Token;
import org.mozilla.javascript.ast.AstNode;
import org.mozilla.javascript.ast.InfixExpression;
import org.mozilla.javascript.ast.Name;
import org.mozilla.javascript.ast.UnaryExpression;

public class Main {
    /**/*w  w  w.  j a  v  a2 s  .co  m*/
     * Check if the Name (AstNode) is a variable.
     * @param node
     */
    public static boolean isVariable(Name name) {

        AstNode parent = name.getParent();

        if (parent instanceof InfixExpression) {
            InfixExpression ie = (InfixExpression) parent;
            if (ie.getOperator() == Token.GETPROP || ie.getOperator() == Token.GETPROPNOWARN) {
                /* If the parent is field access, make sure it is on the LHS. */
                if (ie.getRight() == name)
                    return false;
            } else {
                /* It is some other boolean operator, so it should be a variable. */
                return true;
            }
        }
        if (parent instanceof UnaryExpression) {
            /* It is a variable that is being operated on by a unary expression. */
            return true;
        }

        return true;
    }
}

Related

  1. isPrototypeNameNode(AstNode node)
  2. isPrototypePropertyGet(PropertyGet pg)
  3. isSimplePropertyGet(PropertyGet pg, String expectedObj, String expectedField)
  4. isStaticProperty(Scriptable property)
  5. isValid(Object[] args)
  6. javaToJS(Object o, Scriptable scope)
  7. jsToJava(Object jsObject)
  8. jsToJava(Object obj)
  9. listToNativeArray(List list)