bool AddStatement(Statement statement) { if (reachability.IsReachable(statement)) { return(false); } if (collectedStatements.Contains(statement)) { return(true); } if (statement is BlockStatement && statement.GetChildrenByRole <Statement>(BlockStatement.StatementRole).Any(reachability.IsReachable)) { //There's reachable content return(false); } var prevEnd = statement.GetPrevNode(n => !(n is NewLineNode)).EndLocation; // group multiple continuous statements into one issue var start = statement.StartLocation; collectedStatements.Add(statement); visitor.unreachableNodes.Add(statement); Statement nextStatement; while ((nextStatement = (Statement)statement.GetNextSibling(s => s is Statement)) != null && !(nextStatement is LabelStatement)) { statement = nextStatement; collectedStatements.Add(statement); visitor.unreachableNodes.Add(statement); } var end = statement.EndLocation; var removeAction = new CodeAction(visitor.ctx.TranslateString("Remove unreachable code"), script => { var startOffset = script.GetCurrentOffset(prevEnd); var endOffset = script.GetCurrentOffset(end); script.RemoveText(startOffset, endOffset - startOffset); }, collectedStatements.First().StartLocation, collectedStatements.Last().EndLocation); var commentAction = new CodeAction(visitor.ctx.TranslateString("Comment unreachable code"), script => { var startOffset = script.GetCurrentOffset(prevEnd); script.InsertText(startOffset, Environment.NewLine + "/*"); var endOffset = script.GetCurrentOffset(end); script.InsertText(endOffset, Environment.NewLine + "*/"); }, collectedStatements.First().StartLocation, collectedStatements.Last().EndLocation); var actions = new [] { removeAction, commentAction }; visitor.AddIssue(start, end, visitor.ctx.TranslateString("Code is unreachable"), actions); return(true); }
bool AddStatement(Statement statement) { if (reachability.IsReachable(statement)) { return(false); } if (collectedStatements.Contains(statement)) { return(true); } var prevEnd = statement.GetPrevNode().EndLocation; // group multiple continuous statements into one issue var start = statement.StartLocation; collectedStatements.Add(statement); visitor.unreachableNodes.Add(statement); while (statement.NextSibling is Statement) { statement = (Statement)statement.NextSibling; collectedStatements.Add(statement); visitor.unreachableNodes.Add(statement); } var end = statement.EndLocation; var removeAction = new CodeAction(visitor.ctx.TranslateString("Remove unreachable code"), script => { var startOffset = script.GetCurrentOffset(prevEnd); var endOffset = script.GetCurrentOffset(end); script.RemoveText(startOffset, endOffset - startOffset); }); var commentAction = new CodeAction(visitor.ctx.TranslateString("Comment unreachable code"), script => { var startOffset = script.GetCurrentOffset(prevEnd); script.InsertText(startOffset, Environment.NewLine + "/*"); var endOffset = script.GetCurrentOffset(end); script.InsertText(endOffset, Environment.NewLine + "*/"); }); var actions = new [] { removeAction, commentAction }; visitor.AddIssue(start, end, visitor.ctx.TranslateString("Code is unreachable"), actions); return(true); }
bool AddStatement(Statement statement) { if (reachability.IsReachable(statement)) { return(false); } if (collectedStatements.Contains(statement)) { return(true); } if (statement is BlockStatement && statement.GetChildrenByRole <Statement>(BlockStatement.StatementRole).Any(reachability.IsReachable)) { //There's reachable content return(false); } var prevEnd = GetPrevEnd(statement); TextLocation start; TextLocation end; if (statement.Role == IfElseStatement.TrueRole) { var ife = (IfElseStatement)statement.Parent; start = ife.IfToken.StartLocation; collectedStatements.Add(ife.IfToken); prevEnd = ife.IfToken.StartLocation; collectedStatements.Add(statement); visitor.unreachableNodes.Add(statement); collectedStatements.Add(ife.ElseToken); end = GetNextStart(ife.ElseToken); } else if (statement.Role == IfElseStatement.FalseRole) { var ife = (IfElseStatement)statement.Parent; start = ife.ElseToken.StartLocation; collectedStatements.Add(ife.ElseToken); prevEnd = GetPrevEnd(ife.ElseToken); collectedStatements.Add(statement); visitor.unreachableNodes.Add(statement); end = statement.EndLocation; } else { // group multiple continuous statements into one issue start = statement.StartLocation; collectedStatements.Add(statement); visitor.unreachableNodes.Add(statement); Statement nextStatement; while ((nextStatement = (Statement)statement.GetNextSibling(s => s is Statement)) != null && !(nextStatement is LabelStatement)) { if (nextStatement.Role == IfElseStatement.FalseRole) { break; } statement = nextStatement; collectedStatements.Add(statement); visitor.unreachableNodes.Add(statement); } end = statement.EndLocation; } var removeAction = new CodeAction( visitor.ctx.TranslateString("Remove unreachable code"), script => { var startOffset = script.GetCurrentOffset(prevEnd); var endOffset = script.GetCurrentOffset(end); script.RemoveText(startOffset, endOffset - startOffset); }, collectedStatements.First().StartLocation, collectedStatements.Last().EndLocation); var commentAction = new CodeAction( visitor.ctx.TranslateString("Comment unreachable code"), script => { var startOffset = script.GetCurrentOffset(prevEnd); script.InsertText(startOffset, Environment.NewLine + "/*"); var endOffset = script.GetCurrentOffset(end); script.InsertText(endOffset, Environment.NewLine + "*/"); }, collectedStatements.First().StartLocation, collectedStatements.Last().EndLocation); var actions = new [] { removeAction, commentAction }; visitor.AddIssue(new CodeIssue(start, end, visitor.ctx.TranslateString("Code is unreachable"), actions) { IssueMarker = IssueMarker.GrayOut }); return(true); }