Skip to content

Commit

Permalink
Compile-time type imports (Azure#11298)
Browse files Browse the repository at this point in the history
Partially addresses Azure#10121 

This PR adds compile-time imports for user-defined types. This feature
is only available under a flag and must be enabled with a
bicepconfig.json file that looks like this:

```json
{
  "experimentalFeaturesEnabled": {
    "compileTimeImports": true,
    "userDefinedTypes": true
  }
}
```

With the feature enabled, the `import` keyword can be used to move
user-defined types across templates. For example, given the following
template saved as `mod.bicep`:

```bicep
@export()
type foo = string

@export()
type bar = int
```

The `foo` and `bar` type symbols can be imported into another template
either individually by name using "symbols list" syntax:

```bicep
import {foo, bar} from 'mod.bicep'
```

or with "wildcard" syntax (which is less efficient but more fun to say):

```bicep
import * as myImports from 'mod.bicep'
```

With the "symbols" list syntax, you can import a subset of the target
template's exported symbols and rename/alias them at will:

```bicep
import {foo} from 'mod.bicep' // <-- Omits bar from the compiled template
import {bar} from 'mod.bicep' // <-- Omits foo from the compiled template
import {
  foo as fizz
  bar as buzz
} from 'mod.bicep'                   // <-- Aliases both foo and bar
```

You can also mix and match syntaxes. Symbols will be imported at most
once:

```bicep
import {foo} from 'mod.bicep'
import * as baz from 'mod.bicep'
```

Imported types can be used anywhere a user-defined type might be (i.e.,
within the type clauses of `type`, `param`, and `output` statements).

Only types that bear the `@export()` decorator can be imported. As of
this PR, this decorator can only be used on `type` statements.

###### Microsoft Reviewers:
codeflow:open?pullrequest=https://github.com/Azure/bicep/pull/11298&drop=dogfoodAlpha
  • Loading branch information
jeskew committed Aug 4, 2023
1 parent 09d0d4b commit e39a2f1
Show file tree
Hide file tree
Showing 112 changed files with 4,028 additions and 740 deletions.
15 changes: 15 additions & 0 deletions docs/grammar.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ program -> statement* EOF
statement ->
targetScopeDecl |
importDecl |
compileTimeImportDecl |
metadataDecl |
parameterDecl |
typeDecl |
Expand All @@ -25,6 +26,20 @@ importWithClause -> "with" object
importAsClause -> "as" IDENTIFIER(alias)
compileTimeImportDecl -> decorator* "import" compileTimeImportTarget compileTimeImportFromClause
compileTimeImportTarget ->
importedSymbolsList |
wildcardImport
importedSymbolsList -> "{" ( NL+ ( importedSymbolsListItem NL+ )* )? "}"
importedSymbolsListItem -> IDENTIFIER(originalSymbolName) importAsClause?
wildcardImport -> "*" importAsClause
compileTimeImportFromClause -> "from" interpString(path)
metadataDecl -> "metadata" IDENTIFIER(name) "=" expression NL
parameterDecl ->
Expand Down
7 changes: 4 additions & 3 deletions src/Bicep.Cli/Services/CompilationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Bicep.Core.Diagnostics;
using Bicep.Core.Extensions;
using Bicep.Core.FileSystem;
using Bicep.Core.Navigation;
using Bicep.Core.Registry;
using Bicep.Core.Semantics;
using Bicep.Core.Syntax;
Expand Down Expand Up @@ -126,14 +127,14 @@ public DecompileResult DecompileParams(string inputPath, string outputPath, stri
{
workspace.UpsertSourceFile(SourceFileFactory.CreateBicepFile(fileUri, bicepOutput));
}

return decompilation;
}

private static ImmutableDictionary<BicepSourceFile, ImmutableArray<IDiagnostic>> GetModuleRestoreDiagnosticsByBicepFile(SourceFileGrouping sourceFileGrouping, ImmutableHashSet<ModuleSourceResolutionInfo> originalModulesToRestore, bool forceModulesRestore)
{
static IDiagnostic? DiagnosticForModule(SourceFileGrouping grouping, ModuleDeclarationSyntax module)
=> grouping.TryGetErrorDiagnostic(module) is { } errorBuilder ? errorBuilder(DiagnosticBuilder.ForPosition(module.Path)) : null;
static IDiagnostic? DiagnosticForModule(SourceFileGrouping grouping, IForeignTemplateReference module)
=> grouping.TryGetErrorDiagnostic(module) is { } errorBuilder ? errorBuilder(DiagnosticBuilder.ForPosition(module.ReferenceSourceSyntax)) : null;

static IEnumerable<(BicepFile, IDiagnostic)> GetDiagnosticsForModulesToRestore(SourceFileGrouping grouping, ImmutableHashSet<ModuleSourceResolutionInfo> originalModulesToRestore)
{
Expand Down
Loading

0 comments on commit e39a2f1

Please sign in to comment.