Skip to content
This repository was archived by the owner on Jul 12, 2022. It is now read-only.

Commit acdab71

Browse files
committed
Explicit visibility trivia handling
This change addresses how we handle trivia when inserting explicit visibility on a member / type. The previous code incorrectly used the leading trivia of the node in question to position the first visibility modifier. That would end up combining trivia from say before and after an attribute attached to the node which was incorrect. Now we grab the trivia from the node / type itself and use that. closes #86
1 parent 299dd7a commit acdab71

File tree

2 files changed

+317
-59
lines changed

2 files changed

+317
-59
lines changed

src/Microsoft.DotNet.CodeFormatting.Tests/Rules/ExplicitVisibilityRuleTests.cs

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,162 @@ internal class C
391391
Verify(text, expected, runFormatter: false);
392392
}
393393

394+
[Fact]
395+
public void CommentAttributeAndType()
396+
{
397+
var text = @"
398+
// Hello
399+
[Attr]
400+
// World
401+
class C1 { }
402+
403+
// Hello
404+
[Attr]
405+
// World
406+
partial class C2 { }";
407+
408+
var expected = @"
409+
// Hello
410+
[Attr]
411+
// World
412+
internal class C1 { }
413+
414+
// Hello
415+
[Attr]
416+
// World
417+
internal partial class C2 { }";
418+
419+
Verify(text, expected);
420+
}
421+
422+
[Fact]
423+
public void CommentAttributeAndMethod()
424+
{
425+
var text = @"
426+
class C
427+
{
428+
// Hello
429+
[Attr]
430+
// World
431+
void M1() { }
432+
433+
// Hello
434+
[Attr]
435+
// World
436+
static void M2() { }
437+
};";
438+
439+
var expected = @"
440+
internal class C
441+
{
442+
// Hello
443+
[Attr]
444+
// World
445+
private void M1() { }
446+
447+
// Hello
448+
[Attr]
449+
// World
450+
private static void M2() { }
451+
};";
452+
453+
Verify(text, expected);
454+
455+
}
456+
457+
[Fact]
458+
public void CommentAttributeAndProperty()
459+
{
460+
var text = @"
461+
class C
462+
{
463+
// Hello
464+
[Attr]
465+
// World
466+
int P1 { get { return 0; } }
467+
468+
// Hello
469+
[Attr]
470+
// World
471+
static int P2 { get { return 0; } }
472+
};";
473+
474+
var expected = @"
475+
internal class C
476+
{
477+
// Hello
478+
[Attr]
479+
// World
480+
private int P1 { get { return 0; } }
481+
482+
// Hello
483+
[Attr]
484+
// World
485+
private static int P2 { get { return 0; } }
486+
};";
487+
488+
Verify(text, expected);
489+
490+
}
491+
492+
[Fact]
493+
public void CommentAttributeAndConstructor()
494+
{
495+
var text = @"
496+
class C
497+
{
498+
// Hello
499+
[Attr]
500+
// World
501+
C(int p1) { }
502+
503+
// Hello
504+
[Attr]
505+
// World
506+
unsafe C(int p1, int p2) { }
507+
};";
508+
509+
var expected = @"
510+
internal class C
511+
{
512+
// Hello
513+
[Attr]
514+
// World
515+
private C(int p1) { }
516+
517+
// Hello
518+
[Attr]
519+
// World
520+
private unsafe C(int p1, int p2) { }
521+
};";
522+
523+
Verify(text, expected);
524+
525+
}
526+
527+
public void CommentAttributeAndMultipleField()
528+
{
529+
var text = @"
530+
class C
531+
{
532+
// Hello
533+
[Attr]
534+
// World
535+
int x, y;
536+
};";
537+
538+
var expected = @"
539+
internal class C
540+
{
541+
// Hello
542+
[Attr]
543+
// World
544+
private int x, y;
545+
};";
546+
547+
Verify(text, expected);
548+
}
549+
394550
[Fact]
395551
public void Issue70()
396552
{

0 commit comments

Comments
 (0)