Skip to content

Commit e0857e2

Browse files
Merge pull request #206393 from mumian/0729-linter-unquoted
new linter rule - prefer-unquoted-property-names
2 parents 42301c5 + 98627f9 commit e0857e2

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-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
@@ -2,7 +2,7 @@
22
title: Linter settings for Bicep config
33
description: Describes how to customize configuration values for the Bicep linter
44
ms.topic: conceptual
5-
ms.date: 07/21/2022
5+
ms.date: 07/29/2022
66
---
77

88
# Add linter settings in the Bicep config file
@@ -47,6 +47,9 @@ The following example shows the rules that are available for configuration.
4747
"prefer-interpolation": {
4848
"level": "warning"
4949
},
50+
"prefer-unquoted-property-names": {
51+
"level": "warning"
52+
},
5053
"secure-parameter-default": {
5154
"level": "warning"
5255
},
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
title: Linter rule - prefer unquoted property names
3+
description: Linter rule - prefer unquoted property names
4+
ms.topic: conceptual
5+
ms.date: 07/29/2022
6+
---
7+
8+
# Linter rule - prefer unquoted property names
9+
10+
This rule finds unnecessary single quotes where an object property name is declared and where an object property is dereferenced with array access.
11+
12+
In Bicep, quotes are optionally allowed when the object property keys contain numbers or special characters. For example, space, '-', or '.'. For more information, see [Objects](./data-types.md#objects).
13+
14+
## Linter rule code
15+
16+
Use the following value in the [Bicep configuration file](bicep-config-linter.md) to customize rule settings:
17+
18+
`prefer-unquoted-property-names`
19+
20+
## Solution
21+
22+
Quotes are not required in following code:
23+
24+
```bicep
25+
var obj = {
26+
newProp: {} // Property name is fine.
27+
'my-prop' : {} // Quotes are required.
28+
'1' : {} // Quotes are required.
29+
'myProp': {} // Quotes are NOT required.
30+
}
31+
32+
var x0 = obj.newProp // Code is fine.
33+
var x1 = obj['my-prop'] // Quotes and square brackets are required.
34+
var x2 = obj['1'] // Quotes and square brackets are required.
35+
var x3 = obj['myProp'] // Use obj.myProp instead.
36+
```
37+
38+
You can fix it by removing the unnecessary quotes:
39+
40+
```bicep
41+
var obj = {
42+
newProp: {}
43+
'my-prop' : {}
44+
'1' : {}
45+
myProp: {}
46+
}
47+
48+
49+
var x0 = obj.newProp
50+
var x1 = obj['my-prop']
51+
var x2 = obj['1']
52+
var x3 = obj.myProp
53+
```
54+
55+
## Next steps
56+
57+
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
@@ -2,7 +2,7 @@
22
title: Use Bicep linter
33
description: Learn how to use Bicep linter.
44
ms.topic: conceptual
5-
ms.date: 07/22/2022
5+
ms.date: 07/29/2022
66
---
77

88
# Use Bicep linter
@@ -29,6 +29,7 @@ The default set of linter rules is minimal and taken from [arm-ttk test cases](.
2929
- [no-unused-vars](./linter-rule-no-unused-variables.md)
3030
- [outputs-should-not-contain-secrets](./linter-rule-outputs-should-not-contain-secrets.md)
3131
- [prefer-interpolation](./linter-rule-prefer-interpolation.md)
32+
- [prefer-unquoted-property-names](./linter-rule-prefer-unquoted-property-names.md)
3233
- [secure-parameter-default](./linter-rule-secure-parameter-default.md)
3334
- [simplify-interpolation](./linter-rule-simplify-interpolation.md)
3435
- [use-protectedsettings-for-commandtoexecute-secrets](./linter-rule-use-protectedsettings-for-commandtoexecute-secrets.md)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,8 @@
380380
href: linter-rule-outputs-should-not-contain-secrets.md
381381
- name: Prefer interpolation
382382
href: linter-rule-prefer-interpolation.md
383+
- name: Prefer unquoted property names
384+
href: linter-rule-prefer-unquoted-property-names.md
383385
- name: Secure parameter default
384386
href: linter-rule-secure-parameter-default.md
385387
- name: Simplify interpolation

0 commit comments

Comments
 (0)