Skip to content

Add warning and code fix suggestion for non-public [JSInvokable] methods - #67137

Merged
dariatiurina merged 10 commits into
dotnet:mainfrom
damyanpetev:dpetev/jsinvokable-analyzer
Jul 10, 2026
Merged

dariatiurina merged 10 commits into
dotnet:mainfrom
damyanpetev:dpetev/jsinvokable-analyzer

Conversation

@damyanpetev

@damyanpetev damyanpetev commented Jun 10, 2026 •

Copy link
Copy Markdown
Contributor

Add Blazor Analyzer diagnostic warning and Code fix provider when a [JSInvokable] method is not public

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Added new analyzer and code fix provider to convert [JSInvokable] to public

Description

Added diagnostic warning for [JSInvokable] not being public:
image
And code fix to make public (should also handle multiple modifiers, related to #67365):
image

Closes #65235

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR adds analyzer/code-fix support for [JSInvokable] methods needing to be public static, and improves the existing component-parameter code fix to correctly handle properties with multiple accessibility modifiers.

Changes:

  • Added JSInvokableAnalyzer diagnostics (BL0012, BL0013) plus a code fix that makes methods public static.
  • Added analyzer/code-fix tests and shared test declarations for JSInvokableAttribute.
  • Updated ComponentParametersShouldBePublicCodeFixProvider to rewrite modifier lists more robustly, plus new tests for multi-modifier properties.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/Tools/SDK-Analyzers/Components/test/ComponentParametersShouldBePublicCodeFixProviderTest.cs Adds coverage for multi-modifier parameter properties.
src/Tools/SDK-Analyzers/Components/src/ComponentParametersShouldBePublicCodeFixProvider.cs Updates modifier rewrite logic to handle multi-modifier cases.
src/Components/Analyzers/test/JSInvokableCodeFixProviderTest.cs Adds code-fix tests for making [JSInvokable] methods public static.
src/Components/Analyzers/test/JSInvokableAnalyzerTest.cs Adds diagnostic tests for [JSInvokable] method requirements.
src/Components/Analyzers/test/ComponentsTestDeclarations.cs Adds SourceWithJSInvokable test declaration for the attribute.
src/Components/Analyzers/test/ComponentParametersShouldBePublicCodeFixProviderTest.cs Mirrors multi-modifier parameter-property coverage for this analyzer suite.
src/Components/Analyzers/src/Resources.resx Adds resource strings for the new JSInvokable diagnostics and fix title.
src/Components/Analyzers/src/JSInvokableCodeFixProvider.cs Introduces the code fix to rewrite method modifiers to public static.
src/Components/Analyzers/src/JSInvokableAnalyzer.cs Introduces the analyzer that reports non-public / non-static [JSInvokable] methods.
src/Components/Analyzers/src/DiagnosticDescriptors.cs Adds BL0012 / BL0013 diagnostic descriptors.
src/Components/Analyzers/src/ComponentParametersShouldBePublicCodeFixProvider.cs Updates modifier rewrite logic to handle multi-modifier cases.

@damyanpetev
damyanpetev force-pushed the dpetev/jsinvokable-analyzer branch 3 times, most recently from d3370b8 to d3d7360 Compare June 11, 2026 13:04
@damyanpetev
damyanpetev requested a review from Copilot June 11, 2026 13:05

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 5 comments.

@damyanpetev
damyanpetev force-pushed the dpetev/jsinvokable-analyzer branch from d3d7360 to 985fe24 Compare June 11, 2026 14:04
@javiercn javiercn added community-contribution Indicates that the PR has been added by a community member analyzer Indicates an issue which is related to analyzer experience labels Jun 17, 2026
@dariatiurina
dariatiurina self-requested a review June 17, 2026 14:49
@damyanpetev

Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service agree company="Infragistics, Inc"

@dariatiurina dariatiurina left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you for your work!

I am not sure about static part of the analyzers based on docs.
Also, can you please file new issue for the bug with existing analyzer and then fix it with new PR

@damyanpetev damyanpetev changed the title Add warning and code fix suggestion for non-public/static [JSInvokable] methods Add warning and code fix suggestion for non-public [JSInvokable] methods Jun 22, 2026
@damyanpetev

damyanpetev commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor Author

Went with normalizing line endings on source and result in the CodeFixVerifier, as the Helix tests apparently build on Windows (baking in CLRF in tests) and send jobs to Unix systems and there's already similar handling in:

TestState = { Sources = { source.ReplaceLineEndings() }, OutputKind = OutputKind.ConsoleApplication },
FixedState = { Sources = { fixedSource.ReplaceLineEndings() } },

and of course some tests ignore those completely in the Assert:
Assert.Equal(expectedOutput, actualOutput, ignoreLineEndingDifferences: true);

Assert.Equal(expectedOutput, actualOutput, ignoreLineEndingDifferences: true);

@dariatiurina dariatiurina left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Great work! Other than one test question I have no remarks.

@dariatiurina
dariatiurina merged commit 7a0af92 into dotnet:main Jul 10, 2026
25 checks passed
@damyanpetev
damyanpetev deleted the dpetev/jsinvokable-analyzer branch July 10, 2026 09:02
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jul 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

analyzer Indicates an issue which is related to analyzer experience area-blazor Includes: Blazor, Razor Components community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Blazor] Warn + suggest code fix when a [JSInvokable] method is non-public.*

4 participants