Skip to content

Commit

Permalink
refactor: factor out export parsing function
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Musat authored and gabotechs committed Dec 24, 2022
1 parent 6134c3c commit 9dc4360
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 49 deletions.
119 changes: 74 additions & 45 deletions internal/js/exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,57 +40,86 @@ func (p *Parser) uncachedParseExports(
for _, stmt := range jsFile.Statements {
switch {
case stmt == nil:
continue
// Is this even possible?
case stmt.DeclarationExport != nil:
exported[stmt.DeclarationExport.Name] = filePath
handleDeclarationExport(stmt.DeclarationExport, filePath, exported)
case stmt.ListExport != nil:
if stmt.ListExport.ExportDeconstruction != nil {
for _, name := range stmt.ListExport.ExportDeconstruction.Names {
exportedName := name.Alias
if exportedName == "" {
exportedName = name.Original
}
exported[exportedName] = filePath
}
}
handleListExport(stmt.ListExport, filePath, exported)
case stmt.DefaultExport != nil:
if stmt.DefaultExport.Default {
exported["default"] = filePath
}
handleDefaultExport(stmt.DefaultExport, filePath, exported)
case stmt.ProxyExport != nil:
var exportFrom string
ctx, exportFrom, err = p.ResolvePath(ctx, stmt.ProxyExport.From, path.Dir(filePath))
if err != nil {
return ctx, nil, err
}
var proxyExports map[string]string
ctx, proxyExports, err = p.parseExports(ctx, exportFrom)
if err != nil {
return ctx, nil, err
ctx, err = p.handleProxyExport(ctx, stmt.ProxyExport, filePath, exported)
}
if err != nil {
return ctx, nil, err
}
}
return ctx, exported, nil
}

func handleDeclarationExport(
stmt *grammar.DeclarationExport,
filePath string,
dumpOn map[string]string,
) {
dumpOn[stmt.Name] = filePath
}

func handleListExport(
stmt *grammar.ListExport,
filePath string,
dumpOn map[string]string,
) {
if stmt.ExportDeconstruction != nil {
for _, name := range stmt.ExportDeconstruction.Names {
exportedName := name.Alias
if exportedName == "" {
exportedName = name.Original
}
if stmt.ProxyExport.ExportAll {
if stmt.ProxyExport.ExportAllAlias != "" {
exported[stmt.ProxyExport.ExportAllAlias] = filePath
} else {
exported = utils.Merge(exported, proxyExports)
}
} else if stmt.ProxyExport.ExportDeconstruction != nil {
for _, name := range stmt.ProxyExport.ExportDeconstruction.Names {
alias := name.Alias
original := name.Original
if alias == "" {
alias = original
}
if proxyPath, ok := proxyExports[original]; ok {
exported[alias] = proxyPath
} else {
return ctx, nil, fmt.Errorf("cannot import \"%s\" from %s", original, exportFrom)
}
}
dumpOn[exportedName] = filePath
}
}
}

func handleDefaultExport(
stmt *grammar.DefaultExport,
filePath string,
dumpOn map[string]string,
) {
if stmt.Default {
dumpOn["default"] = filePath
}
}

func (p *Parser) handleProxyExport(
ctx context.Context,
stmt *grammar.ProxyExport,
filePath string,
dumpOn map[string]string,
) (context.Context, error) {
ctx, exportFrom, err := p.ResolvePath(ctx, stmt.From, path.Dir(filePath))
if err != nil {
return ctx, err
}
// WARN: this call is recursive, be aware!!!
ctx, proxyExports, err := p.parseExports(ctx, exportFrom)
switch {
case err != nil:
return ctx, err
case stmt.ExportAll:
if stmt.ExportAllAlias != "" {
dumpOn[stmt.ExportAllAlias] = filePath
} else {
utils.Merge(dumpOn, proxyExports)
}
case stmt.ExportDeconstruction != nil:
for _, name := range stmt.ExportDeconstruction.Names {
if proxyPath, ok := proxyExports[name.Original]; ok {
dumpOn[name.AliasOrOriginal()] = proxyPath
} else {
return ctx, fmt.Errorf("cannot import \"%s\" from %s", name.Original, exportFrom)
}
default:
continue
}
}
return ctx, exported, nil
return ctx, nil
}
7 changes: 7 additions & 0 deletions internal/js/grammar/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ type AliasedName struct {
Alias string `("as" @Ident)?`
}

func (a *AliasedName) AliasOrOriginal() string {
if a.Alias == "" {
return a.Original
}
return a.Alias
}

type ExportDeconstruction struct {
Names []AliasedName `"{" @@ ("," @@)* "}"`
}
Expand Down
6 changes: 2 additions & 4 deletions internal/utils/merge.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package utils

func Merge[T any](rules ...map[string]T) map[string]T {
acc := make(map[string]T)
for _, rule := range rules {
func Merge[T any](acc map[string]T, maps ...map[string]T) {
for _, rule := range maps {
for k, v := range rule {
acc[k] = v
}
}
return acc
}

0 comments on commit 9dc4360

Please sign in to comment.