All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fife.rsta.ac.js.util.RhinoUtil Maven / Gradle / Ivy

/*
 * 06/05/2014
 *
 * Copyright (C) 2014 Robert Futrell
 * robert_futrell at users.sourceforge.net
 * http://fifesoft.com/rsyntaxtextarea
 *
 * This library is distributed under a modified BSD license.  See the included
 * LICENSE.md file for details.
 */
package org.fife.rsta.ac.js.util;

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

import org.mozilla.javascript.Token;
import org.mozilla.javascript.ast.AstNode;
import org.mozilla.javascript.ast.FunctionNode;
import org.mozilla.javascript.ast.Name;
import org.mozilla.javascript.ast.PropertyGet;
import org.mozilla.javascript.ast.StringLiteral;


/**
 * Utility methods for walking ASTs from Rhino.
 *
 * @author Robert Futrell
 * @version 1.0
 */
public class RhinoUtil {


	/**
	 * Private constructor to prevent instantiation.
	 */
	private RhinoUtil() {
	}


	/**
	 * Iterates through a function's parameters and returns a string
	 * representation of them, suitable for presentation as part of the
	 * method's signature.
	 *
	 * @param fn The function node.
	 * @return The string representation of the function's arguments.
	 */
	public static String getFunctionArgsString(FunctionNode fn) {
		StringBuilder sb = new StringBuilder("(");
		int paramCount = fn.getParamCount();
		if (paramCount>0) {
			List fnParams = fn.getParams();
			for (int i=0; iObjectProperty and returns its value, no matter what the
	 * concrete AST node's type.
	 *
	 * @param propKeyNode The AST node for the property key.
	 * @return The property key's value.
	 */
	public static String getPropertyName(AstNode propKeyNode) {
		// TODO: Does Rhino use any other node type for this?
		return (propKeyNode instanceof Name) ?
					((Name)propKeyNode).getIdentifier() :
					((StringLiteral)propKeyNode).getValue();
	}


	public static String getPrototypeClazz(List nodes) {
		return getPrototypeClazz(nodes, -1);
	}


	public static String getPrototypeClazz(List nodes,
                                           int depth) {
		if (depth<0) {
			depth = nodes.size();
		}
		StringBuilder sb = new StringBuilder();
		for (int i=0; iName with the specified
	 * value.
	 *
	 * @param node The AST node.
	 * @param value The expected value.
	 * @return Whether the AST node is a Name with the specified
	 *         value.
	 */
	private static boolean isName(AstNode node, String value) {
		return node instanceof Name && value.equals(((Name)node).getIdentifier());
	}


	public static boolean isPrototypeNameNode(AstNode node) {
		return node instanceof Name &&
				"prototype".equals(((Name)node).getIdentifier());
	}


	public static boolean isPrototypePropertyGet(PropertyGet pg) {
		return pg!=null && pg.getLeft() instanceof Name &&
				isPrototypeNameNode(pg.getRight());
	}


	/**
	 * Returns whether a PropertyGet is a simple one, referencing
	 * an object's value 1 level deep.  For example, Object.create.
	 *
	 * @param pg The PropertyGet.
	 * @param expectedObj The expected object value.
	 * @param expectedField The expected string value.
	 * @return Whether the object is what was expected.
	 */
	public static boolean isSimplePropertyGet(PropertyGet pg,
                                              String expectedObj, String expectedField) {
		return pg!=null && isName(pg.getLeft(), expectedObj) &&
				isName(pg.getRight(), expectedField);
	}


	public static List toList(AstNode... nodes) {
		List list = new ArrayList<>();
        Collections.addAll(list, nodes);
		return list;
	}


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy