class RenameProperties extends java.lang.Object implements CompilerPass
If provided a VariableMap of previously used names, it tries to reuse those names.
To prevent a property from getting renamed you may extern it (add it to your externs file) or put it in quotes.
To avoid run-time JavaScript errors, use quotes when accessing properties that are defined using quotes.
var a = {'myprop': 0}, b = a['myprop']; // correct var x = {'myprop': 0}, y = x.myprop; // incorrect
Modifier and Type | Class and Description |
---|---|
private class |
RenameProperties.ProcessProperties
A traversal callback that collects property names and counts how
frequently each property name occurs.
|
private static class |
RenameProperties.Property
Encapsulates the information needed for renaming a property.
|
Modifier and Type | Field and Description |
---|---|
(package private) static DiagnosticType |
BAD_ARG |
(package private) static DiagnosticType |
BAD_CALL |
private java.util.Map<Node,Node> |
callNodeToParentMap |
private AbstractCompiler |
compiler |
private java.util.Set<java.lang.String> |
externedNames |
private static java.util.Comparator<RenameProperties.Property> |
FREQUENCY_COMPARATOR |
private boolean |
generatePseudoNames |
private NameGenerator |
nameGenerator |
private VariableMap |
prevUsedPropertyMap
Property renaming map from a previous compilation.
|
private java.util.Map<java.lang.String,RenameProperties.Property> |
propertyMap |
private java.util.Set<java.lang.String> |
quotedNames |
(package private) static java.lang.String |
RENAME_PROPERTY_FUNCTION_NAME
The name of a special function that this pass replaces.
|
private char[] |
reservedCharacters |
private java.util.List<Node> |
stringNodesToRename |
Constructor and Description |
---|
RenameProperties(AbstractCompiler compiler,
boolean generatePseudoNames,
NameGenerator nameGenerator)
Creates an instance.
|
RenameProperties(AbstractCompiler compiler,
boolean generatePseudoNames,
VariableMap prevUsedPropertyMap,
char[] reservedCharacters,
NameGenerator nameGenerator)
Creates an instance.
|
RenameProperties(AbstractCompiler compiler,
boolean generatePseudoNames,
VariableMap prevUsedPropertyMap,
NameGenerator nameGenerator)
Creates an instance.
|
Modifier and Type | Method and Description |
---|---|
private void |
generateNames(java.util.Set<RenameProperties.Property> props,
java.util.Set<java.lang.String> reservedNames)
Generates new names for properties.
|
(package private) VariableMap |
getPropertyMap()
Gets the property renaming map (the "answer key").
|
void |
process(Node externs,
Node root)
Process the JS with root node root.
|
private void |
reusePropertyNames(java.util.Set<java.lang.String> reservedNames,
java.util.Collection<RenameProperties.Property> allProps)
Runs through the list of properties and renames as many as possible with
names from the previous compilation.
|
private final AbstractCompiler compiler
private final boolean generatePseudoNames
private final VariableMap prevUsedPropertyMap
private final java.util.List<Node> stringNodesToRename
private final char[] reservedCharacters
private final java.util.Map<java.lang.String,RenameProperties.Property> propertyMap
private final java.util.Set<java.lang.String> externedNames
private final java.util.Set<java.lang.String> quotedNames
private final NameGenerator nameGenerator
private static final java.util.Comparator<RenameProperties.Property> FREQUENCY_COMPARATOR
static final java.lang.String RENAME_PROPERTY_FUNCTION_NAME
static final DiagnosticType BAD_CALL
static final DiagnosticType BAD_ARG
RenameProperties(AbstractCompiler compiler, boolean generatePseudoNames, NameGenerator nameGenerator)
compiler
- The JSCompilergeneratePseudoNames
- Generate pseudo names. e.g foo -> $foo$ instead
of compact obfuscated names. This is used for debugging.nameGenerator
- a shared NameGenerator that this instance can use;
the instance may reset or reconfigure it, so the caller should
not expect any state to be preservedRenameProperties(AbstractCompiler compiler, boolean generatePseudoNames, VariableMap prevUsedPropertyMap, NameGenerator nameGenerator)
compiler
- The JSCompiler.generatePseudoNames
- Generate pseudo names. e.g foo -> $foo$ instead
of compact obfuscated names. This is used for debugging.prevUsedPropertyMap
- The property renaming map used in a previous
compilation.nameGenerator
- a shared NameGenerator that this instance can use;
the instance may reset or reconfigure it, so the caller should
not expect any state to be preservedRenameProperties(AbstractCompiler compiler, boolean generatePseudoNames, VariableMap prevUsedPropertyMap, @Nullable char[] reservedCharacters, NameGenerator nameGenerator)
compiler
- The JSCompiler.generatePseudoNames
- Generate pseudo names. e.g foo -> $foo$ instead
of compact obfuscated names. This is used for debugging.prevUsedPropertyMap
- The property renaming map used in a previous
compilation.reservedCharacters
- If specified these characters won't be used in
generated namesnameGenerator
- a shared NameGenerator that this instance can use;
the instance may reset or reconfigure it, so the caller should
not expect any state to be preservedpublic void process(Node externs, Node root)
CompilerPass
process
in interface CompilerPass
externs
- Top of external JS treeroot
- Top of JS treeprivate void reusePropertyNames(java.util.Set<java.lang.String> reservedNames, java.util.Collection<RenameProperties.Property> allProps)
reservedNames
- Reserved names to use during renaming.allProps
- Properties to rename.private void generateNames(java.util.Set<RenameProperties.Property> props, java.util.Set<java.lang.String> reservedNames)
props
- Properties to generate new names forreservedNames
- A set of names to which properties should not be
renamedVariableMap getPropertyMap()