| 1 | /* |
| 2 | * Copyright 2010 The Closure Compiler Authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.google.javascript.jscomp.jsonml; |
| 18 | |
| 19 | import com.google.common.base.Preconditions; |
| 20 | import com.google.javascript.rhino.Node; |
| 21 | import com.google.javascript.rhino.Token; |
| 22 | |
| 23 | /** |
| 24 | * Non public methods copied from com.google.javascript.jscomp.NodeUtil class. |
| 25 | * |
| 26 | * @author [email protected] (Daniel Hans) |
| 27 | */ |
| 28 | class NodeUtil { |
| 29 | |
| 30 | /** |
| 31 | * @return Whether the node represents a FOR-IN loop. |
| 32 | */ |
| 33 | static boolean isForIn(Node n) { |
| 34 | return n.getType() == Token.FOR |
| 35 | && n.getChildCount() == 3; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @return Whether the node is used as a statement. |
| 40 | */ |
| 41 | static boolean isStatement(Node n) { |
| 42 | Node parent = n.getParent(); |
| 43 | // It is not possible to determine definitely if a node is a statement |
| 44 | // or not if it is not part of the AST. A FUNCTION node can be |
| 45 | // either part of an expression or a statement. |
| 46 | Preconditions.checkState(parent != null); |
| 47 | switch (parent.getType()) { |
| 48 | case Token.SCRIPT: |
| 49 | case Token.BLOCK: |
| 50 | case Token.LABEL: |
| 51 | return true; |
| 52 | default: |
| 53 | return false; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Is this node a function declaration? A function declaration is a function |
| 59 | * that has a name that is added to the current scope (i.e. a function that |
| 60 | * is not part of a expression). |
| 61 | */ |
| 62 | static boolean isFunctionDeclaration(Node n) { |
| 63 | return n.getType() == Token.FUNCTION && isStatement(n); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Is this node a hoisted function declaration? A function declaration in the |
| 68 | * scope root is hoisted to the top of the scope. |
| 69 | * See {@link #isFunctionDeclaration}). |
| 70 | */ |
| 71 | static boolean isHoistedFunctionDeclaration(Node n) { |
| 72 | return isFunctionDeclaration(n) |
| 73 | && (n.getParent().getType() == Token.SCRIPT |
| 74 | || n.getParent().getParent().getType() == Token.FUNCTION); |
| 75 | } |
| 76 | } |