class ReplaceStrings extends NodeTraversal.AbstractPostOrderCallback implements CompilerPass
Modifier and Type | Class and Description |
---|---|
private static class |
ReplaceStrings.Config
Describes a function to look for a which parameters to replace.
|
(package private) static class |
ReplaceStrings.Result
Describes a replacement that occurred.
|
Modifier and Type | Field and Description |
---|---|
(package private) static DiagnosticType |
BAD_REPLACEMENT_CONFIGURATION |
private AbstractCompiler |
compiler |
private static java.lang.String |
DEFAULT_PLACEHOLDER_TOKEN |
private java.util.Map<java.lang.String,ReplaceStrings.Config> |
functions |
private com.google.common.collect.Multimap<java.lang.String,java.lang.String> |
methods |
private DefaultNameGenerator |
nameGenerator |
private java.lang.String |
placeholderToken |
private TypeIRegistry |
registry |
private static java.lang.String |
REPLACE_ALL_MARKER |
private static java.lang.String |
REPLACE_ONE_MARKER |
private java.util.Map<java.lang.String,ReplaceStrings.Result> |
results |
(package private) static com.google.common.base.Predicate<ReplaceStrings.Result> |
USED_RESULTS |
Constructor and Description |
---|
ReplaceStrings(AbstractCompiler compiler,
java.lang.String placeholderToken,
java.util.List<java.lang.String> functionsToInspect,
java.util.Set<java.lang.String> blacklisted,
VariableMap previousMappings) |
Modifier and Type | Method and Description |
---|---|
private Node |
buildReplacement(Node expr,
Node prefix,
java.lang.StringBuilder keyBuilder)
Builds a replacement abstract syntax tree for the string expression
expr . |
private static DefaultNameGenerator |
createNameGenerator(java.lang.Iterable<java.lang.String> reserved)
Use a name generate to create names so the names overlap with the names
used for variable and properties.
|
private void |
doSubstitutions(NodeTraversal t,
ReplaceStrings.Config config,
Node n)
Replace the parameters specified in the config, if possible.
|
private ReplaceStrings.Config |
findMatching(java.lang.String name) |
private ReplaceStrings.Config |
findMatchingClass(TypeI callClassType,
java.util.Collection<java.lang.String> declarationNames) |
private static java.lang.String |
getClassFromDeclarationName(java.lang.String fullDeclarationName)
From a provide name extract the class name.
|
private static java.lang.String |
getMethodFromDeclarationName(java.lang.String fullDeclarationName)
From a provide name extract the method name.
|
private java.lang.String |
getReplacement(java.lang.String key)
Get a replacement string for the provide key text.
|
(package private) java.util.List<ReplaceStrings.Result> |
getResult() |
(package private) VariableMap |
getStringMap() |
private void |
initMapping(VariableMap previousVarMap,
java.util.Set<java.lang.String> reservedNames) |
private void |
parseConfiguration(java.util.List<java.lang.String> functionsToInspect)
Build the data structures need by this pass from the provided
list of functions and methods.
|
private ReplaceStrings.Config |
parseConfiguration(java.lang.String function)
Convert the provide string into a Config.
|
void |
process(Node externs,
Node root)
Process the JS with root node root.
|
private void |
recordReplacement(java.lang.String key)
Record the location the replacement was made.
|
private Node |
replaceExpression(NodeTraversal t,
Node expr,
Node parent)
Replaces a string expression with a short encoded string expression.
|
void |
visit(NodeTraversal t,
Node n,
Node parent)
Visits a node in postorder (after its children have been visited).
|
shouldTraverse
static final DiagnosticType BAD_REPLACEMENT_CONFIGURATION
private static final java.lang.String DEFAULT_PLACEHOLDER_TOKEN
private final java.lang.String placeholderToken
private static final java.lang.String REPLACE_ONE_MARKER
private static final java.lang.String REPLACE_ALL_MARKER
private final AbstractCompiler compiler
private final TypeIRegistry registry
private final java.util.Map<java.lang.String,ReplaceStrings.Config> functions
private final com.google.common.collect.Multimap<java.lang.String,java.lang.String> methods
private final DefaultNameGenerator nameGenerator
private final java.util.Map<java.lang.String,ReplaceStrings.Result> results
static final com.google.common.base.Predicate<ReplaceStrings.Result> USED_RESULTS
ReplaceStrings(AbstractCompiler compiler, java.lang.String placeholderToken, java.util.List<java.lang.String> functionsToInspect, java.util.Set<java.lang.String> blacklisted, VariableMap previousMappings)
placeholderToken
- Separator to use between string parts. Used to replace
non-static string content.functionsToInspect
- A list of function configurations in the form of
function($,,,)
or
class.prototype.method($,,,)blacklisted
- A set of names that should not be used as replacement
strings. Useful to prevent unwanted strings for appearing in the
final output.
where '$' is used to indicate which parameter should be replaced.private void initMapping(VariableMap previousVarMap, java.util.Set<java.lang.String> reservedNames)
java.util.List<ReplaceStrings.Result> getResult()
VariableMap getStringMap()
public void process(Node externs, Node root)
CompilerPass
process
in interface CompilerPass
externs
- Top of external JS treeroot
- Top of JS treepublic void visit(NodeTraversal t, Node n, Node parent)
NodeTraversal.Callback
Visits a node in postorder (after its children have been visited).
A node is visited only if all its parents should be traversed
(NodeTraversal.Callback.shouldTraverse(NodeTraversal, Node, Node)
).
Implementations can have side effects (e.g. modifying the parse tree).
visit
in interface NodeTraversal.Callback
private ReplaceStrings.Config findMatching(java.lang.String name)
name
- The function name to find.private ReplaceStrings.Config findMatchingClass(TypeI callClassType, java.util.Collection<java.lang.String> declarationNames)
private void doSubstitutions(NodeTraversal t, ReplaceStrings.Config config, Node n)
private Node replaceExpression(NodeTraversal t, Node expr, Node parent)
t
- The traversalexpr
- The expression nodeparent
- The expression node's parentprivate java.lang.String getReplacement(java.lang.String key)
private void recordReplacement(java.lang.String key)
private Node buildReplacement(Node expr, Node prefix, java.lang.StringBuilder keyBuilder)
expr
. Appends any string literal values that are encountered to
keyBuilder
, to build the expression's replacement key.expr
- A JS expression that evaluates to a string valueprefix
- The JS expression to which expr
's replacement is
logically being concatenated. It is a partial solution to the
problem at hand and will either be this method's return value or a
descendant of it.keyBuilder
- A builder of the string expression's replacement keyexpr
private static java.lang.String getMethodFromDeclarationName(java.lang.String fullDeclarationName)
private static java.lang.String getClassFromDeclarationName(java.lang.String fullDeclarationName)
private void parseConfiguration(java.util.List<java.lang.String> functionsToInspect)
private ReplaceStrings.Config parseConfiguration(java.lang.String function)
private static DefaultNameGenerator createNameGenerator(java.lang.Iterable<java.lang.String> reserved)