internal void ValidateGeneratedNames() { // check all the variables defined within this scope. // we're looking for uncrunched generated fields. foreach (JsVariableField variableField in NameTable.Values) { if (variableField.IsGenerated && variableField.CrunchedName == null) { // we need to rename this field. // first we need to walk all the child scopes depth-first // looking for references to this field. Once we find a reference, // we then need to add all the other variables referenced in those // scopes and all above them (from here) so we know what names we // can't use. var avoidTable = new HashSet<string>(); GenerateAvoidList(avoidTable, variableField.Name); // now that we have our avoid list, create a crunch enumerator from it JsCrunchEnumerator crunchEnum = new JsCrunchEnumerator(avoidTable); // and use it to generate a new name variableField.CrunchedName = crunchEnum.NextName(); } } // recursively traverse through our children foreach (JsActivationObject scope in ChildScopes) { if (!scope.Existing) { scope.ValidateGeneratedNames(); } } }
internal virtual void AutoRenameFields() { // if we're not known at compile time, then we can't crunch // the local variables in this scope, because we can't know if // something will reference any of it at runtime. // eval is something that will make the scope unknown because we // don't know what eval will evaluate to until runtime if (m_isKnownAtCompileTime) { // get an array of all the uncrunched local variables defined in this scope var localFields = GetUncrunchedLocals(); if (localFields != null) { // create a crunch-name enumerator, taking into account any fields within our // scope that have already been crunched. var avoidSet = new HashSet<string>(); foreach (var field in NameTable.Values) { // if the field can't be crunched, or if it can but we've already crunched it, // or if it's an outer variable (that hasn't been generated) and its OWNING scope isn't known // (and therefore we CANNOT crunch it), // then add it to the avoid list so we don't reuse that name if (!field.CanCrunch || field.CrunchedName != null || (field.OuterField != null && !field.IsGenerated && field.OwningScope != null && !field.OwningScope.IsKnownAtCompileTime)) { avoidSet.Add(field.ToString()); } } var crunchEnum = new JsCrunchEnumerator(avoidSet); foreach (var localField in localFields) { // if we are an unambiguous reference to a named function expression and we are not // referenced by anyone else, then we can just skip this variable because the // name will be stripped from the output anyway. // we also always want to crunch "placeholder" fields. if (localField.CanCrunch && (localField.RefCount > 0 || localField.IsDeclared || localField.IsPlaceholder || !(m_settings.RemoveFunctionExpressionNames && m_settings.IsModificationAllowed(JsTreeModifications.RemoveFunctionExpressionNames)))) { localField.CrunchedName = crunchEnum.NextName(); } } } } // then traverse through our children foreach (JsActivationObject scope in ChildScopes) { scope.AutoRenameFields(); } }