Skip to content

Commit 7481f09

Browse files
committed
New linter rule - use safe access
1 parent f3be22b commit 7481f09

File tree

4 files changed

+116
-2
lines changed

4 files changed

+116
-2
lines changed

articles/azure-resource-manager/bicep/bicep-config-linter.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Linter settings for Bicep config
33
description: Describes how to customize configuration values for the Bicep linter
44
ms.topic: conceptual
55
ms.custom: devx-track-bicep
6-
ms.date: 05/06/2024
6+
ms.date: 07/19/2024
77
---
88

99
# Add linter settings in the Bicep config file
@@ -121,6 +121,9 @@ The following example shows the rules that are available for configuration.
121121
"use-resource-symbol-reference": {
122122
"level": "warning"
123123
},
124+
"use-safe-access": {
125+
"level": "warning"
126+
},
124127
"use-secure-value-for-secure-inputs": {
125128
"level": "error"
126129
},
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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).

articles/azure-resource-manager/bicep/linter.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Use Bicep linter
33
description: Learn how to use Bicep linter.
44
ms.topic: how-to
55
ms.custom: devx-track-bicep
6-
ms.date: 05/06/2024
6+
ms.date: 07/19/2024
77
---
88

99
# Use Bicep linter
@@ -50,6 +50,7 @@ The default set of linter rules is minimal and taken from [arm-ttk test cases](.
5050
- [use-recent-api-versions](./linter-rule-use-recent-api-versions.md)
5151
- [use-resource-id-functions](./linter-rule-use-resource-id-functions.md)
5252
- [use-resource-symbol-reference](./linter-rule-use-resource-symbol-reference.md)
53+
- [use-safe-access](./linter-rule-use-safe-access.md)
5354
- [use-secure-value-for-secure-inputs](./linter-rule-use-secure-value-for-secure-inputs.md)
5455
- [use-stable-resource-identifiers](./linter-rule-use-stable-resource-identifier.md)
5556
- [use-stable-vm-image](./linter-rule-use-stable-vm-image.md)

articles/azure-resource-manager/bicep/toc.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,9 @@
549549
- name: Use resource symbol reference
550550
displayName: linter
551551
href: linter-rule-use-resource-symbol-reference.md
552+
- name: Use safe access
553+
displayName: linter
554+
href: linter-rule-use-safe-access.md
552555
- name: Use secure value for secure inputs
553556
displayName: linter
554557
href: linter-rule-use-secure-value-for-secure-inputs.md

0 commit comments

Comments
 (0)