Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Bicep support #3027

Merged
merged 9 commits into from Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions components.json
Expand Up @@ -184,6 +184,10 @@
},
"owner": "RunDevelopment"
},
"bicep": {
"title": "Bicep",
"owner": "johnnyreilly"
},
"birb": {
"title": "Birb",
"require": "clike",
Expand Down
29 changes: 29 additions & 0 deletions components/prism-bicep.js
@@ -0,0 +1,29 @@
// based loosely upon: https://github.com/Azure/bicep/blob/main/src/textmate/bicep.tmlanguage
Prism.languages.bicep = {
'comment': [
{
// multiline comments eg /* ASDF */
pattern: /(^|[^\\])\/\*[\s\S]*?(?:\*\/|$)/,
lookbehind: true,
greedy: true
},
{
// singleline comments eg // ASDF
pattern: /(^|[^\\:])\/\/.*/,
lookbehind: true,
greedy: true
}
],
'string': {
// this doesn't handle string interpolalation or multiline strings as yet
pattern: /(')(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,
johnnyreilly marked this conversation as resolved.
Show resolved Hide resolved
greedy: true
},
'number': /(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:E[+-]?\d+)?/i,
'boolean': /\b(?:true|false)\b/,
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
'keyword': /\b(?:targetScope|resource|module|param|var|output|for|in|if|existing|null)\b/i, // https://github.com/Azure/bicep/blob/114a3251b4e6e30082a58729f19a8cc4e374ffa6/src/textmate/bicep.tmlanguage#L184
'function': /\b(?:array|concat|contains|createArray|empty|first|intersection|last|length|max|min|range|skip|take|union)(?:\$|\b)/i, // https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/template-functions-array
'operator': /--|\+\+|\*\*=?|=>|&&=?|\|\|=?|[!=]==|<<=?|>>>?=?|[-+*/%&|^!=<>]=?|\.{3}|\?\?=?|\?\.?|[~:]/,
'punctuation': /[{}[\];(),.:]/,
'decorator': /@\w+\b/
};
1 change: 1 addition & 0 deletions components/prism-bicep.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions examples/prism-bicep.html
@@ -0,0 +1,19 @@
<h2>Variable assignment</h2>
<pre><code>var foo = 'bar'</code></pre>

<h2>Operators</h2>
<pre><code>(1 + 2 * 3)/4 >= 3 &amp;&amp; 4 &lt; 5 || 6 > 7</code></pre>

<h2>Keywords</h2>
<pre><code>resource appServicePlan 'Microsoft.Web/serverfarms@2020-09-01' existing = = if (diagnosticsEnabled) {
name: logAnalyticsWsName
}
module cosmosDb './cosmosdb.bicep' = {
name: 'cosmosDbDeploy'
}
param env string
var oneNumber = 123
output databaseName string = cosmosdbDatabaseName
for item in cosmosdbAllowedIpAddresses: {
ipAddressOrRange: item
}</code></pre>
13 changes: 13 additions & 0 deletions tests/languages/bicep/boolean_feature.test
@@ -0,0 +1,13 @@
true
false

----------------------------------------------------

[
["boolean", "true"],
["boolean", "false"]
]

----------------------------------------------------

Checks for booleans.
23 changes: 23 additions & 0 deletions tests/languages/bicep/decorator_feature.test
@@ -0,0 +1,23 @@
@secure()
param demoPassword string
@allowed([
'one'
'two'
])
param demoEnum string

----------------------------------------------------

[
["decorator", "@secure"], ["punctuation", "("], ["punctuation", ")"],
["keyword", "param"], " demoPassword string\r\n",
["decorator", "@allowed"], ["punctuation", "("], ["punctuation", "["],
["string", "'one'"],
["string", "'two'"],
["punctuation", "]"], ["punctuation", ")"],
["keyword", "param"], " demoEnum string"
]

----------------------------------------------------

Checks for functions. Also checks for unicode characters in identifiers.
39 changes: 39 additions & 0 deletions tests/languages/bicep/function_feature.test
@@ -0,0 +1,39 @@
array
concat
contains
createArray
empty
first
intersection
last
length
max
min
range
skip
take
union

----------------------------------------------------

[
["function", "array"],
["function", "concat"],
["function", "contains"],
["function", "createArray"],
["function", "empty"],
["function", "first"],
["function", "intersection"],
["function", "last"],
["function", "length"],
["function", "max"],
["function", "min"],
["function", "range"],
["function", "skip"],
["function", "take"],
["function", "union"]
]

----------------------------------------------------

Checks for functions. Based upon https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/template-functions-array
85 changes: 85 additions & 0 deletions tests/languages/bicep/keyword_feature.test
@@ -0,0 +1,85 @@
targetScope
resource appServicePlan 'Microsoft.Web/serverfarms@2020-09-01' existing = = if (diagnosticsEnabled) {
name: logAnalyticsWsName
}
module cosmosDb './cosmosdb.bicep' = {
name: 'cosmosDbDeploy'
}
param env string
var oneNumber = 123
output databaseName string = cosmosdbDatabaseName
for item in cosmosdbAllowedIpAddresses: {
ipAddressOrRange: item
}
if
null

----------------------------------------------------

[
["keyword", "targetScope"],

["keyword", "resource"],
" appServicePlan ",
["string", "'Microsoft.Web/serverfarms@2020-09-01'"],
["keyword", "existing"],
["operator", "="],
["operator", "="],
["keyword", "if"],
["punctuation", "("],
"diagnosticsEnabled",
["punctuation", ")"],
["punctuation", "{"],

"\r\n name",
["operator", ":"],
" logAnalyticsWsName\r\n",

["punctuation", "}"],

["keyword", "module"],
" cosmosDb ",
["string", "'./cosmosdb.bicep'"],
["operator", "="],
["punctuation", "{"],

"\r\n name",
["operator", ":"],
["string", "'cosmosDbDeploy'"],

["punctuation", "}"],

["keyword", "param"],
" env string\r\n",

["keyword", "var"],
" oneNumber ",
["operator", "="],
["number", "123"],

["keyword", "output"],
" databaseName string ",
["operator", "="],
" cosmosdbDatabaseName\r\n",

["keyword", "for"],
" item ",
["keyword", "in"],
" cosmosdbAllowedIpAddresses",
["operator", ":"],
["punctuation", "{"],

"\r\n\tipAddressOrRange",
["operator", ":"],
" item\r\n",

["punctuation", "}"],

["keyword", "if"],

["keyword", "null"]
]

----------------------------------------------------

Checks for all keywords.
73 changes: 73 additions & 0 deletions tests/languages/bicep/number_feature.test
@@ -0,0 +1,73 @@
42
3.14159
4e10
3.2E+6
2.1e-10
0b1101
0o571
0xbabe
0xBABE

NaN
Infinity

123n
0x123n

1_000_000_000_000
1_000_000.220_720
0b0101_0110_0011_1000
0o12_34_56
0x40_76_38_6A_73
4_642_473_943_484_686_707n
0.000_001
1e10_000

----------------------------------------------------

[
["number", "42"],
["number", "3.14159"],
["number", "4e10"],
["number", "3.2E+6"],
["number", "2.1e-10"],
["number", "0"], "b1101\r\n",
["number", "0"], "o571\r\n",
["number", "0"], "xbabe\r\n",
["number", "0"], "xBABE\r\n\r\nNaN\r\nInfinity\r\n\r\n",

["number", "123"], "n\r\n",
["number", "0"], "x123n\r\n\r\n",

["number", "1"],
"_000_000_000_000\r\n",

["number", "1"],
"_000_000",
["punctuation", "."],
["number", "220"],
"_720\r\n",

["number", "0"],
"b0101_0110_0011_1000\r\n",

["number", "0"],
"o12_34_56\r\n",

["number", "0"],
"x40_76_38_6A_73\r\n",

["number", "4"],
"_642_473_943_484_686_707n\r\n",

["number", "0.000"],
"_001\r\n",

["number", "1e10"],
"_000"
]

----------------------------------------------------

Checks for decimal numbers, binary numbers, octal numbers, hexadecimal numbers.
Also checks for keywords representing numbers.
35 changes: 35 additions & 0 deletions tests/languages/bicep/operator_feature.test
@@ -0,0 +1,35 @@
- -- -=
+ ++ +=
< <= << <<=
> >= >> >>= >>> >>>=
= == === =>
! != !==
& && &= &&=
| || |= ||=
* ** *= **=
/ /= ~
^ ^= % %=
? : ...
?? ?. ??=

----------------------------------------------------

[
["operator", "-"], ["operator", "--"], ["operator", "-="],
["operator", "+"], ["operator", "++"], ["operator", "+="],
["operator", "<"], ["operator", "<="], ["operator", "<<"], ["operator", "<<="],
["operator", ">"], ["operator", ">="], ["operator", ">>"], ["operator", ">>="], ["operator", ">>>"], ["operator", ">>>="],
["operator", "="], ["operator", "=="], ["operator", "==="], ["operator", "=>"],
["operator", "!"], ["operator", "!="], ["operator", "!=="],
["operator", "&"], ["operator", "&&"], ["operator", "&="], ["operator", "&&="],
["operator", "|"], ["operator", "||"], ["operator", "|="], ["operator", "||="],
["operator", "*"], ["operator", "**"], ["operator", "*="], ["operator", "**="],
["operator", "/"], ["operator", "/="], ["operator", "~"],
["operator", "^"], ["operator", "^="], ["operator", "%"], ["operator", "%="],
["operator", "?"], ["operator", ":"], ["operator", "..."],
["operator", "??"], ["operator", "?."], ["operator", "??="]
]

----------------------------------------------------

Checks for all operators.
13 changes: 13 additions & 0 deletions tests/languages/bicep/string_feature.test
@@ -0,0 +1,13 @@
'Microsoft.Web/sites/config@2020-12-01'
'https://${frontDoorName}.azurefd.net/.auth/login/aad/callback'

----------------------------------------------------

[
["string", "'Microsoft.Web/sites/config@2020-12-01'"],
["string", "'https://${frontDoorName}.azurefd.net/.auth/login/aad/callback'"]
]

----------------------------------------------------

Checks for strings.
25 changes: 25 additions & 0 deletions tests/languages/bicep/var_feature.test
@@ -0,0 +1,25 @@
var oneString = 'abc'
var oneNumber = 123
var numbers = [
123
]
var oneObject = {
abc: 123
}

----------------------------------------------------

[
["keyword", "var"], " oneString ", ["operator", "="], ["string", "'abc'"],
["keyword", "var"], " oneNumber ", ["operator", "="], ["number", "123"],
["keyword", "var"], " numbers ", ["operator", "="], ["punctuation", "["],
["number", "123"],
["punctuation", "]"],
["keyword", "var"], " oneObject ", ["operator", "="], ["punctuation", "{"],
"\r\n\tabc", ["operator", ":"], ["number", "123"],
["punctuation", "}"]
]

----------------------------------------------------

Checks for vars.