Skip to content

Commit

Permalink
Ensure minLength and maxLength take non-negative values (#1902)
Browse files Browse the repository at this point in the history
  • Loading branch information
shenglol authored Mar 16, 2021
1 parent b6bc48c commit 676dbf8
Show file tree
Hide file tree
Showing 20 changed files with 252 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,20 @@
"command": "editor.action.triggerParameterHints"
}
},
{
"label": "invalidLength",
"kind": "field",
"detail": "invalidLength",
"deprecated": false,
"preselect": false,
"sortText": "2_invalidLength",
"insertTextFormat": "plainText",
"insertTextMode": "asIs",
"textEdit": {
"range": {},
"newText": "invalidLength"
}
},
{
"label": "json",
"kind": "function",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,20 @@
"command": "editor.action.triggerParameterHints"
}
},
{
"label": "invalidLength",
"kind": "field",
"detail": "invalidLength",
"deprecated": false,
"preselect": false,
"sortText": "2_invalidLength",
"insertTextFormat": "plainText",
"insertTextMode": "asIs",
"textEdit": {
"range": {},
"newText": "invalidLength"
}
},
{
"label": "json",
"kind": "function",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,20 @@
"command": "editor.action.triggerParameterHints"
}
},
{
"label": "invalidLength",
"kind": "field",
"detail": "invalidLength",
"deprecated": false,
"preselect": false,
"sortText": "2_invalidLength",
"insertTextFormat": "plainText",
"insertTextMode": "asIs",
"textEdit": {
"range": {},
"newText": "invalidLength"
}
},
{
"label": "json",
"kind": "function",
Expand Down
4 changes: 4 additions & 0 deletions src/Bicep.Core.Samples/Files/InvalidParameters_LF/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -415,5 +415,9 @@ param unaryMinusOnFunction int
@maxLength(4)
param duplicateDecorators string
@minLength(-1)
@maxLength(-100)
param invalidLength string
// unterminated multi-line comment
/*
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,12 @@ param unaryMinusOnFunction int
//@[0:13) [BCP166 (Error)] Duplicate "maxLength" decorator. |@maxLength(4)|
param duplicateDecorators string
@minLength(-1)
//@[11:13) [BCP168 (Error)] Length must not be a negative value. |-1|
@maxLength(-100)
//@[11:15) [BCP168 (Error)] Length must not be a negative value. |-100|
param invalidLength string
// unterminated multi-line comment
/*
//@[0:7) [BCP002 (Error)] The multi-line comment at this location is not terminated. Terminate it with the */ character sequence. |/* \n|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,10 @@ param unaryMinusOnFunction int
@maxLength(4)
param duplicateDecorators string
@minLength(-1)
@maxLength(-100)
param invalidLength string
// unterminated multi-line comment
/*
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,11 @@ param unaryMinusOnFunction int
param duplicateDecorators string
//@[6:25) Parameter duplicateDecorators. Type: string. Declaration start char: 0, length: 98
@minLength(-1)
@maxLength(-100)
param invalidLength string
//@[6:19) Parameter invalidLength. Type: string. Declaration start char: 0, length: 58
// unterminated multi-line comment
/*
Original file line number Diff line number Diff line change
Expand Up @@ -2726,6 +2726,43 @@ param duplicateDecorators string
//@[26:32) Identifier |string|
//@[32:34) NewLine |\n\n|
@minLength(-1)
//@[0:58) ParameterDeclarationSyntax
//@[0:14) DecoratorSyntax
//@[0:1) At |@|
//@[1:14) FunctionCallSyntax
//@[1:10) IdentifierSyntax
//@[1:10) Identifier |minLength|
//@[10:11) LeftParen |(|
//@[11:13) FunctionArgumentSyntax
//@[11:13) UnaryOperationSyntax
//@[11:12) Minus |-|
//@[12:13) IntegerLiteralSyntax
//@[12:13) Integer |1|
//@[13:14) RightParen |)|
//@[14:15) NewLine |\n|
@maxLength(-100)
//@[0:16) DecoratorSyntax
//@[0:1) At |@|
//@[1:16) FunctionCallSyntax
//@[1:10) IdentifierSyntax
//@[1:10) Identifier |maxLength|
//@[10:11) LeftParen |(|
//@[11:15) FunctionArgumentSyntax
//@[11:15) UnaryOperationSyntax
//@[11:12) Minus |-|
//@[12:15) IntegerLiteralSyntax
//@[12:15) Integer |100|
//@[15:16) RightParen |)|
//@[16:17) NewLine |\n|
param invalidLength string
//@[0:5) Identifier |param|
//@[6:19) IdentifierSyntax
//@[6:19) Identifier |invalidLength|
//@[20:26) TypeSyntax
//@[20:26) Identifier |string|
//@[26:28) NewLine |\n\n|
// unterminated multi-line comment
//@[34:35) NewLine |\n|
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1749,6 +1749,28 @@ param duplicateDecorators string
//@[26:32) Identifier |string|
//@[32:34) NewLine |\n\n|
@minLength(-1)
//@[0:1) At |@|
//@[1:10) Identifier |minLength|
//@[10:11) LeftParen |(|
//@[11:12) Minus |-|
//@[12:13) Integer |1|
//@[13:14) RightParen |)|
//@[14:15) NewLine |\n|
@maxLength(-100)
//@[0:1) At |@|
//@[1:10) Identifier |maxLength|
//@[10:11) LeftParen |(|
//@[11:12) Minus |-|
//@[12:15) Integer |100|
//@[15:16) RightParen |)|
//@[16:17) NewLine |\n|
param invalidLength string
//@[0:5) Identifier |param|
//@[6:19) Identifier |invalidLength|
//@[20:26) Identifier |string|
//@[26:28) NewLine |\n\n|
// unterminated multi-line comment
//@[34:35) NewLine |\n|
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1630,6 +1630,7 @@ resource expressionsInPropertyLoopName 'Microsoft.Network/dnsZones@2018-05-01' =

// resource loop body that isn't an object
@batchSize(-1)
//@[11:13) [BCP154 (Error)] Expected a batch size of at least 1 but the specified value was "-1". |-1|
resource nonObjectResourceLoopBody 'Microsoft.Network/dnsZones@2018-05-01' = [for thing in []: 'test']
//@[95:101) [BCP167 (Error)] Expected the "{" character or the "if" keyword at this location. |'test'|
resource nonObjectResourceLoopBody2 'Microsoft.Network/dnsZones@2018-05-01' = [for thing in []: environment()]
Expand Down
5 changes: 5 additions & 0 deletions src/Bicep.Core.Samples/Files/Parameters_LF/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ param decoratedInt int = 123
@maxValue(-3)
param negativeValues int

// negative zeros are valid lengths
@minLength(-0)
@maxLength(-0)
param negativeZeros string

// negative integer literals in modifiers
param negativeModifiers int {
minValue: -100
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ param decoratedInt int = 123
@maxValue(-3)
param negativeValues int

// negative zeros are valid lengths
@minLength(-0)
@maxLength(-0)
param negativeZeros string

// negative integer literals in modifiers
param negativeModifiers int {
//@[28:64) [BCP161 (Info)] Parameter modifiers are deprecated and will be removed in a future release. Use decorators instead (see https://aka.ms/BicepSpecParams for examples). |{\n minValue: -100\n maxValue: -33\n}|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ param decoratedInt int = 123
@maxValue(-3)
param negativeValues int

// negative zeros are valid lengths
@minLength(-0)
@maxLength(-0)
param negativeZeros string

// negative integer literals in modifiers
param negativeModifiers int {
minValue: -100
Expand Down
7 changes: 6 additions & 1 deletion src/Bicep.Core.Samples/Files/Parameters_LF/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@
"maxValue": -3,
"minValue": -10
},
"negativeZeros": {
"type": "string",
"maxLength": 0,
"minLength": 0
},
"negativeModifiers": {
"type": "int",
"minValue": -100,
Expand Down Expand Up @@ -276,7 +281,7 @@
"_generator": {
"name": "bicep",
"version": "dev",
"templateHash": "1144572426517152891"
"templateHash": "1569062954108981413"
}
}
}
6 changes: 6 additions & 0 deletions src/Bicep.Core.Samples/Files/Parameters_LF/main.symbols.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,12 @@ param decoratedInt int = 123
param negativeValues int
//@[6:20) Parameter negativeValues. Type: int. Declaration start char: 0, length: 53

// negative zeros are valid lengths
@minLength(-0)
@maxLength(-0)
param negativeZeros string
//@[6:19) Parameter negativeZeros. Type: string. Declaration start char: 0, length: 56

// negative integer literals in modifiers
param negativeModifiers int {
//@[6:23) Parameter negativeModifiers. Type: int. Declaration start char: 0, length: 64
Expand Down
39 changes: 39 additions & 0 deletions src/Bicep.Core.Samples/Files/Parameters_LF/main.syntax.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,45 @@ param negativeValues int
//@[21:24) Identifier |int|
//@[24:26) NewLine |\n\n|

// negative zeros are valid lengths
//@[35:36) NewLine |\n|
@minLength(-0)
//@[0:56) ParameterDeclarationSyntax
//@[0:14) DecoratorSyntax
//@[0:1) At |@|
//@[1:14) FunctionCallSyntax
//@[1:10) IdentifierSyntax
//@[1:10) Identifier |minLength|
//@[10:11) LeftParen |(|
//@[11:13) FunctionArgumentSyntax
//@[11:13) UnaryOperationSyntax
//@[11:12) Minus |-|
//@[12:13) IntegerLiteralSyntax
//@[12:13) Integer |0|
//@[13:14) RightParen |)|
//@[14:15) NewLine |\n|
@maxLength(-0)
//@[0:14) DecoratorSyntax
//@[0:1) At |@|
//@[1:14) FunctionCallSyntax
//@[1:10) IdentifierSyntax
//@[1:10) Identifier |maxLength|
//@[10:11) LeftParen |(|
//@[11:13) FunctionArgumentSyntax
//@[11:13) UnaryOperationSyntax
//@[11:12) Minus |-|
//@[12:13) IntegerLiteralSyntax
//@[12:13) Integer |0|
//@[13:14) RightParen |)|
//@[14:15) NewLine |\n|
param negativeZeros string
//@[0:5) Identifier |param|
//@[6:19) IdentifierSyntax
//@[6:19) Identifier |negativeZeros|
//@[20:26) TypeSyntax
//@[20:26) Identifier |string|
//@[26:28) NewLine |\n\n|

// negative integer literals in modifiers
//@[41:42) NewLine |\n|
param negativeModifiers int {
Expand Down
24 changes: 24 additions & 0 deletions src/Bicep.Core.Samples/Files/Parameters_LF/main.tokens.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,30 @@ param negativeValues int
//@[21:24) Identifier |int|
//@[24:26) NewLine |\n\n|

// negative zeros are valid lengths
//@[35:36) NewLine |\n|
@minLength(-0)
//@[0:1) At |@|
//@[1:10) Identifier |minLength|
//@[10:11) LeftParen |(|
//@[11:12) Minus |-|
//@[12:13) Integer |0|
//@[13:14) RightParen |)|
//@[14:15) NewLine |\n|
@maxLength(-0)
//@[0:1) At |@|
//@[1:10) Identifier |maxLength|
//@[10:11) LeftParen |(|
//@[11:12) Minus |-|
//@[12:13) Integer |0|
//@[13:14) RightParen |)|
//@[14:15) NewLine |\n|
param negativeZeros string
//@[0:5) Identifier |param|
//@[6:19) Identifier |negativeZeros|
//@[20:26) Identifier |string|
//@[26:28) NewLine |\n\n|

// negative integer literals in modifiers
//@[41:42) NewLine |\n|
param negativeModifiers int {
Expand Down
9 changes: 7 additions & 2 deletions src/Bicep.Core/Diagnostics/DiagnosticBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ public ErrorDiagnostic ArgumentCountMismatch(int argumentCount, int mininumArgum
$"Unsupported scope for extension resource deployment. Expected a resource reference.");

public Diagnostic RuntimePropertyNotAllowed(string property, IEnumerable<string> usableProperties, string accessedSymbol, IEnumerable<string>? variableDependencyChain) {
var variableDependencyChainClause = variableDependencyChain != null ?
var variableDependencyChainClause = variableDependencyChain != null ?
$"You are referencing a variable which cannot be calculated in time (\"{string.Join("\" -> \"", variableDependencyChain)}\"). " : "";

return new ErrorDiagnostic(
Expand Down Expand Up @@ -886,7 +886,7 @@ public Diagnostic RuntimePropertyNotAllowed(string property, IEnumerable<string>
TextSpan,
"BCP155",
$"The decorator \"{decoratorName}\" can only be attached to resource or module collections.");

public ErrorDiagnostic InvalidResourceTypeSegment(string typeSegment) => new(
TextSpan,
"BCP156",
Expand Down Expand Up @@ -948,6 +948,11 @@ public Diagnostic RuntimePropertyNotAllowed(string property, IEnumerable<string>
TextSpan,
"BCP167",
"Expected the \"{\" character or the \"if\" keyword at this location.");

public ErrorDiagnostic LengthMustNotBeNegative() => new(
TextSpan,
"BCP168",
$"Length must not be a negative value.");
}

public static DiagnosticBuilderInternal ForPosition(TextSpan span)
Expand Down
14 changes: 7 additions & 7 deletions src/Bicep.Core/Semantics/Decorator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Bicep.Core.Diagnostics;
using Bicep.Core.Syntax;
Expand Down Expand Up @@ -45,18 +45,18 @@ public void Validate(DecoratorSyntax decoratorSyntax, TypeSymbol targetType, ITy
return;
}

if (!this.CanAttachTo(targetType))
{
diagnosticWriter.Write(DiagnosticBuilder.ForPosition(decoratorSyntax).CannotAttachDecoratorToTarget(this.Overload.Name, attachableType, targetType));
}

// Custom validator provided.
if (this.validator != null)
{
this.validator.Invoke(this.Overload.Name, decoratorSyntax, targetType, typeManager, diagnosticWriter);

return;
}

// No custom validator provided. Just validate the target type.
if (!this.CanAttachTo(targetType))
{
diagnosticWriter.Write(DiagnosticBuilder.ForPosition(decoratorSyntax).CannotAttachDecoratorToTarget(this.Overload.Name, attachableType, targetType));
}
}

public ObjectSyntax? Evaluate(DecoratorSyntax decoratorSyntax, TypeSymbol targetType, ObjectSyntax? targetObject)
Expand Down
Loading

0 comments on commit 676dbf8

Please sign in to comment.