Skip to content

Commit

Permalink
Merge pull request #31 from nils-a/feature/GH-19
Browse files Browse the repository at this point in the history
(GH-19) added options for specifying rules
  • Loading branch information
nils-a authored Jun 5, 2021
2 parents bbbddf6 + 7048281 commit b15af61
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 2 deletions.
32 changes: 31 additions & 1 deletion src/Cake.ESLint.Tests/ESLintRunnerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public void Should_add_global_args_when_globals_is_set()

var actual = fixture.Run();

actual.Args.ShouldContain("--global require --global exports:true");
actual.Args.ShouldContain("--global \"require\" --global \"exports:true\"");
}

[Fact]
Expand Down Expand Up @@ -280,6 +280,36 @@ public void Should_add_resolve_plugins_relative_to_arg_when_resolvePluginsRelati
actual.Args.ShouldContain("--resolve-plugins-relative-to \"../plugins\"");
}

[Fact]
public void Should_add_rulesdir_arg_when_rulesDirs_is_set()
{
fixture.Settings.AddRulesDir("my-rules", "my-other-rules");

var actual = fixture.Run();

actual.Args.ShouldContain("--rulesdir \"my-rules\" --rulesdir \"my-other-rules\"");
}

[Fact]
public void Should_add_plugin_arg_when_plugins_is_set()
{
fixture.Settings.AddPlugin("jquery", "eslint-plugin-mocha");

var actual = fixture.Run();

actual.Args.ShouldContain("--plugin jquery --plugin eslint-plugin-mocha");
}

[Fact]
public void Should_add_rule_arg_when_Rules_is_set()
{
fixture.Settings.AddRule("guard-for-in: 2", "brace-style: [2, 1tbs]");

var actual = fixture.Run();

actual.Args.ShouldContain("--rule \"guard-for-in: 2\" --rule \"brace-style: [2, 1tbs]\"");
}

// ReSharper disable once ClassNeverInstantiated.Local
private class OutputFormatDataGenerator : IEnumerable<object[]>
{
Expand Down
17 changes: 16 additions & 1 deletion src/Cake.ESLint/ESLintRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private ProcessArgumentBuilder GetArguments(ESLintSettings settings)

foreach (var global in settings.Globals.EnsureNotNull())
{
builder.AppendSwitch("--global", global);
builder.AppendSwitchQuoted("--global", global);
}

if (settings.Parser != null)
Expand All @@ -178,6 +178,21 @@ private ProcessArgumentBuilder GetArguments(ESLintSettings settings)
settings.ResolvePluginsRelativeTo.FullPath);
}

foreach (var dir in settings.RulesDirs.EnsureNotNull())
{
builder.AppendSwitchQuoted("--rulesdir", dir.FullPath);
}

foreach (var plugin in settings.Plugins.EnsureNotNull())
{
builder.AppendSwitch("--plugin", plugin);
}

foreach (var rule in settings.Rules.EnsureNotNull())
{
builder.AppendSwitchQuoted("--rule", rule);
}

// render arguments
foreach (var file in settings.Files.EnsureNotNull())
{
Expand Down
18 changes: 18 additions & 0 deletions src/Cake.ESLint/ESLintSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,24 @@ public ESLintSettings()
/// </summary>
public DirectoryPath ResolvePluginsRelativeTo { get; set; }

/// <summary>
/// Gets or sets a folder where additional rules are located.
/// <para>Option: <c>--rulesdir</c>.</para>
/// </summary>
public IEnumerable<DirectoryPath> RulesDirs { get; set; }

/// <summary>
/// Gets or sets the plugins to use.
/// <para>Option: <c>--plugin</c>.</para>
/// </summary>
public IEnumerable<string> Plugins { get; set; }

/// <summary>
/// Gets or sets the Rule(s).
/// <para>Option: <c>--rule</c>.</para>
/// </summary>
public IEnumerable<string> Rules { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to continue on lint errors.
/// <para>
Expand Down
36 changes: 36 additions & 0 deletions src/Cake.ESLint/ESLintSettingsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,41 @@ public static void AddFile(this ESLintSettings @this, params FilePath[] filePath
paths.AddRange(filePaths);
@this.Files = paths;
}

/// <summary>
/// adds to <see cref="ESLintSettings.RulesDirs"/>.
/// </summary>
/// <param name="this">The <see cref="ESLintSettings"/>.</param>
/// <param name="rulesDir">The paths to add.</param>
public static void AddRulesDir(this ESLintSettings @this, params DirectoryPath[] rulesDir)
{
var paths = @this.RulesDirs?.ToList() ?? new List<DirectoryPath>();
paths.AddRange(rulesDir);
@this.RulesDirs = paths;
}

/// <summary>
/// adds to <see cref="ESLintSettings.Plugins"/>.
/// </summary>
/// <param name="this">The <see cref="ESLintSettings"/>.</param>
/// <param name="plugin">The plugins to add.</param>
public static void AddPlugin(this ESLintSettings @this, params string[] plugin)
{
var plugins = @this.Plugins?.ToList() ?? new List<string>();
plugins.AddRange(plugin);
@this.Plugins = plugins;
}

/// <summary>
/// adds to <see cref="ESLintSettings.Rules"/>.
/// </summary>
/// <param name="this">The <see cref="ESLintSettings"/>.</param>
/// <param name="rule">The plugins to add.</param>
public static void AddRule(this ESLintSettings @this, params string[] rule)
{
var plugins = @this.Rules?.ToList() ?? new List<string>();
plugins.AddRange(rule);
@this.Rules = plugins;
}
}
}

0 comments on commit b15af61

Please sign in to comment.