Skip to content

Commit

Permalink
Enable Meziantou.Analyzer (#1682)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma authored Nov 12, 2023
1 parent 088f08f commit c565793
Show file tree
Hide file tree
Showing 72 changed files with 257 additions and 190 deletions.
27 changes: 27 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,33 @@ dotnet_diagnostic.CA1710.severity = none
# Error CA1716: Identifiers should have correct suffix
dotnet_diagnostic.CA1716.severity = none

# Error MA0026: TODO
dotnet_diagnostic.MA0026.severity = none

# Error MA0048 : File name must match type name
dotnet_diagnostic.MA0048.severity = none

# Error MA0016 : Prefer using collection abstraction instead of implementation
dotnet_diagnostic.MA0016.severity = none

# Error MA0017 : Abstract types should not have public or internal constructors
dotnet_diagnostic.MA0017.severity = none

# Error MA0051 : Method is too long
dotnet_diagnostic.MA0051.severity = none

# Error MA0046 : The delegate must return void
dotnet_diagnostic.MA0046.severity = none

# Error MA0097 : A class that implements IComparable<T> or IComparable should override comparison operators
dotnet_diagnostic.MA0097.severity = none

# Error MA0025 : Implement the functionality (or raise NotSupportedException or PlatformNotSupportedException)
dotnet_diagnostic.MA0025.severity = none

# Error MA0091 : Sender parameter should be 'this' for instance events
dotnet_diagnostic.MA0091.severity = none

# Sort using and Import directives with System.* appearing first
dotnet_sort_system_directives_first = true
dotnet_separate_import_directive_groups = false
Expand Down
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<PackageVersion Include="Esprima" Version="3.0.2" />
<PackageVersion Include="Flurl.Http.Signed" Version="3.2.4" />
<PackageVersion Include="Jurassic" Version="3.2.7" />
<PackageVersion Include="Meziantou.Analyzer" Version="2.0.106" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageVersion Include="MongoDB.Bson.signed" Version="2.19.0" />
Expand Down
2 changes: 2 additions & 0 deletions Jint/Collections/DictionarySlim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

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

[DebuggerDisplay("({key}, {value})->{next}")]
[StructLayout(LayoutKind.Auto)]
private struct Entry
{
public TKey key;
Expand Down
3 changes: 3 additions & 0 deletions Jint/Collections/StringDictionarySlim.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#pragma warning disable MA0006
#pragma warning disable MA0008

#nullable disable

// Licensed to the .NET Foundation under one or more agreements.
Expand Down
2 changes: 1 addition & 1 deletion Jint/Engine.Ast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static Module PrepareModule(string script, string? source = null)

private sealed class AstAnalyzer
{
private readonly Dictionary<string, EnvironmentRecord.BindingName> _bindingNames = new();
private readonly Dictionary<string, EnvironmentRecord.BindingName> _bindingNames = new(StringComparer.Ordinal);

public void NodeVisitor(Node node)
{
Expand Down
4 changes: 2 additions & 2 deletions Jint/Engine.Modules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public partial class Engine
{
internal IModuleLoader ModuleLoader { get; set; } = null!;

private readonly Dictionary<string, ModuleRecord> _modules = new();
private readonly Dictionary<string, ModuleBuilder> _builders = new();
private readonly Dictionary<string, ModuleRecord> _modules = new(StringComparer.Ordinal);
private readonly Dictionary<string, ModuleBuilder> _builders = new(StringComparer.Ordinal);

/// <summary>
/// https://tc39.es/ecma262/#sec-getactivescriptormodule
Expand Down
6 changes: 3 additions & 3 deletions Jint/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public sealed partial class Engine : IDisposable
public ITypeConverter ClrTypeConverter { get; internal set; }

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

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

if ((baseValue._type & InternalTypes.ObjectEnvironmentRecord) == 0
if ((baseValue._type & InternalTypes.ObjectEnvironmentRecord) == InternalTypes.None
&& _referenceResolver.TryPropertyReference(this, reference, ref baseValue))
{
return baseValue;
Expand Down Expand Up @@ -584,7 +584,7 @@ internal JsValue GetValue(Reference reference, bool returnReferenceToPool)
// check if we are accessing a string, boxing operation can be costly to do index access
// we have good chance to have fast path with integer or string indexer
ObjectInstance? o = null;
if ((property._type & (InternalTypes.String | InternalTypes.Integer)) != 0
if ((property._type & (InternalTypes.String | InternalTypes.Integer)) != InternalTypes.None
&& baseValue is JsString s
&& TryHandleStringValue(property, s, ref o, out var jsValue))
{
Expand Down
6 changes: 3 additions & 3 deletions Jint/Extensions/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,23 +139,23 @@ public static bool TryConvertViaTypeCoercion(
return true;
}

if (memberType == typeof(bool) && (valueCoercionType & ValueCoercionType.Boolean) != 0)
if (memberType == typeof(bool) && (valueCoercionType & ValueCoercionType.Boolean) != ValueCoercionType.None)
{
converted = TypeConverter.ToBoolean(value);
return true;
}

if (memberType == typeof(string)
&& !value.IsNullOrUndefined()
&& (valueCoercionType & ValueCoercionType.String) != 0)
&& (valueCoercionType & ValueCoercionType.String) != ValueCoercionType.None)
{
// we know how to print out correct string presentation for primitives
// that are non-null and non-undefined
converted = TypeConverter.ToString(value);
return true;
}

if (memberType is not null && memberType.IsClrNumericCoercible() && (valueCoercionType & ValueCoercionType.Number) != 0)
if (memberType is not null && memberType.IsClrNumericCoercible() && (valueCoercionType & ValueCoercionType.Number) != ValueCoercionType.None)
{
// we know how to print out correct string presentation for primitives
// that are non-null and non-undefined
Expand Down
14 changes: 7 additions & 7 deletions Jint/HoistingScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ public static void GetImportsAndExports(
treeWalker.Visit(module);

importEntries = treeWalker._importEntries;
requestedModules = treeWalker._requestedModules ?? new();
var importedBoundNames = new HashSet<string>();
requestedModules = treeWalker._requestedModules ?? new(StringComparer.Ordinal);
var importedBoundNames = new HashSet<string>(StringComparer.Ordinal);

if (importEntries != null)
{
Expand Down Expand Up @@ -171,9 +171,9 @@ public static void GetImportsAndExports(
for (var j = 0; j < importEntries!.Count; j++)
{
var ie = importEntries[j];
if (ie.LocalName == ee.LocalName)
if (string.Equals(ie.LocalName, ee.LocalName, StringComparison.Ordinal))
{
if (ie.ImportName == "*")
if (string.Equals(ie.ImportName, "*", StringComparison.Ordinal))
{
localExportEntries.Add(ee);
}
Expand All @@ -187,7 +187,7 @@ public static void GetImportsAndExports(
}
}
}
else if (ee.ImportName == "*" && ee.ExportName is null)
else if (string.Equals(ee.ImportName, "*", StringComparison.Ordinal) && ee.ExportName is null)
{
starExportEntries.Add(ee);
}
Expand Down Expand Up @@ -300,14 +300,14 @@ internal void Visit(Node node)
if (childNode.Type == Nodes.ImportDeclaration)
{
_importEntries ??= new();
_requestedModules ??= new();
_requestedModules ??= new(StringComparer.Ordinal);
var import = (ImportDeclaration) childNode;
import.GetImportEntries(_importEntries, _requestedModules);
}
else if (childNode.Type is Nodes.ExportAllDeclaration or Nodes.ExportDefaultDeclaration or Nodes.ExportNamedDeclaration)
{
_exportEntries ??= new();
_requestedModules ??= new();
_requestedModules ??= new(StringComparer.Ordinal);
var export = (ExportDeclaration) childNode;
export.GetExportEntries(_exportEntries, _requestedModules);
}
Expand Down
1 change: 1 addition & 0 deletions Jint/Jint.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

<ItemGroup>
<PackageReference Include="Esprima" />
<PackageReference Include="Meziantou.Analyzer" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
Expand Down
10 changes: 5 additions & 5 deletions Jint/JsValueExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static class JsValueExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsPrimitive(this JsValue value)
{
return (value._type & (InternalTypes.Primitive | InternalTypes.Undefined | InternalTypes.Null)) != 0;
return (value._type & (InternalTypes.Primitive | InternalTypes.Undefined | InternalTypes.Null)) != InternalTypes.None;
}

[Pure]
Expand Down Expand Up @@ -76,28 +76,28 @@ public static bool IsRegExp(this JsValue value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsObject(this JsValue value)
{
return (value._type & InternalTypes.Object) != 0;
return (value._type & InternalTypes.Object) != InternalTypes.None;
}

[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsString(this JsValue value)
{
return (value._type & InternalTypes.String) != 0;
return (value._type & InternalTypes.String) != InternalTypes.None;
}

[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNumber(this JsValue value)
{
return (value._type & (InternalTypes.Number | InternalTypes.Integer)) != 0;
return (value._type & (InternalTypes.Number | InternalTypes.Integer)) != InternalTypes.None;
}

[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsBigInt(this JsValue value)
{
return (value._type & InternalTypes.BigInt) != 0;
return (value._type & InternalTypes.BigInt) != InternalTypes.None;
}

[Pure]
Expand Down
12 changes: 6 additions & 6 deletions Jint/Key.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Jint
private Key(string name)
{
Name = name;
HashCode = name.GetHashCode();
HashCode = StringComparer.Ordinal.GetHashCode(name);
}

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

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(in Key a, in Key b)
{
return a.HashCode != b.HashCode || a.Name != b.Name;
return a.HashCode != b.HashCode || !string.Equals(a.Name, b.Name, StringComparison.Ordinal);
}

public static bool operator ==(in Key a, string b)
{
return a.Name == b;
return string.Equals(a.Name, b, StringComparison.Ordinal);
}

public static bool operator !=(in Key a, string b)
{
return a.Name != b;
return !string.Equals(a.Name, b, StringComparison.Ordinal);
}

public bool Equals(Key other)
{
return HashCode == other.HashCode && Name == other.Name;
return HashCode == other.HashCode && string.Equals(Name, other.Name, StringComparison.Ordinal);
}

public override bool Equals(object? obj)
Expand Down
2 changes: 1 addition & 1 deletion Jint/ModuleBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public sealed class ModuleBuilder
private readonly string _specifier;
private Module? _module;
private readonly List<string> _sourceRaw = new();
private readonly Dictionary<string, JsValue> _exports = new();
private readonly Dictionary<string, JsValue> _exports = new(StringComparer.Ordinal);
private readonly ParserOptions _options;

internal ModuleBuilder(Engine engine, string specifier)
Expand Down
2 changes: 1 addition & 1 deletion Jint/Native/Argument/ArgumentsInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Jint.Native.Argument
internal sealed class ArgumentsInstance : ObjectInstance
{
// cache property container for array iteration for less allocations
private static readonly ThreadLocal<HashSet<string>> _mappedNamed = new(() => new HashSet<string>());
private static readonly ThreadLocal<HashSet<string>> _mappedNamed = new(() => new HashSet<string>(StringComparer.Ordinal));

private FunctionInstance _func = null!;
private Key[] _names = null!;
Expand Down
8 changes: 4 additions & 4 deletions Jint/Native/Array/ArrayInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ internal bool CanUseFastAccess
{
get
{
if ((_objectChangeFlags & ObjectChangeFlags.NonDefaultDataDescriptorUsage) != 0)
if ((_objectChangeFlags & ObjectChangeFlags.NonDefaultDataDescriptorUsage) != ObjectChangeFlags.None)
{
// could be a mutating property for example, length might change, not safe anymore
return false;
Expand All @@ -92,7 +92,7 @@ internal bool CanUseFastAccess
return false;
}

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

return (arrayPrototypePrototype._objectChangeFlags & ObjectChangeFlags.ArrayIndex) == 0;
return (arrayPrototypePrototype._objectChangeFlags & ObjectChangeFlags.ArrayIndex) == ObjectChangeFlags.None;
}
}

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

public sealed override List<JsValue> GetOwnPropertyKeys(Types types = Types.None | Types.String | Types.Symbol)
{
if ((types & Types.String) == 0)
if ((types & Types.String) == Types.None)
{
return base.GetOwnPropertyKeys(types);
}
Expand Down
6 changes: 3 additions & 3 deletions Jint/Native/Array/ArrayOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public virtual JsValue[] GetAll(
for (uint i = 0; i < (uint) jsValues.Length; i++)
{
var jsValue = skipHoles && !HasProperty(i) ? JsValue.Undefined : Get(i);
if ((jsValue.Type & elementTypes) == 0)
if ((jsValue.Type & elementTypes) == Types.None)
{
ExceptionHelper.ThrowTypeErrorNoEngine("invalid type");
}
Expand Down Expand Up @@ -231,7 +231,7 @@ public override bool TryGetValue(ulong index, out JsValue value)

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

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

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

if ((value.Type & elementTypes) == 0)
if ((value.Type & elementTypes) == Types.None)
{
ExceptionHelper.ThrowTypeErrorNoEngine("invalid type");
}
Expand Down
4 changes: 2 additions & 2 deletions Jint/Native/Array/ArrayPrototype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ private JsValue Map(JsValue thisObject, JsValue[] arguments)

if (len > ArrayOperations.MaxArrayLength)
{
ExceptionHelper.ThrowRangeError(_realm, "Invalid array length");;
ExceptionHelper.ThrowRangeError(_realm, "Invalid array length");
}

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

if (k < final && final - k > ArrayOperations.MaxArrayLength)
{
ExceptionHelper.ThrowRangeError(_realm, "Invalid array length");;
ExceptionHelper.ThrowRangeError(_realm, "Invalid array length");
}

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

0 comments on commit c565793

Please sign in to comment.