terradart_hcl
A pure Dart front-end for Terraform configurations: an HCL native-syntax
parser, a *.tf.json decoder, and a shared module model (TfModule) that
both produce. It is the input side of terradart migrate (the HCL → Dart
migrator, #80) and
depends on no other TerraDart package.
What it does
- Lossless structure. Block bodies are ordered lists of entries, so
repeated blocks (
container {}×2,env {}×3,lifecycle_rule {}×2) survive exactly as written. Every node carries aSourceRange, and comments are kept on the entry they precede, so a caller can copy any block back out verbatim. - Shallow expressions. Literals, tuples, objects, traversals
(
google_pubsub_topic.t.name,var.x,local.y[0]) and templates ("${var.a}-x", heredocs) are parsed exactly. Everything else — function calls, operators, conditionals,for, splats — is kept asRawExprwith its verbatim source and balanced brackets. Migration needs classification, not evaluation, so the full HCL expression grammar is deliberately not implemented. - Two front-ends, one model.
parseHclanddecodeTfJsonboth yield anHclFile;TfModule.fromFilesreads the Terraform structure (terraform settings, providers, variables, locals, outputs, resources, data sources, module calls, andmoved/import/check/removedkept opaque) from either. - Serializer.
HclWriterrenders a file or expression back to HCL; parse → write → parse is structurally identical.
Usage
import 'package:terradart_hcl/terradart_hcl.dart';
void describe(String source) {
final file = parseHcl(source, fileName: 'main.tf');
for (final block in file.body.blocksOf('resource')) {
print('${block.labels[0].text}.${block.labels[1].text}');
}
final module = TfModule.fromFiles([file]);
for (final r in module.resources) {
final name = r.body.attribute('name')?.value; // an Expr
print('${r.type}.${r.name}: ${name is LiteralExpr ? name.value : name}');
}
}
Conformance
test/specsuite_test.dart runs every case of the
hashicorp/hcl specsuite
through the parser: cases the suite expects to succeed must parse, and the
heredoc / literal cases are checked against the suite's expected values. The
suite is fetched with a shallow git clone into .dart_tool/hcl_specsuite
on first run (it is not vendored); set TERRADART_HCL_SPECSUITE to point at
an existing checkout, or TERRADART_HCL_SPECSUITE_SKIP=1 to skip the test
offline.
Status
Alpha, like the rest of TerraDart. Published to pub.dev in lockstep with the other packages.
Libraries
- terradart_hcl
- Pure Dart HCL parser,
*.tf.jsondecoder and Terraform module model — the input side ofterradart-migrate.