diff --git a/Jint.Benchmark/DictionaryBenchmark.cs b/Jint.Benchmark/DictionaryBenchmark.cs index b345b25f69..b88f6f57bd 100644 --- a/Jint.Benchmark/DictionaryBenchmark.cs +++ b/Jint.Benchmark/DictionaryBenchmark.cs @@ -7,7 +7,7 @@ namespace Jint.Benchmark; public class DictionaryBenchmark { private static readonly string[] _keys = - { + [ "some", "key and", "another", @@ -20,7 +20,7 @@ public class DictionaryBenchmark "asdadsasa", "23323232323", "asdadsada sa213" - }; + ]; [Params(0, 2, 3, 5, 8, 9, 10)] public int N { get; set; } diff --git a/Jint.Benchmark/UncacheableExpressionsBenchmark.cs b/Jint.Benchmark/UncacheableExpressionsBenchmark.cs index 3031fc7781..9b118fac2c 100644 --- a/Jint.Benchmark/UncacheableExpressionsBenchmark.cs +++ b/Jint.Benchmark/UncacheableExpressionsBenchmark.cs @@ -103,6 +103,6 @@ private void CreateEngine(string script) engine = new Engine(InitializeEngine); engine.Execute(script); engine.Execute(targetObject); - targetJsObject = new[] { engine.GetValue("d") }; + targetJsObject = [engine.GetValue("d")]; } } diff --git a/Jint.Tests/Runtime/ArrayTests.cs b/Jint.Tests/Runtime/ArrayTests.cs index 62e06c8599..ed444ea270 100644 --- a/Jint.Tests/Runtime/ArrayTests.cs +++ b/Jint.Tests/Runtime/ArrayTests.cs @@ -346,7 +346,7 @@ public void Shift() public void ShouldBeAbleToInitFromArray() { var engine = new Engine(); - var propertyDescriptors = new JsArray(engine, new JsValue[] { 1 }).GetOwnProperties().ToArray(); + var propertyDescriptors = new JsArray(engine, [1]).GetOwnProperties().ToArray(); Assert.Equal(2, propertyDescriptors.Length); Assert.Equal("0", propertyDescriptors[0].Key); Assert.Equal(1, propertyDescriptors[0].Value.Value); diff --git a/Jint.Tests/Runtime/Domain/Dimensional.cs b/Jint.Tests/Runtime/Domain/Dimensional.cs index dc05d3ebdc..f4a8f85aa9 100644 --- a/Jint.Tests/Runtime/Domain/Dimensional.cs +++ b/Jint.Tests/Runtime/Domain/Dimensional.cs @@ -2,7 +2,7 @@ public class Dimensional : IComparable, IEquatable { - private readonly MeasureUnit[] PossibleMeasureUnits = new MeasureUnit[] { new MeasureUnit("Mass", "kg", 1.0), new MeasureUnit("Mass", "gr", 0.001), new MeasureUnit("Count", "piece", 1.0) }; + private readonly MeasureUnit[] PossibleMeasureUnits = [new MeasureUnit("Mass", "kg", 1.0), new MeasureUnit("Mass", "gr", 0.001), new MeasureUnit("Count", "piece", 1.0)]; public MeasureUnit MeasureUnit { get; private set; } diff --git a/Jint.Tests/Runtime/Domain/IntegerIndexer.cs b/Jint.Tests/Runtime/Domain/IntegerIndexer.cs index bdafd7c378..e4b67a0622 100644 --- a/Jint.Tests/Runtime/Domain/IntegerIndexer.cs +++ b/Jint.Tests/Runtime/Domain/IntegerIndexer.cs @@ -6,7 +6,7 @@ public class IntegerIndexer public IntegerIndexer() { - data = new[] {123, 0, 0, 0, 0}; + data = [123, 0, 0, 0, 0]; } public int this[int i] diff --git a/Jint.Tests/Runtime/EngineTests.cs b/Jint.Tests/Runtime/EngineTests.cs index 7137610fec..d13bf3f209 100644 --- a/Jint.Tests/Runtime/EngineTests.cs +++ b/Jint.Tests/Runtime/EngineTests.cs @@ -1187,12 +1187,12 @@ public static System.Collections.Generic.IEnumerable TestDates { get { - yield return new object[] { new DateTime(2000, 1, 1) }; - yield return new object[] { new DateTime(2000, 1, 1, 0, 15, 15, 15) }; - yield return new object[] { new DateTime(2000, 6, 1, 0, 15, 15, 15) }; - yield return new object[] { new DateTime(1900, 1, 1) }; - yield return new object[] { new DateTime(1900, 1, 1, 0, 15, 15, 15) }; - yield return new object[] { new DateTime(1900, 6, 1, 0, 15, 15, 15) }; + yield return [new DateTime(2000, 1, 1)]; + yield return [new DateTime(2000, 1, 1, 0, 15, 15, 15)]; + yield return [new DateTime(2000, 6, 1, 0, 15, 15, 15)]; + yield return [new DateTime(1900, 1, 1)]; + yield return [new DateTime(1900, 1, 1, 0, 15, 15, 15)]; + yield return [new DateTime(1900, 6, 1, 0, 15, 15, 15)]; } } diff --git a/Jint.Tests/Runtime/ExtensionMethods/CustomStringExtensions.cs b/Jint.Tests/Runtime/ExtensionMethods/CustomStringExtensions.cs index 2ea629842a..96fac5af06 100644 --- a/Jint.Tests/Runtime/ExtensionMethods/CustomStringExtensions.cs +++ b/Jint.Tests/Runtime/ExtensionMethods/CustomStringExtensions.cs @@ -22,13 +22,13 @@ public static ExpandoObject DeserializeObject(this string json) public static string[] Split(this string value, string split, StringSplitOptions options) { - return Array.Empty(); + return []; } public static string[] Split(this string value, int position) { var first = value.Substring(0, position); var second = value.Substring(position); - return new string[] { first, second }; + return [first, second]; } } \ No newline at end of file diff --git a/Jint.Tests/Runtime/FunctionTests.cs b/Jint.Tests/Runtime/FunctionTests.cs index 5d9522310f..8d47f595eb 100644 --- a/Jint.Tests/Runtime/FunctionTests.cs +++ b/Jint.Tests/Runtime/FunctionTests.cs @@ -84,7 +84,7 @@ function test() { engine .SetValue("testFunction", new ClrFunction(engine, "testFunction", (thisValue, args) => { - return engine.Invoke(thisValue, "then", new[] { JsValue.Undefined, args.At(0) }); + return engine.Invoke(thisValue, "then", [JsValue.Undefined, args.At(0)]); })) .SetValue("assertEqual", new Action((a, b) => Assert.Equal(b, a))) .Execute(Script); @@ -142,10 +142,10 @@ void addListener(Func callback) Assert.Equal(5, engine.Evaluate("a")); - ev(null, new JsValue[0]); + ev(null, []); Assert.Equal(10, engine.Evaluate("a")); - ev(null, new JsValue[] { 20 }); + ev(null, [20]); Assert.Equal(30, engine.Evaluate("a")); } @@ -174,10 +174,10 @@ void addListener(Func callback) Assert.Equal(5, engine.Evaluate("a")); - ev(null, new JsValue[0]); + ev(null, []); Assert.Equal(10, engine.Evaluate("a")); - ev(null, new JsValue[] { 20 }); + ev(null, [20]); Assert.Equal(30, engine.Evaluate("a")); } @@ -196,11 +196,11 @@ void addListener(Func callback) engine.Execute(@"addListener(Boolean)"); - Assert.Equal(true, ev(JsValue.Undefined, new JsValue[] { "test" })); - Assert.Equal(true, ev(JsValue.Undefined, new JsValue[] { 5 })); - Assert.Equal(false, ev(JsValue.Undefined, new JsValue[] { false })); - Assert.Equal(false, ev(JsValue.Undefined, new JsValue[] { 0})); - Assert.Equal(false, ev(JsValue.Undefined, new JsValue[] { JsValue.Undefined })); + Assert.Equal(true, ev(JsValue.Undefined, ["test"])); + Assert.Equal(true, ev(JsValue.Undefined, [5])); + Assert.Equal(false, ev(JsValue.Undefined, [false])); + Assert.Equal(false, ev(JsValue.Undefined, [0])); + Assert.Equal(false, ev(JsValue.Undefined, [JsValue.Undefined])); } [Fact] diff --git a/Jint.Tests/Runtime/InteropTests.cs b/Jint.Tests/Runtime/InteropTests.cs index 683db6a237..9b7d137a4d 100644 --- a/Jint.Tests/Runtime/InteropTests.cs +++ b/Jint.Tests/Runtime/InteropTests.cs @@ -2813,7 +2813,7 @@ public void CanCheckIfCallable() var result = engine.GetValue("f"); Assert.True(result.IsCallable); - Assert.True(result.Call(Array.Empty()).AsBoolean()); + Assert.True(result.Call([]).AsBoolean()); Assert.True(result.Call().AsBoolean()); } @@ -3422,7 +3422,7 @@ public Strings(string[] strings) public class Utils { - public IStringCollection GetStrings() => new Strings(new[] { "a", "b", "c" }); + public IStringCollection GetStrings() => new Strings(["a", "b", "c"]); } [Fact] @@ -3722,7 +3722,7 @@ public class Lion : Animal public class Zoo { public Animal king { get => (new Animal[] { new Lion() })[0]; } - public Animal[] animals { get => new Animal[] { new Lion(), new Elephant() }; } + public Animal[] animals { get => [new Lion(), new Elephant()]; } } [Fact] diff --git a/Jint.Tests/Runtime/JsonSerializerTests.cs b/Jint.Tests/Runtime/JsonSerializerTests.cs index 1e61f62db3..684d60d824 100644 --- a/Jint.Tests/Runtime/JsonSerializerTests.cs +++ b/Jint.Tests/Runtime/JsonSerializerTests.cs @@ -59,7 +59,7 @@ public void JsonStringOutputIsIndentedWhenSpacerDefined() JsObject instance = new JsObject(engine); instance["a"] = "b"; instance["b"] = 2; - instance["c"] = new JsArray(engine, new JsValue[] { new JsNumber(4), new JsNumber(5), new JsNumber(6) }); + instance["c"] = new JsArray(engine, [new JsNumber(4), new JsNumber(5), new JsNumber(6)]); instance["d"] = true; string actual = serializer.Serialize(instance, JsValue.Undefined, new JsNumber(2)).ToString(); @@ -75,7 +75,7 @@ public void JsonStringOutputIsCompactWithoutSpacer() JsObject instance = new JsObject(engine); instance["a"] = "b"; instance["b"] = 2; - instance["c"] = new JsArray(engine, new JsValue[] { new JsNumber(4), new JsNumber(5), new JsNumber(6) }); + instance["c"] = new JsArray(engine, [new JsNumber(4), new JsNumber(5), new JsNumber(6)]); instance["d"] = true; string actual = serializer.Serialize(instance, JsValue.Undefined, JsValue.Undefined).ToString(); @@ -88,7 +88,7 @@ public void ArrayWithUndefinedWillBeNull() using var engine = new Engine(); var serializer = new JsonSerializer(engine); - JsArray array = new JsArray(engine, new JsValue[] { JsValue.Undefined, new JsNumber(42) }); + JsArray array = new JsArray(engine, [JsValue.Undefined, new JsNumber(42)]); string actual = serializer.Serialize(array, JsValue.Undefined, JsValue.Undefined).ToString(); Assert.Equal("[null,42]", actual); } @@ -125,7 +125,7 @@ public void InfinityAndNaNGetsSerializedAsNull() { using var engine = new Engine(); var serializer = new JsonSerializer(engine); - JsArray array = new JsArray(engine, new JsValue[] { JsNumber.DoubleNegativeInfinity, JsNumber.DoublePositiveInfinity, JsNumber.DoubleNaN }); + JsArray array = new JsArray(engine, [JsNumber.DoubleNegativeInfinity, JsNumber.DoublePositiveInfinity, JsNumber.DoubleNaN]); string actual = serializer.Serialize(array, JsValue.Undefined, JsValue.Undefined).ToString(); Assert.Equal("[null,null,null]", actual); } @@ -139,7 +139,7 @@ public void ArrayAsReplacedDictatesPropertiesToSerializer() JsObject instance = new JsObject(engine); instance["a"] = 21; instance["b"] = 42; - JsValue replacer = new JsArray(engine, new JsValue[] { new JsString("b"), new JsString("z") }); + JsValue replacer = new JsArray(engine, [new JsString("b"), new JsString("z")]); string actual = serializer.Serialize(instance, replacer, JsValue.Undefined).ToString(); Assert.Equal("{\"b\":42}", actual); } diff --git a/Jint.Tests/Runtime/ModuleTests.cs b/Jint.Tests/Runtime/ModuleTests.cs index 65b5804578..16285cdca1 100644 --- a/Jint.Tests/Runtime/ModuleTests.cs +++ b/Jint.Tests/Runtime/ModuleTests.cs @@ -229,20 +229,18 @@ public void ShouldAddModuleFromClrFunction() export const result = [fns.act_noargs(), fns.act_args('ok'), fns.fn_noargs(), fns.fn_args('ok')];"); var ns = _engine.Modules.Import("my-module"); - Assert.Equal(new[] - { + Assert.Equal([ "act_noargs", "act_args:ok", "fn_noargs", "fn_args:ok" - }, received.ToArray()); - Assert.Equal(new[] - { + ], received.ToArray()); + Assert.Equal([ "undefined", "undefined", "ret", "ret" - }, ns.Get("result").AsArray().Select(x => x.ToString()).ToArray()); + ], ns.Get("result").AsArray().Select(x => x.ToString()).ToArray()); } private class ImportedClass @@ -398,7 +396,7 @@ public void EngineExecutePassesSourceForModuleResolving() log(value); })(); "; - List logStatements = new List(); + List logStatements = []; engine.SetValue("log", logStatements.Add); engine.Execute(code, source: "file:///folder/main.js"); @@ -423,7 +421,7 @@ public void EngineExecuteUsesScriptSourceForSource() log(value); })(); "; - List logStatements = new List(); + List logStatements = []; engine.SetValue("log", logStatements.Add); var script = Engine.PrepareScript(code, source: "file:///folder/main.js"); @@ -449,7 +447,7 @@ public void EngineEvaluatePassesSourceForModuleResolving() log(value); })(); "; - List logStatements = new List(); + List logStatements = []; engine.SetValue("log", logStatements.Add); engine.Evaluate(code, source: "file:///folder/main.js"); @@ -474,7 +472,7 @@ public void EngineEvaluateUsesScriptSourceForSource() log(value); })(); "; - List logStatements = new List(); + List logStatements = []; engine.SetValue("log", logStatements.Add); var script = Engine.PrepareScript(code, source: "file:///folder/main.js"); diff --git a/Jint.Tests/Runtime/PromiseTests.cs b/Jint.Tests/Runtime/PromiseTests.cs index e791153d7e..91cd477ce3 100644 --- a/Jint.Tests/Runtime/PromiseTests.cs +++ b/Jint.Tests/Runtime/PromiseTests.cs @@ -503,7 +503,7 @@ public void WithResolvers_calling_resolve_resolves_promise() { // Arrange using var engine = new Engine(); - List logMessages = new(); + List logMessages = []; engine.SetValue("log", logMessages.Add); // Act @@ -520,7 +520,7 @@ public void WithResolvers_calling_resolve_resolves_promise() engine.RunAvailableContinuations(); // Assert - List expected = new() { "start", "end", "resolved" }; + List expected = ["start", "end", "resolved"]; Assert.Equal(expected, logMessages); } @@ -529,7 +529,7 @@ public void WithResolvers_calling_reject_rejects_promise() { // Arrange using var engine = new Engine(); - List logMessages = new(); + List logMessages = []; engine.SetValue("log", logMessages.Add); // Act @@ -546,7 +546,7 @@ public void WithResolvers_calling_reject_rejects_promise() engine.RunAvailableContinuations(); // Assert - List expected = new() { "start", "end", "rejected" }; + List expected = ["start", "end", "rejected"]; Assert.Equal(expected, logMessages); } } diff --git a/Jint.Tests/Runtime/RegExpTests.cs b/Jint.Tests/Runtime/RegExpTests.cs index 20d3d3dc2d..6508123533 100644 --- a/Jint.Tests/Runtime/RegExpTests.cs +++ b/Jint.Tests/Runtime/RegExpTests.cs @@ -125,7 +125,7 @@ public void ShouldAllowProblematicGroupNames() var match = engine.Evaluate("'abc'.match(/(?<$group>b)/)").AsArray(); var groups = match.Get("groups").AsObject(); - Assert.Equal(new[] { "$group" }, groups.GetOwnPropertyKeys().Select(k => k.AsString())); + Assert.Equal(["$group"], groups.GetOwnPropertyKeys().Select(k => k.AsString())); Assert.Equal("b", groups["$group"]); var result = engine.Evaluate("'abc'.replace(/(?<$group>b)/g, '-$<$group>-')").AsString(); diff --git a/Jint/Engine.Ast.cs b/Jint/Engine.Ast.cs index e22f19cc4f..31548c1bb6 100644 --- a/Jint/Engine.Ast.cs +++ b/Jint/Engine.Ast.cs @@ -164,7 +164,7 @@ public CachedHoistingScope(Program program) { Scope = HoistingScope.GetProgramLevelDeclarations(program); - VarNames = new List(); + VarNames = []; GatherVarNames(Scope, VarNames); LexNames = DeclarationCacheBuilder.Build(Scope._lexicalDeclarations); diff --git a/Jint/HoistingScope.cs b/Jint/HoistingScope.cs index 558188146c..5334c83804 100644 --- a/Jint/HoistingScope.cs +++ b/Jint/HoistingScope.cs @@ -100,9 +100,9 @@ public static void GetImportsAndExports( } var exportEntries = treeWalker._exportEntries; - localExportEntries = new(); - indirectExportEntries = new(); - starExportEntries = new(); + localExportEntries = []; + indirectExportEntries = []; + starExportEntries = []; if (exportEntries != null) { @@ -177,11 +177,11 @@ public void Visit(Node node, Node? parent) var variableDeclaration = (VariableDeclaration)childNode; if (variableDeclaration.Kind == VariableDeclarationKind.Var) { - _variableDeclarations ??= new List(); + _variableDeclarations ??= []; _variableDeclarations.Add(variableDeclaration); if (_collectVarNames) { - _varNames ??= new List(); + _varNames ??= []; ref readonly var nodeList = ref variableDeclaration.Declarations; foreach (var declaration in nodeList) { @@ -195,11 +195,11 @@ public void Visit(Node node, Node? parent) if (parent is null or AstModule && variableDeclaration.Kind != VariableDeclarationKind.Var) { - _lexicalDeclarations ??= new List(); + _lexicalDeclarations ??= []; _lexicalDeclarations.Add(variableDeclaration); if (_collectLexicalNames) { - _lexicalNames ??= new List(); + _lexicalNames ??= []; ref readonly var nodeList = ref variableDeclaration.Declarations; foreach (var declaration in nodeList) { @@ -216,13 +216,13 @@ public void Visit(Node node, Node? parent) // function declarations are not hoisted if they are under block or case clauses if (parent is null || (node.Type != NodeType.BlockStatement && node.Type != NodeType.SwitchCase)) { - _functions ??= new List(); + _functions ??= []; _functions.Add((FunctionDeclaration)childNode); } } else if (childType == NodeType.ClassDeclaration && parent is null or AstModule) { - _lexicalDeclarations ??= new List(); + _lexicalDeclarations ??= []; _lexicalDeclarations.Add((Declaration) childNode); } diff --git a/Jint/Jint.csproj b/Jint/Jint.csproj index b738a4afd8..dc9c3242b0 100644 --- a/Jint/Jint.csproj +++ b/Jint/Jint.csproj @@ -10,7 +10,9 @@ enable true + true latest-Recommended + false true README.md diff --git a/Jint/Native/Promise/PromisePrototype.cs b/Jint/Native/Promise/PromisePrototype.cs index 2bb9c64a6f..1ebceffd4f 100644 --- a/Jint/Native/Promise/PromisePrototype.cs +++ b/Jint/Native/Promise/PromisePrototype.cs @@ -77,7 +77,7 @@ private JsValue Then(JsValue thisValue, JsCallArguments arguments) // 1. Let promise be the this value. // 2. Return ? Invoke(promise, "then", « undefined, onRejected »). private JsValue Catch(JsValue thisValue, JsCallArguments arguments) => - _engine.Invoke(thisValue, "then", new[] {Undefined, arguments.At(0)}); + _engine.Invoke(thisValue, "then", [Undefined, arguments.At(0)]); // https://tc39.es/ecma262/#sec-promise.prototype.finally private JsValue Finally(JsValue thisValue, JsCallArguments arguments) diff --git a/Jint/Native/String/StringPrototype.cs b/Jint/Native/String/StringPrototype.cs index 6a8e6381bd..accd048d5c 100644 --- a/Jint/Native/String/StringPrototype.cs +++ b/Jint/Native/String/StringPrototype.cs @@ -550,7 +550,7 @@ private JsValue Search(JsValue thisObject, JsCallArguments arguments) } } - var rx = (JsRegExp) _realm.Intrinsics.RegExp.Construct(new[] {regex}); + var rx = (JsRegExp) _realm.Intrinsics.RegExp.Construct([regex]); var s = TypeConverter.ToJsString(thisObject); return _engine.Invoke(rx, GlobalSymbolRegistry.Search, [s]); } @@ -719,7 +719,7 @@ private JsValue Match(JsValue thisObject, JsCallArguments arguments) } } - var rx = (JsRegExp) _realm.Intrinsics.RegExp.Construct(new[] {regex}); + var rx = (JsRegExp) _realm.Intrinsics.RegExp.Construct([regex]); var s = TypeConverter.ToJsString(thisObject); return _engine.Invoke(rx, GlobalSymbolRegistry.Match, [s]); @@ -741,7 +741,7 @@ private JsValue MatchAll(JsValue thisObject, JsCallArguments arguments) ExceptionHelper.ThrowTypeError(_realm); } } - var matcher = GetMethod(_realm, (ObjectInstance) regex, GlobalSymbolRegistry.MatchAll); + var matcher = GetMethod(_realm, regex, GlobalSymbolRegistry.MatchAll); if (matcher != null) { return matcher.Call(regex, thisObject); @@ -749,7 +749,7 @@ private JsValue MatchAll(JsValue thisObject, JsCallArguments arguments) } var s = TypeConverter.ToJsString(thisObject); - var rx = (JsRegExp) _realm.Intrinsics.RegExp.Construct(new[] { regex, "g" }); + var rx = (JsRegExp) _realm.Intrinsics.RegExp.Construct([regex, "g"]); return _engine.Invoke(rx, GlobalSymbolRegistry.MatchAll, [s]); } diff --git a/Jint/Runtime/Interop/InteropHelper.cs b/Jint/Runtime/Interop/InteropHelper.cs index 77135da8e9..418c41e462 100644 --- a/Jint/Runtime/Interop/InteropHelper.cs +++ b/Jint/Runtime/Interop/InteropHelper.cs @@ -323,7 +323,7 @@ internal static IEnumerable FindBestMatch( continue; } - matchingByParameterCount ??= new List(); + matchingByParameterCount ??= []; matchingByParameterCount.Add(new MethodMatch(method, arguments, score)); } }