-
Notifications
You must be signed in to change notification settings - Fork 756
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add linter rule to encourage usage of UDTs
- Loading branch information
1 parent
b2ae5b6
commit d8ebd30
Showing
5 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/Bicep.Core.UnitTests/Diagnostics/LinterRuleTests/UseUserDefinedTypesRuleTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
41
src/Bicep.Core/Analyzers/Linter/Rules/UseUserDefinedTypesRule.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters