private void HandleWord(string tokenText) { if (DoBlockJustClosed) { Append(" "); Append(tokenText); Append(" "); DoBlockJustClosed = false; return; } if (tokenText == "function") { if (Flags.VarLine && LastText != "=") { Flags.VarLineReindented = !Opts.KeepFunctionIndentation; } if ((JustAddedNewline || LastText == ";") && LastText != "{") { // make sure there is a nice clean space of at least one blank line // before a new function definition var haveNewlines = NNewlines; if (!JustAddedNewline) { haveNewlines = 0; } if (!Opts.PreserveNewlines) { haveNewlines = 1; } foreach (var i in Enumerable.Range(0, 2 - haveNewlines)) { AppendNewline(false); } } if (LastText == "get" || LastText == "set" || LastText == "new" || LastType == "TK_WORD") { Append(" "); } if (LastType == "TK_WORD") { if (LastText == "get" || LastText == "set" || LastText == "new" || LastText == "return") { Append(" "); } else { AppendNewline(); } } else if (LastType == "TK_OPERATOR" || LastText == "=") // foo = function { Append(" "); } else if (IsExpression(Flags.Mode)) { // (function } else { AppendNewline(); } Append("function"); LastWord = "function"; return; } if (tokenText == "case" || tokenText == "default" && Flags.InCaseStatement) { AppendNewline(); if (Flags.CaseBody) { RemoveIndent(); Flags.CaseBody = false; Flags.IndentationLevel -= 1; } Append(tokenText); Flags.InCase = true; Flags.InCaseStatement = true; return; } var prefix = "NONE"; if (LastType == "TK_END_BLOCK") { if (tokenText != "else" && tokenText != "catch" && tokenText != "finally") { prefix = "NEWLINE"; } else { if (Opts.BraceStyle == BraceStyle.Expand || Opts.BraceStyle == BraceStyle.EndExpand) { prefix = "NEWLINE"; } else { prefix = "SPACE"; Append(" "); } } } else if (LastType == "TK_SEMICOLON" && (Flags.Mode == "BLOCK" || Flags.Mode == "DO_BLOCK")) { prefix = "NEWLINE"; } else if (LastType == "TK_SEMICOLON" && IsExpression(Flags.Mode)) { prefix = "SPACE"; } else if (LastType == "TK_STRING") { prefix = "NEWLINE"; } else if (LastType == "TK_WORD") { if (LastText == "else") { TrimOutput(true); } prefix = "SPACE"; } else if (LastType == "TK_START_BLOCK") { prefix = "NEWLINE"; } else if (LastType == "TK_END_EXPR") { Append(" "); prefix = "NEWLINE"; } if (Flags.IfLine && LastType == "TK_END_EXPR") { Flags.IfLine = false; } if (LineStarters.Contains(tokenText)) { if (LastText == "else") { prefix = "SPACE"; } else { prefix = "NEWLINE"; } } if (tokenText == "else" || tokenText == "catch" || tokenText == "finally") { if (LastType != "TK_END_BLOCK" || Opts.BraceStyle == BraceStyle.Expand || Opts.BraceStyle == BraceStyle.EndExpand) { AppendNewline(); } else { TrimOutput(true); Append(" "); } } else if (prefix == "NEWLINE") { if (IsSpecialWord(LastText)) { // no newline between return nnn Append(" "); } else if (LastType != "TK_END_EXPR") { if ((LastType != "TK_START_EXPR" || tokenText != "var") && LastText != ":") { if (tokenText == "if" && LastWord == "else" && LastText != "{") { Append(" "); } else { Flags.VarLine = false; Flags.VarLineReindented = false; AppendNewline(); } } } else if (LineStarters.Contains(tokenText) && LastText != ")") { Flags.VarLine = false; Flags.VarLineReindented = false; AppendNewline(); } } else if (IsArray(Flags.Mode) && LastText == "," && LastLastText == "}") { AppendNewline(); //}, in lists get a newline } else if (prefix == "SPACE") { Append(" "); } Append(tokenText); LastWord = tokenText; if (tokenText == "var") { Flags.VarLine = true; Flags.VarLineReindented = false; Flags.VarLineTainted = false; } if (tokenText == "if") { Flags.IfLine = true; } if (tokenText == "else") { Flags.IfLine = false; } }
private void HandleOperator(string tokenText) { var spaceBefore = true; var spaceAfter = true; if (IsSpecialWord(LastText)) { // return had a special handling in TK_WORD Append(" "); Append(tokenText); return; } // hack for actionscript's import .*; if (tokenText == "*" && LastType == "TK_DOT" && !LastLastText.All(char.IsDigit)) { Append(tokenText); return; } if (tokenText == ":" && Flags.InCase) { Flags.CaseBody = true; Indent(); Append(tokenText); AppendNewline(); Flags.InCase = true; return; } if (tokenText == "::") { // no spaces around the exotic namespacing syntax operator Append(tokenText); return; } if (tokenText == "++" || tokenText == "--" || tokenText == "!" || (tokenText == "+" || tokenText == "-") && (LastType == "TK_START_BLOCK" || LastType == "TK_START_EXPR" || LastType == "TK_EQUALS" || LastType == "TK_OPERATOR" || LineStarters.Contains(LastText) || LastText == ",")) { spaceBefore = false; spaceAfter = false; if (LastText == ";" && IsExpression(Flags.Mode)) { spaceBefore = true; } if (LastText == "TK_WORD" && LineStarters.Contains(LastText)) { spaceBefore = true; } if (Flags.Mode == "BLOCK" && (LastText == ";" || LastText == "{")) { AppendNewline(); } } else if (tokenText == ":") { if (Flags.TernaryDepth == 0) { if (Flags.Mode == "BLOCK") { Flags.Mode = "OBJECT"; } spaceBefore = false; } else { Flags.TernaryDepth -= 1; } } else if (tokenText == "?") { Flags.TernaryDepth += 1; } if (spaceBefore) { Append(" "); } Append(tokenText); if (spaceAfter) { Append(" "); } }
private void HandleStartExpr(string tokenText) { if (tokenText == "[") { if (LastType == "TK_WORD" || LastText == ")") { if (LineStarters.Contains(LastText)) { Append(" "); } SetMode("(EXPRESSION)"); Append(tokenText); return; } if (Flags.Mode == "[EXPRESSION]" || Flags.Mode == "[INDENTED-EXPRESSION]") { if (LastLastText == "]" && LastText == ",") { // # ], [ goes to a new line if (Flags.Mode == "[EXPRESSION]") { Flags.Mode = "[INDENTED-EXPRESSION]"; if (!Opts.KeepArrayIndentation) { Indent(); } } SetMode("[EXPRESSION]"); if (!Opts.KeepArrayIndentation) { AppendNewline(); } } else if (LastText == "[") { if (Flags.Mode == "[EXPRESSION]") { Flags.Mode = "[INDENTED-EXPRESSION]"; if (!Opts.KeepArrayIndentation) { Indent(); } } SetMode("[EXPRESSION]"); if (!Opts.KeepArrayIndentation) { AppendNewline(); } } else { SetMode("[EXPRESSION]"); } } else { SetMode("[EXPRESSION]"); } } else { if (LastText == "for") { SetMode("(FOR-EXPRESSION)"); } else if (LastText == "if" || LastText == "while") { SetMode("(COND-EXPRESSION)"); } else { SetMode("(EXPRESSION)"); } } if (LastText == ";" || LastType == "TK_START_BLOCK") { AppendNewline(); } else if (LastType == "TK_END_EXPR" || LastType == "TK_START_EXPR" || LastType == "TK_END_BLOCK" || LastText == ".") { // do nothing on (( and )( and ][ and ]( and .( if (WantedNewline) { AppendNewline(); } } else if (LastType != "TK_WORD" && LastType != "TK_OPERATOR") { Append(" "); } else if (LastWord == "function" || LastWord == "typeof") { // function() vs function (), typeof() vs typeof () if (Opts.JslintHappy) { Append(" "); } } else if (LineStarters.Contains(LastText) || LastText == "catch") { Append(" "); } Append(tokenText); }