@@ -65,6 +65,32 @@ type Augmentor struct {
6565 packages map [string ]* pkgOverrideInfo
6666}
6767
68+ func (aug * Augmentor ) Augment (xctx XContext , pkg * PackageData , fileSet * token.FileSet , file * ast.File ) error {
69+ pkgAug := aug .getPackageOverrides (xctx , pkg , fileSet )
70+
71+ augmentOriginalImports (pkg .ImportPath , file )
72+
73+ if len (pkgAug .overrides ) > 0 {
74+ augmentOriginalFile (file , pkgAug .overrides )
75+ }
76+
77+ if len (pkgAug .overlayFiles ) > 0 {
78+ // Append the overlay files to the first file of the package.
79+ // This is to ensure that the package is augmented with all the
80+ // additional methods and information from the natives.
81+ err := astutil .ConcatenateFiles (file , pkgAug .overlayFiles ... )
82+ if err != nil {
83+ panic (fmt .Errorf ("failed to concatenate overlay files onto %q: %w" , fileSet .Position (file .Package ).Filename , err ))
84+ }
85+ pkgAug .overlayFiles = nil
86+ }
87+
88+ return nil
89+ }
90+
91+ // getPackageOverrides looks up an already loaded package override
92+ // or loads the package's natives, parses the overlay files, and
93+ // stores the overrides for the package in the augmentor for next time.
6894func (aug * Augmentor ) getPackageOverrides (xctx XContext , pkg * PackageData , fileSet * token.FileSet ) * pkgOverrideInfo {
6995 importPath := pkg .ImportPath
7096 if pkgAug , ok := aug .packages [importPath ]; ok {
@@ -92,29 +118,6 @@ func (aug *Augmentor) getPackageOverrides(xctx XContext, pkg *PackageData, fileS
92118 return pkgAug
93119}
94120
95- func (aug * Augmentor ) Augment (xctx XContext , pkg * PackageData , fileSet * token.FileSet , file * ast.File ) error {
96- pkgAug := aug .getPackageOverrides (xctx , pkg , fileSet )
97-
98- augmentOriginalImports (pkg .ImportPath , file )
99-
100- if len (pkgAug .overrides ) > 0 {
101- augmentOriginalFile (file , pkgAug .overrides )
102- }
103-
104- if len (pkgAug .overlayFiles ) > 0 {
105- // Append the overlay files to the first file of the package.
106- // This is to ensure that the package is augmented with all the
107- // additional methods and information from the natives.
108- err := astutil .ConcatenateFiles (file , pkgAug .overlayFiles ... )
109- if err != nil {
110- panic (fmt .Errorf ("failed to concatenate overlay files onto %q: %w" , fileSet .Position (file .Package ).Filename , err ))
111- }
112- pkgAug .overlayFiles = nil
113- }
114-
115- return nil
116- }
117-
118121// parseOverlayFiles loads and parses overlay files
119122// to augment the original files with.
120123func parseOverlayFiles (xctx XContext , pkg * PackageData , fileSet * token.FileSet ) ([]JSFile , []* ast.File ) {
0 commit comments