WiseTechGlobal / WTG.Analyzers

Analyzers from WiseTech Global to enforce our styles, behaviours, and prevent common mistakes.
Other
15 stars 3 forks source link

WTG3007 code fix adds extra new lines when overridden member is adjacent to a #region or #endregion #200

Open andrewhongnsw opened 1 year ago

andrewhongnsw commented 1 year ago

The code fix to remove pointlessly overridden methods works as expected, except for cases where the overridden method comes immediately after, or comes immediately before a #region or #endregion directive.

In such cases, extra new lines are left in the returned document.

For example a code fix on the following snippet:

#region Form Setup
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
        }

        void SetControlModuleIDs()

Will lead to:

#region Form Setup

        void SetControlModuleIDs()

This leads to SA1507errors on the lines between the #region directive and the following method.

yaakov-h commented 1 year ago

This is somewhat expected. We remove the method, but keep exterior trivia (leading and trailing). If there are comments there, or in your case #region trivia, we want to keep it.

If we want to get fancy and start balancing the trivia ourselves, then we'd have to pick one set of rules to use. SA1507 doesn't like the multiline whitespace, but it is also feasible for a project to have the opposite and require two blank lines - or to require no blank lines at all.

SA1507 also includes a code fix, so you should be able to immediately fix up that secondary warning too.