RefasterJsScanner scanner = new RefasterJsScanner(); scanner.setTypeMatchingStrategy(typeMatchingStrategy); scanner.loadRefasterJsTemplate(refasterJsTemplate); CompilerOptions options = new CompilerOptions(); options.setEnvironment(environment);
@Override public boolean matches(Node node, NodeMetadata metadata) { if (templates == null) { try { initialize(metadata.getCompiler()); } catch (Exception e) { Throwables.propagate(e); } } matchedTemplate = null; for (RefasterJsTemplate template : templates) { if (template.matcher.matches(node, metadata)) { matchedTemplate = template; return true; } } return false; }
clone.addChildToBack(transformNode(child, templateNodeToMatchMap));
@Override public List<SuggestedFix> processMatch(Match match) { SuggestedFix.Builder fix = new SuggestedFix.Builder(); Node newNode = transformNode( matchedTemplate.afterTemplate.getLastChild(), matchedTemplate.matcher.getTemplateNodeToMatchMap()); Node nodeToReplace = match.getNode(); fix.setOriginalMatchedNode(nodeToReplace); fix.replace(nodeToReplace, newNode, match.getMetadata().getCompiler()); // If the template is a multiline template, make sure to delete the same number of sibling nodes // as the template has. Node n = match.getNode().getNext(); for (int i = 1; i < matchedTemplate.beforeTemplate.getLastChild().getChildCount(); i++) { Preconditions.checkNotNull( n, "Found mismatched sibling count between before template and matched node.\n" + "Template: %s\nMatch: %s", matchedTemplate.beforeTemplate.getLastChild(), match.getNode()); fix.delete(n); n = n.getNext(); } // Add/remove any goog.requires for (String require : matchedTemplate.getGoogRequiresToAdd()) { fix.addGoogRequire(match, require); } for (String require : matchedTemplate.getGoogRequiresToRemove()) { fix.removeGoogRequire(match, require); } return ImmutableList.of(fix.build()); }