Beispiel #1
0
 /// <summary>
 /// We really only care about caching strings that are likely
 /// to be identifiers.  Everything else we'll coalesce into the
 /// empty string.
 /// </summary>
 private static bool DontCacheString(string strValue)
 {
     if (strValue.Length > 100)
     {
         return(true);
     }
     for (int i = 0; i < strValue.Length; i++)
     {
         char ch = strValue[i];
         if (ch == '-' || ch == '.' || ch == '/' || ch == '\\')
         {
             // not valid identifiers, but likely to show up in
             // require string literals, so we still care about preserving
             // these strings.
             continue;
         }
         if (!JSScanner.IsValidIdentifierPart(strValue[i]))
         {
             return(true);
         }
     }
     return(false);
 }
Beispiel #2
0
        public override string ToCode(ToCodeFormat format)
        {
            if (format == ToCodeFormat.NoBraces && m_list.Count == 0)
            {
                return(string.Empty);
            }

            StringBuilder sb         = new StringBuilder();
            bool          closeBrace = false;
            bool          unindent   = false;

            // if the format is N, then we never enclose in braces.
            // if the format is B or T, then we always enclose in braces.
            // anything else, then we enclose in braces if there's more than
            // one enclosing line
            if (format != ToCodeFormat.NoBraces && Parent != null)
            {
                if (format == ToCodeFormat.AlwaysBraces ||
                    format == ToCodeFormat.NestedTry ||
                    m_list.Count > 1)
                {
                    // opening brace on a new line
                    Parser.Settings.NewLine(sb);

                    // up the indent level for the content within
                    Parser.Settings.Indent();

                    sb.Append("{");
                    closeBrace = true;
                }
                else if (m_list.Count == 1 && format != ToCodeFormat.ElseIf)
                {
                    // we're pretty-printing a single-line block.
                    // we still won't enclose in brackets, but we need to indent it
                    Parser.Settings.Indent();
                    unindent = true;
                }
            }

            bool requireSeparator   = true;
            bool endsWithEmptyBlock = false;
            bool mightNeedSpace     = false;

            for (int ndx = 0; ndx < m_list.Count; ++ndx)
            {
                AstNode item = m_list[ndx];
                if (item != null)
                {
                    // see if we need to add a semi-colon
                    if (ndx > 0 && requireSeparator)
                    {
                        sb.Append(';');
                        if (Parser.Settings.OutputMode == OutputMode.SingleLine && item is ImportantComment)
                        {
                            // if this item is an important comment and we're in single-line mode,
                            // we'll start on a new line
                            sb.Append('\n');
                        }
                        // we no longer require a separator next time around
                        requireSeparator = false;
                    }

                    string itemText = item.ToCode();
                    if (itemText.Length > 0)
                    {
                        // if this is an else-if construct, we don't want to break to a new line.
                        // but all other formats put the next statement on a newline
                        if (format != ToCodeFormat.ElseIf)
                        {
                            Parser.Settings.NewLine(sb);
                        }

                        if (mightNeedSpace && JSScanner.IsValidIdentifierPart(itemText[0]))
                        {
                            sb.Append(' ');
                        }

                        sb.Append(itemText);
                        requireSeparator   = (item.RequiresSeparator && !itemText.EndsWith(";", StringComparison.Ordinal));
                        endsWithEmptyBlock = item.EndsWithEmptyBlock;

                        mightNeedSpace =
                            (item is ConditionalCompilationStatement) &&
                            JSScanner.IsValidIdentifierPart(itemText[itemText.Length - 1]);
                    }
                }
            }
            if (endsWithEmptyBlock)
            {
                sb.Append(';');
            }

            if (closeBrace)
            {
                // unindent now that the block is done
                Parser.Settings.Unindent();
                Parser.Settings.NewLine(sb);
                sb.Append("}");
            }
            else if (unindent)
            {
                Parser.Settings.Unindent();
            }
            return(sb.ToString());
        }