Skip to content

Commit

Permalink
Formatting rule improvements (#13945)
Browse files Browse the repository at this point in the history
Updated two formatting rules based on [user
feedback](#13676). Main changes:
- The formatter no longer put `if` expression on a different line unless
there is a single-line comment between `=` and `if`.
- If the last function argument ends with a multi-line object or array,
the function argument list will not be forced to wrap, unless the width
limit is exceeded. This happens to be a heuristic implemented by
Prettier.

Closes #13676.

###### Microsoft Reviewers: [Open in
CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/Azure/bicep/pull/13945)
  • Loading branch information
shenglol committed Apr 29, 2024
1 parent 73c2bdf commit ed77ca3
Show file tree
Hide file tree
Showing 57 changed files with 2,223 additions and 1,117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,14 @@ module modANoName './modulea.bicep' = {
// #completionTest(0) -> moduleATopLevelProperties
}

module modANoNameWithCondition './modulea.bicep' =
if (true) {
// #completionTest(0) -> moduleAWithConditionTopLevelProperties
}
module modANoNameWithCondition './modulea.bicep' = if (true) {
// #completionTest(0) -> moduleAWithConditionTopLevelProperties
}

module modWithReferenceInCondition './main.bicep' =
if (reference('Micorosft.Management/managementGroups/MG', '2020-05-01').name == 'something') {}
module modWithReferenceInCondition './main.bicep' = if (reference(
'Micorosft.Management/managementGroups/MG',
'2020-05-01'
).name == 'something') {}

module modWithListKeysInCondition './main.bicep' = if (listKeys('foo', '2020-05-01').bar == true) {}

Expand All @@ -77,13 +78,12 @@ module modANoInputs './modulea.bicep' = {
// #completionTest(0,1,2) -> moduleATopLevelPropertiesMinusName
}

module modANoInputsWithCondition './modulea.bicep' =
if (length([
'foo'
]) == 1) {
name: 'modANoInputs'
// #completionTest(0,1,2) -> moduleAWithConditionTopLevelPropertiesMinusName
}
module modANoInputsWithCondition './modulea.bicep' = if (length([
'foo'
]) == 1) {
name: 'modANoInputs'
// #completionTest(0,1,2) -> moduleAWithConditionTopLevelPropertiesMinusName
}

module modAEmptyInputs './modulea.bicep' = {
name: 'modANoInputs'
Expand All @@ -92,13 +92,12 @@ module modAEmptyInputs './modulea.bicep' = {
}
}

module modAEmptyInputsWithCondition './modulea.bicep' =
if (1 + 2 == 2) {
name: 'modANoInputs'
params: {
// #completionTest(0,1,2,3,4) -> moduleAWithConditionParams
}
module modAEmptyInputsWithCondition './modulea.bicep' = if (1 + 2 == 2) {
name: 'modANoInputs'
params: {
// #completionTest(0,1,2,3,4) -> moduleAWithConditionParams
}
}

// #completionTest(55) -> moduleATopLevelPropertyAccess
var modulePropertyAccessCompletions = modAEmptyInputs.o
Expand Down Expand Up @@ -420,15 +419,14 @@ module directRefToCollectionViaSingleBody 'modulea.bicep' = {
}
}

module directRefToCollectionViaSingleConditionalBody 'modulea.bicep' =
if (true) {
name: 'hello2'
params: {
arrayParam: concat(wrongModuleParameterInLoop, nonexistentArrays)
objParam: {}
stringParamB: ''
}
module directRefToCollectionViaSingleConditionalBody 'modulea.bicep' = if (true) {
name: 'hello2'
params: {
arrayParam: concat(wrongModuleParameterInLoop, nonexistentArrays)
objParam: {}
stringParamB: ''
}
}

module directRefToCollectionViaLoopBody 'modulea.bicep' = [
for test in []: {
Expand Down Expand Up @@ -470,14 +468,13 @@ module anyTypeInScope 'empty.bicep' = {
scope: any(42)
}

module anyTypeInScopeConditional 'empty.bicep' =
if (false) {
dependsOn: [
any('s')
]
module anyTypeInScopeConditional 'empty.bicep' = if (false) {
dependsOn: [
any('s')
]

scope: any(42)
}
scope: any(42)
}

module anyTypeInScopeLoop 'empty.bicep' = [
for thing in []: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,21 @@ resource foo 'Microsoft.Foo/foos@2020-02-02-alpha'= if ( ) {
}

// invalid condition type
resource foo 'Microsoft.Foo/foos@2020-02-02-alpha' =
if (123) {
name: 'foo'
}
resource foo 'Microsoft.Foo/foos@2020-02-02-alpha' = if (123) {
name: 'foo'
}

// runtime functions are no allowed in resource conditions
resource foo 'Microsoft.Foo/foos@2020-02-02-alpha' =
if (reference('Micorosft.Management/managementGroups/MG', '2020-05-01').name == 'something') {
name: 'foo'
}
resource foo 'Microsoft.Foo/foos@2020-02-02-alpha' = if (reference(
'Micorosft.Management/managementGroups/MG',
'2020-05-01'
).name == 'something') {
name: 'foo'
}

resource foo 'Microsoft.Foo/foos@2020-02-02-alpha' =
if (listKeys('foo', '2020-05-01').bar == true) {
name: 'foo'
}
resource foo 'Microsoft.Foo/foos@2020-02-02-alpha' = if (listKeys('foo', '2020-05-01').bar == true) {
name: 'foo'
}

// duplicate property at the top level
resource foo 'Microsoft.Foo/foos@2020-02-02-alpha' = {
Expand Down Expand Up @@ -448,10 +448,9 @@ resource discriminatorKeyMissing 'Microsoft.Resources/deploymentScripts@2020-10-
/*
Discriminator key missing (conditional)
*/
resource discriminatorKeyMissing_if 'Microsoft.Resources/deploymentScripts@2020-10-01' =
if (true) {
// #completionTest(0,1,2) -> discriminatorProperty
}
resource discriminatorKeyMissing_if 'Microsoft.Resources/deploymentScripts@2020-10-01' = if (true) {
// #completionTest(0,1,2) -> discriminatorProperty
}

/*
Discriminator key missing (loop)
Expand Down Expand Up @@ -561,15 +560,14 @@ var discriminatorKeySetOneCompletions3 = discriminatorKeySetOne.properties[]
/*
Discriminator value set 1 (conditional)
*/
resource discriminatorKeySetOne_if 'Microsoft.Resources/deploymentScripts@2020-10-01' =
if (2 == 3) {
kind: 'AzureCLI'
// #completionTest(0,1,2) -> deploymentScriptTopLevel
resource discriminatorKeySetOne_if 'Microsoft.Resources/deploymentScripts@2020-10-01' = if (2 == 3) {
kind: 'AzureCLI'
// #completionTest(0,1,2) -> deploymentScriptTopLevel

properties: {
// #completionTest(0,1,2,3,4) -> deploymentScriptCliProperties
}
properties: {
// #completionTest(0,1,2,3,4) -> deploymentScriptCliProperties
}
}
// #completionTest(81) -> cliPropertyAccess
var discriminatorKeySetOneCompletions_if = discriminatorKeySetOne_if.properties.a
// #completionTest(81) -> cliPropertyAccess
Expand Down Expand Up @@ -782,14 +780,13 @@ var nestedDiscriminatorMissingKeyIndexCompletions = nestedDiscriminatorMissingKe
/*
Nested discriminator missing key (conditional)
*/
resource nestedDiscriminatorMissingKey_if 'Microsoft.DocumentDB/databaseAccounts@2020-06-01-preview' =
if (bool(1)) {
name: 'test'
location: 'l'
properties: {
//createMode: 'Default'
}
resource nestedDiscriminatorMissingKey_if 'Microsoft.DocumentDB/databaseAccounts@2020-06-01-preview' = if (bool(1)) {
name: 'test'
location: 'l'
properties: {
//createMode: 'Default'
}
}
// #completionTest(96) -> createMode
var nestedDiscriminatorMissingKeyCompletions_if = nestedDiscriminatorMissingKey_if.properties.cr
// #completionTest(98) -> createMode
Expand Down Expand Up @@ -863,14 +860,13 @@ var nestedDiscriminatorArrayIndexCompletions = nestedDiscriminator.properties[a]
/*
Nested discriminator (conditional)
*/
resource nestedDiscriminator_if 'Microsoft.DocumentDB/databaseAccounts@2020-06-01-preview' =
if (true) {
name: 'test'
location: 'l'
properties: {
createMode: 'Default'
}
resource nestedDiscriminator_if 'Microsoft.DocumentDB/databaseAccounts@2020-06-01-preview' = if (true) {
name: 'test'
location: 'l'
properties: {
createMode: 'Default'
}
}
// #completionTest(75) -> defaultCreateModeProperties
var nestedDiscriminatorCompletions_if = nestedDiscriminator_if.properties.a
// #completionTest(79) -> defaultCreateModeProperties
Expand Down Expand Up @@ -932,14 +928,13 @@ var nestedDiscriminatorCompletions4_for_if = nestedDiscriminator_for_if[0]['prop
var nestedDiscriminatorArrayIndexCompletions_for_if = nestedDiscriminator_for_if[0].properties[a]

// sample resource to validate completions on the next declarations
resource nestedPropertyAccessOnConditional 'Microsoft.Compute/virtualMachines@2020-06-01' =
if (true) {
location: 'test'
name: 'test'
properties: {
additionalCapabilities: {}
}
resource nestedPropertyAccessOnConditional 'Microsoft.Compute/virtualMachines@2020-06-01' = if (true) {
location: 'test'
name: 'test'
properties: {
additionalCapabilities: {}
}
}
// this validates that we can get nested property access completions on a conditional resource
//#completionTest(56) -> vmProperties
var sigh = nestedPropertyAccessOnConditional.properties.
Expand Down Expand Up @@ -1259,14 +1254,13 @@ resource directRefViaSingleResourceBody 'Microsoft.Network/dnszones@2018-05-01'
}
}

resource directRefViaSingleConditionalResourceBody 'Microsoft.Network/dnszones@2018-05-01' =
if (true) {
name: 'myZone3'
location: 'global'
properties: {
registrationVirtualNetworks: concat(premiumStorages, stuffs)
}
resource directRefViaSingleConditionalResourceBody 'Microsoft.Network/dnszones@2018-05-01' = if (true) {
name: 'myZone3'
location: 'global'
properties: {
registrationVirtualNetworks: concat(premiumStorages, stuffs)
}
}

@batchSize()
resource directRefViaSingleLoopResourceBody 'Microsoft.Network/virtualNetworks@2020-06-01' = [
Expand Down Expand Up @@ -1455,10 +1449,9 @@ resource anyTypeInScope 'Microsoft.Authorization/locks@2016-09-01' = {
scope: any(invalidExistingLocationRef)
}

resource anyTypeInScopeConditional 'Microsoft.Authorization/locks@2016-09-01' =
if (true) {
scope: any(invalidExistingLocationRef)
}
resource anyTypeInScopeConditional 'Microsoft.Authorization/locks@2016-09-01' = if (true) {
scope: any(invalidExistingLocationRef)
}

resource anyTypeInExistingScope 'Microsoft.Network/dnsZones/AAAA@2018-05-01' existing = {
parent: any('')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,17 @@ var flattenedEmptyArray = flatten([])

var mapSayHi = map(['abc', 'def', 'ghi'], foo => 'Hi ${foo}!')
var mapEmpty = map([], foo => 'Hi ${foo}!')
var mapObject = map(
range(0, length(doggos)),
i => {
i: i
doggo: doggos[i]
greeting: 'Ahoy, ${doggos[i]}!'
}
)
var mapObject = map(range(0, length(doggos)), i => {
i: i
doggo: doggos[i]
greeting: 'Ahoy, ${doggos[i]}!'
})
var mapArray = flatten(map(range(1, 3), i => [i * 2, (i * 2) + 1]))
var mapMultiLineArray = flatten(map(
range(1, 3),
i => [
i * 3
(i * 3) + 1
(i * 3) + 2
]
))
var mapMultiLineArray = flatten(map(range(1, 3), i => [
i * 3
(i * 3) + 1
(i * 3) + 2
]))

var filterEqualityCheck = filter(['abc', 'def', 'ghi'], foo => 'def' == foo)
var filterEmpty = filter([], foo => 'def' == foo)
Expand Down Expand Up @@ -94,30 +88,22 @@ var mappedModOutputProps = map(myMod.outputs.outputThis, doggo => '${doggo} says
var parentheses = map([123], (i => '${i}'))

var objectMap = toObject([123, 456, 789], i => '${i / 100}')
var objectMap2 = toObject(
range(0, 10),
i => '${i}',
i => {
isEven: (i % 2) == 0
isGreaterThan4: (i > 4)
}
)
var objectMap2 = toObject(range(0, 10), i => '${i}', i => {
isEven: (i % 2) == 0
isGreaterThan4: (i > 4)
})
var objectMap3 = toObject(sortByObjectKey, x => x.name)
var objectMap4 = toObject(sortByObjectKey, x => x.name)
var objectMap5 = toObject(
sortByObjectKey,
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx => xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.name
)
var objectMap6 = toObject(
range(0, 10),
i => '${i}',
i =>
// comment
{
isEven: (i % 2) == 0
isGreaterThan4: (i > 4)
}
)
var objectMap6 = toObject(range(0, 10), i => '${i}', i =>
// comment
{
isEven: (i % 2) == 0
isGreaterThan4: (i > 4)
})

var multiLine = reduce(['abc', 'def', 'ghi'], '', (cur, next) => concat(cur, next))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ module mySubscriptionMod 'modules/subscription.bicep' = {
scope: subscription('ee44cd78-68c6-43d9-874e-e684ec8d1191')
}

module mySubscriptionModWithCondition 'modules/subscription.bicep' =
if (length('foo') == 3) {
name: 'mySubscriptionModWithCondition'
scope: subscription('ee44cd78-68c6-43d9-874e-e684ec8d1191')
}
module mySubscriptionModWithCondition 'modules/subscription.bicep' = if (length('foo') == 3) {
name: 'mySubscriptionModWithCondition'
scope: subscription('ee44cd78-68c6-43d9-874e-e684ec8d1191')
}

module mySubscriptionModWithDuplicatedNameButDifferentScope 'modules/subscription_empty.bicep' = {
name: 'mySubscriptionMod'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ module modBWithCondition './child/moduleb.bicep' = if (1 + 1 == 2) {
}
}

module modBWithCondition2 './child/moduleb.bicep' =
// awkward comment
if (1 + 1 == 2) {
name: 'modBWithCondition2'
params: {
location: 'East US'
}
}

module modC './child/modulec.json' = {
name: 'modC'
params: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ module modBWithCondition './child/moduleb.bicep' = if (1 + 1 == 2) {
}
}

module modBWithCondition2 './child/moduleb.bicep' =
// awkward comment
if (1 + 1 == 2) {
name: 'modBWithCondition2'
params: {
location: 'East US'
}
}

module modC './child/modulec.json' = {
name: 'modC'
params: {
Expand Down
Loading

0 comments on commit ed77ca3

Please sign in to comment.