Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/NodeApi.DotNetHost/JSMarshaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2203,7 +2203,8 @@ private LambdaExpression BuildConvertFromJSValueExpression(Type toType)
{
statements = new[] { Expression.Default(typeof(ValueTuple)) };
}
else if (genericTypeDefinition?.Name.StartsWith("ValueTuple`") == true)
else if (genericTypeDefinition?.Name.StartsWith(
"ValueTuple`", StringComparison.Ordinal) == true)
{
/*
* new ValueTuple((T1)value[0], (T2)value[1], ...)
Expand Down Expand Up @@ -2323,7 +2324,8 @@ Expression TupleItem(int index) => InlineOrInvoke(
{
statements = BuildFromJSToCollectionClassExpressions(toType, valueParameter);
}
else if (toType.IsGenericType && toType.Name.StartsWith("Tuple`"))
else if (toType.IsGenericType &&
toType.Name.StartsWith("Tuple`", StringComparison.Ordinal))
{
/*
* new Tuple((T1)value[0], (T2)value[1], ...)
Expand Down Expand Up @@ -2527,7 +2529,8 @@ private LambdaExpression BuildConvertToJSValueExpression(Type fromType)
typeof(JSValue)),
};
}
else if (genericTypeDefinition?.Name.StartsWith("ValueTuple`") == true)
else if (genericTypeDefinition?.Name.StartsWith(
"ValueTuple`", StringComparison.Ordinal) == true)
{
/*
* new JSArray(new JSValue[] { (JSValue)value.Item1, (JSValue)value.Item2... })
Expand Down Expand Up @@ -2646,7 +2649,8 @@ Expression TupleItem(int index) => InlineOrInvoke(
statements = BuildToJSFromCollectionClassExpressions(
fromType, variables, valueExpression);
}
else if (fromType.IsGenericType && fromType.Name.StartsWith("Tuple`") == true)
else if (fromType.IsGenericType &&
fromType.Name.StartsWith("Tuple`", StringComparison.Ordinal) == true)
{
/*
* new JSArray(new JSValue[] { (JSValue)value.Item1, (JSValue)value.Item2... })
Expand Down Expand Up @@ -3673,7 +3677,8 @@ private static bool IsTypedArrayType(Type elementType)
private string FullMethodName(MethodInfo method, string? prefix = null)
{
string name = method.Name;
if (name.StartsWith("get_") || name.StartsWith("set_"))
if (name.StartsWith("get_", StringComparison.Ordinal) ||
name.StartsWith("set_", StringComparison.Ordinal))
{
prefix ??= name.Substring(0, 4);
name = name.Substring(4);
Expand Down
16 changes: 10 additions & 6 deletions src/NodeApi.DotNetHost/TypeExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ private static bool IsExtensionTargetTypeSupported(Type targetType, string exten
targetType == typeof(object) ||
targetType == typeof(string) ||
targetType == typeof(Type) ||
targetType.Name == nameof(Task) || targetType.Name.StartsWith(nameof(Task) + '`'))
targetType.Name == nameof(Task) ||
targetType.Name.StartsWith(nameof(Task) + '`', StringComparison.Ordinal))
{
TraceDebug($"Target type '{targetType.FormatName()}' not supported for " +
$"extension method '{extensionMethodName}'.");
Expand All @@ -323,8 +324,9 @@ private static bool IsExtensionTargetTypeSupported(Type targetType, string exten
else if ((targetType.GetInterface(nameof(System.Collections.IEnumerable)) != null &&
(targetType.Namespace == typeof(System.Collections.IEnumerable).Namespace ||
targetType.Namespace == typeof(IEnumerable<>).Namespace)) ||
targetType.Name.StartsWith("IAsyncEnumerable`") ||
targetType.Name == nameof(Tuple) || targetType.Name.StartsWith(nameof(Tuple) + '`'))
targetType.Name.StartsWith("IAsyncEnumerable`", StringComparison.Ordinal) ||
targetType.Name == nameof(Tuple) ||
targetType.Name.StartsWith(nameof(Tuple) + '`', StringComparison.Ordinal))
{
TraceDebug($"Collection target type '{targetType.FormatName()}' not supported for " +
$"extension method '{extensionMethodName}'.");
Expand Down Expand Up @@ -996,9 +998,11 @@ private static bool IsSupportedType(Type type)
if (type.IsPointer ||
type == typeof(void) ||
type.Namespace == "System.Reflection" ||
(type.Namespace?.StartsWith("System.Collections.") == true && !type.IsGenericType) ||
(type.Namespace?.StartsWith("System.Threading.") == true && type != typeof(Task) &&
!(type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Task<>))))
(type.Namespace?.StartsWith("System.Collections.", StringComparison.Ordinal) == true &&
!type.IsGenericType) ||
(type.Namespace?.StartsWith("System.Threading.", StringComparison.Ordinal) == true &&
type != typeof(Task) &&
!(type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Task<>))))
{
return false;
}
Expand Down
12 changes: 6 additions & 6 deletions src/NodeApi.Generator/ExpressionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private static string ToCS(
lambda.Parameters.Select((p) => p.Name!))]),

ParameterExpression parameter =>
(parameter.IsByRef && parameter.Name?.StartsWith(OutParameterPrefix) == true) ?
(parameter.IsByRef && parameter.Name?.StartsWith(OutParameterPrefix, StringComparison.Ordinal) == true) ?
parameter.Name.Substring(OutParameterPrefix.Length) : parameter.Name ?? "_",

BlockExpression block => FormatBlock(block, path, variables),
Expand Down Expand Up @@ -128,27 +128,27 @@ member.Expression is ParameterExpression parameterExpression &&
call.Arguments.Take(call.Arguments.Count - 1), path, variables, "[]") +
" = " + ToCS(call.Arguments.Last(), path, variables) :
#if !STRING_AS_SPAN
call.Method.Name.StartsWith("get_") ?
call.Method.Name.StartsWith("get_", StringComparison.Ordinal) ?
(call.Method.IsStatic ?
FormatType(call.Method.DeclaringType!) +
"." + call.Method.Name.Substring(4):
WithParentheses(call.Object!, path, variables) +
"." + call.Method.Name.Substring(4)) :
call.Method.Name.StartsWith("set_") ?
call.Method.Name.StartsWith("set_", StringComparison.Ordinal) ?
(call.Method.IsStatic ?
FormatType(call.Method.DeclaringType!) +
"." + call.Method.Name.Substring(4) :
WithParentheses(call.Object!, path, variables) +
"." + call.Method.Name.Substring(4)) +
" = " + ToCS(call.Arguments.Single(), path, variables) :
#else
call.Method.Name.StartsWith("get_") ?
call.Method.Name.StartsWith("get_", StringComparison.Ordinal) ?
(call.Method.IsStatic ?
string.Concat(FormatType(call.Method.DeclaringType!),
".", call.Method.Name.AsSpan(4)) :
string.Concat(WithParentheses(call.Object!, path, variables),
".", call.Method.Name.AsSpan(4))) :
call.Method.Name.StartsWith("set_") ?
call.Method.Name.StartsWith("set_", StringComparison.Ordinal) ?
(call.Method.IsStatic ?
string.Concat(FormatType(call.Method.DeclaringType!),
".", call.Method.Name.AsSpan(4)) :
Expand Down Expand Up @@ -215,7 +215,7 @@ private static string ToCS(this ParameterExpression parameter)

if (parameter.IsByRef)
{
if (name.StartsWith(OutParameterPrefix))
if (name.StartsWith(OutParameterPrefix, StringComparison.Ordinal))
{
prefix = "out ";
name = name.Substring(OutParameterPrefix.Length);
Expand Down
2 changes: 1 addition & 1 deletion src/NodeApi.Generator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ private static void ResolveSystemAssemblies(
s_targetFramework = s_targetFramework.Substring(0, s_targetFramework.IndexOf('-'));
}

if (s_targetFramework.StartsWith("net4"))
if (s_targetFramework.StartsWith("net4", StringComparison.Ordinal))
{
if (targetingPacks.Count > 0)
{
Expand Down
3 changes: 2 additions & 1 deletion src/NodeApi.Generator/SourceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ private void AppendLine(string line)
{
IncreaseIndent();
}
else if (line.EndsWith('(') || line.EndsWith('?') || line.EndsWith("=>"))
else if (line.EndsWith('(') || line.EndsWith('?') ||
line.EndsWith("=>", StringComparison.Ordinal))
{
// 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."
Expand Down
4 changes: 2 additions & 2 deletions src/NodeApi.Generator/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ 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 StartsWith(this string s, char c) => s.StartsWith(c.ToString(), StringComparison.Ordinal);

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

#endif
2 changes: 1 addition & 1 deletion src/NodeApi.Generator/SymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ private static Type BuildSymbolicObjectType(
foreach (AttributeData attribute in typeSymbol.GetAttributes())
{
if (attribute.AttributeClass!.ContainingNamespace.ToString()!.StartsWith(
typeof(JSExportAttribute).Namespace!))
typeof(JSExportAttribute).Namespace!, StringComparison.Ordinal))
{
Type attributeType = attribute.AttributeClass.AsType();
ConstructorInfo constructor = attributeType.GetConstructor(
Expand Down
52 changes: 30 additions & 22 deletions src/NodeApi.Generator/TypeDefinitionsGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,8 @@ public TypeDefinitionsGenerator(
_assembly = assembly;
_referenceAssemblies = referenceAssemblies;
_imports = new HashSet<string>();
_isSystemAssembly = assembly.GetName().Name!.StartsWith("System.");
_isSystemAssembly = assembly.GetName().Name!.StartsWith(
"System.", StringComparison.Ordinal);
}

public bool ExportAll { get; set; }
Expand Down Expand Up @@ -1030,7 +1031,8 @@ private static bool HasExplicitInterfaceImplementations(Type type, Type interfac
foreach (MethodInfo method in type.GetMethods(
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly))
{
if (method.IsFinal && method.IsPrivate && method.Name.StartsWith(methodNamePrefix))
if (method.IsFinal && method.IsPrivate &&
method.Name.StartsWith(methodNamePrefix, StringComparison.Ordinal))
{
return true;
}
Expand Down Expand Up @@ -1137,9 +1139,11 @@ private static bool IsExtensionTargetTypeSupported(Type targetType)
(targetType.Namespace == typeof(System.Collections.IEnumerable).Namespace ||
targetType.Namespace == typeof(Collection<>).Namespace ||
targetType.Namespace == typeof(IEnumerable<>).Namespace)) ||
targetType.Name.StartsWith("IAsyncEnumerable`") ||
targetType.Name == nameof(Tuple) || targetType.Name.StartsWith(nameof(Tuple) + '`') ||
targetType.Name == nameof(Task) || targetType.Name.StartsWith(nameof(Task) + '`'))
targetType.Name.StartsWith("IAsyncEnumerable`", StringComparison.Ordinal) ||
targetType.Name == nameof(Tuple) ||
targetType.Name.StartsWith(nameof(Tuple) + '`', StringComparison.Ordinal) ||
targetType.Name == nameof(Task) ||
targetType.Name.StartsWith(nameof(Task) + '`', StringComparison.Ordinal))
{
return false;
}
Expand Down Expand Up @@ -1185,7 +1189,7 @@ private void ExportTypeMember(ref SourceBuilder s, MemberInfo member, bool asExt
string modifiers = (isStatic ? "static " : "") +
(property.SetMethod == null ? "readonly " : "");
string optionalToken = string.Empty;
if (propertyType.EndsWith(UndefinedTypeSuffix))
if (propertyType.EndsWith(UndefinedTypeSuffix, StringComparison.Ordinal))
{
propertyType = propertyType.Substring(
0, propertyType.Length - UndefinedTypeSuffix.Length);
Expand Down Expand Up @@ -1380,9 +1384,10 @@ private bool IsExcluded(MethodBase method)
}

// Exclude old style Begin/End async methods, as they always have Task-based alternatives.
if ((method.Name.StartsWith("Begin") &&
if ((method.Name.StartsWith("Begin", StringComparison.Ordinal) &&
(method as MethodInfo)?.ReturnType.FullName == typeof(IAsyncResult).FullName) ||
(method.Name.StartsWith("End") && methodParams.Length == 1 &&
(method.Name.StartsWith("End", StringComparison.Ordinal) &&
methodParams.Length == 1 &&
methodParams[0].ParameterType.FullName == typeof(IAsyncResult).FullName))
{
return true;
Expand Down Expand Up @@ -1527,13 +1532,13 @@ private string GetTSType(ParameterInfo parameter)
if (parameter.Position < 0 && method != null)
{
if (parameter.ParameterType.FullName == typeof(bool).FullName &&
parameter.Member.Name.StartsWith("Try") &&
parameter.Member.Name.StartsWith("Try", StringComparison.Ordinal) &&
method.GetParameters().Count((p) => p.IsOut) == 1)
{
// A method with Try* pattern simply returns the out-value or undefined
// instead of an object with the bool and out-value properties.
tsType = GetTSType(method.GetParameters().Last());
if (!tsType.EndsWith(UndefinedTypeSuffix))
if (!tsType.EndsWith(UndefinedTypeSuffix, StringComparison.Ordinal))
{
tsType += UndefinedTypeSuffix;
}
Expand All @@ -1549,7 +1554,7 @@ private string GetTSType(ParameterInfo parameter)
{
string propertyType = GetTSType(p);
string optionalToken = string.Empty;
if (propertyType.EndsWith(UndefinedTypeSuffix))
if (propertyType.EndsWith(UndefinedTypeSuffix, StringComparison.Ordinal))
{
propertyType = propertyType.Substring(
0, propertyType.Length - UndefinedTypeSuffix.Length);
Expand Down Expand Up @@ -1721,15 +1726,17 @@ private string GetTSType(
{
tsType = "() => void";
}
else if (type.IsGenericType && type.Name.StartsWith(nameof(Action) + "`"))
else if (type.IsGenericType &&
type.Name.StartsWith(nameof(Action) + "`", StringComparison.Ordinal))
{
NullabilityInfo[]? typeArgsNullability = nullability?.GenericTypeArguments;
string[] parameters = type.GetGenericArguments().Select((t, i) =>
$"arg{i + 1}: {GetTSType(t, typeArgsNullability?[i], allowTypeParams)}")
.ToArray();
tsType = $"({string.Join(", ", parameters)}) => void";
}
else if (type.IsGenericType && type.Name.StartsWith("Func`"))
else if (type.IsGenericType &&
type.Name.StartsWith("Func`", StringComparison.Ordinal))
{
Type[] typeArgs = type.GetGenericArguments();
NullabilityInfo[]? typeArgsNullability = nullability?.GenericTypeArguments;
Expand All @@ -1742,7 +1749,8 @@ private string GetTSType(
allowTypeParams);
tsType = $"({string.Join(", ", parameters)}) => {returnType}";
}
else if (type.IsGenericType && type.Name.StartsWith("Predicate`"))
else if (type.IsGenericType &&
type.Name.StartsWith("Predicate`", StringComparison.Ordinal))
{
Type typeArg = type.GetGenericArguments()[0];
NullabilityInfo[]? typeArgsNullability = nullability?.GenericTypeArguments;
Expand Down Expand Up @@ -1851,7 +1859,7 @@ private string GetTSType(
{
string elementType =
GetTSType(typeArgs[0], typeArgsNullability?[0], allowTypeParams);
if (elementType.EndsWith(UndefinedTypeSuffix))
if (elementType.EndsWith(UndefinedTypeSuffix, StringComparison.Ordinal))
{
elementType = $"({elementType})";
}
Expand All @@ -1862,7 +1870,7 @@ private string GetTSType(
{
string elementType =
GetTSType(typeArgs[1], typeArgsNullability?[0], allowTypeParams);
if (elementType.EndsWith(UndefinedTypeSuffix))
if (elementType.EndsWith(UndefinedTypeSuffix, StringComparison.Ordinal))
{
elementType = $"({elementType})";
}
Expand All @@ -1873,7 +1881,7 @@ private string GetTSType(
{
string elementType =
GetTSType(typeArgs[0], typeArgsNullability?[0], allowTypeParams);
if (elementType.EndsWith(UndefinedTypeSuffix))
if (elementType.EndsWith(UndefinedTypeSuffix, StringComparison.Ordinal))
{
elementType = $"({elementType})";
}
Expand Down Expand Up @@ -1935,8 +1943,8 @@ private string GetTSType(
string valueTSType = GetTSType(typeArgs[1], typeArgsNullability?[1], allowTypeParams);
tsType = $"[{keyTSType}, {valueTSType}]";
}
else if (typeDefinitionName.StartsWith("System.Tuple`") ||
typeDefinitionName.StartsWith("System.ValueTuple`"))
else if (typeDefinitionName.StartsWith("System.Tuple`", StringComparison.Ordinal) ||
typeDefinitionName.StartsWith("System.ValueTuple`", StringComparison.Ordinal))
{
IEnumerable<string> itemTSTypes = typeArgs.Select((typeArg, index) =>
GetTSType(typeArg, typeArgsNullability?[index], allowTypeParams));
Expand All @@ -1960,7 +1968,7 @@ private string GetTSType(
#if !(NETFRAMEWORK || NETSTANDARD)
!type.IsGenericTypeParameter && !type.IsGenericMethodParameter &&
#endif
!tsType.EndsWith(UndefinedTypeSuffix))
!tsType.EndsWith(UndefinedTypeSuffix, StringComparison.Ordinal))
{
tsType += UndefinedTypeSuffix;
}
Expand Down Expand Up @@ -2022,7 +2030,7 @@ static string GetOptionalToken(ParameterInfo parameter, ref string parameterType
{
if (parameter.IsOptional)
{
if (parameterType.EndsWith(UndefinedTypeSuffix))
if (parameterType.EndsWith(UndefinedTypeSuffix, StringComparison.Ordinal))
{
parameterType = parameterType.Substring(
0, parameterType.Length - UndefinedTypeSuffix.Length);
Expand All @@ -2042,7 +2050,7 @@ static string GetOptionalToken(ParameterInfo parameter, ref string parameterType
else if (parameters.Length == 1)
{
string parameterType = GetTSType(parameters[0]);
if (parameterType.StartsWith("..."))
if (parameterType.StartsWith("...", StringComparison.Ordinal))
{
return $"...{TSIdentifier(parameters[0].Name)}: {parameterType.Substring(3)}";
}
Expand Down
3 changes: 2 additions & 1 deletion src/NodeApi/DotNetHost/NativeHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ private JSValue InitializeManagedHost(JSCallbackArgs args)
try
{
JSValue exports;
if (!targetFramework.Contains('.') && targetFramework.StartsWith("net") &&
if (!targetFramework.Contains('.') &&
targetFramework.StartsWith("net", StringComparison.Ordinal) &&
targetFramework.Length >= 5)
{
// .NET Framework
Expand Down
Loading