goplantuml
goplantuml copied to clipboard
Bug: parsing and rendering of named imports
I think I have discovered 2 related problems with the way that named imports are being parsed and rendered.
Problem 1
func (p *ClassParser) parseImports(impt *ast.ImportSpec)
is the func which maps import statements' names to the import path. Since the imports map is global and shared across all packages for an entire puml diagram, and since the map associates each name with only 1 import path, it results incorrect parsing/rendering in the following scenario:
- Any parsing run which has 2+ files which each import different import paths, but give them the same name, will result in the name being associated with only the last of the imported paths. This can result in inaccurate alias connections in the rendered graph, since some of the classes which use the same import name will be connected to an import path that they don't actually import, and will not be connected to the import path they do actually import.
In order to fix this problem, you will have to perform parsing and rendering of named imports, and of other code constructs that use the import name/alias, in a way that correctly associates the import name with the import path associated with that import name in that file (and without assuming that the import name is associated with a single, unique import path across the entire set of parsed files/packages).
Problem 2
Related to the problem above, func (p *ClassParser) parseImports(impt *ast.ImportSpec)
is called from func (p *ClassParser) parsePackage(node ast.Node)
, which iterates over an unsorted map of the package's files. Therefore:
- Any single package that has 2 files which each import different import paths, but give them the same name, will result in the name being randomly associated with only one of the imported paths. This causes the same problem(s) as in Problem 1 (above), and also additionally causes non-repeatable output (see #61), since the 'last' import path parsed will vary from run to run.
To fix the non-repeatability (#61) portion of the problem you will need to parse the go files in a repeatable order within func (p *ClassParser) parsePackage(node ast.Node)
.