dotnet / razor

Compiler and tooling experience for Razor ASP.NET Core apps in Visual Studio, Visual Studio for Mac, and VS Code.
https://asp.net
MIT License
505 stars 195 forks source link

[Blazor] Add @classname directive that identifies the class name #11116

Open mwpowellhtx opened 3 years ago

mwpowellhtx commented 3 years ago

Summary

Add an optional @classname directive, used in lieu of the default class naming convention.

Motivation and goals

By default Razor uses the file name to identify the class name, which is fine in perhaps 75-85% of the cases. In our case, and in a couple of others that I know of, we have a naming convention something like MyComponent{T1, T2, ...}.razor in order to better identify files and their generic types, which ends up naming a component something like MyComponent_T1_T2..._. This is incorrect, obviously. So we propose an optional @classname directive to allow folks to work around that limitation.

Risks / unknowns

I cannot think of any; I cannot imagine it would be any better or worse than similarly named directives, i.e. @typename, for instance. In fact we think it would be fairly consistent with those other, similar directives.

Examples

<!-- ... -->

@classname MyComponent

@typename T1
@typename T2
<!-- ... -->

Which we think should yield the following generated code:

public partial class MyComponent<T1, T2, ...> { ... }

Or, if there is truly a conflict, perhaps something like @componentname. We are not attached to the naming conventions therein; only that we have a directive based alternative to the file name being the convention.

Detailed design

Might follow an evaluation pattern, something like;

if (TryGenerateClassnameDirectiveClass(...) || TryGenerateFilenameClass(...)) { ... }

I think if would be fairly easy to identify, assuming sufficient metadata is available at the moment of CG. This is one opinion; happy to modify that opinion given a more concrete source context from which to work.

In whatever form that takes, bottom line, we think that it would be simple to prioritize the directive first, then fall back on a default convention when the directive was not discovered.

If this looks okay, would be happy to submit a PR, given direction to a file or files when component naming occurs, with availability of directive meta data, etc.

ghost commented 3 years ago

Thanks for contacting us. We're moving this issue to the Next sprint planning milestone for future evaluation / consideration. We will evaluate the request when we are planning the work for the next milestone. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

mwpowellhtx commented 3 years ago

Wow, thank you. I've got a sense of the code base at the moment, a working draft in progress, so feel to contact me privately to discuss further.

ghost commented 3 years ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

ghost commented 3 years ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

KifoPL commented 2 years ago

Does removing this issue from .NET 7 Planning milestone mean it won't be considered for the upcoming release and we may expect it at the earliest of .NET 8?

FelixCCWork commented 9 months ago

What is the current state of this issue? Would be really helpful for component libraries.

TonyValenti commented 8 months ago

I could really use this too.

A9G-Data-Droid commented 7 months ago

I am not seeing a class name that matches the filename. I am seeing something randomly generated like AspNetCore_$(GUID). I ended up here looking for a way to define it. Should it be the filename? And what could have I done to make it not the filename?

mwpowellhtx commented 7 months ago

I am not seeing a class name that matches the filename. I am seeing something randomly generated like AspNetCore_$(GUID). I ended up here looking for a way to define it. Should it be the filename? And what could have I done to make it not the filename?

Not sure what you are talking about, "file name"? Correlated to the class name? Does not have to be, that would be silly if that was the implementation.

A9G-Data-Droid commented 7 months ago

Sorry for the confusion. My filename and class name are the same. However, references to the class as seen by intellisense show this generated class ID. As a result, I get odd warnings in my IDE about class members not being found. I looked for a naming conflict and I don't see any other object or property with the same name as the class. The class name in my case is unique.