| 1 | /* |
| 2 | * Copyright 2011 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; |
| 18 | |
| 19 | import com.google.javascript.jscomp.NodeTraversal.AbstractShallowCallback; |
| 20 | import com.google.javascript.jscomp.NodeTraversal.Callback; |
| 21 | import com.google.javascript.rhino.Node; |
| 22 | import com.google.javascript.rhino.jstype.JSType; |
| 23 | import com.google.javascript.rhino.jstype.JSTypeRegistry; |
| 24 | import com.google.javascript.rhino.jstype.ObjectType; |
| 25 | |
| 26 | /** |
| 27 | * A CleanupPass implementation that will remove all field declarations on |
| 28 | * JSTypes contributed by the original file. |
| 29 | * <p> |
| 30 | * This pass is expected to clear out declarations contributed to any JSType, |
| 31 | * even if the constructor declaration is not provided in the file being |
| 32 | * updated. |
| 33 | * |
| 34 | * @author [email protected] (Tyler Goodwin) |
| 35 | */ |
| 36 | public class FieldCleanupPass implements HotSwapCompilerPass { |
| 37 | |
| 38 | private final AbstractCompiler compiler; |
| 39 | |
| 40 | public FieldCleanupPass(AbstractCompiler compiler) { |
| 41 | this.compiler = compiler; |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public void hotSwapScript(Node scriptRoot, Node originalRoot) { |
| 46 | String srcName = originalRoot.getSourceFileName(); |
| 47 | Callback cb = |
| 48 | new QualifiedNameSearchTraversal(compiler.getTypeRegistry(), srcName); |
| 49 | new NodeTraversal(compiler, cb).traverse(originalRoot); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public void process(Node externs, Node root) { |
| 54 | // FieldCleanupPass should not do work during process. |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Search for fields to cleanup by looking for nodes in the tree which are |
| 59 | * root nodes of qualified names and getting the final token of the qualified |
| 60 | * name as a candidate field. |
| 61 | * <p> |
| 62 | * Once a candidate field is found, ask the {@code JSTypeRegistry} for all |
| 63 | * JSTypes that have a field with the same name, and check if the field on |
| 64 | * that type is defined in the file the compiler is cleaning up. If so, remove |
| 65 | * the field, and update the {@code JSTypeRegistry} to no longer associate the |
| 66 | * type with the field. |
| 67 | * <p> |
| 68 | * This algorithm was chosen for simplicity and is less than optimally |
| 69 | * efficient in two ways: |
| 70 | * <p> |
| 71 | * 1) All types with a matching field name are iterated over (when only types |
| 72 | * that extend or implement the JSType indicated by the containing object in |
| 73 | * the found Qualified Name need to be checked). |
| 74 | * <p> |
| 75 | * 2) All Qualified Names are checked, even those which are not L-Values or |
| 76 | * single declarations of an Type Expression. In general field should only be |
| 77 | * declared as part of an assignment ('ns.Type.a = 3;') or stand alone name |
| 78 | * declaration ('ns.Type.a;'). |
| 79 | */ |
| 80 | static class QualifiedNameSearchTraversal extends AbstractShallowCallback { |
| 81 | |
| 82 | private final JSTypeRegistry typeRegistry; |
| 83 | private final String srcName; |
| 84 | |
| 85 | public QualifiedNameSearchTraversal( |
| 86 | JSTypeRegistry typeRegistry, String srcName) { |
| 87 | this.typeRegistry = typeRegistry; |
| 88 | this.srcName = srcName; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void visit(NodeTraversal t, Node n, Node p) { |
| 93 | // We are a root GetProp |
| 94 | if (n.isGetProp() && !p.isGetProp()) { |
| 95 | String propName = getFieldName(n); |
| 96 | JSType type = n.getFirstChild().getJSType(); |
| 97 | if (type == null || type.toObjectType() == null) { |
| 98 | // Note cases like <primitive>.field |
| 99 | return; |
| 100 | } |
| 101 | removeProperty(type.toObjectType(), propName); |
| 102 | } |
| 103 | if (n.getJSDocInfo() != null) { |
| 104 | n.getJSDocInfo().setAssociatedNode(null); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Removes a given property from a type and updates type-registry. |
| 110 | * |
| 111 | * @param type the object type to be updated, should not be null |
| 112 | * @param propName the property to remove |
| 113 | */ |
| 114 | private void removeProperty(ObjectType type, String propName) { |
| 115 | Node pNode = type.getPropertyNode(propName); |
| 116 | if (pNode != null && srcName.equals(pNode.getSourceFileName())) { |
| 117 | typeRegistry.unregisterPropertyOnType(propName, type); |
| 118 | type.removeProperty(propName); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | private String getFieldName(Node n) { |
| 123 | return n.getLastChild().getString(); |
| 124 | } |
| 125 | } |
| 126 | } |