scope.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package parser
  2. import (
  3. "github.com/dop251/goja/ast"
  4. "github.com/dop251/goja/unistring"
  5. )
  6. type _scope struct {
  7. outer *_scope
  8. allowIn bool
  9. allowLet bool
  10. inIteration bool
  11. inSwitch bool
  12. inFuncParams bool
  13. inFunction bool
  14. inAsync bool
  15. allowAwait bool
  16. allowYield bool
  17. declarationList []*ast.VariableDeclaration
  18. labels []unistring.String
  19. }
  20. func (self *_parser) openScope() {
  21. self.scope = &_scope{
  22. outer: self.scope,
  23. allowIn: true,
  24. }
  25. }
  26. func (self *_parser) closeScope() {
  27. self.scope = self.scope.outer
  28. }
  29. func (self *_scope) declare(declaration *ast.VariableDeclaration) {
  30. self.declarationList = append(self.declarationList, declaration)
  31. }
  32. func (self *_scope) hasLabel(name unistring.String) bool {
  33. for _, label := range self.labels {
  34. if label == name {
  35. return true
  36. }
  37. }
  38. if self.outer != nil && !self.inFunction {
  39. // Crossing a function boundary to look for a label is verboten
  40. return self.outer.hasLabel(name)
  41. }
  42. return false
  43. }