Skip to content

Conversation

DustinCampbell
Copy link
Member

There are two extension methods targeting RazorCodeDocument that allow consumers to set or get the file kind for the document. However, this can result in inconsistencies because that file kind value (stored in RazorCodeDocument.Items) is independent from RazorCodeDocument.ParserOptions.FileKind. This PR removes both methods in favor RazorParserOptions.FileKind.

Summary of changes:

  • Remove the GetFileKind() and SetFileKind(...) extension methods and expose a convenience RazorCodeDocument.FileKind property that delegates to ParserOptions.FileKind.
  • Add RazorParserOptions.Create(...) and RazorCodeGenerationOptions.Create(...) convenience factory methods.
  • Remove LanguageVersion and FileKind from RazorCodeGenerationOptions to avoid the possibility of a RazorCodeDocument containing parser options and code generation options that disagree about these values.
  • Update tests (mostly in tooling) to stop calling SetFileKind(...) and pass the value as part of RazorCodeDocument creation.

Note

GetFileKind() and SetFileKind(...) are public extension methods but are not used by the RazorSdk, so they should be safe to remove.

Move the GetFileKind and SetFileKind extension methods to RazorCodeDocument and make them instance methods backed by a simple field.
Add a RazorParserOptions.Create(...) convenience factory method to make it easier to create RazorParserOptions with different RazorLanguageVersion and FileKind values.
RazorCodeDocument already exposes ParserOptions.FileKind, so a separate mechanism for getting and setting the file kind is both unneeded and a potential source of bugs.

Most of the changes are in tooling tests, which have unfortunately proliferated a bit of an anti-pattern for constructing code documents through copy-paste. I'll take a look at cleaning that up in a follow-up pull request. This change tries to keep test clean up to a minimum.
This is a purely mechanical change that makes the RazorCodeDocument.GetFileKind() a readonly, computed property that delegates to ParserOptions.FileKind.
This property is unused and it is less error-prone to only define it in a single place, i.e. RazorParserOptions.
There's just one use of RazorCodeGenerationOptions.Builder.LanguageVersion blocking its removal, and it can easily be removed.
This property is unused and it is less error-prone to only define it in a single place, i.e. RazorParserOptions.
For consistency with RazorParserOptions, add a RazorCodeGenerationOptions.Create(...) convenience factory method to make it easier to create new RazorCodeGenerationOptions.
@DustinCampbell DustinCampbell requested review from a team as code owners March 12, 2025 19:15
Copy link
Member

@chsienki chsienki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compiler changes LGTM

Copy link
Contributor

@ryzngard ryzngard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tooling LGTM

Copy link
Member

@davidwengier davidwengier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only one way to skin a cat? How can anyone work in these conditions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants