Skip to content

Commit c565793

Browse files
authored
Enable Meziantou.Analyzer (#1682)
1 parent 088f08f commit c565793

File tree

72 files changed

+257
-190
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+257
-190
lines changed

.editorconfig

+27
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,33 @@ dotnet_diagnostic.CA1710.severity = none
5151
# Error CA1716: Identifiers should have correct suffix
5252
dotnet_diagnostic.CA1716.severity = none
5353

54+
# Error MA0026: TODO
55+
dotnet_diagnostic.MA0026.severity = none
56+
57+
# Error MA0048 : File name must match type name
58+
dotnet_diagnostic.MA0048.severity = none
59+
60+
# Error MA0016 : Prefer using collection abstraction instead of implementation
61+
dotnet_diagnostic.MA0016.severity = none
62+
63+
# Error MA0017 : Abstract types should not have public or internal constructors
64+
dotnet_diagnostic.MA0017.severity = none
65+
66+
# Error MA0051 : Method is too long
67+
dotnet_diagnostic.MA0051.severity = none
68+
69+
# Error MA0046 : The delegate must return void
70+
dotnet_diagnostic.MA0046.severity = none
71+
72+
# Error MA0097 : A class that implements IComparable<T> or IComparable should override comparison operators
73+
dotnet_diagnostic.MA0097.severity = none
74+
75+
# Error MA0025 : Implement the functionality (or raise NotSupportedException or PlatformNotSupportedException)
76+
dotnet_diagnostic.MA0025.severity = none
77+
78+
# Error MA0091 : Sender parameter should be 'this' for instance events
79+
dotnet_diagnostic.MA0091.severity = none
80+
5481
# Sort using and Import directives with System.* appearing first
5582
dotnet_sort_system_directives_first = true
5683
dotnet_separate_import_directive_groups = false

Directory.Packages.props

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<PackageVersion Include="Esprima" Version="3.0.2" />
99
<PackageVersion Include="Flurl.Http.Signed" Version="3.2.4" />
1010
<PackageVersion Include="Jurassic" Version="3.2.7" />
11+
<PackageVersion Include="Meziantou.Analyzer" Version="2.0.106" />
1112
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
1213
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
1314
<PackageVersion Include="MongoDB.Bson.signed" Version="2.19.0" />

Jint/Collections/DictionarySlim.cs

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Collections;
88
using System.Diagnostics;
99
using System.Runtime.CompilerServices;
10+
using System.Runtime.InteropServices;
1011

1112
namespace Jint.Collections
1213
{
@@ -32,6 +33,7 @@ internal class DictionarySlim<TKey, TValue> : IReadOnlyCollection<KeyValuePair<T
3233
private Entry[] _entries;
3334

3435
[DebuggerDisplay("({key}, {value})->{next}")]
36+
[StructLayout(LayoutKind.Auto)]
3537
private struct Entry
3638
{
3739
public TKey key;

Jint/Collections/StringDictionarySlim.cs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#pragma warning disable MA0006
2+
#pragma warning disable MA0008
3+
14
#nullable disable
25

36
// Licensed to the .NET Foundation under one or more agreements.

Jint/Engine.Ast.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static Module PrepareModule(string script, string? source = null)
4343

4444
private sealed class AstAnalyzer
4545
{
46-
private readonly Dictionary<string, EnvironmentRecord.BindingName> _bindingNames = new();
46+
private readonly Dictionary<string, EnvironmentRecord.BindingName> _bindingNames = new(StringComparer.Ordinal);
4747

4848
public void NodeVisitor(Node node)
4949
{

Jint/Engine.Modules.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public partial class Engine
1212
{
1313
internal IModuleLoader ModuleLoader { get; set; } = null!;
1414

15-
private readonly Dictionary<string, ModuleRecord> _modules = new();
16-
private readonly Dictionary<string, ModuleBuilder> _builders = new();
15+
private readonly Dictionary<string, ModuleRecord> _modules = new(StringComparer.Ordinal);
16+
private readonly Dictionary<string, ModuleBuilder> _builders = new(StringComparer.Ordinal);
1717

1818
/// <summary>
1919
/// https://tc39.es/ecma262/#sec-getactivescriptormodule

Jint/Engine.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public sealed partial class Engine : IDisposable
5959
public ITypeConverter ClrTypeConverter { get; internal set; }
6060

6161
// cache of types used when resolving CLR type names
62-
internal readonly Dictionary<string, Type?> TypeCache = new();
62+
internal readonly Dictionary<string, Type?> TypeCache = new(StringComparer.Ordinal);
6363

6464
// we use registered type reference as prototype if it's known
6565
internal Dictionary<Type,TypeReference>? _typeReferences;
@@ -553,7 +553,7 @@ internal JsValue GetValue(Reference reference, bool returnReferenceToPool)
553553
ExceptionHelper.ThrowReferenceError(Realm, reference);
554554
}
555555

556-
if ((baseValue._type & InternalTypes.ObjectEnvironmentRecord) == 0
556+
if ((baseValue._type & InternalTypes.ObjectEnvironmentRecord) == InternalTypes.None
557557
&& _referenceResolver.TryPropertyReference(this, reference, ref baseValue))
558558
{
559559
return baseValue;
@@ -584,7 +584,7 @@ internal JsValue GetValue(Reference reference, bool returnReferenceToPool)
584584
// check if we are accessing a string, boxing operation can be costly to do index access
585585
// we have good chance to have fast path with integer or string indexer
586586
ObjectInstance? o = null;
587-
if ((property._type & (InternalTypes.String | InternalTypes.Integer)) != 0
587+
if ((property._type & (InternalTypes.String | InternalTypes.Integer)) != InternalTypes.None
588588
&& baseValue is JsString s
589589
&& TryHandleStringValue(property, s, ref o, out var jsValue))
590590
{

Jint/Extensions/ReflectionExtensions.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -139,23 +139,23 @@ public static bool TryConvertViaTypeCoercion(
139139
return true;
140140
}
141141

142-
if (memberType == typeof(bool) && (valueCoercionType & ValueCoercionType.Boolean) != 0)
142+
if (memberType == typeof(bool) && (valueCoercionType & ValueCoercionType.Boolean) != ValueCoercionType.None)
143143
{
144144
converted = TypeConverter.ToBoolean(value);
145145
return true;
146146
}
147147

148148
if (memberType == typeof(string)
149149
&& !value.IsNullOrUndefined()
150-
&& (valueCoercionType & ValueCoercionType.String) != 0)
150+
&& (valueCoercionType & ValueCoercionType.String) != ValueCoercionType.None)
151151
{
152152
// we know how to print out correct string presentation for primitives
153153
// that are non-null and non-undefined
154154
converted = TypeConverter.ToString(value);
155155
return true;
156156
}
157157

158-
if (memberType is not null && memberType.IsClrNumericCoercible() && (valueCoercionType & ValueCoercionType.Number) != 0)
158+
if (memberType is not null && memberType.IsClrNumericCoercible() && (valueCoercionType & ValueCoercionType.Number) != ValueCoercionType.None)
159159
{
160160
// we know how to print out correct string presentation for primitives
161161
// that are non-null and non-undefined

Jint/HoistingScope.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ public static void GetImportsAndExports(
133133
treeWalker.Visit(module);
134134

135135
importEntries = treeWalker._importEntries;
136-
requestedModules = treeWalker._requestedModules ?? new();
137-
var importedBoundNames = new HashSet<string>();
136+
requestedModules = treeWalker._requestedModules ?? new(StringComparer.Ordinal);
137+
var importedBoundNames = new HashSet<string>(StringComparer.Ordinal);
138138

139139
if (importEntries != null)
140140
{
@@ -171,9 +171,9 @@ public static void GetImportsAndExports(
171171
for (var j = 0; j < importEntries!.Count; j++)
172172
{
173173
var ie = importEntries[j];
174-
if (ie.LocalName == ee.LocalName)
174+
if (string.Equals(ie.LocalName, ee.LocalName, StringComparison.Ordinal))
175175
{
176-
if (ie.ImportName == "*")
176+
if (string.Equals(ie.ImportName, "*", StringComparison.Ordinal))
177177
{
178178
localExportEntries.Add(ee);
179179
}
@@ -187,7 +187,7 @@ public static void GetImportsAndExports(
187187
}
188188
}
189189
}
190-
else if (ee.ImportName == "*" && ee.ExportName is null)
190+
else if (string.Equals(ee.ImportName, "*", StringComparison.Ordinal) && ee.ExportName is null)
191191
{
192192
starExportEntries.Add(ee);
193193
}
@@ -300,14 +300,14 @@ internal void Visit(Node node)
300300
if (childNode.Type == Nodes.ImportDeclaration)
301301
{
302302
_importEntries ??= new();
303-
_requestedModules ??= new();
303+
_requestedModules ??= new(StringComparer.Ordinal);
304304
var import = (ImportDeclaration) childNode;
305305
import.GetImportEntries(_importEntries, _requestedModules);
306306
}
307307
else if (childNode.Type is Nodes.ExportAllDeclaration or Nodes.ExportDefaultDeclaration or Nodes.ExportNamedDeclaration)
308308
{
309309
_exportEntries ??= new();
310-
_requestedModules ??= new();
310+
_requestedModules ??= new(StringComparer.Ordinal);
311311
var export = (ExportDeclaration) childNode;
312312
export.GetExportEntries(_exportEntries, _requestedModules);
313313
}

Jint/Jint.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
<ItemGroup>
2020
<PackageReference Include="Esprima" />
21+
<PackageReference Include="Meziantou.Analyzer" PrivateAssets="all" />
2122
</ItemGroup>
2223

2324
<ItemGroup>

Jint/JsValueExtensions.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static class JsValueExtensions
1919
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2020
public static bool IsPrimitive(this JsValue value)
2121
{
22-
return (value._type & (InternalTypes.Primitive | InternalTypes.Undefined | InternalTypes.Null)) != 0;
22+
return (value._type & (InternalTypes.Primitive | InternalTypes.Undefined | InternalTypes.Null)) != InternalTypes.None;
2323
}
2424

2525
[Pure]
@@ -76,28 +76,28 @@ public static bool IsRegExp(this JsValue value)
7676
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7777
public static bool IsObject(this JsValue value)
7878
{
79-
return (value._type & InternalTypes.Object) != 0;
79+
return (value._type & InternalTypes.Object) != InternalTypes.None;
8080
}
8181

8282
[Pure]
8383
[MethodImpl(MethodImplOptions.AggressiveInlining)]
8484
public static bool IsString(this JsValue value)
8585
{
86-
return (value._type & InternalTypes.String) != 0;
86+
return (value._type & InternalTypes.String) != InternalTypes.None;
8787
}
8888

8989
[Pure]
9090
[MethodImpl(MethodImplOptions.AggressiveInlining)]
9191
public static bool IsNumber(this JsValue value)
9292
{
93-
return (value._type & (InternalTypes.Number | InternalTypes.Integer)) != 0;
93+
return (value._type & (InternalTypes.Number | InternalTypes.Integer)) != InternalTypes.None;
9494
}
9595

9696
[Pure]
9797
[MethodImpl(MethodImplOptions.AggressiveInlining)]
9898
public static bool IsBigInt(this JsValue value)
9999
{
100-
return (value._type & InternalTypes.BigInt) != 0;
100+
return (value._type & InternalTypes.BigInt) != InternalTypes.None;
101101
}
102102

103103
[Pure]

Jint/Key.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Jint
1313
private Key(string name)
1414
{
1515
Name = name;
16-
HashCode = name.GetHashCode();
16+
HashCode = StringComparer.Ordinal.GetHashCode(name);
1717
}
1818

1919
internal readonly string Name;
@@ -29,28 +29,28 @@ public static implicit operator Key(string name)
2929
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3030
public static bool operator ==(in Key a, in Key b)
3131
{
32-
return a.HashCode == b.HashCode && a.Name == b.Name;
32+
return a.HashCode == b.HashCode && string.Equals(a.Name, b.Name, StringComparison.Ordinal);
3333
}
3434

3535
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3636
public static bool operator !=(in Key a, in Key b)
3737
{
38-
return a.HashCode != b.HashCode || a.Name != b.Name;
38+
return a.HashCode != b.HashCode || !string.Equals(a.Name, b.Name, StringComparison.Ordinal);
3939
}
4040

4141
public static bool operator ==(in Key a, string b)
4242
{
43-
return a.Name == b;
43+
return string.Equals(a.Name, b, StringComparison.Ordinal);
4444
}
4545

4646
public static bool operator !=(in Key a, string b)
4747
{
48-
return a.Name != b;
48+
return !string.Equals(a.Name, b, StringComparison.Ordinal);
4949
}
5050

5151
public bool Equals(Key other)
5252
{
53-
return HashCode == other.HashCode && Name == other.Name;
53+
return HashCode == other.HashCode && string.Equals(Name, other.Name, StringComparison.Ordinal);
5454
}
5555

5656
public override bool Equals(object? obj)

Jint/ModuleBuilder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public sealed class ModuleBuilder
1313
private readonly string _specifier;
1414
private Module? _module;
1515
private readonly List<string> _sourceRaw = new();
16-
private readonly Dictionary<string, JsValue> _exports = new();
16+
private readonly Dictionary<string, JsValue> _exports = new(StringComparer.Ordinal);
1717
private readonly ParserOptions _options;
1818

1919
internal ModuleBuilder(Engine engine, string specifier)

Jint/Native/Argument/ArgumentsInstance.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Jint.Native.Argument
1616
internal sealed class ArgumentsInstance : ObjectInstance
1717
{
1818
// cache property container for array iteration for less allocations
19-
private static readonly ThreadLocal<HashSet<string>> _mappedNamed = new(() => new HashSet<string>());
19+
private static readonly ThreadLocal<HashSet<string>> _mappedNamed = new(() => new HashSet<string>(StringComparer.Ordinal));
2020

2121
private FunctionInstance _func = null!;
2222
private Key[] _names = null!;

Jint/Native/Array/ArrayInstance.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ internal bool CanUseFastAccess
7979
{
8080
get
8181
{
82-
if ((_objectChangeFlags & ObjectChangeFlags.NonDefaultDataDescriptorUsage) != 0)
82+
if ((_objectChangeFlags & ObjectChangeFlags.NonDefaultDataDescriptorUsage) != ObjectChangeFlags.None)
8383
{
8484
// could be a mutating property for example, length might change, not safe anymore
8585
return false;
@@ -92,7 +92,7 @@ internal bool CanUseFastAccess
9292
return false;
9393
}
9494

95-
if ((arrayPrototype._objectChangeFlags & ObjectChangeFlags.ArrayIndex) != 0)
95+
if ((arrayPrototype._objectChangeFlags & ObjectChangeFlags.ArrayIndex) != ObjectChangeFlags.None)
9696
{
9797
// maybe somebody moved integer property to prototype? not safe anymore
9898
return false;
@@ -104,7 +104,7 @@ internal bool CanUseFastAccess
104104
return false;
105105
}
106106

107-
return (arrayPrototypePrototype._objectChangeFlags & ObjectChangeFlags.ArrayIndex) == 0;
107+
return (arrayPrototypePrototype._objectChangeFlags & ObjectChangeFlags.ArrayIndex) == ObjectChangeFlags.None;
108108
}
109109
}
110110

@@ -316,7 +316,7 @@ protected sealed override bool TryGetProperty(JsValue property, [NotNullWhen(tru
316316

317317
public sealed override List<JsValue> GetOwnPropertyKeys(Types types = Types.None | Types.String | Types.Symbol)
318318
{
319-
if ((types & Types.String) == 0)
319+
if ((types & Types.String) == Types.None)
320320
{
321321
return base.GetOwnPropertyKeys(types);
322322
}

Jint/Native/Array/ArrayOperations.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public virtual JsValue[] GetAll(
5656
for (uint i = 0; i < (uint) jsValues.Length; i++)
5757
{
5858
var jsValue = skipHoles && !HasProperty(i) ? JsValue.Undefined : Get(i);
59-
if ((jsValue.Type & elementTypes) == 0)
59+
if ((jsValue.Type & elementTypes) == Types.None)
6060
{
6161
ExceptionHelper.ThrowTypeErrorNoEngine("invalid type");
6262
}
@@ -231,7 +231,7 @@ public override bool TryGetValue(ulong index, out JsValue value)
231231

232232
public override JsValue Get(ulong index) => _target.Get((uint) index);
233233

234-
public override JsValue[] GetAll(Types elementTypes, bool skipHoles = false)
234+
public override JsValue[] GetAll(Types elementTypes = Types.Undefined | Types.Null | Types.Boolean | Types.String | Types.Symbol | Types.Number | Types.Object, bool skipHoles = false)
235235
{
236236
var n = _target.GetLength();
237237

@@ -251,7 +251,7 @@ public override JsValue[] GetAll(Types elementTypes, bool skipHoles = false)
251251
value = _target.Prototype?.Get(i) ?? JsValue.Undefined;
252252
}
253253

254-
if ((value.Type & elementTypes) == 0)
254+
if ((value.Type & elementTypes) == Types.None)
255255
{
256256
ExceptionHelper.ThrowTypeErrorNoEngine("invalid type");
257257
}

Jint/Native/Array/ArrayPrototype.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ private JsValue Map(JsValue thisObject, JsValue[] arguments)
487487

488488
if (len > ArrayOperations.MaxArrayLength)
489489
{
490-
ExceptionHelper.ThrowRangeError(_realm, "Invalid array length");;
490+
ExceptionHelper.ThrowRangeError(_realm, "Invalid array length");
491491
}
492492

493493
var callbackfn = arguments.At(0);
@@ -1136,7 +1136,7 @@ private JsValue Slice(JsValue thisObject, JsValue[] arguments)
11361136

11371137
if (k < final && final - k > ArrayOperations.MaxArrayLength)
11381138
{
1139-
ExceptionHelper.ThrowRangeError(_realm, "Invalid array length");;
1139+
ExceptionHelper.ThrowRangeError(_realm, "Invalid array length");
11401140
}
11411141

11421142
var length = (uint) System.Math.Max(0, (long) final - (long) k);

0 commit comments

Comments
 (0)