Skip to content

Commit

Permalink
added linter rule type source generator (Azure#8428)
Browse files Browse the repository at this point in the history
  • Loading branch information
majastrz committed Sep 27, 2022
1 parent c166b19 commit adfd76f
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 36 deletions.
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
},
"sdk": {
"allowPrerelease": false,
"version": "6.0.100",
"version": "6.0.400",
"rollForward": "latestFeature"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void AllDefinedRulesAreListInLinterRulesProvider()
var actualTypeNames = ruleTypes.Select(t => t.FullName ?? throw new ArgumentNullException("bad type"));
var expectedTypeNames = expectedRuleTypes.Select(t => t.FullName ?? throw new ArgumentNullException("bad type"));

actualTypeNames.Should().BeEquivalentTo(expectedTypeNames, "LinterRuleProvider.GetRuleTypes() needs to be updated to list all defined rules in the core assembly");
actualTypeNames.Should().BeEquivalentTo(expectedTypeNames, "Please verify that the {nameof(LinterRuleTypeGenerator)} source generator is working correctly");
}

[TestMethod]
Expand Down
2 changes: 2 additions & 0 deletions src/Bicep.Core/Analyzers/Interfaces/IBicepAnalyzerRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace Bicep.Core.Analyzers.Interfaces
/// constructor which can be discoverd through
/// reflection
/// </summary>
/// <remarks>Do not rename or move this type to a different namespace.
/// We are using a source generator that requires the fully qualified type name of this interface to not change.</remarks>
public interface IBicepAnalyzerRule
{
string AnalyzerName { get; }
Expand Down
38 changes: 4 additions & 34 deletions src/Bicep.Core/Analyzers/Linter/LinterRulesProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
using System.Reflection;
using Bicep.Core.Analyzers.Interfaces;
using Bicep.Core.Analyzers.Linter.Rules;
using Bicep.RoslynAnalyzers;

namespace Bicep.Core.Analyzers.Linter
{
public class LinterRulesProvider : ILinterRulesProvider
public partial class LinterRulesProvider : ILinterRulesProvider
{
private readonly Lazy<ImmutableDictionary<string, string>> linterRulesLazy;

Expand All @@ -39,39 +40,8 @@ public LinterRulesProvider()
return rules;
}

public IEnumerable<Type> GetRuleTypes()
{
// Can't use reflection to get this list because the output dotnet executable is trimmed,
// and dependencies that can't be determined at compile time get removed.
return new[] {
typeof(AdminUsernameShouldNotBeLiteralRule),
typeof(ArtifactsParametersRule),
typeof(ExplicitValuesForLocationParamsRule),
typeof(MaxNumberOutputsRule),
typeof(MaxNumberParametersRule),
typeof(MaxNumberResourcesRule),
typeof(MaxNumberVariablesRule),
typeof(NoHardcodedEnvironmentUrlsRule),
typeof(NoHardcodedLocationRule),
typeof(NoLocationExprOutsideParamsRule),
typeof(NoUnnecessaryDependsOnRule),
typeof(NoUnusedExistingResourcesRule),
typeof(NoUnusedParametersRule),
typeof(NoUnusedVariablesRule),
typeof(OutputsShouldNotContainSecretsRule),
typeof(PreferInterpolationRule),
typeof(PreferUnquotedPropertyNamesRule),
typeof(SecretsInParamsMustBeSecureRule),
typeof(SecureParameterDefaultRule),
typeof(SecureParamsInNestedDeploymentsRule),
typeof(SimplifyInterpolationRule),
typeof(ProtectCommandToExecuteSecretsRule),
typeof(UseRecentApiVersionRule),
typeof(UseResourceIdFunctionsRule),
typeof(UseStableResourceIdentifiersRule),
typeof(UseStableVMImageRule),
};
}
[LinterRuleTypesGenerator]
public partial IEnumerable<Type> GetRuleTypes();

public ImmutableDictionary<string, string> GetLinterRules() => linterRulesLazy.Value;
}
Expand Down
1 change: 1 addition & 0 deletions src/Bicep.Core/Bicep.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<PackageReference Include="Azure.Bicep.Types.Az" Version="0.2.115" />
<PackageReference Include="Azure.Bicep.Types.K8s" Version="0.1.11" />
<PackageReference Include="System.IO.Abstractions" Version="17.2.3" />
<PackageReference Include="Azure.Bicep.Internal.RoslynAnalyzers" Version="0.1.17" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>
<Compile Update="CoreResources.Designer.cs">
Expand Down
6 changes: 6 additions & 0 deletions src/Bicep.Core/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
"version": 1,
"dependencies": {
"net6.0": {
"Azure.Bicep.Internal.RoslynAnalyzers": {
"type": "Direct",
"requested": "[0.1.17, )",
"resolved": "0.1.17",
"contentHash": "ItQGvv0R1znYHxbYx6VQTQdqqhLG+OsE5XtI6gGmPDiKXcSUpJD4UJNB3kOWAxbZDnB1HwEAJFegbjvv6YlcbA=="
},
"Azure.Bicep.Types": {
"type": "Direct",
"requested": "[0.2.115, )",
Expand Down

0 comments on commit adfd76f

Please sign in to comment.