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

Inconsistent new line behavior between properties with and without initializer. #3227

Open
ElektroKill opened this issue Jun 22, 2024 · 0 comments
Labels
Bug Decompiler The decompiler engine itself

Comments

@ElektroKill
Copy link
Contributor

ElektroKill commented Jun 22, 2024

Input code

ICSharpCode.Decompiler.dll coming from the latest preview release (9.0-preview2)
Class: DecompileRun

Erroneous output

internal class DecompileRun
{
	public HashSet<string> DefinedSymbols { get; } = new HashSet<string>();


	public HashSet<string> Namespaces { get; } = new HashSet<string>();


	public CancellationToken CancellationToken { get; set; }

	public DecompilerSettings Settings { get; }

	public IDocumentationProvider DocumentationProvider { get; set; }

	public Dictionary<ITypeDefinition, RecordDecompiler> RecordDecompilers { get; } = new Dictionary<ITypeDefinition, RecordDecompiler>();


	public Dictionary<ITypeDefinition, bool> TypeHierarchyIsKnown { get; } = new Dictionary<ITypeDefinition, bool>();


	private Lazy<UsingScope> usingScope => new Lazy<UsingScope>(() => CreateUsingScope(Namespaces));

	public UsingScope UsingScope => usingScope.Value;

	public DecompileRun(DecompilerSettings settings)
	{
		Settings = settings ?? throw new ArgumentNullException("settings");
	}

	private UsingScope CreateUsingScope(HashSet<string> requiredNamespacesSuperset)
	{
		UsingScope usingScope = new UsingScope();
		foreach (string ns in requiredNamespacesSuperset)
		{
			string[] parts = ns.Split(new char[1] { '.' });
			AstType nsType = new SimpleType(parts[0]);
			for (int i = 1; i < parts.Length; i++)
			{
				nsType = new MemberType
				{
					Target = nsType,
					MemberName = parts[i]
				};
			}
			if (nsType.ToTypeReference(NameLookupMode.TypeInUsingDeclaration) is TypeOrNamespaceReference reference)
			{
				usingScope.Usings.Add(reference);
			}
		}
		return usingScope;
	}
}

The new lines between the properties is inconsistent and, in my opinion, is non ideal. From observation it seems that properties without an initializer get a single new line after them while properties which include an initializer get two new lines after them. This is most likely a bug in CSharpOutputVisitor.VisitPropertyDeclaration. It seems that replacing the call to Semicolon() here:


with WriteToken(Roles.Semicolon); fixes the issue. This means that the Semicolon() call unnecessarily inserts a new line in this case.

Details

  • Reproduced using latest ILSpy 9.0-preview2 release.
@ElektroKill ElektroKill added Bug Decompiler The decompiler engine itself labels Jun 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Decompiler The decompiler engine itself
Projects
None yet
Development

No branches or pull requests

1 participant