Skip to content

Commit

Permalink
Add linter rule to encourage usage of UDTs
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-c-martin committed Dec 13, 2024
1 parent b2ae5b6 commit 5ff8306
Show file tree
Hide file tree
Showing 29 changed files with 249 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,5 @@ resource resE 'My.Rp/myResourceType/childType@2020-01-01' = {
}

output resourceCProperties object = resC.properties
//@[27:33) [use-user-defined-types (Warning)] Use user-defined types instead of 'object' or 'array'. (bicep core linter https://aka.ms/bicep/linter/use-user-defined-types) |object|

Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@ output foo string = buildUrl(true, 'google.com', 'search')
func sayHello(name string) string => 'Hi ${name}!'

output hellos array = map(['Evie', 'Casper'], name => sayHello(name))
//@[14:19) [use-user-defined-types (Warning)] Use user-defined types instead of 'object' or 'array'. (bicep core linter https://aka.ms/bicep/linter/use-user-defined-types) |array|

func objReturnType(name string) object => {
//@[32:38) [use-user-defined-types (Warning)] Use user-defined types instead of 'object' or 'array'. (bicep core linter https://aka.ms/bicep/linter/use-user-defined-types) |object|
hello: 'Hi ${name}!'
}

func arrayReturnType(name string) array => [
//@[34:39) [use-user-defined-types (Warning)] Use user-defined types instead of 'object' or 'array'. (bicep core linter https://aka.ms/bicep/linter/use-user-defined-types) |array|
name
]

func asdf(name string) array => [
//@[23:28) [use-user-defined-types (Warning)] Use user-defined types instead of 'object' or 'array'. (bicep core linter https://aka.ms/bicep/linter/use-user-defined-types) |array|
'asdf'
name
]
Expand All @@ -25,13 +29,18 @@ type positiveInt = int
func typedArg(input string[]) positiveInt => length(input)

func barTest() array => ['abc', 'def']
//@[15:20) [use-user-defined-types (Warning)] Use user-defined types instead of 'object' or 'array'. (bicep core linter https://aka.ms/bicep/linter/use-user-defined-types) |array|
func fooTest() array => map(barTest(), a => 'Hello ${a}!')
//@[15:20) [use-user-defined-types (Warning)] Use user-defined types instead of 'object' or 'array'. (bicep core linter https://aka.ms/bicep/linter/use-user-defined-types) |array|

output fooValue array = fooTest()
//@[16:21) [use-user-defined-types (Warning)] Use user-defined types instead of 'object' or 'array'. (bicep core linter https://aka.ms/bicep/linter/use-user-defined-types) |array|

func test() object => loadJsonContent('./repro-data.json')
//@[12:18) [use-user-defined-types (Warning)] Use user-defined types instead of 'object' or 'array'. (bicep core linter https://aka.ms/bicep/linter/use-user-defined-types) |object|
func test2() string => loadTextContent('./repro-data.json')
func test3() object => loadYamlContent('./repro-data.json')
//@[13:19) [use-user-defined-types (Warning)] Use user-defined types instead of 'object' or 'array'. (bicep core linter https://aka.ms/bicep/linter/use-user-defined-types) |object|
func test4() string => loadFileAsBase64('./repro-data.json')

// validate formatter works (https://github.com/Azure/bicep/issues/12913)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ param funcvarparam bool = concat
//@[06:18) [no-unused-params (Warning)] Parameter "funcvarparam" is declared but never used. (bicep core linter https://aka.ms/bicep/linter/no-unused-params) |funcvarparam|
//@[26:32) [BCP063 (Error)] The name "concat" is not a parameter, variable, resource or module. (bicep https://aka.ms/bicep/core-diagnostics#BCP063) |concat|
output funcvarout array = padLeft
//@[18:23) [use-user-defined-types (Warning)] Use user-defined types instead of 'object' or 'array'. (bicep core linter https://aka.ms/bicep/linter/use-user-defined-types) |array|
//@[26:33) [BCP063 (Error)] The name "padLeft" is not a parameter, variable, resource or module. (bicep https://aka.ms/bicep/core-diagnostics#BCP063) |padLeft|

// non-existent function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func noLambda3 = string 'asdf'
//@[15:16) [BCP018 (Error)] Expected the "(" character at this location. (bicep https://aka.ms/bicep/core-diagnostics#BCP018) |=|

func argLengthMismatch(a string, b string, c string) array => ([a, b, c])
//@[53:58) [use-user-defined-types (Warning)] Use user-defined types instead of 'object' or 'array'. (bicep core linter https://aka.ms/bicep/linter/use-user-defined-types) |array|
var sdf = argLengthMismatch('asdf')
//@[04:07) [no-unused-vars (Warning)] Variable "sdf" is declared but never used. (bicep core linter https://aka.ms/bicep/linter/no-unused-vars) |sdf|
//@[27:35) [BCP071 (Error)] Expected 3 arguments, but got 1. (bicep https://aka.ms/bicep/core-diagnostics#BCP071) |('asdf')|
Expand All @@ -38,6 +39,7 @@ var asdfwdf = noLambda('asd')

func sayHello(name string) string => 'Hi ${name}!'
output hellos array = map(['Evie', 'Casper'], sayHello) // this syntax not supported currently, but should it be?
//@[14:19) [use-user-defined-types (Warning)] Use user-defined types instead of 'object' or 'array'. (bicep core linter https://aka.ms/bicep/linter/use-user-defined-types) |array|
//@[46:54) [BCP063 (Error)] The name "sayHello" is not a parameter, variable, resource or module. (bicep https://aka.ms/bicep/core-diagnostics#BCP063) |sayHello|

func sayHelloBadNewlines(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
param ids array
//@[10:15) [use-user-defined-types (Warning)] Use user-defined types instead of 'object' or 'array'. (bicep core linter https://aka.ms/bicep/linter/use-user-defined-types) |array|

var flatten1 = flatten('abc')
//@[04:12) [no-unused-vars (Warning)] Variable "flatten1" is declared but never used. (bicep core linter https://aka.ms/bicep/linter/no-unused-vars) |flatten1|
Expand Down Expand Up @@ -146,17 +147,23 @@ resource stg 'Microsoft.Storage/storageAccounts@2021-09-01' = [for i in range(0,
}]

output stgKeys array = map(range(0, 2), i => stg[i].listKeys().keys[0].value)
//@[15:20) [use-user-defined-types (Warning)] Use user-defined types instead of 'object' or 'array'. (bicep core linter https://aka.ms/bicep/linter/use-user-defined-types) |array|
//@[49:50) [BCP247 (Error)] Using lambda variables inside resource or module array access is not currently supported. Found the following lambda variable(s) being accessed: "i". (bicep https://aka.ms/bicep/core-diagnostics#BCP247) |i|
output stgKeys2 array = map(range(0, 2), j => stg[((j + 2) % 123)].listKeys().keys[0].value)
//@[16:21) [use-user-defined-types (Warning)] Use user-defined types instead of 'object' or 'array'. (bicep core linter https://aka.ms/bicep/linter/use-user-defined-types) |array|
//@[50:65) [BCP247 (Error)] Using lambda variables inside resource or module array access is not currently supported. Found the following lambda variable(s) being accessed: "j". (bicep https://aka.ms/bicep/core-diagnostics#BCP247) |((j + 2) % 123)|
output stgKeys3 array = map(ids, id => listKeys(id, stg[0].apiVersion).keys[0].value)
//@[16:21) [use-user-defined-types (Warning)] Use user-defined types instead of 'object' or 'array'. (bicep core linter https://aka.ms/bicep/linter/use-user-defined-types) |array|
//@[39:70) [outputs-should-not-contain-secrets (Warning)] Outputs should not contain secrets. Found possible secret: function 'listKeys' (bicep core linter https://aka.ms/bicep/linter/outputs-should-not-contain-secrets) |listKeys(id, stg[0].apiVersion)|
//@[39:70) [BCP248 (Error)] Using lambda variables inside the "listKeys" function is not currently supported. Found the following lambda variable(s) being accessed: "id". (bicep https://aka.ms/bicep/core-diagnostics#BCP248) |listKeys(id, stg[0].apiVersion)|
output accessTiers array = map(range(0, 2), k => stg[k].properties.accessTier)
//@[19:24) [use-user-defined-types (Warning)] Use user-defined types instead of 'object' or 'array'. (bicep core linter https://aka.ms/bicep/linter/use-user-defined-types) |array|
//@[53:54) [BCP247 (Error)] Using lambda variables inside resource or module array access is not currently supported. Found the following lambda variable(s) being accessed: "k". (bicep https://aka.ms/bicep/core-diagnostics#BCP247) |k|
output accessTiers2 array = map(range(0, 2), x => map(range(0, 2), y => stg[x / y].properties.accessTier))
//@[20:25) [use-user-defined-types (Warning)] Use user-defined types instead of 'object' or 'array'. (bicep core linter https://aka.ms/bicep/linter/use-user-defined-types) |array|
//@[76:81) [BCP247 (Error)] Using lambda variables inside resource or module array access is not currently supported. Found the following lambda variable(s) being accessed: "x", "y". (bicep https://aka.ms/bicep/core-diagnostics#BCP247) |x / y|
output accessTiers3 array = map(ids, foo => reference('${foo}').accessTier)
//@[20:25) [use-user-defined-types (Warning)] Use user-defined types instead of 'object' or 'array'. (bicep core linter https://aka.ms/bicep/linter/use-user-defined-types) |array|
//@[44:63) [BCP248 (Error)] Using lambda variables inside the "reference" function is not currently supported. Found the following lambda variable(s) being accessed: "foo". (bicep https://aka.ms/bicep/core-diagnostics#BCP248) |reference('${foo}')|

module modLoop './empty.bicep' = [for item in range(0, 5): {
Expand All @@ -167,6 +174,7 @@ var modLoopNames = map(modLoop, i => i.name)
//@[04:16) [no-unused-vars (Warning)] Variable "modLoopNames" is declared but never used. (bicep core linter https://aka.ms/bicep/linter/no-unused-vars) |modLoopNames|
//@[23:30) [BCP144 (Error)] Directly referencing a resource or module collection is not currently supported here. Apply an array indexer to the expression. (bicep https://aka.ms/bicep/core-diagnostics#BCP144) |modLoop|
output modOutputs array = map(range(0, 5), i => modLoop[i].outputs.foo)
//@[18:23) [use-user-defined-types (Warning)] Use user-defined types instead of 'object' or 'array'. (bicep core linter https://aka.ms/bicep/linter/use-user-defined-types) |array|
//@[43:70) [BCP070 (Error)] Argument of type "int => error" is not assignable to parameter of type "(any[, int]) => any". (bicep https://aka.ms/bicep/core-diagnostics#BCP070) |i => modLoop[i].outputs.foo|
//@[56:57) [BCP247 (Error)] Using lambda variables inside resource or module array access is not currently supported. Found the following lambda variable(s) being accessed: "i". (bicep https://aka.ms/bicep/core-diagnostics#BCP247) |i|
//@[67:70) [BCP052 (Error)] The type "outputs" does not contain property "foo". (bicep https://aka.ms/bicep/core-diagnostics#BCP052) |foo|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ module nonexistentArrays 'modulea.bicep' = [for evenMoreDuplicates in alsoDoesNo
}]

output directRefToCollectionViaOutput array = nonexistentArrays
//@[038:043) [use-user-defined-types (Warning)] Use user-defined types instead of 'object' or 'array'. (bicep core linter https://aka.ms/bicep/linter/use-user-defined-types) |array|
//@[046:063) [BCP144 (Error)] Directly referencing a resource or module collection is not currently supported here. Apply an array indexer to the expression. (bicep https://aka.ms/bicep/core-diagnostics#BCP144) |nonexistentArrays|

module directRefToCollectionViaSingleBody 'modulea.bicep' = {
Expand Down
Loading

0 comments on commit 5ff8306

Please sign in to comment.