/* * Copyright 2009 The Closure Compiler Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.javascript.jscomp; import com.google.common.base.Preconditions; import com.google.common.collect.Lists; import com.google.javascript.jscomp.NameReferenceGraph.Name; import com.google.javascript.jscomp.NameReferenceGraph.Reference; import com.google.javascript.jscomp.graph.DiGraph.DiGraphEdge; import com.google.javascript.jscomp.graph.DiGraph.DiGraphNode; import com.google.javascript.rhino.Node; import com.google.javascript.rhino.jstype.JSType; import java.util.Collections; import java.util.Comparator; import java.util.List; /** * Generate a nice HTML file describing the name reference graph. * For each declaration, list the sites where the declaration's name * is referenced, and list all the names that the declaration references. * For each, name exactly where use occurs in the source code. * *
This report should be useful both for internal compiler * developers and for engineers trying to understand running behavior * of their code or who want to understand why the compiler won't * move their code into a new module. * * @author bowdidge@google.com (Robert Bowdidge) */ final class NameReferenceGraphReport { private NameReferenceGraph graph = null; /** * Create a NameReferenceGraphReport object. * * @param g name reference graph to describe in report. */ NameReferenceGraphReport(NameReferenceGraph g) { this.graph = g; } /** * Generate a nice HTML file describing the name reference graph. * For each declaration, list the sites where the declaration's name * is referenced, and list all the names that the declaration references. * For each, name exactly where use occurs in the source code. * *
This report should be useful both for internal compiler
* developers and for engineers trying to understand running
* behavior of their code or who want to understand why
* AbstractCompiler won't move their code into a new module.
*
* @return String containing the entire HTML for the report.
*/
public String getHtmlReport() {
StringBuilder builder = new StringBuilder();
ListName Reference Graph Dump
\n");
builder.append("OVERALL STATS\n");
builder.append("\n");
builder.append("
\n");
builder.append("ALL NAMES\n");
builder.append("\n");
// Sort declarations in alphabetical order.
Collections.sort(nodes, new DiGraphNodeComparator());
for (DiGraphNode
\n");
generateHtmlReportFooter(builder);
return builder.toString();
}
/**
* Given a node, find the name of the containing source file.
*
* @param node Parse tree node whose filename is requested
* @return String containing name of source file, or empty string if name
* cannot be identified.
*/
private String getSourceFile(Node node) {
String filename = node.getSourceFileName();
if (filename == null) {
return "";
}
return filename;
}
/**
* Generate the HTML for describing a specific declaration.
* @param builder contents of report to be generated
* @param declarationNode declaration to describe
*/
private void generateDeclarationReport(StringBuilder builder,
DiGraphNode");
if (outEdges.size() > 0) {
builder.append("
\n");
}
}
builder.append("
\n");
builder.append("");
for (DiGraphEdge
\n");
}
if (inEdges.size() > 0) {
builder.append("
\n");
builder.append("");
for (DiGraphEdge
");
}
builder.append("
No definitions found
");
} else {
// Otherwise, provide a list of definitions in a dotted list.
// For each definition, print the location where that definition is
// found.
builder.append("");
for (DefinitionsRemover.Definition def : defs) {
Node fnDef = def.getRValue();
String sourceFileName = getSourceFile(fnDef);
builder.append("
");
}
}
/**
* Generate the HTML header for the report style.
* Borrowed straight from NameAnalyzer's report style.
*
* @param builder contents of the report to be generated
*/
private void generateHtmlReportHeader(StringBuilder builder) {
builder.append("\n" +
"" +
"" +
"" +
"