Beispiel #1
0
    public override void CodeGen(OutputContext output)
    {
        string GetName(AstSymbol symbol)
        {
            return(symbol.Thedef?.MangledName ?? symbol.Thedef?.Name ?? symbol.Name);
        }

        var allowShortHand = output.Options.Ecma >= 6;
        var keyString      = Key switch
        {
            AstString str => str.Value,
            AstNumber num => num.Value.ToString("R", CultureInfo.InvariantCulture),
            AstSymbolRef _ => null,
            AstSymbol key => GetName(key),
            _ => null
        };

        if (allowShortHand &&
            Value is AstSymbol value && keyString != null &&
            GetName(value) == keyString &&
            OutputContext.IsIdentifierString(keyString) &&
            OutputContext.IsIdentifier(keyString)
            )
        {
            output.PrintPropertyName(keyString);
        }
Beispiel #2
0
        public virtual string NextMangled(ScopeOptions options, SymbolDef symbolDef)
        {
            var ext = Enclosed;

            while (true)
            {
again:
                var m = Base54(options.Chars, Cname++);
                if (!OutputContext.IsIdentifier(m))
                {
                    continue;                                 // skip over "do"
                }
                // https://github.com/mishoo/UglifyJS2/issues/242 -- do not
                // shadow a name reserved from mangling.
                if (options.Reserved.Contains(m))
                {
                    continue;
                }

                // we must ensure that the mangled name does not shadow a name
                // from some parent scope that is referenced in this or in
                // inner scopes.
                for (var i = 0u; i < ext.Count; i++)
                {
                    var sym  = ext[i];
                    var name = sym.MangledName ?? (sym.Unmangleable(options) ? sym.Name : null);
                    if (m == name)
                    {
                        goto again;
                    }
                }

                return(m);
            }
        }
Beispiel #3
0
        protected override void Visit(AstNode node)
        {
            switch (node)
            {
            case AstLabeledStatement _:
            {
                // _labelIndex is incremented when we get to the AstLabel
                var saveLabelIndex = _labelIndex;
                DescendOnce();
                _labelIndex = saveLabelIndex;
                break;
            }

            case AstScope scope:
            {
                foreach (var def in scope.Variables.Values)
                {
                    if (_options.Reserved.Contains(def.Name))
                    {
                        continue;
                    }
                    _toMangle.Add(def);
                }

                break;
            }

            case IMayBeBlockScope blockScope when blockScope.IsBlockScope:
            {
                foreach (var def in blockScope.BlockScope.Variables.Values)
                {
                    if (_options.Reserved.Contains(def.Name))
                    {
                        continue;
                    }
                    _toMangle.Add(def);
                }

                break;
            }

            case AstLabel label:
            {
                string name;
                do
                {
                    name = AstScope.Base54(_options.Chars, _labelIndex++);
                }while (!OutputContext.IsIdentifier(name));
                label.MangledName = name;
                StopDescending();
                break;
            }

            case AstSymbolCatch symbolCatch:
                _toMangle.Add(symbolCatch.Thedef);
                break;
            }
        }
Beispiel #4
0
        public override void CodeGen(OutputContext output)
        {
            string GetName(AstSymbol symbol)
            {
                return(symbol.Thedef?.MangledName ?? symbol.Thedef?.Name ?? symbol.Name);
            }

            var    allowShortHand = output.Options.Shorthand;
            string?keyString      = null;

            if (Key is AstSymbol)
            {
                keyString = GetName((AstSymbol)Key);
            }
            else if (Key is AstString str)
            {
                keyString = str.Value;
            }

            if (allowShortHand &&
                Value is AstSymbol && keyString != null &&
                GetName((AstSymbol)Value) == keyString &&
                OutputContext.IsIdentifierString(keyString) &&
                OutputContext.IsIdentifier(keyString)
                )
            {
                output.PrintPropertyName(keyString);
            }
            else if (allowShortHand &&
                     Value is AstDefaultAssign defAssign && keyString != null &&
                     defAssign.Left is AstSymbol &&
                     OutputContext.IsIdentifierString(keyString) &&
                     GetName((AstSymbol)defAssign.Left) == keyString
                     )
            {
                output.PrintPropertyName(keyString);
                output.Space();
                output.Print("=");
                output.Space();
                defAssign.Right.Print(output);
            }
Beispiel #5
0
        public override void CodeGen(OutputContext output)
        {
            string GetName(AstSymbol symbol)
            {
                return(symbol.Thedef?.MangledName ?? symbol.Thedef?.Name ?? symbol.Name);
            }

            var allowShortHand = output.Options.Shorthand;
            var keyString      = Key switch
            {
                AstString str => str.Value,
                AstNumber num => num.Value.ToString("R", CultureInfo.InvariantCulture),
                AstSymbol key => GetName(key),
                _ => null
            };

            if (allowShortHand &&
                Value is AstSymbol && keyString != null &&
                GetName((AstSymbol)Value) == keyString &&
                OutputContext.IsIdentifierString(keyString) &&
                OutputContext.IsIdentifier(keyString)
                )
            {
                output.PrintPropertyName(keyString);
            }
            else if (allowShortHand &&
                     Value is AstDefaultAssign defAssign && keyString != null &&
                     defAssign.Left is AstSymbol &&
                     OutputContext.IsIdentifierString(keyString) &&
                     GetName((AstSymbol)defAssign.Left) == keyString
                     )
            {
                output.PrintPropertyName(keyString);
                output.Space();
                output.Print("=");
                output.Space();
                defAssign.Right.Print(output);
            }