Skip to content

Commit 0e1ce42

Browse files
Merge pull request #1247 from GrahamTheCoder/claude/fix-issue-1242-winforms-nullref
Claude/fix issue 1242 winforms nullref
2 parents 68d3696 + 75268ea commit 0e1ce42

File tree

3 files changed

+164
-36
lines changed

3 files changed

+164
-36
lines changed

CodeConverter/CSharp/WinformsConversions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ private IEnumerable<Assignment> GetAssignmentsFromInitializeComponent(VBSyntax.M
5656
return CreateNameAssignment(maes.Expression.LastOrDefaultDescendant<VBSyntax.IdentifierNameSyntax>());
5757
}
5858
} else if (ShouldReassignProperty(s)){
59-
return CreatePropertyAssignment(s.Left.LastOrDefaultDescendant<VBSyntax.IdentifierNameSyntax>());
59+
var id = s.Left is VBSyntax.IdentifierNameSyntax directId ? directId : s.Left.LastOrDefaultDescendant<VBSyntax.IdentifierNameSyntax>();
60+
if (id == null) return null;
61+
return CreatePropertyAssignment(id);
6062
}
6163

6264
return null;

Tests/CSharp/MemberTests/EventMemberTests.cs

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,132 @@ private void _dep_SomeEvent(object sender, EventArgs e)
734734
");
735735
}
736736

737+
[Fact]
738+
public async Task Issue1242_WinformsNullRefOnBareIdentifierInInitializeComponentAsync()
739+
{
740+
// Regression test: bare identifier (no Me. prefix) in InitializeComponent caused a NullReferenceException
741+
// in WinformsConversions.CreatePropertyAssignment when s.Left is IdentifierNameSyntax and
742+
// LastOrDefaultDescendant returns null (since DescendantNodes does not include the node itself).
743+
await TestConversionVisualBasicToCSharpAsync(@"Imports System
744+
Imports System.Windows.Forms
745+
Imports Microsoft.VisualBasic.CompilerServices
746+
747+
Partial Class BaseForm
748+
Inherits Form
749+
Friend WithEvents BaseButton As Button
750+
End Class
751+
752+
<DesignerGenerated>
753+
Partial Class BaseForm
754+
Inherits System.Windows.Forms.Form
755+
756+
Private Sub InitializeComponent()
757+
BaseButton = New Button()
758+
End Sub
759+
End Class
760+
761+
<DesignerGenerated>
762+
Partial Class Form1
763+
Inherits BaseForm
764+
Private Sub InitializeComponent()
765+
Me.Button1 = New Button()
766+
End Sub
767+
Friend WithEvents Button1 As Button
768+
End Class
769+
770+
Partial Class Form1
771+
Private Sub MultiClickHandler(sender As Object, e As EventArgs) Handles Button1.Click,
772+
BaseButton.Click
773+
End Sub
774+
End Class", @"using System;
775+
using System.Runtime.CompilerServices;
776+
using System.Windows.Forms;
777+
using Microsoft.VisualBasic.CompilerServices; // Install-Package Microsoft.VisualBasic
778+
779+
internal partial class BaseForm : Form
780+
{
781+
private Button _BaseButton;
782+
783+
internal virtual Button BaseButton
784+
{
785+
[MethodImpl(MethodImplOptions.Synchronized)]
786+
get
787+
{
788+
return _BaseButton;
789+
}
790+
791+
[MethodImpl(MethodImplOptions.Synchronized)]
792+
set
793+
{
794+
_BaseButton = value;
795+
}
796+
}
797+
798+
public BaseForm()
799+
{
800+
InitializeComponent();
801+
BaseButton = _BaseButton;
802+
}
803+
}
804+
805+
[DesignerGenerated]
806+
internal partial class BaseForm : Form
807+
{
808+
809+
private void InitializeComponent()
810+
{
811+
_BaseButton = new Button();
812+
}
813+
}
814+
815+
[DesignerGenerated]
816+
internal partial class Form1 : BaseForm
817+
{
818+
internal override Button BaseButton
819+
{
820+
[MethodImpl(MethodImplOptions.Synchronized)]
821+
get
822+
{
823+
return base.BaseButton;
824+
}
825+
826+
[MethodImpl(MethodImplOptions.Synchronized)]
827+
set
828+
{
829+
if (base.BaseButton != null)
830+
{
831+
base.BaseButton.Click -= MultiClickHandler;
832+
}
833+
834+
base.BaseButton = value;
835+
if (base.BaseButton != null)
836+
{
837+
base.BaseButton.Click += MultiClickHandler;
838+
}
839+
}
840+
}
841+
842+
public Form1()
843+
{
844+
InitializeComponent();
845+
}
846+
private void InitializeComponent()
847+
{
848+
Button1 = new Button();
849+
Button1.Click += new EventHandler(MultiClickHandler);
850+
}
851+
internal Button Button1;
852+
}
853+
854+
internal partial class Form1
855+
{
856+
private void MultiClickHandler(object sender, EventArgs e)
857+
{
858+
}
859+
}
860+
");
861+
}
862+
737863
[Fact]
738864
public async Task Test_Issue701_MultiLineHandlesSyntaxAsync()
739865
{

web/package.json

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
{
2-
"name": "codeconverter",
3-
"version": "10.0.0",
4-
"private": true,
5-
"type": "module",
6-
"scripts": {
7-
"dev": "vite",
8-
"start": "vite",
9-
"test": "vitest",
10-
"build": "tsc && vite build",
11-
"preview": "vite preview",
12-
"lint": "eslint ./src/"
13-
},
14-
"dependencies": {
15-
"@monaco-editor/react": "^4.7.0",
16-
"axios": "^1.15.0",
17-
"bootstrap": "^5.3.8",
18-
"monaco-editor": "^0.55.1",
19-
"react": "^19.2.4",
20-
"react-dom": "^19.2.4",
21-
"react-router-bootstrap": "^0.26.3",
22-
"react-router-dom": "^7.13.0",
23-
"reactstrap": "^9.2.3"
24-
},
25-
"devDependencies": {
26-
"@types/node": "^22.10.5",
27-
"@types/react": "^19.2.13",
28-
"@types/react-dom": "^19.2.3",
29-
"@vitejs/plugin-react": "^4.3.4",
30-
"jsdom": "^27.0.1",
31-
"typescript": "~5.7.2",
32-
"vite": "^6.4.2",
33-
"vitest": "^3.2.4"
34-
}
35-
}
1+
{
2+
"name": "codeconverter",
3+
"version": "10.0.0",
4+
"private": true,
5+
"type": "module",
6+
"scripts": {
7+
"dev": "vite",
8+
"start": "vite",
9+
"test": "vitest",
10+
"build": "tsc && vite build",
11+
"preview": "vite preview",
12+
"lint": "eslint ./src/"
13+
},
14+
"dependencies": {
15+
"@monaco-editor/react": "^4.7.0",
16+
"axios": "^1.15.0",
17+
"bootstrap": "^5.3.8",
18+
"monaco-editor": "^0.55.1",
19+
"react": "^19.2.4",
20+
"react-dom": "^19.2.4",
21+
"react-router-bootstrap": "^0.26.3",
22+
"react-router-dom": "^7.13.0",
23+
"reactstrap": "^9.2.3"
24+
},
25+
"devDependencies": {
26+
"@types/node": "^22.10.5",
27+
"@types/react": "^19.2.13",
28+
"@types/react-dom": "^19.2.3",
29+
"@vitejs/plugin-react": "^4.3.4",
30+
"jsdom": "^27.0.1",
31+
"typescript": "~5.7.2",
32+
"vite": "^6.4.2",
33+
"vitest": "^3.2.4"
34+
}
35+
}

0 commit comments

Comments
 (0)