Skip to content

Commit bf474ba

Browse files
authored
Fix StartsWith/EndsWith issues reported by dotnet format (#463)
1 parent 50d4991 commit bf474ba

16 files changed

+80
-57
lines changed

src/NodeApi.DotNetHost/JSMarshaller.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,7 +2203,8 @@ private LambdaExpression BuildConvertFromJSValueExpression(Type toType)
22032203
{
22042204
statements = new[] { Expression.Default(typeof(ValueTuple)) };
22052205
}
2206-
else if (genericTypeDefinition?.Name.StartsWith("ValueTuple`") == true)
2206+
else if (genericTypeDefinition?.Name.StartsWith(
2207+
"ValueTuple`", StringComparison.Ordinal) == true)
22072208
{
22082209
/*
22092210
* new ValueTuple((T1)value[0], (T2)value[1], ...)
@@ -2323,7 +2324,8 @@ Expression TupleItem(int index) => InlineOrInvoke(
23232324
{
23242325
statements = BuildFromJSToCollectionClassExpressions(toType, valueParameter);
23252326
}
2326-
else if (toType.IsGenericType && toType.Name.StartsWith("Tuple`"))
2327+
else if (toType.IsGenericType &&
2328+
toType.Name.StartsWith("Tuple`", StringComparison.Ordinal))
23272329
{
23282330
/*
23292331
* new Tuple((T1)value[0], (T2)value[1], ...)
@@ -2527,7 +2529,8 @@ private LambdaExpression BuildConvertToJSValueExpression(Type fromType)
25272529
typeof(JSValue)),
25282530
};
25292531
}
2530-
else if (genericTypeDefinition?.Name.StartsWith("ValueTuple`") == true)
2532+
else if (genericTypeDefinition?.Name.StartsWith(
2533+
"ValueTuple`", StringComparison.Ordinal) == true)
25312534
{
25322535
/*
25332536
* new JSArray(new JSValue[] { (JSValue)value.Item1, (JSValue)value.Item2... })
@@ -2646,7 +2649,8 @@ Expression TupleItem(int index) => InlineOrInvoke(
26462649
statements = BuildToJSFromCollectionClassExpressions(
26472650
fromType, variables, valueExpression);
26482651
}
2649-
else if (fromType.IsGenericType && fromType.Name.StartsWith("Tuple`") == true)
2652+
else if (fromType.IsGenericType &&
2653+
fromType.Name.StartsWith("Tuple`", StringComparison.Ordinal) == true)
26502654
{
26512655
/*
26522656
* new JSArray(new JSValue[] { (JSValue)value.Item1, (JSValue)value.Item2... })
@@ -3673,7 +3677,8 @@ private static bool IsTypedArrayType(Type elementType)
36733677
private string FullMethodName(MethodInfo method, string? prefix = null)
36743678
{
36753679
string name = method.Name;
3676-
if (name.StartsWith("get_") || name.StartsWith("set_"))
3680+
if (name.StartsWith("get_", StringComparison.Ordinal) ||
3681+
name.StartsWith("set_", StringComparison.Ordinal))
36773682
{
36783683
prefix ??= name.Substring(0, 4);
36793684
name = name.Substring(4);

src/NodeApi.DotNetHost/TypeExporter.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,8 @@ private static bool IsExtensionTargetTypeSupported(Type targetType, string exten
308308
targetType == typeof(object) ||
309309
targetType == typeof(string) ||
310310
targetType == typeof(Type) ||
311-
targetType.Name == nameof(Task) || targetType.Name.StartsWith(nameof(Task) + '`'))
311+
targetType.Name == nameof(Task) ||
312+
targetType.Name.StartsWith(nameof(Task) + '`', StringComparison.Ordinal))
312313
{
313314
TraceDebug($"Target type '{targetType.FormatName()}' not supported for " +
314315
$"extension method '{extensionMethodName}'.");
@@ -323,8 +324,9 @@ private static bool IsExtensionTargetTypeSupported(Type targetType, string exten
323324
else if ((targetType.GetInterface(nameof(System.Collections.IEnumerable)) != null &&
324325
(targetType.Namespace == typeof(System.Collections.IEnumerable).Namespace ||
325326
targetType.Namespace == typeof(IEnumerable<>).Namespace)) ||
326-
targetType.Name.StartsWith("IAsyncEnumerable`") ||
327-
targetType.Name == nameof(Tuple) || targetType.Name.StartsWith(nameof(Tuple) + '`'))
327+
targetType.Name.StartsWith("IAsyncEnumerable`", StringComparison.Ordinal) ||
328+
targetType.Name == nameof(Tuple) ||
329+
targetType.Name.StartsWith(nameof(Tuple) + '`', StringComparison.Ordinal))
328330
{
329331
TraceDebug($"Collection target type '{targetType.FormatName()}' not supported for " +
330332
$"extension method '{extensionMethodName}'.");
@@ -996,9 +998,11 @@ private static bool IsSupportedType(Type type)
996998
if (type.IsPointer ||
997999
type == typeof(void) ||
9981000
type.Namespace == "System.Reflection" ||
999-
(type.Namespace?.StartsWith("System.Collections.") == true && !type.IsGenericType) ||
1000-
(type.Namespace?.StartsWith("System.Threading.") == true && type != typeof(Task) &&
1001-
!(type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Task<>))))
1001+
(type.Namespace?.StartsWith("System.Collections.", StringComparison.Ordinal) == true &&
1002+
!type.IsGenericType) ||
1003+
(type.Namespace?.StartsWith("System.Threading.", StringComparison.Ordinal) == true &&
1004+
type != typeof(Task) &&
1005+
!(type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Task<>))))
10021006
{
10031007
return false;
10041008
}

src/NodeApi.Generator/ExpressionExtensions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private static string ToCS(
6363
lambda.Parameters.Select((p) => p.Name!))]),
6464

6565
ParameterExpression parameter =>
66-
(parameter.IsByRef && parameter.Name?.StartsWith(OutParameterPrefix) == true) ?
66+
(parameter.IsByRef && parameter.Name?.StartsWith(OutParameterPrefix, StringComparison.Ordinal) == true) ?
6767
parameter.Name.Substring(OutParameterPrefix.Length) : parameter.Name ?? "_",
6868

6969
BlockExpression block => FormatBlock(block, path, variables),
@@ -128,27 +128,27 @@ member.Expression is ParameterExpression parameterExpression &&
128128
call.Arguments.Take(call.Arguments.Count - 1), path, variables, "[]") +
129129
" = " + ToCS(call.Arguments.Last(), path, variables) :
130130
#if !STRING_AS_SPAN
131-
call.Method.Name.StartsWith("get_") ?
131+
call.Method.Name.StartsWith("get_", StringComparison.Ordinal) ?
132132
(call.Method.IsStatic ?
133133
FormatType(call.Method.DeclaringType!) +
134134
"." + call.Method.Name.Substring(4):
135135
WithParentheses(call.Object!, path, variables) +
136136
"." + call.Method.Name.Substring(4)) :
137-
call.Method.Name.StartsWith("set_") ?
137+
call.Method.Name.StartsWith("set_", StringComparison.Ordinal) ?
138138
(call.Method.IsStatic ?
139139
FormatType(call.Method.DeclaringType!) +
140140
"." + call.Method.Name.Substring(4) :
141141
WithParentheses(call.Object!, path, variables) +
142142
"." + call.Method.Name.Substring(4)) +
143143
" = " + ToCS(call.Arguments.Single(), path, variables) :
144144
#else
145-
call.Method.Name.StartsWith("get_") ?
145+
call.Method.Name.StartsWith("get_", StringComparison.Ordinal) ?
146146
(call.Method.IsStatic ?
147147
string.Concat(FormatType(call.Method.DeclaringType!),
148148
".", call.Method.Name.AsSpan(4)) :
149149
string.Concat(WithParentheses(call.Object!, path, variables),
150150
".", call.Method.Name.AsSpan(4))) :
151-
call.Method.Name.StartsWith("set_") ?
151+
call.Method.Name.StartsWith("set_", StringComparison.Ordinal) ?
152152
(call.Method.IsStatic ?
153153
string.Concat(FormatType(call.Method.DeclaringType!),
154154
".", call.Method.Name.AsSpan(4)) :
@@ -215,7 +215,7 @@ private static string ToCS(this ParameterExpression parameter)
215215

216216
if (parameter.IsByRef)
217217
{
218-
if (name.StartsWith(OutParameterPrefix))
218+
if (name.StartsWith(OutParameterPrefix, StringComparison.Ordinal))
219219
{
220220
prefix = "out ";
221221
name = name.Substring(OutParameterPrefix.Length);

src/NodeApi.Generator/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ private static void ResolveSystemAssemblies(
417417
s_targetFramework = s_targetFramework.Substring(0, s_targetFramework.IndexOf('-'));
418418
}
419419

420-
if (s_targetFramework.StartsWith("net4"))
420+
if (s_targetFramework.StartsWith("net4", StringComparison.Ordinal))
421421
{
422422
if (targetingPacks.Count > 0)
423423
{

src/NodeApi.Generator/SourceBuilder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ private void AppendLine(string line)
8787
{
8888
IncreaseIndent();
8989
}
90-
else if (line.EndsWith('(') || line.EndsWith('?') || line.EndsWith("=>"))
90+
else if (line.EndsWith('(') || line.EndsWith('?') ||
91+
line.EndsWith("=>", StringComparison.Ordinal))
9192
{
9293
// The "extra" indent persists until the end of the set of lines appended together
9394
// (before the split) or until a line ending with a semicolon."

src/NodeApi.Generator/StringExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ internal static class StringExtensions
1313
{
1414
public static bool Contains(this string s, char c) => s.Contains(c.ToString());
1515

16-
public static bool StartsWith(this string s, char c) => s.StartsWith(c.ToString());
16+
public static bool StartsWith(this string s, char c) => s.StartsWith(c.ToString(), StringComparison.Ordinal);
1717

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

2121
#endif

src/NodeApi.Generator/SymbolExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ private static Type BuildSymbolicObjectType(
329329
foreach (AttributeData attribute in typeSymbol.GetAttributes())
330330
{
331331
if (attribute.AttributeClass!.ContainingNamespace.ToString()!.StartsWith(
332-
typeof(JSExportAttribute).Namespace!))
332+
typeof(JSExportAttribute).Namespace!, StringComparison.Ordinal))
333333
{
334334
Type attributeType = attribute.AttributeClass.AsType();
335335
ConstructorInfo constructor = attributeType.GetConstructor(

src/NodeApi.Generator/TypeDefinitionsGenerator.cs

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,8 @@ public TypeDefinitionsGenerator(
375375
_assembly = assembly;
376376
_referenceAssemblies = referenceAssemblies;
377377
_imports = new HashSet<string>();
378-
_isSystemAssembly = assembly.GetName().Name!.StartsWith("System.");
378+
_isSystemAssembly = assembly.GetName().Name!.StartsWith(
379+
"System.", StringComparison.Ordinal);
379380
}
380381

381382
public bool ExportAll { get; set; }
@@ -1030,7 +1031,8 @@ private static bool HasExplicitInterfaceImplementations(Type type, Type interfac
10301031
foreach (MethodInfo method in type.GetMethods(
10311032
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly))
10321033
{
1033-
if (method.IsFinal && method.IsPrivate && method.Name.StartsWith(methodNamePrefix))
1034+
if (method.IsFinal && method.IsPrivate &&
1035+
method.Name.StartsWith(methodNamePrefix, StringComparison.Ordinal))
10341036
{
10351037
return true;
10361038
}
@@ -1137,9 +1139,11 @@ private static bool IsExtensionTargetTypeSupported(Type targetType)
11371139
(targetType.Namespace == typeof(System.Collections.IEnumerable).Namespace ||
11381140
targetType.Namespace == typeof(Collection<>).Namespace ||
11391141
targetType.Namespace == typeof(IEnumerable<>).Namespace)) ||
1140-
targetType.Name.StartsWith("IAsyncEnumerable`") ||
1141-
targetType.Name == nameof(Tuple) || targetType.Name.StartsWith(nameof(Tuple) + '`') ||
1142-
targetType.Name == nameof(Task) || targetType.Name.StartsWith(nameof(Task) + '`'))
1142+
targetType.Name.StartsWith("IAsyncEnumerable`", StringComparison.Ordinal) ||
1143+
targetType.Name == nameof(Tuple) ||
1144+
targetType.Name.StartsWith(nameof(Tuple) + '`', StringComparison.Ordinal) ||
1145+
targetType.Name == nameof(Task) ||
1146+
targetType.Name.StartsWith(nameof(Task) + '`', StringComparison.Ordinal))
11431147
{
11441148
return false;
11451149
}
@@ -1185,7 +1189,7 @@ private void ExportTypeMember(ref SourceBuilder s, MemberInfo member, bool asExt
11851189
string modifiers = (isStatic ? "static " : "") +
11861190
(property.SetMethod == null ? "readonly " : "");
11871191
string optionalToken = string.Empty;
1188-
if (propertyType.EndsWith(UndefinedTypeSuffix))
1192+
if (propertyType.EndsWith(UndefinedTypeSuffix, StringComparison.Ordinal))
11891193
{
11901194
propertyType = propertyType.Substring(
11911195
0, propertyType.Length - UndefinedTypeSuffix.Length);
@@ -1380,9 +1384,10 @@ private bool IsExcluded(MethodBase method)
13801384
}
13811385

13821386
// Exclude old style Begin/End async methods, as they always have Task-based alternatives.
1383-
if ((method.Name.StartsWith("Begin") &&
1387+
if ((method.Name.StartsWith("Begin", StringComparison.Ordinal) &&
13841388
(method as MethodInfo)?.ReturnType.FullName == typeof(IAsyncResult).FullName) ||
1385-
(method.Name.StartsWith("End") && methodParams.Length == 1 &&
1389+
(method.Name.StartsWith("End", StringComparison.Ordinal) &&
1390+
methodParams.Length == 1 &&
13861391
methodParams[0].ParameterType.FullName == typeof(IAsyncResult).FullName))
13871392
{
13881393
return true;
@@ -1527,13 +1532,13 @@ private string GetTSType(ParameterInfo parameter)
15271532
if (parameter.Position < 0 && method != null)
15281533
{
15291534
if (parameter.ParameterType.FullName == typeof(bool).FullName &&
1530-
parameter.Member.Name.StartsWith("Try") &&
1535+
parameter.Member.Name.StartsWith("Try", StringComparison.Ordinal) &&
15311536
method.GetParameters().Count((p) => p.IsOut) == 1)
15321537
{
15331538
// A method with Try* pattern simply returns the out-value or undefined
15341539
// instead of an object with the bool and out-value properties.
15351540
tsType = GetTSType(method.GetParameters().Last());
1536-
if (!tsType.EndsWith(UndefinedTypeSuffix))
1541+
if (!tsType.EndsWith(UndefinedTypeSuffix, StringComparison.Ordinal))
15371542
{
15381543
tsType += UndefinedTypeSuffix;
15391544
}
@@ -1549,7 +1554,7 @@ private string GetTSType(ParameterInfo parameter)
15491554
{
15501555
string propertyType = GetTSType(p);
15511556
string optionalToken = string.Empty;
1552-
if (propertyType.EndsWith(UndefinedTypeSuffix))
1557+
if (propertyType.EndsWith(UndefinedTypeSuffix, StringComparison.Ordinal))
15531558
{
15541559
propertyType = propertyType.Substring(
15551560
0, propertyType.Length - UndefinedTypeSuffix.Length);
@@ -1721,15 +1726,17 @@ private string GetTSType(
17211726
{
17221727
tsType = "() => void";
17231728
}
1724-
else if (type.IsGenericType && type.Name.StartsWith(nameof(Action) + "`"))
1729+
else if (type.IsGenericType &&
1730+
type.Name.StartsWith(nameof(Action) + "`", StringComparison.Ordinal))
17251731
{
17261732
NullabilityInfo[]? typeArgsNullability = nullability?.GenericTypeArguments;
17271733
string[] parameters = type.GetGenericArguments().Select((t, i) =>
17281734
$"arg{i + 1}: {GetTSType(t, typeArgsNullability?[i], allowTypeParams)}")
17291735
.ToArray();
17301736
tsType = $"({string.Join(", ", parameters)}) => void";
17311737
}
1732-
else if (type.IsGenericType && type.Name.StartsWith("Func`"))
1738+
else if (type.IsGenericType &&
1739+
type.Name.StartsWith("Func`", StringComparison.Ordinal))
17331740
{
17341741
Type[] typeArgs = type.GetGenericArguments();
17351742
NullabilityInfo[]? typeArgsNullability = nullability?.GenericTypeArguments;
@@ -1742,7 +1749,8 @@ private string GetTSType(
17421749
allowTypeParams);
17431750
tsType = $"({string.Join(", ", parameters)}) => {returnType}";
17441751
}
1745-
else if (type.IsGenericType && type.Name.StartsWith("Predicate`"))
1752+
else if (type.IsGenericType &&
1753+
type.Name.StartsWith("Predicate`", StringComparison.Ordinal))
17461754
{
17471755
Type typeArg = type.GetGenericArguments()[0];
17481756
NullabilityInfo[]? typeArgsNullability = nullability?.GenericTypeArguments;
@@ -1851,7 +1859,7 @@ private string GetTSType(
18511859
{
18521860
string elementType =
18531861
GetTSType(typeArgs[0], typeArgsNullability?[0], allowTypeParams);
1854-
if (elementType.EndsWith(UndefinedTypeSuffix))
1862+
if (elementType.EndsWith(UndefinedTypeSuffix, StringComparison.Ordinal))
18551863
{
18561864
elementType = $"({elementType})";
18571865
}
@@ -1862,7 +1870,7 @@ private string GetTSType(
18621870
{
18631871
string elementType =
18641872
GetTSType(typeArgs[1], typeArgsNullability?[0], allowTypeParams);
1865-
if (elementType.EndsWith(UndefinedTypeSuffix))
1873+
if (elementType.EndsWith(UndefinedTypeSuffix, StringComparison.Ordinal))
18661874
{
18671875
elementType = $"({elementType})";
18681876
}
@@ -1873,7 +1881,7 @@ private string GetTSType(
18731881
{
18741882
string elementType =
18751883
GetTSType(typeArgs[0], typeArgsNullability?[0], allowTypeParams);
1876-
if (elementType.EndsWith(UndefinedTypeSuffix))
1884+
if (elementType.EndsWith(UndefinedTypeSuffix, StringComparison.Ordinal))
18771885
{
18781886
elementType = $"({elementType})";
18791887
}
@@ -1935,8 +1943,8 @@ private string GetTSType(
19351943
string valueTSType = GetTSType(typeArgs[1], typeArgsNullability?[1], allowTypeParams);
19361944
tsType = $"[{keyTSType}, {valueTSType}]";
19371945
}
1938-
else if (typeDefinitionName.StartsWith("System.Tuple`") ||
1939-
typeDefinitionName.StartsWith("System.ValueTuple`"))
1946+
else if (typeDefinitionName.StartsWith("System.Tuple`", StringComparison.Ordinal) ||
1947+
typeDefinitionName.StartsWith("System.ValueTuple`", StringComparison.Ordinal))
19401948
{
19411949
IEnumerable<string> itemTSTypes = typeArgs.Select((typeArg, index) =>
19421950
GetTSType(typeArg, typeArgsNullability?[index], allowTypeParams));
@@ -1960,7 +1968,7 @@ private string GetTSType(
19601968
#if !(NETFRAMEWORK || NETSTANDARD)
19611969
!type.IsGenericTypeParameter && !type.IsGenericMethodParameter &&
19621970
#endif
1963-
!tsType.EndsWith(UndefinedTypeSuffix))
1971+
!tsType.EndsWith(UndefinedTypeSuffix, StringComparison.Ordinal))
19641972
{
19651973
tsType += UndefinedTypeSuffix;
19661974
}
@@ -2022,7 +2030,7 @@ static string GetOptionalToken(ParameterInfo parameter, ref string parameterType
20222030
{
20232031
if (parameter.IsOptional)
20242032
{
2025-
if (parameterType.EndsWith(UndefinedTypeSuffix))
2033+
if (parameterType.EndsWith(UndefinedTypeSuffix, StringComparison.Ordinal))
20262034
{
20272035
parameterType = parameterType.Substring(
20282036
0, parameterType.Length - UndefinedTypeSuffix.Length);
@@ -2042,7 +2050,7 @@ static string GetOptionalToken(ParameterInfo parameter, ref string parameterType
20422050
else if (parameters.Length == 1)
20432051
{
20442052
string parameterType = GetTSType(parameters[0]);
2045-
if (parameterType.StartsWith("..."))
2053+
if (parameterType.StartsWith("...", StringComparison.Ordinal))
20462054
{
20472055
return $"...{TSIdentifier(parameters[0].Name)}: {parameterType.Substring(3)}";
20482056
}

src/NodeApi/DotNetHost/NativeHost.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ private JSValue InitializeManagedHost(JSCallbackArgs args)
122122
try
123123
{
124124
JSValue exports;
125-
if (!targetFramework.Contains('.') && targetFramework.StartsWith("net") &&
125+
if (!targetFramework.Contains('.') &&
126+
targetFramework.StartsWith("net", StringComparison.Ordinal) &&
126127
targetFramework.Length >= 5)
127128
{
128129
// .NET Framework

0 commit comments

Comments
 (0)