/** * Removes a goog.require for the given namespace to the file if it * already exists. */ public Builder removeGoogRequire(Match m, String namespace) { Node googRequireNode = findGoogRequireNode(m.getNode(), m.getMetadata(), namespace); if (googRequireNode != null) { return deleteWithoutRemovingWhitespaceBefore(googRequireNode); } return this; }
/** * Returns a Matcher that matches any function call. */ public static Matcher functionCall() { return functionCall(null); }
/** * Returns a Matcher that matches any property access. */ public static Matcher propertyAccess() { return propertyAccess(null); }
private static SuggestedFix removeNode(JSError error) { return new SuggestedFix.Builder() .setOriginalMatchedNode(error.node) .delete(error.node).build(); }
private static SuggestedFix getFixForMissingSemicolon(JSError error) { return new SuggestedFix.Builder() .setOriginalMatchedNode(error.node) .insertAfter(error.node, ";") .build(); }
@Override public void visit(NodeTraversal t, Node n, Node parent) { NodeMetadata metadata = new NodeMetadata(t.getCompiler()); if (scanner.matches(n, metadata)) { Match match = new Match(n, metadata); fixes.addAll(scanner.processMatch(match)); matches.add(match); } } }
/** * Returns a Matcher that matches any function call that has the given * number of arguments and the given name. * @param name The name of the function to match. For non-static functions, * this must be the fully qualified name that includes the type of the * object. For instance: {@code ns.AppContext.prototype.get} will match * {@code appContext.get} and {@code this.get} when called from the * AppContext class. */ public static Matcher functionCallWithNumArgs(final String name, final int numArgs) { return allOf(functionCallWithNumArgs(numArgs), functionCall(name)); }
private Builder insertBefore( Node nodeToInsertBefore, Node n, AbstractCompiler compiler, String sortKey) { return insertBefore(nodeToInsertBefore, generateCode(compiler, n), sortKey); }
@Override public boolean matches(Node node, NodeMetadata metadata) { // TODO(mknichel): Handle the case when functions are applied through .call or .apply. return node.isCall() && propertyAccess(name).matches(node.getFirstChild(), metadata); } };
/** * Returns a Matcher that matches a declaration of a method on the * prototype of a class. */ public static Matcher prototypeMethodDeclaration() { return matcherForPrototypeDeclaration(true /* requireFunctionType */); }
@Override public boolean matches(Node node, NodeMetadata metadata) { for (Matcher m : matchers) { if (!m.matches(node, metadata)) { return false; } } return true; } };
/** * Inserts a new node before the provided node. */ public Builder insertBefore(Node nodeToInsertBefore, Node n, AbstractCompiler compiler) { return insertBefore(nodeToInsertBefore, n, compiler, ""); }
/** * Deletes a node and its contents from the source file. */ public Builder deleteWithoutRemovingWhitespaceBefore(Node n) { return delete(n, false); }
RefasterJsTemplate( AbstractCompiler compiler, TypeMatchingStrategy typeMatchingStrategy, Node beforeTemplate, Node afterTemplate) { this.matcher = new JsSourceMatcher(compiler, beforeTemplate, typeMatchingStrategy); this.beforeTemplate = beforeTemplate; this.afterTemplate = afterTemplate; }
/** * Renames a given node to the provided name. * @param n The node to rename. * @param name The new name for the node. */ public Builder rename(Node n, String name) { return rename(n, name, false); }
/** * Returns a matcher that matches any constructor definitions. */ public static Matcher constructor() { return constructor(null); }
/** * Returns a Matcher that matches a declaration of a variable on the * prototype of a class. */ public static Matcher prototypeVariableDeclaration() { return matcherForPrototypeDeclaration(false /* requireFunctionType */); }
@Override public boolean matches(Node node, NodeMetadata metadata) { for (Matcher m : matchers) { if (m.matches(node, metadata)) { return true; } } return false; } };
/** * Inserts a string before the provided node. This is useful for inserting * comments into a file since the JS Compiler doesn't currently support * printing comments. */ public Builder insertBefore(Node nodeToInsertBefore, String content) { return insertBefore(nodeToInsertBefore, content, ""); }
/** * Deletes a node and its contents from the source file. If the node is a child of a * block or top level statement, this will also delete the whitespace before the node. */ public Builder delete(Node n) { return delete(n, true); }