protected void NewLine() { this._builder.AppendLine(); this._builder.Append(this._indents.ToString()); this._bracketCount = (this._parenCount = 0); this._lineFlags = this._nextLineFlags; this._nextLineFlags = JSLineFlags.None; }
public string Format(string javascript) { this._builder = new StringBuilder(javascript.Length); this._indents = new Indents(); this._parenCount = 0; this._bracketCount = 0; this._lineFlags = JSLineFlags.None; this._nextLineFlags = JSLineFlags.None; JSTokenizer jSTokenizer = new JSTokenizer(javascript); bool flag = false; bool flag2 = true; while (jSTokenizer.GetToken()) { JSToken token = jSTokenizer.Token; if (this._builder.Length > 0) { flag2 = flag; if (flag) { this.NewLine(); flag = false; } } switch (token.Type) { case JSTokenTypes.OpenBrace: { if (!flag2) { if (this.OpenBraceOnNewLine && this._builder.Length > 0) { this.NewLine(); } else if (token.PreviousType != JSTokenTypes.CRLF && token.PreviousType != JSTokenTypes.OpenParen && token.PreviousType != JSTokenTypes.OpenBracket) { this._builder.Append(' '); } } this._builder.Append(token.Value); JSToken jSToken = jSTokenizer.PeekToken(); if (jSToken.Type != JSTokenTypes.CloseBrace) { JSIndentFlags jSIndentFlags = JSIndentFlags.None; if (JSFormatter.HasFlags((int)this._lineFlags, 2)) { jSIndentFlags |= JSIndentFlags.DoBlock; } else if (JSFormatter.HasFlags((int)this._lineFlags, 8)) { jSIndentFlags |= JSIndentFlags.CaseBlock; } this._indents.Indent(jSIndentFlags); flag = true; continue; } jSTokenizer.GetToken(); this._builder.Append(jSTokenizer.Token.Value); jSToken = jSTokenizer.PeekToken(); if (jSToken.Type != JSTokenTypes.SemiColon && jSToken.Type != JSTokenTypes.Comma) { while (JSFormatter.HasFlags((int)this._indents.Current, 1)) { this._indents.Unindent(); } flag = true; continue; } if (jSToken.Type == JSTokenTypes.Comma) { jSTokenizer.GetToken(); this._builder.Append(jSTokenizer.Token.Value); continue; } continue; } case JSTokenTypes.CloseBrace: { if (JSFormatter.HasFlags((int)this._indents.Current, 4)) { this._indents.Unindent(); if (flag2) { Indents.StripTrailingIndent(this._builder); } } while (JSFormatter.HasFlags((int)this._indents.Current, 1)) { this._indents.Unindent(); } this._indents.Unindent(); if (flag2) { Indents.StripTrailingIndent(this._builder); } else { this.NewLine(); } this._builder.Append(token.Value); JSToken jSToken = jSTokenizer.PeekToken(); if (jSToken.Value != "catch" && jSToken.Value != "finally" && jSToken.Value != ":") { while (JSFormatter.HasFlags((int)this._indents.Current, 1)) { this._indents.Unindent(); } } if (JSFormatter.HasFlags((int)this._indents.LastIndent, 2)) { this._lineFlags |= JSLineFlags.EndDoBlock; } if (jSToken.Type != JSTokenTypes.CRLF && jSToken.Type != JSTokenTypes.SemiColon && jSToken.Type != JSTokenTypes.CloseParen && jSToken.Type != JSTokenTypes.CloseBracket && jSToken.Type != JSTokenTypes.Comma && jSToken.Type != JSTokenTypes.OpenParen && jSToken.Type != JSTokenTypes.Colon && !JSFormatter.HasFlags((int)this._lineFlags, 4)) { flag = true; continue; } continue; } case JSTokenTypes.OpenParen: if (!flag2 && token.PreviousType != JSTokenTypes.CRLF && token.PreviousType != JSTokenTypes.OpenParen && token.PreviousType != JSTokenTypes.UnaryPrefix && token.PreviousType != JSTokenTypes.CloseBracket && token.PreviousType != JSTokenTypes.CloseParen && token.PreviousType != JSTokenTypes.CloseBrace && (token.PreviousType != JSTokenTypes.Symbol || (JSFormatter.HasFlags((int)this._lineFlags, 1) && this._parenCount == 0))) { this._builder.Append(' '); } this._builder.Append(token.Value); this._parenCount++; continue; case JSTokenTypes.CloseParen: { this._builder.Append(token.Value); this._parenCount--; if (this._parenCount < 0) { this._parenCount = 0; } if (this._parenCount != 0 || !JSFormatter.HasFlags((int)this._lineFlags, 1)) { continue; } JSToken jSToken = jSTokenizer.PeekToken(); if (jSToken.Type != JSTokenTypes.OpenBrace) { this._indents.Indent(JSIndentFlags.NoBraces); flag = true; continue; } continue; } case JSTokenTypes.OpenBracket: { if (!flag2 && token.PreviousType != JSTokenTypes.CRLF && token.PreviousType != JSTokenTypes.Symbol && token.PreviousType != JSTokenTypes.OpenParen && token.PreviousType != JSTokenTypes.CloseParen && token.PreviousType != JSTokenTypes.CloseBracket) { this._builder.Append(' '); } JSToken jSToken = jSTokenizer.PeekToken(); if (JSFormatter.HasFlags((int)this._lineFlags, 16) && jSToken.Type != JSTokenTypes.CloseBracket && jSToken.Type == JSTokenTypes.OpenBrace && this._parenCount == 0) { if (this.OpenBraceOnNewLine) { this.NewLine(); } this._indents.Indent(JSIndentFlags.BracketBlock); flag = true; } this._builder.Append(token.Value); this._bracketCount++; continue; } case JSTokenTypes.CloseBracket: this._bracketCount = Math.Max(this._bracketCount - 1, 0); if (!JSFormatter.HasFlags((int)this._indents.Current, 8)) { this._builder.Append(token.Value); continue; } this._indents.Unindent(); if (flag2) { Indents.StripTrailingIndent(this._builder); this._builder.Append(token.Value); continue; } this.NewLine(); this._builder.Append(token.Value); continue; case JSTokenTypes.Symbol: { bool flag3 = Array.IndexOf <string>(JSFormatter._blockKeywords, token.Value) > -1; if (token.Value == "else" && jSTokenizer.PeekToken().Value != "if") { flag3 = true; } if (JSFormatter.HasFlags((int)this._indents.Current, 4) && (token.Value == "case" || token.Value == "default")) { Indents.StripTrailingIndent(this._builder); this._indents.Unindent(); } if (this._parenCount != 0 || !flag3) { if (!flag2 && token.PreviousType != JSTokenTypes.CRLF && token.PreviousType != JSTokenTypes.OpenParen && token.PreviousType != JSTokenTypes.OpenBracket && token.PreviousType != JSTokenTypes.UnaryPrefix && token.PreviousType != JSTokenTypes.Dot) { this._builder.Append(' '); } if (token.Value == "case" || token.Value == "default") { this._lineFlags |= JSLineFlags.CaseKeyword; } this._builder.Append(token.Value); continue; } if (!flag2) { this._builder.Append(' '); } this._builder.Append(token.Value); if (JSFormatter.HasFlags((int)this._lineFlags, 4) && !(token.Value != "while")) { continue; } if (token.Value == "do") { this._lineFlags |= JSLineFlags.DoKeyword; } JSToken jSToken = jSTokenizer.PeekToken(); if (jSToken.Type == JSTokenTypes.OpenBrace || jSToken.Type == JSTokenTypes.OpenParen) { this._lineFlags |= JSLineFlags.BlockKeyword; continue; } JSIndentFlags jSIndentFlags2 = JSIndentFlags.NoBraces; if (JSFormatter.HasFlags((int)this._lineFlags, 2)) { jSIndentFlags2 |= JSIndentFlags.DoBlock; } this._indents.Indent(jSIndentFlags2); flag = true; continue; } case JSTokenTypes.String: case JSTokenTypes.Number: case JSTokenTypes.RegEx: if (!flag2 && token.PreviousType != JSTokenTypes.CRLF && token.PreviousType != JSTokenTypes.OpenParen && token.PreviousType != JSTokenTypes.OpenBracket && token.PreviousType != JSTokenTypes.UnaryPrefix) { this._builder.Append(' '); } this._builder.Append(token.Value); continue; case JSTokenTypes.SemiColon: this._builder.Append(token.Value); if (this._parenCount == 0) { while (JSFormatter.HasFlags((int)this._indents.Current, 1)) { this._indents.Unindent(); } if (JSFormatter.HasFlags((int)this._indents.LastIndent, 2)) { this._nextLineFlags |= JSLineFlags.EndDoBlock; } JSToken jSToken = jSTokenizer.PeekToken(); if (jSToken.Type == JSTokenTypes.FullLineComment || jSToken.Type == JSTokenTypes.InlineComment) { bool flag4; if (jSToken.Type == JSTokenTypes.FullLineComment) { flag4 = this.NewLineBeforeLineComment; } else { flag4 = this.NewLineBeforeInlineComment; } jSTokenizer.GetToken(); if (flag4) { this.NewLine(); } else { this._builder.Append(' '); } this._builder.Append(jSTokenizer.Token.Value); } flag = true; continue; } continue; case JSTokenTypes.Comma: this._builder.Append(token.Value); if (token.PreviousType == JSTokenTypes.CloseBrace || (JSFormatter.HasFlags((int)this._lineFlags, 16) && this._parenCount == 0 && this._bracketCount == 0 && this._indents.Count > 0)) { flag = true; continue; } continue; case JSTokenTypes.Colon: if (JSFormatter.HasFlags((int)this._lineFlags, 8)) { this._builder.Append(token.Value); this._indents.Indent(JSIndentFlags.CaseBlock); flag = true; continue; } if (!flag2 && (JSFormatter.HasFlags((int)this._lineFlags, 32) || token.PreviousType == JSTokenTypes.CloseBrace)) { this._builder.Append(' '); } this._builder.Append(token.Value); if (!JSFormatter.HasFlags((int)this._lineFlags, 32)) { this._lineFlags |= JSLineFlags.JsonColon; continue; } continue; case JSTokenTypes.QuestionMark: this._lineFlags |= JSLineFlags.QuestionMark; if (!flag2) { this._builder.Append(' '); } this._builder.Append(token.Value); continue; case JSTokenTypes.BinaryOperator: case JSTokenTypes.UnaryPrefix: if (!flag2 && token.PreviousType != JSTokenTypes.OpenParen && token.PreviousType != JSTokenTypes.OpenBracket && token.PreviousType != JSTokenTypes.UnaryPrefix) { this._builder.Append(' '); } this._builder.Append(token.Value); continue; case JSTokenTypes.FullLineComment: if (!flag2) { if (this.NewLineBeforeLineComment) { this.NewLine(); } else { this._builder.Append(' '); } } this._builder.Append(token.Value); flag = true; continue; case JSTokenTypes.InlineComment: if (!flag2) { if (this.NewLineBeforeInlineComment) { this.NewLine(); } else { this._builder.Append(' '); } } this._builder.Append(token.Value); if (this.NewLineAfterInlineComment) { flag = true; continue; } continue; case JSTokenTypes.CRLF: if (!flag2) { flag = true; continue; } continue; } this._builder.Append(token.Value); } this._builder.AppendLine(); return(this._builder.ToString()); }