Редактиране

Споделяне чрез


Linter rule - use the safe access operator

This rule looks for the use of the contains() function for checking property existence before access and provides a simpler automatic replacement. It serves to recommend and introduce users to a simplified equivalent syntax without introducing any functional code changes. For more information, see Safe dereference operator.

The specific patterns it's looking for are:

  • Ternary operator to check for property access:

    contains(<object>, '<property>') ? <object>.<property> : <default-value>
    

    The following replacement is suggested:

    <object>.?<property> ?? <default-value>
    
  • Ternary operator to check for variable-named property access:

    contains(<object>, <property-name>) ? foo[<property-name>] : <default-value>
    

    The following replacement is suggested:

    <object>[?<property-name>] ?? <default-value>
    

Linter rule code

To customize rule settings, use the following value in the Bicep configuration file:

use-safe-access

Solution

Accept the editor code action to automatically perform the refactor.

Examples

Named Property Access

The following example triggers the rule:

param foo object
var test = contains(foo, 'bar') ? foo.bar : 'baz'

Accepting the code action results in the following Bicep:

param foo object
var test = foo.?bar ?? 'baz'

Variable Property Access

The following example triggers the rule:

param foo object
param target string
var test = contains(foo, target) ? foo[target] : 'baz'

Accepting the code action results in the following Bicep:

param foo object
param target string
var test = foo[?target] ?? 'baz'

Non-issues

The following examples don't trigger the rule:

Difference between the property being checked and accessed:

param foo object
var test = contains(foo, 'bar') ? foo.baz : 'baz'

Difference between the variable property being checked and accessed:

param foo object
param target string
param notTarget string
var test = contains(foo, target) ? bar[notTarget] : 'baz'

Next steps

For more information about the linter, see Use Bicep linter.