Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1878 (SA1133CodeFixProvider crash with System.InvalidCastException) #1880

Merged
merged 6 commits into from
Dec 2, 2015

Conversation

NikolayIT
Copy link
Member

Fix #1878

@codecov-io
Copy link

Current coverage is 79.44%

Merging #1880 into stabilization will increase coverage by +0.03% as of 127039e

@@            stabilization   #1880   diff @@
=============================================
  Files                 558     558       
  Stmts               34807   34824    +17
  Branches             9637    9644     +7
  Methods                 0               
=============================================
+ Hit                 27642   27665    +23
- Partial              5624    5631     +7
+ Missed               1541    1528    -13

Review entire Coverage Diff as of 127039e

Powered by Codecov. Updated on successful CI builds.

@sharwell sharwell added this to the 1.0.0 RC 2 milestone Dec 1, 2015
@@ -51,8 +51,18 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
{
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var violatingAttribute = (AttributeSyntax)syntaxRoot.FindNode(diagnostic.Location.SourceSpan).Parent;
var attributeList = (AttributeListSyntax)violatingAttribute.Parent;
var nodeInSourceSpan = syntaxRoot.FindNode(diagnostic.Location.SourceSpan);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've run into problems in the past where error nodes in the tree caused FindNode to return unexpected nodes. Normally this is solved by setting getInnermostNodeForTie: true, but I'm not sure that would behave properly here. We probably want to have a third case handled where nodeInSourceSpan.Parent is neither an AttributeListSyntax nor an AttributeSyntax, and just return.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❗ A better solution would be to use getInnermostNodeForTie: true and then call FirstAncestorOrSelf<AttributeListSyntax>() on the found node. That call should never fail, as the analyzer works on AttributeSyntaxList.

@NikolayIT
Copy link
Member Author

@sharwell added default case which just returns the document object

@Noryoko
Copy link
Contributor

Noryoko commented Dec 1, 2015

Could you add these tests?

[Foo]
[Bar, Car]

[Foo, Bar]
[Car]

[Foo]
[Bar, Car]
[Ear]

[Foo, Bar, Car] Already covered

@NikolayIT
Copy link
Member Author

@Noryoko unit tests added.

@NikolayIT
Copy link
Member Author

@Noryoko, @vweijsters, do you think the following tests should also pass?

[InlineData("[Foo][Bar, Car]", "[Foo]\r\n[Bar]\r\n[Car]", 2, 12)]
[InlineData("[Foo, Bar][Car]", "[Foo]\r\n[Bar]\r\n[Car]", 2, 7)]
[InlineData("[Foo][Bar, Car][Ear]", "[Foo]\r\n[Bar]\r\n[Car]\r\n[Ear]", 2, 12)]

With the current code they are failing with:

Xunit.Sdk.EqualException

Assert.Equal() Failure
                                     ↓ (pos 27)
Expected: ···ystem;[Foo][Bar][Car]public class TestClass{}···
Actual:   ···ystem;[Foo][Bar][Car]public class TestClass{}pu···
                                     ↑ (pos 27)

Here you can find the test code: https://github.com/NikolayIT/StyleCopAnalyzers/commit/74bef9273a0a148b7a2e1a8fb32db9e5529b2ed9#diff-9e4ac25db4b34d81902b3ff7fedf0e6cR176

@Noryoko
Copy link
Contributor

Noryoko commented Dec 1, 2015

I think so. The fix is already splitting the attributes onto their own lines. Would make the end result look consistent.

@vweijsters
Copy link
Contributor

I don't think they should pass, as they are partly in the domain of SA1134. The SA1133 code fix adds the new lines to make sure that it doesn't immediately introduces a SA1134 after code fixing, but it will not / should not attempt to fix any preexisting SA1134 cases.

@NikolayIT
Copy link
Member Author

OK, then this pull request is ready for approval.

@Noryoko
Copy link
Contributor

Noryoko commented Dec 1, 2015

I'll change my vote to agree with @vweijsters.

@vweijsters
Copy link
Contributor

@NikolayIT I would like to see my remark on SA1133CodeFixProvider.cs to be addressed before I'm ok with this pull request.

@NikolayIT
Copy link
Member Author

@vweijsters is that what you meant?

@vweijsters
Copy link
Contributor

No, I meant:

❗ A better solution would be to use getInnermostNodeForTie: true and then call FirstAncestorOrSelf() on the found node. That call should never fail, as the analyzer works on AttributeSyntaxList.

@NikolayIT
Copy link
Member Author

Sorry, didn't see it. Is now OK? @vweijsters

@vweijsters
Copy link
Contributor

Totally 😉

👍 Looks good to me

@Noryoko
Copy link
Contributor

Noryoko commented Dec 1, 2015

👍 Looks good to me

sharwell added a commit that referenced this pull request Dec 2, 2015
Fix #1878 (SA1133CodeFixProvider crash with System.InvalidCastException)
@sharwell sharwell merged commit 73afee8 into DotNetAnalyzers:stabilization Dec 2, 2015
@vweijsters
Copy link
Contributor

@sharwell Could you merge this into master as well?

@sharwell
Copy link
Member

sharwell commented Dec 2, 2015

@vweijsters yes I'll merge it tonight

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants