Azure/bicep

Permit newline breaks in ternary (`a ? b : c`), `for` and `if` statements

anthony-c-martin opened this issue · 9 comments

Created from discussion in #146

If, For and Ternary statements often end up taking up a lot of horizontal space, and it would be nice to have the freedom to use line breaks to avoid this in Bicep. See below for some mockups.

Ternary

var foo = abc ? def : ghi
// could be rewritten as
var foo = abc ?
  def :
  ghi

If

resource foo 'Microsoft.Authorization/roleDefinitions@2018-01-01-preview' = if (someCondition) {
...
// could be rewritten as
resource foo 'Microsoft.Authorization/roleDefinitions@2018-01-01-preview' =
  if (someCondition) {
    ...
  }

For

resource foo 'Microsoft.Authorization/roleDefinitions@2018-01-01-preview' = [for (val, i) in someArray: {
...
// could be rewritten as
resource foo 'Microsoft.Authorization/roleDefinitions@2018-01-01-preview' =
  [for (val, i) in someArray: {
    ...
   }]

How could look with both if and for

resource foo 'Microsoft.Authorization/roleDefinitions@2018-01-01-preview' = [for (val, i) in someArray: if (someCondition) {
...
// could be rewritten as
resource foo 'Microsoft.Authorization/roleDefinitions@2018-01-01-preview' =
  [for (val, i) in someArray: 
  if (someCondition) {
...

Is this something that's on the roadmap?
Also, I saw that this issue has at least one duplicate, #1760.

@anthony-c-martin is there any plans to continue the multi-line work for ternary operators and if statements?

Hi anthony-c-martin, this issue has been marked as stale because it was labeled as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. Thanks for contributing to bicep! 😄 🦾

@anthony-c-martin @alex-frankel can this be re-opened? It is not completed and actually I have recently asked for status.

Another case to add here is the lambdas:

Lambda

(a, b, c) => '${a}-${b}-${c}'
(a, b, c) =>
  '${a}-${b}-${c}'

This is somewhat related to the prettier formatter. I'd be interested in implementing this.

Progress:

  • if-expression
  • for-expression
  • conditional (ternary) operator
  • lambdas

I note a difference in the ternary implementation from #5829 (comment)

Ternary

var foo = abc ? def : ghi
// could be rewritten as
var foo = abc ?
  def :
  ghi

The supported syntax ended up being the following instead.

var foo = abc
  ? def
  : ghi

ref: https://github.com/Azure/bicep/blob/v0.19.5/src/Bicep.Core.Samples/Files/PrettyPrint_LF/main.syntax.bicep#L1430-L1449

@shenglol it seems new line does not work inside for(). Below gives error and it is not the same if you do not use for().

module modules 'modules/power-bi-embedded-capacities.bicep' = [for (resourceGroup, i) in resourceGroups:
  if (!empty(resourceGroup.modules)) {
  name: 'modules-${uniqueString(deploymentLocation)}-${i}'
  scope: resourceGroupsRes[i]
  params: {
    modules: union(defaultResourceGroup, resourceGroup).modules
  }
}]

@shenglol also does not work on module names
image