Skip to content

Commit

Permalink
Merge pull request #25153 from dotnet/merges/dev15.7.x-to-master
Browse files Browse the repository at this point in the history
Merge dev15.7.x to master
  • Loading branch information
Shyam-Gupta authored Mar 2, 2018
2 parents ba26721 + b2a3766 commit 5ee82c6
Show file tree
Hide file tree
Showing 22 changed files with 275 additions and 50 deletions.
1 change: 1 addition & 0 deletions build/Targets/Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@
<SystemXmlXmlSerializerVersion>4.3.0</SystemXmlXmlSerializerVersion>
<SystemXmlXPathXDocumentVersion>4.3.0</SystemXmlXPathXDocumentVersion>
<SQLitePCLRawbundle_greenVersion>1.1.2</SQLitePCLRawbundle_greenVersion>
<UIAComWrapperVersion>1.1.0.14</UIAComWrapperVersion>
<MicrosoftVSSDKBuildToolsVersion>15.1.192</MicrosoftVSSDKBuildToolsVersion>
<VSExternalAPIsCodingConventionsVersion>1.0.60704.2</VSExternalAPIsCodingConventionsVersion>
<VSLangProjVersion>7.0.3300</VSLangProjVersion>
Expand Down
9 changes: 8 additions & 1 deletion build/scripts/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ param (
[switch]$sign = $false,
[switch]$pack = $false,
[switch]$binaryLog = $false,
[switch]$noAnalyzers = $false,
[string]$signType = "",

# Test options
Expand Down Expand Up @@ -134,7 +135,12 @@ function Run-MSBuild([string]$projectFilePath, [string]$buildArgs = "", [string]
if ($parallel) {
$args += " /m"
}


if ($noAnalyzers -or ($cibuild -and $testVsi)) {
# Avoid spending time in analyzers when requested, and also in the slowest integration test builds
$args += " /p:UseRoslynAnalyzers=false"
}

if ($binaryLog) {
if ($logFileName -eq "") {
$logFileName = [IO.Path]::GetFileNameWithoutExtension($projectFilePath)
Expand Down Expand Up @@ -588,6 +594,7 @@ function Ensure-ProcDump() {
function Redirect-Temp() {
$temp = Join-Path $binariesDir "Temp"
Create-Directory $temp
Copy-Item (Join-Path $repoDir "src\Workspaces\CoreTestUtilities\TestFiles\.editorconfig") $temp
Copy-Item (Join-Path $repoDir "src\Workspaces\CoreTestUtilities\TestFiles\Directory.Build.props") $temp
Copy-Item (Join-Path $repoDir "src\Workspaces\CoreTestUtilities\TestFiles\Directory.Build.targets") $temp
${env:TEMP} = $temp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal static UsedNamespaceOrType CreateXmlNamespace(string prefix, string xml

public override bool Equals(object obj)
{
return obj is UsedNamespaceOrType && base.Equals((UsedNamespaceOrType)obj);
return obj is UsedNamespaceOrType other && Equals(other);
}

public bool Equals(UsedNamespaceOrType other)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
Expand All @@ -9,6 +10,7 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.UseAutoProperty;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Editor.CSharp.UseAutoProperty
{
Expand Down Expand Up @@ -135,19 +137,44 @@ private bool CheckExpressionSyntactically(ExpressionSyntax expression)
protected override ExpressionSyntax GetGetterExpression(IMethodSymbol getMethod, CancellationToken cancellationToken)
{
// Getter has to be of the form:
//
// get { return field; } or
// 1. Getter can be defined as accessor or expression bodied lambda
// get { return field; }
// get => field;
// int Property => field;
// 2. Underlying field can be accessed with this qualifier or not
// get { return field; }
// get { return this.field; }
var getAccessor = getMethod.DeclaringSyntaxReferences[0].GetSyntax(cancellationToken) as AccessorDeclarationSyntax;
var statements = getAccessor?.Body?.Statements;
var expr = GetGetterExpressionFromSymbol(getMethod, cancellationToken);
if (expr == null)
{
return null;
}

return CheckExpressionSyntactically(expr) ? expr : null;
}

private ExpressionSyntax GetGetterExpressionFromSymbol(IMethodSymbol getMethod, CancellationToken cancellationToken)
{
var declaration = getMethod.DeclaringSyntaxReferences[0].GetSyntax(cancellationToken);
switch (declaration)
{
case AccessorDeclarationSyntax accessorDeclaration:
return accessorDeclaration.ExpressionBody?.Expression ??
GetSingleStatementFromAccessor<ReturnStatementSyntax>(accessorDeclaration)?.Expression;
case ArrowExpressionClauseSyntax arrowExpression:
return arrowExpression.Expression;
case null: return null;
default: throw ExceptionUtilities.Unreachable;
}
}

private T GetSingleStatementFromAccessor<T>(AccessorDeclarationSyntax accessorDeclaration) where T : StatementSyntax
{
var statements = accessorDeclaration?.Body?.Statements;
if (statements?.Count == 1)
{
var statement = statements.Value[0];
if (statement.Kind() == SyntaxKind.ReturnStatement)
{
var expr = ((ReturnStatementSyntax)statement).Expression;
return CheckExpressionSyntactically(expr) ? expr : null;
}
return statement as T;
}

return null;
Expand All @@ -157,31 +184,29 @@ protected override ExpressionSyntax GetSetterExpression(IMethodSymbol setMethod,
{
// Setter has to be of the form:
//
// set { field = value; } or
// set { field = value; }
// set { this.field = value; }
// set => field = value;
// set => this.field = value;
var setAccessor = setMethod.DeclaringSyntaxReferences[0].GetSyntax(cancellationToken) as AccessorDeclarationSyntax;
var statements = setAccessor?.Body?.Statements;
if (statements?.Count == 1)
var setExpression = GetExpressionFromSetter(setAccessor);
if (setExpression?.Kind() == SyntaxKind.SimpleAssignmentExpression)
{
var statement = statements.Value[0];
if (statement.IsKind(SyntaxKind.ExpressionStatement))
var assignmentExpression = (AssignmentExpressionSyntax)setExpression;
if (assignmentExpression.Right.Kind() == SyntaxKind.IdentifierName &&
((IdentifierNameSyntax)assignmentExpression.Right).Identifier.ValueText == "value")
{
var expressionStatement = (ExpressionStatementSyntax)statement;
if (expressionStatement.Expression.Kind() == SyntaxKind.SimpleAssignmentExpression)
{
var assignmentExpression = (AssignmentExpressionSyntax)expressionStatement.Expression;
if (assignmentExpression.Right.Kind() == SyntaxKind.IdentifierName &&
((IdentifierNameSyntax)assignmentExpression.Right).Identifier.ValueText == "value")
{
return CheckExpressionSyntactically(assignmentExpression.Left) ? assignmentExpression.Left : null;
}
}
return CheckExpressionSyntactically(assignmentExpression.Left) ? assignmentExpression.Left : null;
}
}

return null;
}

private ExpressionSyntax GetExpressionFromSetter(AccessorDeclarationSyntax setAccessor)
=> setAccessor?.ExpressionBody?.Expression ??
GetSingleStatementFromAccessor<ExpressionStatementSyntax>(setAccessor)?.Expression;

protected override SyntaxNode GetNodeToFade(FieldDeclarationSyntax fieldDeclaration, VariableDeclaratorSyntax variableDeclarator)
{
return fieldDeclaration.Declaration.Variables.Count == 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ protected override async Task<SyntaxNode> UpdatePropertyAsync(
var project = propertyDocument.Project;
var sourceText = await propertyDocument.GetTextAsync(cancellationToken).ConfigureAwait(false);

var getAccessor = propertyDeclaration.AccessorList.Accessors.First(d => d.IsKind(SyntaxKind.GetAccessorDeclaration));

var updatedProperty = propertyDeclaration.WithAccessorList(UpdateAccessorList(propertyDeclaration.AccessorList));
var updatedProperty = propertyDeclaration.WithAccessorList(UpdateAccessorList(propertyDeclaration.AccessorList))
.WithExpressionBody(null)
.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.None));

// We may need to add a setter if the field is written to outside of the constructor
// of it's class.
Expand Down Expand Up @@ -126,7 +126,7 @@ private async Task<ExpressionSyntax> GetFieldInitializerAsync(IFieldSymbol field

private bool NeedsSetter(Compilation compilation, PropertyDeclarationSyntax propertyDeclaration, bool isWrittenOutsideOfConstructor)
{
if (propertyDeclaration.AccessorList.Accessors.Any(SyntaxKind.SetAccessorDeclaration))
if (propertyDeclaration.AccessorList?.Accessors.Any(SyntaxKind.SetAccessorDeclaration) == true)
{
// Already has a setter.
return false;
Expand All @@ -150,14 +150,23 @@ private bool SupportsReadOnlyProperties(Compilation compilation)

private AccessorListSyntax UpdateAccessorList(AccessorListSyntax accessorList)
{
if (accessorList == null)
{
var getter = SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken));
return SyntaxFactory.AccessorList(SyntaxFactory.List(Enumerable.Repeat(getter, 1)));
}

return accessorList.WithAccessors(SyntaxFactory.List(GetAccessors(accessorList.Accessors)));
}

private IEnumerable<AccessorDeclarationSyntax> GetAccessors(SyntaxList<AccessorDeclarationSyntax> accessors)
{
foreach (var accessor in accessors)
{
yield return accessor.WithBody(null).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken));
yield return accessor.WithBody(null)
.WithExpressionBody(null)
.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,166 @@ object IFoo.Bar
set { bar = value; }
}
}
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseAutoProperty)]
public async Task ExpressionBodiedMemberGetOnly()
{
await TestInRegularAndScriptAsync(
@"class Class
{
int [|i|];
int P
{
get => i;
}
}",
@"class Class
{
int P { get; }
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseAutoProperty)]
public async Task ExpressionBodiedMemberGetOnlyWithInitializer()
{
await TestInRegularAndScriptAsync(
@"class Class
{
int [|i|] = 1;
int P
{
get => i;
}
}",
@"class Class
{
int P { get; } = 1;
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseAutoProperty)]
public async Task ExpressionBodiedMemberGetOnlyWithInitializerAndNeedsSetter()
{
await TestInRegularAndScriptAsync(
@"class Class
{
int [|i|] = 1;
int P
{
get => i;
}
void M() { i = 2; }
}",
@"class Class
{
int P { get; set; } = 1;
void M() { P = 2; }
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseAutoProperty)]
public async Task ExpressionBodiedMemberGetterAndSetter()
{
await TestInRegularAndScriptAsync(
@"class Class
{
int [|i|];
int P
{
get => i;
set { i = value; }
}
}",
@"class Class
{
int P { get; set; }
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseAutoProperty)]
public async Task ExpressionBodiedMemberGetter()
{
await TestInRegularAndScriptAsync(
@"class Class
{
int [|i|];
int P => i;
}",
@"class Class
{
int P { get; }
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseAutoProperty)]
public async Task ExpressionBodiedMemberGetterWithSetterNeeded()
{
await TestInRegularAndScriptAsync(
@"class Class
{
int [|i|];
int P => i;
void M() { i = 1; }
}",
@"class Class
{
int P { get; set; }
void M() { P = 1; }
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseAutoProperty)]
public async Task ExpressionBodiedMemberGetterWithInitializer()
{
await TestInRegularAndScriptAsync(
@"class Class
{
int [|i|] = 1;
int P => i;
}",
@"class Class
{
int P { get; } = 1;
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseAutoProperty)]
public async Task ExpressionBodiedGetterAndSetter()
{
await TestInRegularAndScriptAsync(
@"class Class
{
int [|i|];
int P {
get => i;
set => i = value;
}
}",
@"class Class
{
int P { get; set; }
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseAutoProperty)]
public async Task ExpressionBodiedGetterAndSetterWithInitializer()
{
await TestInRegularAndScriptAsync(
@"class Class
{
int [|i|] = 1;
int P {
get => i;
set => i = value;
}
}",
@"class Class
{
int P { get; set; } = 1;
}");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ protected void SetUpEditor(string markupCode)
{
VisualStudio.Editor.SetText(code);
VisualStudio.Editor.MoveCaret(caretPosition);
VisualStudio.Editor.Activate();
}
finally
{
Expand Down
Loading

0 comments on commit 5ee82c6

Please sign in to comment.