d2ir: fix substitutions in imports
This commit is contained in:
parent
676e7d2e18
commit
26fd14455d
4 changed files with 7 additions and 12 deletions
|
|
@ -6,3 +6,4 @@
|
||||||
|
|
||||||
- Fix executable plugins that implement standalone router [#1910](https://github.com/terrastruct/d2/pull/1910)
|
- Fix executable plugins that implement standalone router [#1910](https://github.com/terrastruct/d2/pull/1910)
|
||||||
- Fix compiler error with multiple nested spread substitutions [#1913](https://github.com/terrastruct/d2/pull/1913)
|
- Fix compiler error with multiple nested spread substitutions [#1913](https://github.com/terrastruct/d2/pull/1913)
|
||||||
|
- Fix substitutions from imports into different scopes [#1914](https://github.com/terrastruct/d2/pull/1914)
|
||||||
|
|
|
||||||
|
|
@ -31,8 +31,7 @@ type compiler struct {
|
||||||
imports []string
|
imports []string
|
||||||
// importStack is used to detect cyclic imports.
|
// importStack is used to detect cyclic imports.
|
||||||
importStack []string
|
importStack []string
|
||||||
// importCache enables reuse of files imported multiple times.
|
seenImports map[string]struct{}
|
||||||
importCache map[string]*Map
|
|
||||||
utf16Pos bool
|
utf16Pos bool
|
||||||
|
|
||||||
// Stack of globs that must be recomputed at each new object in and below the current scope.
|
// Stack of globs that must be recomputed at each new object in and below the current scope.
|
||||||
|
|
@ -62,7 +61,7 @@ func Compile(ast *d2ast.Map, opts *CompileOptions) (*Map, []string, error) {
|
||||||
err: &d2parser.ParseError{},
|
err: &d2parser.ParseError{},
|
||||||
fs: opts.FS,
|
fs: opts.FS,
|
||||||
|
|
||||||
importCache: make(map[string]*Map),
|
seenImports: make(map[string]struct{}),
|
||||||
utf16Pos: opts.UTF16Pos,
|
utf16Pos: opts.UTF16Pos,
|
||||||
}
|
}
|
||||||
m := &Map{}
|
m := &Map{}
|
||||||
|
|
|
||||||
|
|
@ -82,16 +82,11 @@ func (c *compiler) __import(imp *d2ast.Import) (*Map, bool) {
|
||||||
|
|
||||||
// Only get immediate imports.
|
// Only get immediate imports.
|
||||||
if len(c.importStack) == 2 {
|
if len(c.importStack) == 2 {
|
||||||
if _, ok := c.importCache[impPath]; !ok {
|
if _, ok := c.seenImports[impPath]; !ok {
|
||||||
c.imports = append(c.imports, imp.PathWithPre())
|
c.imports = append(c.imports, imp.PathWithPre())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ir, ok := c.importCache[impPath]
|
|
||||||
if ok {
|
|
||||||
return ir, true
|
|
||||||
}
|
|
||||||
|
|
||||||
var f fs.File
|
var f fs.File
|
||||||
var err error
|
var err error
|
||||||
if c.fs == nil {
|
if c.fs == nil {
|
||||||
|
|
@ -113,13 +108,13 @@ func (c *compiler) __import(imp *d2ast.Import) (*Map, bool) {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
ir = &Map{}
|
ir := &Map{}
|
||||||
ir.initRoot()
|
ir.initRoot()
|
||||||
ir.parent.(*Field).References[0].Context_.Scope = ast
|
ir.parent.(*Field).References[0].Context_.Scope = ast
|
||||||
|
|
||||||
c.compileMap(ir, ast, ast)
|
c.compileMap(ir, ast, ast)
|
||||||
|
|
||||||
c.importCache[impPath] = ir
|
c.seenImports[impPath] = struct{}{}
|
||||||
|
|
||||||
return ir, true
|
return ir, true
|
||||||
}
|
}
|
||||||
|
|
|
||||||
2
testdata/d2compiler/TestCompile/vars-in-imports.exp.json
generated
vendored
2
testdata/d2compiler/TestCompile/vars-in-imports.exp.json
generated
vendored
|
|
@ -371,7 +371,7 @@
|
||||||
],
|
],
|
||||||
"attributes": {
|
"attributes": {
|
||||||
"label": {
|
"label": {
|
||||||
"value": "Dev Environment"
|
"value": "Qa Environment"
|
||||||
},
|
},
|
||||||
"labelDimensions": {
|
"labelDimensions": {
|
||||||
"width": 0,
|
"width": 0,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue