codecadwallader / codemaid

CodeMaid is an open source Visual Studio extension to cleanup and simplify our C#, C++, F#, VB, PHP, PowerShell, JSON, XAML, XML, ASP, HTML, CSS, LESS, SCSS, JavaScript and TypeScript coding.
http://www.codemaid.net
GNU Lesser General Public License v3.0
1.89k stars 356 forks source link

Commented property attributes are unexpectedly removed #303

Open jboeke opened 8 years ago

jboeke commented 8 years ago

Environment

using System;
using System.ComponentModel.DataAnnotations;

namespace DomainRepository
{
    public class Person
    {
        #region Properties
        [Key]
        public Guid PersonGuid { get; set; }

        [Display(Name = "Fax")]
        //[DataType(DataType.PhoneNumber)]
        //[RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Entered phone format is not valid.")]
        //[Required(ErrorMessage = "Fax Number is required!")]
        public string FaxNumber { get; set; }

        #endregion Properties
    }
}

Steps to recreate

  1. Copy code above into Person.cs class file.
  2. Run CodeMaid > Cleanup Active Document menu item.

    Current behavior

Lines 13, 14 and 15 (commented lines) are removed by CodeMaid.

Expected behavior

I expect my comments to remain. I could not find any settings relevant to this behavior.

codecadwallader commented 8 years ago

Thanks for reporting the issue, I have reproduced it.

I've done some tracing and I believe this is tied to the logic that inserts explicit access modifiers on properties. The logic extracts the property prefix by getting the start point and searching forwards for an opening brace. In your example you have braces within your regular expression, so it is terminating the search early and assuming that an explicit access modifier is not present. Where it gets odd is that when it asks the IDE to add the access modifier, you would think it would do nothing if it already exists but instead it appears to be wiping out your comments entirely.

This logic won't need to exist in a future version rewritten on the Roslyn compiler. I'll see what I can find as a working solution until then. As an immediate workaround, you can either move the comment line away that contains a brace or disable CodeMaid's insertion of explicit access modifiers for properties.

Thanks again for reporting this issue.

jboeke commented 8 years ago

Confirmed that disabling "Insert explicit access modifiers on ... properties" works.Thanks for the workaround and your commitment to maintaining a great tool!

codecadwallader commented 7 years ago

I took another look at this but I didn't see any clean way to catch this scenario without the aforementioned Roslyn compiler work in place first.

RSchwoerer commented 9 months ago

I am seeing this same issue on any simple comment below an attribute. Note that these comments do not have braces in them, so this may be a slightly different issue, but appears to be related.

Example code...

        [XmlAnyElement]
        // foo -- THIS COMMENT WILL BE CLEANED AND REMOVED
        protected string _foo;

        // bar
        [XmlAnyElement]
        protected string _bar;

As a workaround for my issue I also need to disable "Insert explicit access modifiers on ... fields".