Example #1
0
 private void AddRename(string oldName, string newName, JsDeclarationScope scope)
 {
     _result[scope][oldName] = newName;
     _usedNames[scope].Add(newName);
     foreach (var c in _hierarchy[scope].ChildScopes)
     {
         AddRename(oldName, newName, c);
     }
 }
Example #2
0
 private JsDeclarationScope FindDeclaringScope(string name, JsDeclarationScope scope)
 {
     for (;;)
     {
         if (_locals[scope].Contains(name))
         {
             return(scope);
         }
         scope = _hierarchy[scope].ParentScope;
     }
 }
Example #3
0
            private void FillAllVisibleLocals(JsDeclarationScope scope)
            {
                var hier = _hierarchy[scope];

                _allVisibleLocals[scope] = new HashSet <string>(hier.ParentScope != null ? _allVisibleLocals[hier.ParentScope] : (IEnumerable <string>) new string[0]);
                _allVisibleLocals[scope].UnionWith(_locals[scope]);
                foreach (var c in hier.ChildScopes)
                {
                    FillAllVisibleLocals(c);
                }
            }
            private IEnumerable <JsStatement> MakePrefix(JsDeclarationScope scope)
            {
                var result = new List <JsStatement>();

                if (_locals != null)
                {
                    result.Add(JsExpression.Invocation(JsExpression.Identifier("locals"), _locals[scope].OrderBy(x => x).Select(JsExpression.Identifier)));
                }
                if (_globals != null)
                {
                    result.Add(JsExpression.Invocation(JsExpression.Identifier("globals"), _globals[scope].OrderBy(x => x).Select(JsExpression.Identifier)));
                }
                return(result);
            }
Example #5
0
            private void FillUsedNames(JsDeclarationScope scope)
            {
                var hier = _hierarchy[scope];
                var set  = new HashSet <string>();

                foreach (var c in hier.ChildScopes)
                {
                    FillUsedNames(c);
                    set.UnionWith(_usedNames[c]);
                }
                set.UnionWith(_locals[scope]);
                set.UnionWith(_globals[scope]);
                _usedNames[scope] = set;
            }
Example #6
0
            private void Analyze(JsDeclarationScope scope)
            {
                var allVisibleLocals = _allVisibleLocals[scope];
                var introducedNames  = _introducedNames[scope];

                foreach (var toRename in introducedNames.Where(allVisibleLocals.Contains))
                {
                    var declaringScope = FindDeclaringScope(toRename, scope);
                    var newName        = FindNewName(toRename, declaringScope);
                    AddRename(toRename, newName, declaringScope);
                }
                foreach (var c in _hierarchy[scope].ChildScopes)
                {
                    Analyze(c);
                }
            }
Example #7
0
            private Tuple <Dictionary <string, string>, HashSet <string> > BuildMap(Dictionary <string, string> prev, JsDeclarationScope declarationScope)
            {
                var newLocals  = _locals[declarationScope];
                var newGlobals = _globals[declarationScope];

                if (newLocals.Count == 0)
                {
                    return(Tuple.Create(prev ?? new Dictionary <string, string>(), newGlobals));
                }

                var result    = prev != null ? new Dictionary <string, string>(prev) : new Dictionary <string, string>();
                var usedNames = new HashSet <string>(result.Values.Concat(newGlobals));

                foreach (var nl in newLocals)
                {
                    if (!result.ContainsKey(nl))
                    {
                        var n = _generateName(nl, usedNames);
                        usedNames.Add(n);
                        result[nl] = n;
                    }
                }
                return(Tuple.Create(result, newGlobals));
            }
Example #8
0
 public DeclarationScopeHierarchy(JsDeclarationScope parentScope)
 {
     ParentScope = parentScope;
     ChildScopes = new List <JsDeclarationScope>();
 }
		public DeclarationScopeHierarchy(JsDeclarationScope parentScope) {
			ParentScope = parentScope;
			ChildScopes = new List<JsDeclarationScope>();
		}
Example #10
0
            private string FindNewName(string name, JsDeclarationScope scope)
            {
                var usedNames = _usedNames[scope];

                return(_namer.GetVariableName(name, usedNames));
            }