Skip to content

Commit

Permalink
Add linter rule to encourage usage of UDTs
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-c-martin committed Dec 13, 2024
1 parent b2ae5b6 commit d8ebd30
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Bicep.Core.Analyzers.Linter.Rules;
using Bicep.Core.UnitTests.Assertions;
using Bicep.Core.UnitTests.Utils;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Bicep.Core.UnitTests.Diagnostics.LinterRuleTests;

[TestClass]
public class UseUserDefinedTypesRuleTests : LinterRuleTestsBase
{
private void AssertDiagnostics(string inputFile, int expectedCount = 1)
=> AssertLinterRuleDiagnostics(UseUserDefinedTypesRule.Code, inputFile, expectedCount);

private void AssertNoDiagnostics(string inputFile)
=> AssertLinterRuleDiagnostics(UseUserDefinedTypesRule.Code, inputFile, [], new(OnCompileErrors.Ignore, IncludePosition.None));

[TestMethod]
public void Rule_ignores_user_defined_types() => AssertNoDiagnostics("""
param foo {
bar: string
}
""");

[TestMethod]
public void Rule_defaults_to_off()
{
var result = CompilationHelper.Compile("""
param foo object
""");
result.ExcludingDiagnostics("no-unused-params").Should().NotHaveAnyDiagnostics();
}

[TestMethod]
public void Rule_flags_usage_of_object() => AssertDiagnostics("""
param foo object
""");

[TestMethod]
public void Rule_flags_usage_of_array() => AssertDiagnostics("""
param foo object
""");
}
41 changes: 41 additions & 0 deletions src/Bicep.Core/Analyzers/Linter/Rules/UseUserDefinedTypesRule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Bicep.Core.CodeAction;
using Bicep.Core.Diagnostics;
using Bicep.Core.Parsing;
using Bicep.Core.Semantics;
using Bicep.Core.Semantics.Namespaces;
using Bicep.Core.Syntax;
using Bicep.Core.Syntax.Comparers;
using Bicep.Core.Syntax.Visitors;
using Bicep.Core.TypeSystem;
using Bicep.Core.TypeSystem.Types;

namespace Bicep.Core.Analyzers.Linter.Rules;

public sealed class UseUserDefinedTypesRule : LinterRuleBase
{
public new const string Code = "use-user-defined-types";

public UseUserDefinedTypesRule() : base(
code: Code,
description: CoreResources.UseUserDefinedTypesRule_Description,
LinterRuleCategory.BestPractice,
docUri: new Uri($"https://aka.ms/bicep/linter/{Code}"),
// this is an optional coding standard, not something that should be enforced by default
overrideCategoryDefaultDiagnosticLevel: DiagnosticLevel.Off)
{ }

public override IEnumerable<IDiagnostic> AnalyzeInternal(SemanticModel model, DiagnosticLevel diagnosticLevel)
{
foreach (var typeVariable in SyntaxAggregator.AggregateByType<TypeVariableAccessSyntax>(model.Root.Syntax))
{
if (typeVariable.NameEquals(LanguageConstants.ObjectType) ||
typeVariable.NameEquals(LanguageConstants.ArrayType))
{
yield return CreateDiagnosticForSpan(diagnosticLevel, typeVariable.Span);
}
}
}
}
9 changes: 9 additions & 0 deletions src/Bicep.Core/CoreResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Bicep.Core/CoreResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,9 @@
<data name="UseSafeAccessRule_Description" xml:space="preserve">
<value>Use the safe access (.?) operator.</value>
</data>
<data name="UseUserDefinedTypesRule_Description" xml:space="preserve">
<value>Use user-defined types instead of 'object' or 'array'.</value>
</data>
<data name="UseSafeAccessRule_CodeFix" xml:space="preserve">
<value>Use the safe access (.?) operator</value>
</data>
Expand Down
10 changes: 10 additions & 0 deletions src/vscode-bicep/schemas/bicepconfig.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,16 @@
}
]
},
"use-user-defined-types": {
"allOf": [
{
"description": "Use user-defined types instead of 'object' or 'array'. Defaults to 'Off'. See https://aka.ms/bicep/linter/use-user-defined-types"
},
{
"$ref": "#/definitions/rule-def-level-off"
}
]
},
"what-if-short-circuiting": {
"allOf": [
{
Expand Down

0 comments on commit d8ebd30

Please sign in to comment.