本文整理汇总了C#中IfStatementSyntax类的典型用法代码示例。如果您正苦于以下问题:C# IfStatementSyntax类的具体用法?C# IfStatementSyntax怎么用?C# IfStatementSyntax使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
IfStatementSyntax类属于命名空间,在下文中一共展示了IfStatementSyntax类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: HasConflictingNames
static bool HasConflictingNames(SyntaxNodeAnalysisContext nodeContext, IfStatementSyntax ifElseStatement)
{
var block = ifElseStatement.Else.Statement as BlockSyntax;
if (block == null || block.Statements.Count == 0)
return false;
var member = ifElseStatement.Ancestors().FirstOrDefault(a => a is MemberDeclarationSyntax);
var priorLocalDeclarations = new List<string>();
foreach (var localDecl in member.DescendantNodes().Where(n => n.SpanStart < ifElseStatement.Else.SpanStart).OfType<LocalDeclarationStatementSyntax>()) {
foreach (var v in localDecl.Declaration.Variables)
priorLocalDeclarations.Add(v.Identifier.ValueText);
}
foreach (var sym in block.Statements)
{
var decl = sym as LocalDeclarationStatementSyntax;
if (decl == null)
continue;
if (priorLocalDeclarations.Contains(s => decl.Declaration.Variables.Any(v => v.Identifier.ValueText == s)))
return true;
}
return false;
}
示例2: VisitIfStatement
protected override SyntaxNode VisitIfStatement(IfStatementSyntax node)
{
if (!node.DescendentNodes().OfType<BlockSyntax>().Any())
{
node = node.Update (node.IfKeyword, node.OpenParenToken, node.Condition, node.CloseParenToken,
Syntax.Block (statements: node.Statement), node.ElseOpt);
}
if (node.ElseOpt != null)
{
ElseClauseSyntax elseOpt = node.ElseOpt;
IfStatementSyntax ifSyntax = elseOpt.Statement as IfStatementSyntax;
if (ifSyntax != null)
{
if (!ifSyntax.DescendentNodes().OfType<BlockSyntax>().Any())
{
ifSyntax = ifSyntax.Update (ifSyntax.IfKeyword, ifSyntax.OpenParenToken, ifSyntax.Condition, ifSyntax.CloseParenToken,
Syntax.Block (statements: ifSyntax.Statement), ifSyntax.ElseOpt);
elseOpt = elseOpt.Update (elseOpt.ElseKeyword, ifSyntax);
}
}
else if (!elseOpt.DescendentNodes().OfType<BlockSyntax>().Any())
elseOpt = node.ElseOpt.Update (node.ElseOpt.ElseKeyword, Syntax.Block (statements: node.ElseOpt.Statement));
if (elseOpt != node.ElseOpt)
node = node.Update (node.IfKeyword, node.OpenParenToken, node.Condition, node.CloseParenToken, node.Statement, elseOpt);
}
return base.VisitIfStatement (node);
}
示例3: GetMatch
static bool GetMatch(IfStatementSyntax node, out ExpressionSyntax c, out ReturnStatementSyntax e1, out ReturnStatementSyntax e2, out ReturnStatementSyntax rs)
{
rs = e1 = e2 = null;
c = node.Condition;
//attempt to match if(condition) return else return
e1 = ConvertIfStatementToNullCoalescingExpressionAction.GetSimpleStatement(node.Statement) as ReturnStatementSyntax;
if (e1 == null)
return false;
e2 = node.Else != null ? ConvertIfStatementToNullCoalescingExpressionAction.GetSimpleStatement(node.Else.Statement) as ReturnStatementSyntax : null;
//match
if (e1 != null && e2 != null)
{
return true;
}
//attempt to match if(condition) return
if (e1 != null)
{
rs = node.Parent.ChildThatContainsPosition(node.GetTrailingTrivia().Max(t => t.FullSpan.End) + 1).AsNode() as ReturnStatementSyntax;
if (rs != null)
{
e2 = rs;
return true;
}
}
return false;
}
示例4: CalculateNewRoot
private static SyntaxNode CalculateNewRoot(SyntaxNode root, IfStatementSyntax ifStatement)
{
SyntaxNode newRoot;
var isTrue = ifStatement.Condition.IsKind(SyntaxKind.TrueLiteralExpression);
if (isTrue)
{
var block = ifStatement.Statement as BlockSyntax;
newRoot = block == null
? root.ReplaceNode(ifStatement, ifStatement.Statement)
: root.ReplaceNode(ifStatement, block.Statements);
}
else
{
if (ifStatement.Else == null)
{
newRoot = root.RemoveNode(ifStatement, SyntaxRemoveOptions.KeepNoTrivia);
}
else
{
var block = ifStatement.Else.Statement as BlockSyntax;
newRoot = block == null
? root.ReplaceNode(ifStatement, ifStatement.Else.Statement)
: root.ReplaceNode(ifStatement, block.Statements);
}
}
return newRoot.WithAdditionalAnnotations(Formatter.Annotation);
}
开发者ID:peterstevens130561,项目名称:sonarlint-vs,代码行数:29,代码来源:IfConditionalAlwaysTrueOrFalseCodeFixProvider.cs
示例5: GetMatch
static bool GetMatch(IfStatementSyntax node, out ExpressionSyntax c, out ReturnStatementSyntax e1, out ReturnStatementSyntax e2, out ReturnStatementSyntax rs)
{
rs = e1 = e2 = null;
c = node.Condition;
//attempt to match if(condition) return else return
e1 = ConvertIfStatementToNullCoalescingExpressionAction.GetSimpleStatement(node.Statement) as ReturnStatementSyntax;
if (e1 == null)
return false;
e2 = node.Else != null ? ConvertIfStatementToNullCoalescingExpressionAction.GetSimpleStatement(node.Else.Statement) as ReturnStatementSyntax : null;
//match
if (e1 != null && e2 != null)
{
return true;
}
//attempt to match if(condition) return; return
if (e1 != null)
{
var parentBlock = node.Parent as BlockSyntax;
if (parentBlock == null)
return false;
var index = parentBlock.Statements.IndexOf(node);
if (index + 1 < parentBlock.Statements.Count)
{
rs = parentBlock.Statements[index + 1] as ReturnStatementSyntax;
}
if (rs != null)
{
e2 = rs;
return true;
}
}
return false;
}
开发者ID:alecor191,项目名称:RefactoringEssentials,代码行数:35,代码来源:ConvertIfStatementToReturnStatementAction.cs
示例6: LopsidedTerminalBranchesToGuardedBranches
public static IEnumerable<StatementSyntax> LopsidedTerminalBranchesToGuardedBranches(IfStatementSyntax syntax)
{
Contract.Requires(syntax != null);
Contract.Requires(syntax.Parent is BlockSyntax);
if (syntax.Statement.IsGuaranteedToJumpOut()) return new[] { syntax };
if (syntax.Else != null && syntax.Else.Statement.IsGuaranteedToJumpOut()) return new[] { syntax };
var allowedJump = syntax.TryGetEquivalentJumpAfterStatement();
if (allowedJump == null) return new[] { syntax };
var trueBloat = syntax.Statement.Bloat();
var falseBloat = syntax.Else == null ? 0 : syntax.Else.Statement.Bloat();
if (trueBloat < falseBloat * 2 - 10) {
// inline the false branch, guard with the true branch
return syntax.Else.Statement.Statements().Prepend(
syntax.WithStatement(syntax.Statement.BracedTo(syntax.Statement.Statements().Concat(new[] {allowedJump})))
.WithElse(null));
}
if (falseBloat < trueBloat * 2 - 10) {
// inline the true branch, guard with the false branch
return syntax.Statement.Statements().Prepend(
syntax.WithCondition(syntax.Condition.Inverted())
.WithStatement(syntax.Else == null ? allowedJump : syntax.Else.Statement.BracedTo(syntax.Else.Statement.Statements().Concat(new[] {allowedJump})))
.WithElse(null));
}
return new[] { syntax };
}
示例7: FixSpacingAsync
private async Task<Document> FixSpacingAsync(Document document, IfStatementSyntax ifStatement, CancellationToken c)
{
// This method will generate a new if-statement node with a single space between the if-keyword and the opening parenthesis.
var generator = SyntaxGenerator.GetGenerator(document);
// The new if-statement will need to retain the same statements as the old if-statement.
var ifBlock = ifStatement.Statement as BlockSyntax;
var ifBlockStatements = ifBlock.Statements;
// The new if-statement should retain the formatting of the trivia before and after the if-statement.
// The following statements extract the original trivia.
var ifKeyword = ifStatement.IfKeyword;
var leadingTrivia = ifKeyword.LeadingTrivia;
var closeBrace = ifBlock.CloseBraceToken;
var trailingTrivia = closeBrace.TrailingTrivia;
// If-statements generated using SyntaxGenerator are formatted with the desired spacing by default so this is not specified explicitly.
var newIfStatement = generator.IfStatement(ifStatement.Condition, ifBlockStatements).WithLeadingTrivia(leadingTrivia).WithTrailingTrivia(trailingTrivia);
// This statement gets the top of the syntax tree so that the old if-statement node can be replaced with the new one.
var root = await document.GetSyntaxRootAsync();
// A new root is created with the old if-statement replaced by the new one.
var newRoot = root.ReplaceNode(ifStatement, newIfStatement);
// A new document with the new root is returned.
var newDocument = document.WithSyntaxRoot(newRoot);
return newDocument;
}
示例8: BoundIfStatement
public BoundIfStatement(IfStatementSyntax syntax, BoundExpression condition, BoundStatement consequence, BoundStatement alternativeOpt)
: base(BoundNodeKind.IfStatement, syntax)
{
Condition = condition;
Consequence = consequence;
AlternativeOpt = alternativeOpt;
}
示例9: AddBraces
public static IfStatementSyntax AddBraces(IfStatementSyntax ifStatement)
{
Debug.Assert(ifStatement != null && NeedsBraces(ifStatement));
return ifStatement
.WithStatement(SyntaxFactory.Block(ifStatement.Statement))
.WithAdditionalAnnotations(Formatter.Annotation);
}
示例10: HandleIf
private void HandleIf(SyntaxNodeAnalysisContext context, IfStatementSyntax ifStatement)
{
if (ifStatement.Statement is BlockSyntax)
{
return;
}
context.ReportDiagnostic(Diagnostic.Create(Rule, ifStatement.IfKeyword.GetLocation()));
}
示例11: ConvertToConditional
private Document ConvertToConditional(Document document, SemanticModel semanticModel, IfStatementSyntax ifStatement, StatementSyntax replacementStatement, CancellationToken cancellationToken)
{
var oldRoot = semanticModel.SyntaxTree.GetRoot();
var newRoot = oldRoot.ReplaceNode(
oldNode: ifStatement,
newNode: replacementStatement.WithAdditionalAnnotations(Formatter.Annotation));
return document.WithSyntaxRoot(newRoot);
}
示例12: BoundIfStatement
public BoundIfStatement(
BoundExpression boundExpression,
BoundScopeStatement boundStatements,
IfStatementSyntax statementSyntax)
: base(statementSyntax)
{
BoundExpression = boundExpression;
BoundStatements = boundStatements;
}
示例13: AddBracesAsync
private async Task<Document> AddBracesAsync(Document document, IfStatementSyntax ifStatement, CancellationToken cancellationToken)
{
var nonBlockedStatement = ifStatement.Statement as ExpressionStatementSyntax;
var newBlockedStatement = SyntaxFactory.Block(statements: nonBlockedStatement).WithAdditionalAnnotations(annotations: Formatter.Annotation);
var newIfStatement = ifStatement.ReplaceNode(oldNode: nonBlockedStatement, newNode: newBlockedStatement);
var root = await document.GetSyntaxRootAsync();
var newRoot = root.ReplaceNode(oldNode: ifStatement, newNode: newIfStatement);
var newDocument = document.WithSyntaxRoot(newRoot);
return newDocument;
}
示例14: IfStatement
public static string IfStatement(IfStatementSyntax statement)
{
var output = statement.IfKeyword.Text + " (";
output += SyntaxNode(statement.Condition);
output += ")" + NewLine + SyntaxNode(statement.Statement);
if (statement.Else != null)
{
output += SyntaxNode(statement.Else);
}
return output;
}
示例15: ParseIfStatement
internal static bool ParseIfStatement(IfStatementSyntax node, out ExpressionSyntax condition, out ExpressionSyntax target, out AssignmentExpressionSyntax whenTrue, out AssignmentExpressionSyntax whenFalse)
{
condition = null;
target = null;
whenTrue = null;
whenFalse = null;
if (node == null || node.Else == null || node.Parent is IfStatementSyntax || node.Else.Statement is IfStatementSyntax)
return false;
condition = node.Condition;
//make sure to check for multiple statements
ExpressionStatementSyntax whenTrueExprStatement, whenFalseExprStatement;
var embeddedBlock = node.Statement as BlockSyntax;
if (embeddedBlock != null)
{
if (embeddedBlock.Statements.Count > 1)
return false;
var childNodes = embeddedBlock.ChildNodes();
if (childNodes.Count() > 1)
return false;
whenTrueExprStatement = childNodes.OfType<ExpressionStatementSyntax>().FirstOrDefault();
}
else
{
whenTrueExprStatement = node.Statement as ExpressionStatementSyntax;
}
var elseBlock = node.Else.Statement as BlockSyntax;
if (elseBlock != null)
{
if (elseBlock.Statements.Count > 1)
return false;
var childNodes = elseBlock.ChildNodes();
if (childNodes.Count() > 1)
return false;
whenFalseExprStatement = childNodes.OfType<ExpressionStatementSyntax>().FirstOrDefault();
}
else
{
whenFalseExprStatement = node.Else.Statement as ExpressionStatementSyntax;
}
if (whenTrueExprStatement == null || whenFalseExprStatement == null)
return false;
whenTrue = whenTrueExprStatement.Expression as AssignmentExpressionSyntax;
whenFalse = whenFalseExprStatement.Expression as AssignmentExpressionSyntax;
if (whenTrue == null || whenFalse == null || whenTrue.Kind() != whenFalse.Kind() ||
!SyntaxFactory.AreEquivalent(whenTrue.Left, whenFalse.Left))
return false;
return true;
}
开发者ID:alecor191,项目名称:RefactoringEssentials,代码行数:54,代码来源:ConvertIfStatementToConditionalTernaryExpressionCodeRefactoringProvider.cs