belav / csharpier

CSharpier is an opinionated code formatter for c#.
https://csharpier.com
MIT License
1.34k stars 91 forks source link

Using statement ordering and conditional compilation #1291

Open Rudomitori opened 3 months ago

Rudomitori commented 3 months ago

Ordering of using statement incorrectly handles conditional compilation preprocessor directives

Input: The code is from csharpier-repos

#if MONO_SECURITY_ALIAS
extern alias MonoSecurity;
#endif

#if MONO_SECURITY_ALIAS
using MonoSecurity::Mono.Security.Interface;
using MSX = MonoSecurity::Mono.Security.X509;
using MonoSecurity::Mono.Security.X509.Extensions;
#else
using Mono.Security.Interface;
using MSX = Mono.Security.X509;
using Mono.Security.X509.Extensions;
#endif

using System;
using System.Net;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Net.Configuration;
using System.Text.RegularExpressions;
using System.Security.Cryptography.X509Certificates;

using System.Globalization;
using System.Net.Security;
using System.Diagnostics;

Output:

#if MONO_SECURITY_ALIAS
extern alias MonoSecurity;
#endif

#else
using Mono.Security.Interface;
#endif

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Diagnostics;
using System.Globalization;
using System.Net;
using System.Net.Configuration;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text.RegularExpressions;
using System.Threading;
using Mono.Security.X509.Extensions;
#if MONO_SECURITY_ALIAS
using MonoSecurity::Mono.Security.Interface;
using MonoSecurity::Mono.Security.X509.Extensions;
using MSX = MonoSecurity::Mono.Security.X509;
using MSX = Mono.Security.X509;

Tested in the playground for the version 0.28.1

belav commented 3 months ago

If I recall correctly there were a couple edge cases like this from csharpier-repos. Fixing them didn't seem straightforward and some of the official analzyers don't deal well with conditional compilation so I let them slide.