Skip to content

Commit

Permalink
Clean up analyzer / formatting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jasongin committed Sep 19, 2023
1 parent 20c5955 commit b6916bd
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 55 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_parentheses = false
csharp_space_between_square_brackets = false

# ThrowIf* APIs are not available in .NET Framework
dotnet_diagnostic.CA1510.severity = none
dotnet_diagnostic.CA1513.severity = none

# License header
file_header_template = Copyright (c) Microsoft Corporation.\nLicensed under the MIT License.

Expand Down
17 changes: 10 additions & 7 deletions src/NodeApi.DotNetHost/JSMarshaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
using System.Runtime.Loader;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -835,7 +834,7 @@ public LambdaExpression BuildFromJSFunctionExpression(MethodInfo method)
}

return Expression.Lambda(
_delegates.Value.GetFromJSDelegateType(method.DeclaringType!),
JSMarshallerDelegates.GetFromJSDelegateType(method.DeclaringType!),
body: Expression.Block(typeof(JSValue), variables, statements),
$"from_{FullMethodName(method)}",
parameters: new[] { thisParameter, s_argsParameter });
Expand Down Expand Up @@ -2465,7 +2464,7 @@ private LambdaExpression BuildConvertToJSPromiseExpression(Type fromType)

private IEnumerable<Expression> BuildFromJSToArrayExpressions(
Type elementType,
ICollection<ParameterExpression> variables,
List<ParameterExpression> variables,
ParameterExpression valueVariable)
{
/*
Expand Down Expand Up @@ -2509,7 +2508,7 @@ private IEnumerable<Expression> BuildFromJSToArrayExpressions(

private IEnumerable<Expression> BuildToJSFromArrayExpressions(
Type elementType,
ICollection<ParameterExpression> variables,
List<ParameterExpression> variables,
Expression valueExpression)
{
/*
Expand Down Expand Up @@ -2542,7 +2541,7 @@ private IEnumerable<Expression> BuildToJSFromArrayExpressions(

private IEnumerable<Expression> BuildFromJSToStructExpressions(
Type toType,
ICollection<ParameterExpression> variables,
List<ParameterExpression> variables,
ParameterExpression valueVariable)
{
/*
Expand Down Expand Up @@ -2578,7 +2577,7 @@ private IEnumerable<Expression> BuildFromJSToStructExpressions(

private IEnumerable<Expression> BuildToJSFromStructExpressions(
Type fromType,
ICollection<ParameterExpression> variables,
List<ParameterExpression> variables,
Expression valueExpression)
{
/*
Expand Down Expand Up @@ -3153,10 +3152,14 @@ internal static AssemblyBuilder CreateAssemblyBuilder(Type forType)
{
string assemblyName = forType.FullName + "_" + Environment.CurrentManagedThreadId;

#if NETFRAMEWORK
bool collectible = false;
#else
// Make the dynamic assembly collectible if in a collectible load context.
// The delegate types generated by lambda expressions are not collectible by default;
// the custom marshalling delegates resolve that problem.
bool collectible = AssemblyLoadContext.Default.IsCollectible;
bool collectible = System.Runtime.Loader.AssemblyLoadContext.Default.IsCollectible;
#endif

return AssemblyBuilder.DefineDynamicAssembly(
new AssemblyName(assemblyName),
Expand Down
60 changes: 28 additions & 32 deletions src/NodeApi.DotNetHost/JSMarshallerDelegates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public JSMarshallerDelegates()

private delegate JSValue FromJS<T>(T thisParameter, JSCallbackArgs args);

public Type GetFromJSDelegateType(Type thisParameterType)
public static Type GetFromJSDelegateType(Type thisParameterType)
{
return typeof(FromJS<>).MakeGenericType(thisParameterType);
}
Expand Down Expand Up @@ -84,57 +84,53 @@ public Type GetToJSDelegateType(Type returnType, params ParameterExpression[] pa

if (returnType == typeof(void))
{
switch (parameters.Length)
return parameters.Length switch
{
case 0: return typeof(ToJSVoid);
case 1: return typeof(ToJSVoid<>).MakeGenericType(parameters[0].Type);
case 2: return typeof(ToJSVoid<,>).MakeGenericType(
parameters[0].Type, parameters[1].Type);
case 3: return typeof(ToJSVoid<,,>).MakeGenericType(
parameters[0].Type, parameters[1].Type, parameters[2].Type);
case 4: return typeof(ToJSVoid<,,,>).MakeGenericType(
parameters[0].Type,
parameters[1].Type,
parameters[2].Type,
parameters[3].Type);
case 5: return typeof(ToJSVoid<,,,,>).MakeGenericType(
0 => typeof(ToJSVoid),
1 => typeof(ToJSVoid<>).MakeGenericType(parameters[0].Type),
2 => typeof(ToJSVoid<,>).MakeGenericType(parameters[0].Type, parameters[1].Type),
3 => typeof(ToJSVoid<,,>).MakeGenericType(
parameters[0].Type, parameters[1].Type, parameters[2].Type),
4 => typeof(ToJSVoid<,,,>).MakeGenericType(
parameters[0].Type, parameters[1].Type, parameters[2].Type, parameters[3].Type),
5 => typeof(ToJSVoid<,,,,>).MakeGenericType(
parameters[0].Type,
parameters[1].Type,
parameters[2].Type,
parameters[3].Type,
parameters[4].Type);
default: throw new NotSupportedException("Method has too many parameters.");
}
parameters[4].Type),
_ => throw new NotSupportedException("Method has too many parameters."),
};
}

switch (parameters.Length)
return parameters.Length switch
{
case 0: return typeof(ToJS<>).MakeGenericType(returnType);
case 1: return typeof(ToJS<,>).MakeGenericType(parameters[0].Type, returnType);
case 2: return typeof(ToJS<,,>).MakeGenericType(
parameters[0].Type, parameters[1].Type, returnType);
case 3: return typeof(ToJS<,,,>).MakeGenericType(
parameters[0].Type, parameters[1].Type, parameters[2].Type, returnType);
case 4: return typeof(ToJS<,,,,>).MakeGenericType(
0 => typeof(ToJS<>).MakeGenericType(returnType),
1 => typeof(ToJS<,>).MakeGenericType(parameters[0].Type, returnType),
2 => typeof(ToJS<,,>).MakeGenericType(
parameters[0].Type, parameters[1].Type, returnType),
3 => typeof(ToJS<,,,>).MakeGenericType(
parameters[0].Type, parameters[1].Type, parameters[2].Type, returnType),
4 => typeof(ToJS<,,,,>).MakeGenericType(
parameters[0].Type,
parameters[1].Type,
parameters[2].Type,
parameters[3].Type,
returnType);
case 5: return typeof(ToJS<,,,,,>).MakeGenericType(
returnType),
5 => typeof(ToJS<,,,,,>).MakeGenericType(
parameters[0].Type,
parameters[1].Type,
parameters[2].Type,
parameters[3].Type,
parameters[4].Type,
returnType);
default: throw new NotSupportedException("Method has too many parameters.");
}
returnType),
_ => throw new NotSupportedException("Method has too many parameters."),
};
}

private static readonly Type[] s_delegateCtorSignature = { typeof(object), typeof(IntPtr) };

private Type MakeCustomDelegate(Type[] parameterTypes, Type returnType)
private TypeInfo MakeCustomDelegate(Type[] parameterTypes, Type returnType)
{
// TODO: Consider caching custom delegate types?

Expand Down Expand Up @@ -169,4 +165,4 @@ private Type MakeCustomDelegate(Type[] parameterTypes, Type returnType)
.SetImplementationFlags(implAttributes);
return builder.CreateTypeInfo()!;
}
}
}
4 changes: 2 additions & 2 deletions src/NodeApi.Generator/ExpressionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ private static string WithParentheses(
{
string cs = ToCS(expression, path, variables);

if (cs.StartsWith("(") &&
if (cs.StartsWith('(') &&
(expression.NodeType == ExpressionType.TypeAs ||
expression.NodeType == ExpressionType.Convert ||
expression.NodeType == ExpressionType.Call ||
Expand Down Expand Up @@ -278,7 +278,7 @@ private static string FormatStatement(

s += ToCS(expression, path, variables);

if (!s.EndsWith("}"))
if (!s.EndsWith('}'))
{
s += ';';
}
Expand Down
2 changes: 1 addition & 1 deletion src/NodeApi.Generator/ModuleGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ private IEnumerable<ISymbol> GetModuleExportItems()
/// <param name="moduleInitializer">Optional custom module class or module initialization method.</param>
/// <param name="exportItems">Enumeration of all exported types and functions (static methods).</param>
/// <returns>The generated source.</returns>
private SourceText GenerateModuleInitializer(
private SourceBuilder GenerateModuleInitializer(
ISymbol? moduleInitializer,
IEnumerable<ISymbol> exportItems)
{
Expand Down
14 changes: 5 additions & 9 deletions src/NodeApi.Generator/SourceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@ public void Insert(int index, string text)

private void AppendLine(string line)
{
#if NETFRAMEWORK
if (line.Contains("\n"))
#else
if (line.Contains('\n'))
#endif
{
foreach (string singleLine in line.Split('\n'))
{
Expand All @@ -71,11 +67,11 @@ private void AppendLine(string line)
return;
}

if (line.StartsWith("}"))
if (line.StartsWith('}'))
{
DecreaseIndent();
}
else if (line.StartsWith("{") || line.StartsWith(")"))
else if (line.StartsWith('{') || line.StartsWith(')'))
{
ResetExtraIndent();
}
Expand All @@ -87,17 +83,17 @@ private void AppendLine(string line)

_text.AppendLine(line);

if (line.EndsWith("{"))
if (line.EndsWith('{'))
{
IncreaseIndent();
}
else if (line.EndsWith("(") || line.EndsWith("?") || line.EndsWith("=>"))
else if (line.EndsWith('(') || line.EndsWith('?') || line.EndsWith("=>"))
{
// The "extra" indent persists until the end of the set of lines appended together
// (before the split) or until a line ending with a semicolon."
IncreaseExtraIndent();
}
else if (line.EndsWith(";"))
else if (line.EndsWith(';'))
{
ResetExtraIndent();
}
Expand Down
21 changes: 21 additions & 0 deletions src/NodeApi.Generator/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#if NETFRAMEWORK

using System;

/// <summary>
/// Fills in extension methods for the <see cref="string" /> class that are not present
/// in .NET Framework.
/// </summary>
internal static class StringExtensions
{
public static bool Contains(this string s, char c) => s.Contains(c.ToString());

public static bool StartsWith(this string s, char c) => s.StartsWith(c.ToString());

public static bool EndsWith(this string s, char c) => s.EndsWith(c.ToString());
}

#endif
2 changes: 1 addition & 1 deletion src/NodeApi.Generator/SymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private static ModuleBuilder ModuleBuilder
}
}

private static IDictionary<string, Type> SymbolicTypes
private static Dictionary<string, Type> SymbolicTypes
{
get
{
Expand Down
3 changes: 1 addition & 2 deletions src/NodeApi.Generator/TypeDefinitionsGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -117,7 +116,7 @@ public enum ModuleType

private readonly Assembly _assembly;
private readonly IDictionary<string, Assembly> _referenceAssemblies;
private readonly ISet<string> _imports;
private readonly HashSet<string> _imports;
private readonly XDocument? _assemblyDoc;
private bool _exportAll;
private bool _autoCamelCase;
Expand Down
2 changes: 1 addition & 1 deletion src/NodeApi/JSDispatcherQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ internal void ValidateNoLock()
}
}

private IDisposable CreateDeferral()
private Deferral CreateDeferral()
{
IncrementDeferralCount();
return new Deferral(DecrementDeferralCount);
Expand Down

0 comments on commit b6916bd

Please sign in to comment.