bicep 的 ast- grep 语境
安装
在pnpm项目中,运行:
pnpm install @ast-grep/lang-bicep
pnpm install @ast-grep/napi
# install the tree-sitter-cli if no prebuild is available
pnpm install tree-sitter-cli --save-dev
使用量
import bicep from '@ast-grep/lang-bicep'
import { registerDynamicLanguage, parse } from '@ast-grep/napi'
registerDynamicLanguage({ bicep })
const code = `
param location string = 'eastus'
param storageAccountName string = 'mystorageaccount'
resource storageAccount 'Microsoft.Storage/storageAccounts@2023-05-01' = {
name: storageAccountName
location: location
sku: {
name: 'Standard_LRS'
}
kind: 'StorageV2'
}
`
const sg = parse('bicep', code)
sg.root().kind()
关于Bicep
Azure Bicep是用于公开部署Azure资源的特定域语言(DSL). 它提供了相对于ARM模板的透明抽象,并旨在大幅简化作者的经验.
详情请访问 bicep 正式文件。 。 。 .
模式匹配
由于 Bicep 不支持 $ 作为标识符字符, ast- grep 用途 _ (下分数)作为图案匹配的扩展字符 :
// Find all parameter declarations
const params = sg.root().findAll('param $NAME string')
// Find all resource declarations
const resources = sg.root().findAll('resource $NAME $TYPE = { $$$ }')
注:当你写的时候 $ 在图案字符串中,ast-grep在内部将其翻译为 _ 当解析 Bicep 代码时.