|
| 1 | +--- |
| 2 | +title: Linter rule - Use the safe access (.?) operator |
| 3 | +description: Use the safe access (.?) operator instead of checking object contents with the 'contains' function. |
| 4 | +ms.topic: conceptual |
| 5 | +ms.custom: devx-track-bicep |
| 6 | +ms.date: 07/19/2024 |
| 7 | +--- |
| 8 | + |
| 9 | +# Linter rule - use the safe access operator |
| 10 | + |
| 11 | +This rule looks for the use of the [`contains()`](./bicep-functions-object.md#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](./operator-safe-dereference.md). |
| 12 | + |
| 13 | +The specific patterns it's looking for are: |
| 14 | + |
| 15 | +- Ternary operator to check for property access: |
| 16 | + |
| 17 | + ```bicep |
| 18 | + contains(<object>, '<property>') ? <object>.<property> : <default-value> |
| 19 | + ``` |
| 20 | +
|
| 21 | + The following replacement is suggested: |
| 22 | +
|
| 23 | + ```bicep |
| 24 | + <object>.?<property> ?? <default-value> |
| 25 | + ``` |
| 26 | +
|
| 27 | +- Ternary operator to check for variable-named property access: |
| 28 | +
|
| 29 | + ```bicep |
| 30 | + contains(<object>, <property-name>) ? foo[<property-name>] : <default-value> |
| 31 | + ``` |
| 32 | +
|
| 33 | + The following replacement is suggested: |
| 34 | +
|
| 35 | + ```bicep |
| 36 | + <object>[?<property-name>] ?? <default-value> |
| 37 | + ``` |
| 38 | +
|
| 39 | +## Linter rule code |
| 40 | +
|
| 41 | +To customize rule settings, use the following value in the [Bicep configuration file](./bicep-config-linter.md): |
| 42 | +
|
| 43 | +`use-safe-access` |
| 44 | +
|
| 45 | +## Solution |
| 46 | +
|
| 47 | +Accept the editor code action to automatically perform the refactor. |
| 48 | +
|
| 49 | +## Examples |
| 50 | +
|
| 51 | +### Named Property Access |
| 52 | +
|
| 53 | +The following example triggers the rule: |
| 54 | +
|
| 55 | +```bicep |
| 56 | +param foo object |
| 57 | +var test = contains(foo, 'bar') ? foo.bar : 'baz' |
| 58 | +``` |
| 59 | + |
| 60 | +Accepting the code action results in the following Bicep: |
| 61 | + |
| 62 | +```bicep |
| 63 | +param foo object |
| 64 | +var test = foo.?bar ?? 'baz' |
| 65 | +``` |
| 66 | + |
| 67 | +### Variable Property Access |
| 68 | + |
| 69 | +The following example triggers the rule: |
| 70 | + |
| 71 | +```bicep |
| 72 | +param foo object |
| 73 | +param target string |
| 74 | +var test = contains(foo, target) ? foo[target] : 'baz' |
| 75 | +``` |
| 76 | + |
| 77 | +Accepting the code action results in the following Bicep: |
| 78 | + |
| 79 | +```bicep |
| 80 | +param foo object |
| 81 | +param target string |
| 82 | +var test = foo[?target] ?? 'baz' |
| 83 | +``` |
| 84 | + |
| 85 | +### Non-issues |
| 86 | + |
| 87 | +The following examples don't trigger the rule: |
| 88 | + |
| 89 | +Difference between the property being checked and accessed: |
| 90 | + |
| 91 | +```bicep |
| 92 | +param foo object |
| 93 | +var test = contains(foo, 'bar') ? foo.baz : 'baz' |
| 94 | +``` |
| 95 | + |
| 96 | +Difference between the variable property being checked and accessed: |
| 97 | + |
| 98 | +```bicep |
| 99 | +param foo object |
| 100 | +param target string |
| 101 | +param notTarget string |
| 102 | +var test = contains(foo, target) ? bar[notTarget] : 'baz' |
| 103 | +``` |
| 104 | + |
| 105 | +## Next steps |
| 106 | + |
| 107 | +For more information about the linter, see [Use Bicep linter](./linter.md). |
0 commit comments