Skip to content

Commit

Permalink
Patch: Generate Port.OnMessage type as OnMessageEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
mingyaulee committed Sep 11, 2024
1 parent 93ab5cf commit bf972e8
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ namespace WebExtensions.Net.Generator.ClrTypeTranslators
public class PropertyDefinitionTranslator
{
private readonly ClrTypeStore clrTypeStore;
private readonly ClassTranslationOptions classTranslationOptions;

public PropertyDefinitionTranslator(ClrTypeStore clrTypeStore)
public PropertyDefinitionTranslator(ClrTypeStore clrTypeStore, ClassTranslationOptions classTranslationOptions)
{
this.clrTypeStore = clrTypeStore;
this.classTranslationOptions = classTranslationOptions;
}

public ClrPropertyInfo TranslatePropertyDefinition(string propertyName, PropertyDefinition propertyDefinition, NamespaceEntity namespaceEntity, ClrTypeInfo clrTypeInfo)
Expand All @@ -33,6 +35,15 @@ public ClrPropertyInfo TranslatePropertyDefinition(string propertyName, Property
propertyType = propertyType.MakeNullable();
}

if (classTranslationOptions.ReplacePropertyTypes is not null &&
classTranslationOptions.ReplacePropertyTypes.TryGetValue($"{clrTypeInfo.CSharpName}.{propertyName}", out var replacePropertyType))
{
propertyType = clrTypeStore.GetClrType(new TypeReference()
{
Ref = replacePropertyType
}, new NamespaceEntity(null, string.Empty, string.Empty));
}

clrTypeInfo.AddRequiredNamespaces(propertyType.ReferenceNamespaces);

if (propertyName.Equals(clrTypeInfo.CSharpName, StringComparison.OrdinalIgnoreCase))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class ClassTranslationOptions
public IDictionary<string, string> NamespaceAliases { get; set; }
public IDictionary<string, string> Aliases { get; set; }
public IDictionary<string, string> ReplaceNames { get; set; }
public IDictionary<string, string> ReplacePropertyTypes { get; init; }
#pragma warning restore CS8618
}
}
3 changes: 3 additions & 0 deletions src/WebExtensions.Net.Generator/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@
"ReplaceNames": {
"URL": "Url",
"DOM": "Dom"
},
"ReplacePropertyTypes": {
"Port.onMessage": "Runtime.OnMessageEvent"
}
}
}
2 changes: 1 addition & 1 deletion src/WebExtensions.Net/Generated/Runtime/Port.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public partial class Port : BaseObject
[JsAccessPath("onMessage")]
[JsonPropertyName("onMessage")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public Event OnMessage { get; set; }
public OnMessageEvent OnMessage { get; set; }

/// <summary>This property will 'b'only'/b' be present on ports passed to onConnect/onConnectExternal listeners.</summary>
[JsAccessPath("sender")]
Expand Down
2 changes: 1 addition & 1 deletion src/WebExtensions.Net/Generated/generated.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
This file is auto generated at 2024-06-21T23:17:36
This file is auto generated at 2024-09-11T14:47:52

0 comments on commit bf972e8

Please sign in to comment.