Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decorators #1324

Merged
merged 25 commits into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f5a9a32
Parsing and semantic analysis for decorators
shenglol Jan 15, 2021
0856841
First iteration of decorator emitter
shenglol Jan 20, 2021
e2b712a
Simplify decorator definitions
shenglol Jan 20, 2021
785b75b
Remove constructors not needed
shenglol Jan 20, 2021
0632101
Emit default value
shenglol Jan 21, 2021
363b323
Ban using parameter decorators and modifiers together
shenglol Jan 21, 2021
16aafc9
Minor refactoring
shenglol Jan 21, 2021
dfbac3b
Add formatting support for decorators
shenglol Jan 21, 2021
4ac9156
Fix parser and spans
shenglol Jan 21, 2021
16b9e94
Improve ObjectSyntax merging methods
shenglol Jan 21, 2021
133f8ec
Update test baselines
shenglol Jan 22, 2021
ac2e9aa
Add missing test cases
shenglol Jan 22, 2021
46c31b6
Merge branch 'main' into shenglol/decorators
shenglol Jan 22, 2021
a2451cb
Remove unnecessary recovery logic
shenglol Jan 25, 2021
13cea1c
Move decorators to sys namespace
shenglol Jan 26, 2021
6b0507b
Move leading nodes to a base class
shenglol Jan 26, 2021
cf1697d
Handle dangling decorators
shenglol Jan 26, 2021
7a2df8a
Add grammar for decorators
shenglol Jan 26, 2021
a1ac8bc
Fix a file name; clean up garbage
shenglol Jan 27, 2021
14963fd
Add missing validator for @secure()
shenglol Jan 28, 2021
8cee64e
Add a note to warn people to not break dangling decorators tests
shenglol Jan 28, 2021
6b2e7b2
Fine-tune decorator parsing logic
shenglol Jan 28, 2021
9d24c78
Copy and convert parameter modifier tests to use decorators
shenglol Jan 28, 2021
7da0ef0
Merge branch 'main' into shenglol/decorators
shenglol Jan 28, 2021
70907f4
Merge branch 'main' into shenglol/decorators
shenglol Jan 28, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void ValidBicep_TemplateEmiterShouldProduceExpectedTemplate(DataSet dataS
[TestMethod]
public void TemplateEmitter_output_should_not_include_UTF8_BOM()
{
var syntaxTreeGrouping = SyntaxFactory.CreateFromText("");
var syntaxTreeGrouping = SyntaxTreeGroupingFactory.CreateFromText("");
var compiledFilePath = FileHelper.GetResultFilePath(this.TestContext, "main.json");

// emitting the template should be successful
Expand Down
6 changes: 3 additions & 3 deletions src/Bicep.Core.IntegrationTests/ModuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ param inputb string
};


var compilation = new Compilation(TestResourceTypeProvider.Create(), SyntaxFactory.CreateForFiles(files, mainUri));
var compilation = new Compilation(TestResourceTypeProvider.Create(), SyntaxTreeGroupingFactory.CreateForFiles(files, mainUri));

var (success, diagnosticsByFile) = GetSuccessAndDiagnosticsByFile(compilation);
diagnosticsByFile.Values.SelectMany(x => x).Should().BeEmpty();
Expand Down Expand Up @@ -102,7 +102,7 @@ param inputb string
};


var compilation = new Compilation(TestResourceTypeProvider.Create(), SyntaxFactory.CreateForFiles(files, mainUri));
var compilation = new Compilation(TestResourceTypeProvider.Create(), SyntaxTreeGroupingFactory.CreateForFiles(files, mainUri));

var (success, diagnosticsByFile) = GetSuccessAndDiagnosticsByFile(compilation);
diagnosticsByFile[mainUri].Should().HaveDiagnostics(new[] {
Expand Down Expand Up @@ -157,7 +157,7 @@ param inputb string
};


var compilation = new Compilation(TestResourceTypeProvider.Create(), SyntaxFactory.CreateForFiles(files, mainUri));
var compilation = new Compilation(TestResourceTypeProvider.Create(), SyntaxTreeGroupingFactory.CreateForFiles(files, mainUri));

var (success, diagnosticsByFile) = GetSuccessAndDiagnosticsByFile(compilation);
diagnosticsByFile[mainUri].Should().HaveDiagnostics(new[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class CompilationTests
[TestMethod]
public void EmptyProgram_SyntaxTreeGrouping_should_be_persisted()
{
var program = SyntaxFactory.CreateFromText(DataSets.Empty.Bicep);
var program = SyntaxTreeGroupingFactory.CreateFromText(DataSets.Empty.Bicep);
var compilation = new Compilation(TestResourceTypeProvider.Create(), program);

compilation.SyntaxTreeGrouping.Should().BeSameAs(program);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void ProgramsShouldProduceExpectedDiagnostics(DataSet dataSet)
[TestMethod]
public void EndOfFileFollowingSpaceAfterParameterKeyWordShouldNotThrow()
{
var compilation = new Compilation(TestResourceTypeProvider.Create(), SyntaxFactory.CreateFromText("parameter "));
var compilation = new Compilation(TestResourceTypeProvider.Create(), SyntaxTreeGroupingFactory.CreateFromText("parameter "));
compilation.GetEntrypointSemanticModel().GetParseDiagnostics();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private static SemanticModel GetSemanticModelForTest(string programText, IEnumer
{
var typeProvider = ResourceTypeProviderHelper.CreateMockTypeProvider(definedTypes);

var compilation = new Compilation(typeProvider, SyntaxFactory.CreateFromText(programText));
var compilation = new Compilation(typeProvider, SyntaxTreeGroupingFactory.CreateFromText(programText));
return compilation.GetEntrypointSemanticModel();
}

Expand Down
6 changes: 6 additions & 0 deletions src/Bicep.Core.Samples/Files/InvalidModules_LF/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,9 @@ module moduleWithDuplicateName1 './empty.bicep' = {
module moduleWithDuplicateName2 './empty.bicep' = {
name: 'moduleWithDuplicateName'
}

//@az.secure()
@minValue()
module moduleWithNotAttachableDecorators './empty.bicep' = {
name: 'moduleWithNotAttachableDecorators'
}
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,10 @@ module moduleWithDuplicateName2 './empty.bicep' = {
//@[8:33) [BCP122 (Error)] Modules: "moduleWithDuplicateName1", "moduleWithDuplicateName2" are defined with this same name and this same scope in a file. Rename them or split into different modules. |'moduleWithDuplicateName'|
}

//@az.secure()
@minValue()
//@[1:9) [BCP128 (Error)] Function "minValue" cannot be used as a module decorator. |minValue|
module moduleWithNotAttachableDecorators './empty.bicep' = {
name: 'moduleWithNotAttachableDecorators'
}

Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,9 @@ module moduleWithDuplicateName1 './empty.bicep' = {
module moduleWithDuplicateName2 './empty.bicep' = {
name: 'moduleWithDuplicateName'
}

//@az.secure()
@minValue()
module moduleWithNotAttachableDecorators './empty.bicep' = {
name: 'moduleWithNotAttachableDecorators'
}
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,10 @@ module moduleWithDuplicateName2 './empty.bicep' = {
name: 'moduleWithDuplicateName'
}

//@az.secure()
@minValue()
module moduleWithNotAttachableDecorators './empty.bicep' = {
//@[7:40) Module moduleWithNotAttachableDecorators. Type: module. Declaration start char: 0, length: 118
name: 'moduleWithNotAttachableDecorators'
}

34 changes: 34 additions & 0 deletions src/Bicep.Core.Samples/Files/InvalidModules_LF/main.syntax.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -1463,6 +1463,40 @@ module moduleWithDuplicateName2 './empty.bicep' = {
//@[33:34) NewLine |\n|
}
//@[0:1) RightBrace |}|
//@[1:3) NewLine |\n\n|

//@az.secure()
//@[14:15) NewLine |\n|
@minValue()
//@[0:118) ModuleDeclarationSyntax
//@[0:11) DecoratorSyntax
//@[0:1) At |@|
//@[1:11) FunctionCallSyntax
//@[1:9) IdentifierSyntax
//@[1:9) Identifier |minValue|
//@[9:10) LeftParen |(|
//@[10:11) RightParen |)|
//@[11:12) NewLine |\n|
module moduleWithNotAttachableDecorators './empty.bicep' = {
//@[0:6) Identifier |module|
//@[7:40) IdentifierSyntax
//@[7:40) Identifier |moduleWithNotAttachableDecorators|
//@[41:56) StringSyntax
//@[41:56) StringComplete |'./empty.bicep'|
//@[57:58) Assignment |=|
//@[59:106) ObjectSyntax
//@[59:60) LeftBrace |{|
//@[60:61) NewLine |\n|
name: 'moduleWithNotAttachableDecorators'
//@[2:43) ObjectPropertySyntax
//@[2:6) IdentifierSyntax
//@[2:6) Identifier |name|
//@[6:7) Colon |:|
//@[8:43) StringSyntax
//@[8:43) StringComplete |'moduleWithNotAttachableDecorators'|
//@[43:44) NewLine |\n|
}
//@[0:1) RightBrace |}|
//@[1:2) NewLine |\n|

//@[0:0) EndOfFile ||
24 changes: 24 additions & 0 deletions src/Bicep.Core.Samples/Files/InvalidModules_LF/main.tokens.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,30 @@ module moduleWithDuplicateName2 './empty.bicep' = {
//@[33:34) NewLine |\n|
}
//@[0:1) RightBrace |}|
//@[1:3) NewLine |\n\n|

//@az.secure()
//@[14:15) NewLine |\n|
@minValue()
//@[0:1) At |@|
//@[1:9) Identifier |minValue|
//@[9:10) LeftParen |(|
//@[10:11) RightParen |)|
//@[11:12) NewLine |\n|
module moduleWithNotAttachableDecorators './empty.bicep' = {
//@[0:6) Identifier |module|
//@[7:40) Identifier |moduleWithNotAttachableDecorators|
//@[41:56) StringComplete |'./empty.bicep'|
//@[57:58) Assignment |=|
//@[59:60) LeftBrace |{|
//@[60:61) NewLine |\n|
name: 'moduleWithNotAttachableDecorators'
//@[2:6) Identifier |name|
//@[6:7) Colon |:|
//@[8:43) StringComplete |'moduleWithNotAttachableDecorators'|
//@[43:44) NewLine |\n|
}
//@[0:1) RightBrace |}|
//@[1:2) NewLine |\n|

//@[0:0) EndOfFile ||
4 changes: 4 additions & 0 deletions src/Bicep.Core.Samples/Files/InvalidOutputs_CRLF/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,7 @@ output exp string = 2 + 3
output union string = true ? 's' : 1
output bad int = true && !4
output deeper bool = true ? -true : (14 && 's') + 10

@az.maxValue(20)
@minValue(10)
output notAttachableDecorators int = 32
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,9 @@ output deeper bool = true ? -true : (14 && 's') + 10
//@[28:33) [BCP044 (Error)] Cannot apply operator "-" to operand of type "bool". |-true|
//@[37:46) [BCP045 (Error)] Cannot apply operator "&&" to operands of type "int" and "'s'". |14 && 's'|

@az.maxValue(20)
//@[4:12) [BCP129 (Error)] Function "maxValue" cannot be used as an output decorator. |maxValue|
@minValue(10)
//@[1:9) [BCP129 (Error)] Function "minValue" cannot be used as an output decorator. |minValue|
output notAttachableDecorators int = 32

Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,7 @@ output exp string = 2 + 3
output union string = true ? 's' : 1
output bad int = true && !4
output deeper bool = true ? -true : (14 && 's') + 10

@az.maxValue(20)
@minValue(10)
output notAttachableDecorators int = 32
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,8 @@ output bad int = true && !4
output deeper bool = true ? -true : (14 && 's') + 10
//@[7:13) Output deeper. Type: bool. Declaration start char: 0, length: 52

@az.maxValue(20)
@minValue(10)
output notAttachableDecorators int = 32
//@[7:30) Output notAttachableDecorators. Type: int. Declaration start char: 0, length: 72

Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,46 @@ output deeper bool = true ? -true : (14 && 's') + 10
//@[48:49) Plus |+|
//@[50:52) NumericLiteralSyntax
//@[50:52) Number |10|
//@[52:54) NewLine |\r\n|
//@[52:56) NewLine |\r\n\r\n|

@az.maxValue(20)
//@[0:72) OutputDeclarationSyntax
//@[0:16) DecoratorSyntax
//@[0:1) At |@|
//@[1:16) InstanceFunctionCallSyntax
//@[1:3) VariableAccessSyntax
//@[1:3) IdentifierSyntax
//@[1:3) Identifier |az|
//@[3:4) Dot |.|
//@[4:12) IdentifierSyntax
//@[4:12) Identifier |maxValue|
//@[12:13) LeftParen |(|
//@[13:15) FunctionArgumentSyntax
//@[13:15) NumericLiteralSyntax
//@[13:15) Number |20|
//@[15:16) RightParen |)|
//@[16:18) NewLine |\r\n|
@minValue(10)
//@[0:13) DecoratorSyntax
//@[0:1) At |@|
//@[1:13) FunctionCallSyntax
//@[1:9) IdentifierSyntax
//@[1:9) Identifier |minValue|
//@[9:10) LeftParen |(|
//@[10:12) FunctionArgumentSyntax
//@[10:12) NumericLiteralSyntax
//@[10:12) Number |10|
//@[12:13) RightParen |)|
//@[13:15) NewLine |\r\n|
output notAttachableDecorators int = 32
//@[0:6) Identifier |output|
//@[7:30) IdentifierSyntax
//@[7:30) Identifier |notAttachableDecorators|
//@[31:34) TypeSyntax
//@[31:34) Identifier |int|
//@[35:36) Assignment |=|
//@[37:39) NumericLiteralSyntax
//@[37:39) Number |32|
//@[39:41) NewLine |\r\n|

//@[0:0) EndOfFile ||
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,30 @@ output deeper bool = true ? -true : (14 && 's') + 10
//@[46:47) RightParen |)|
//@[48:49) Plus |+|
//@[50:52) Number |10|
//@[52:54) NewLine |\r\n|
//@[52:56) NewLine |\r\n\r\n|

@az.maxValue(20)
//@[0:1) At |@|
//@[1:3) Identifier |az|
//@[3:4) Dot |.|
//@[4:12) Identifier |maxValue|
//@[12:13) LeftParen |(|
//@[13:15) Number |20|
//@[15:16) RightParen |)|
//@[16:18) NewLine |\r\n|
@minValue(10)
//@[0:1) At |@|
//@[1:9) Identifier |minValue|
//@[9:10) LeftParen |(|
//@[10:12) Number |10|
//@[12:13) RightParen |)|
//@[13:15) NewLine |\r\n|
output notAttachableDecorators int = 32
//@[0:6) Identifier |output|
//@[7:30) Identifier |notAttachableDecorators|
//@[31:34) Identifier |int|
//@[35:36) Assignment |=|
//@[37:39) Number |32|
//@[39:41) NewLine |\r\n|

//@[0:0) EndOfFile ||
Loading