Skip to content

Commit

Permalink
feat(compiler:analyzer): no pub entities in main pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
emil14 committed Sep 28, 2023
1 parent 89e3105 commit 42dc888
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
10 changes: 10 additions & 0 deletions internal/compiler/analyzer/main_pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var (
ErrMainEntityNotFound = errors.New("entity main is not found")
ErrMainEntityIsNotComponent = errors.New("main entity is not a component")
ErrMainEntityExported = errors.New("main entity is exported")
ErrMainPkgExports = errors.New("main pkg must not have exported entities")
)

func (a Analyzer) mainSpecificPkgValidation(pkg src.Package, pkgs map[string]src.Package) error {
Expand All @@ -31,5 +32,14 @@ func (a Analyzer) mainSpecificPkgValidation(pkg src.Package, pkgs map[string]src
return fmt.Errorf("analyze main component: %w", err)
}

if err := pkg.Entities(func(entity src.Entity, entityName, fileName string) error { // FIXME will conflict with general validation
if entity.Exported {
return fmt.Errorf("%w: file %v, entity %v", ErrMainPkgExports, fileName, entityName)
}
return nil
}); err != nil {
return err
}

return nil
}
11 changes: 11 additions & 0 deletions internal/compiler/src/src.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ func (p Package) Entity(name string) (Entity, bool) {
return Entity{}, false
}

func (p Package) Entities(f func(entity Entity, entityName string, fileName string) error) error {
for fileName, file := range p {
for entityName, entity := range file.Entities {
if err := f(entity, entityName, fileName); err != nil {
return err
}
}
}
return nil
}

type File struct {
Imports map[string]string
Entities map[string]Entity
Expand Down

0 comments on commit 42dc888

Please sign in to comment.