1 // Package scope parses out a scope tree for a javascript module or script 2 package scope // import "vimagination.zapto.org/javascript/scope" 3 4 import ( 5 "vimagination.zapto.org/javascript" 6 ) 7 8 // ErrDuplicateDeclaration is an error when a binding is declared more than once with a scope 9 type ErrDuplicateDeclaration struct { 10 Declaration, Duplicate *javascript.Token 11 } 12 13 func (ErrDuplicateDeclaration) Error() string { 14 return "duplicate declaration" 15 } 16 17 // BindingType indicates where the binding came from 18 type BindingType uint8 19 20 // Binding Types 21 const ( 22 BindingRef BindingType = iota 23 BindingBare 24 BindingVar 25 BindingHoistable 26 BindingLexicalLet 27 BindingLexicalConst 28 BindingImport 29 BindingFunctionParam 30 BindingCatch 31 ) 32 33 // Binding represents a single instance of a bound name 34 type Binding struct { 35 BindingType 36 *Scope 37 *javascript.Token 38 } 39 40 // Scope represents a single level of variable scope 41 type Scope struct { 42 IsLexicalScope bool 43 Parent *Scope 44 Scopes map[javascript.Type]*Scope 45 Bindings map[string][]Binding 46 } 47 48 func (s *Scope) setBinding(t *javascript.Token, bindingType BindingType) error { 49 name := t.Data 50 binding := Binding{BindingType: bindingType, Token: t, Scope: s} 51 if b, ok := s.Bindings[name]; ok { 52 if bindingType == BindingVar && len(b) > 0 && (b[0].BindingType == BindingVar || b[0].BindingType == BindingCatch) { 53 s.Bindings[name] = append(b, binding) 54 if b[0].BindingType == BindingCatch && bindingType == BindingVar { 55 return nil 56 } 57 } else { 58 var bd *javascript.Token 59 if len(b) > 0 { 60 bd = b[0].Token 61 } 62 return ErrDuplicateDeclaration{ 63 Declaration: bd, 64 Duplicate: t, 65 } 66 } 67 } else { 68 s.Bindings[name] = []Binding{binding} 69 } 70 if s.IsLexicalScope && (bindingType == BindingHoistable || bindingType == BindingVar) { 71 Loop: 72 for s.IsLexicalScope && s.Parent != nil { 73 s = s.Parent 74 if bindingType == BindingVar { 75 if b, ok := s.Bindings[name]; ok && len(b) > 0 { 76 switch b[0].BindingType { 77 case BindingCatch: 78 break Loop 79 case BindingVar, BindingBare: 80 default: 81 return ErrDuplicateDeclaration{ 82 Declaration: b[0].Token, 83 Duplicate: t, 84 } 85 } 86 } 87 } 88 } 89 if b, ok := s.Bindings[name]; !ok { 90 s.Bindings[name] = []Binding{binding} 91 } else if bindingType == BindingVar { 92 s.Bindings[name] = append(b, binding) 93 } 94 } 95 return nil 96 } 97 98 func (s *Scope) addBinding(t *javascript.Token, bindingType BindingType) { 99 name := t.Data 100 binding := Binding{BindingType: bindingType, Token: t, Scope: s} 101 for { 102 if bs, ok := s.Bindings[name]; ok { 103 s.Bindings[name] = append(bs, binding) 104 if !s.IsLexicalScope || len(bs) == 0 || bs[0].BindingType != BindingVar { 105 return 106 } 107 } 108 if s.Parent == nil { 109 s.Bindings[name] = []Binding{binding} 110 return 111 } 112 s = s.Parent 113 } 114 } 115 116 // NewScope returns a init'd Scope type 117 func NewScope() *Scope { 118 return &Scope{ 119 Scopes: make(map[javascript.Type]*Scope), 120 Bindings: make(map[string][]Binding), 121 } 122 } 123 124 func (s *Scope) newFunctionScope(js javascript.Type) *Scope { 125 if ns, ok := s.Scopes[js]; ok { 126 return ns 127 } 128 ns := &Scope{ 129 Parent: s, 130 Scopes: make(map[javascript.Type]*Scope), 131 Bindings: map[string][]Binding{ 132 "this": {}, 133 "arguments": {}, 134 }, 135 } 136 s.Scopes[js] = ns 137 return ns 138 } 139 140 func (s *Scope) newArrowFunctionScope(js javascript.Type) *Scope { 141 if ns, ok := s.Scopes[js]; ok { 142 return ns 143 } 144 ns := &Scope{ 145 Parent: s, 146 Scopes: make(map[javascript.Type]*Scope), 147 Bindings: make(map[string][]Binding), 148 } 149 s.Scopes[js] = ns 150 return ns 151 } 152 153 func (s *Scope) newLexicalScope(js javascript.Type) *Scope { 154 if ns, ok := s.Scopes[js]; ok { 155 return ns 156 } 157 ns := &Scope{ 158 Parent: s, 159 IsLexicalScope: true, 160 Scopes: make(map[javascript.Type]*Scope), 161 Bindings: make(map[string][]Binding), 162 } 163 s.Scopes[js] = ns 164 return ns 165 } 166 167 // ModuleScope parses out the scope tree for a javascript Module 168 func ModuleScope(m *javascript.Module, global *Scope) (*Scope, error) { 169 if global == nil { 170 global = NewScope() 171 } 172 if err := processModule(m, global, true); err != nil { 173 return nil, err 174 } 175 _ = processModule(m, global, false) 176 return global, nil 177 } 178 179 // ScriptScope parses out the scope tree for a javascript script 180 func ScriptScope(s *javascript.Script, global *Scope) (*Scope, error) { 181 if global == nil { 182 global = NewScope() 183 } 184 for n := range s.StatementList { 185 if err := processStatementListItem(&s.StatementList[n], global, true); err != nil { 186 return nil, err 187 } 188 } 189 for n := range s.StatementList { 190 _ = processStatementListItem(&s.StatementList[n], global, false) 191 } 192 return global, nil 193 } 194 195 func processModule(m *javascript.Module, global *Scope, set bool) error { 196 for _, i := range m.ModuleListItems { 197 if i.ImportDeclaration != nil && i.ImportDeclaration.ImportClause != nil { 198 if set { 199 if i.ImportDeclaration.ImportedDefaultBinding != nil { 200 if err := global.setBinding(i.ImportDeclaration.ImportedDefaultBinding, BindingImport); err != nil { 201 return err 202 } 203 } 204 if i.ImportDeclaration.NameSpaceImport != nil { 205 if err := global.setBinding(i.ImportDeclaration.NameSpaceImport, BindingImport); err != nil { 206 return err 207 } 208 } 209 if i.ImportDeclaration.NamedImports != nil { 210 for _, is := range i.ImportDeclaration.NamedImports.ImportList { 211 if is.ImportedBinding != nil { 212 if err := global.setBinding(is.ImportedBinding, BindingImport); err != nil { 213 return err 214 } 215 } 216 } 217 } 218 } 219 } else if i.StatementListItem != nil { 220 if err := processStatementListItem(i.StatementListItem, global, set); err != nil { 221 return err 222 } 223 } else if i.ExportDeclaration != nil { 224 if i.ExportDeclaration.VariableStatement != nil { 225 if err := processVariableStatement(i.ExportDeclaration.VariableStatement, global, set); err != nil { 226 return err 227 } 228 } else if i.ExportDeclaration.Declaration != nil { 229 if err := processDeclaration(i.ExportDeclaration.Declaration, global, set); err != nil { 230 return err 231 } 232 } else if i.ExportDeclaration.DefaultFunction != nil { 233 if err := processFunctionDeclaration(i.ExportDeclaration.DefaultFunction, global, set, false); err != nil { 234 return err 235 } 236 } else if i.ExportDeclaration.DefaultClass != nil { 237 if err := processClassDeclaration(i.ExportDeclaration.DefaultClass, global, set, false); err != nil { 238 return err 239 } 240 } else if i.ExportDeclaration.DefaultAssignmentExpression != nil { 241 if err := processAssignmentExpression(i.ExportDeclaration.DefaultAssignmentExpression, global, set); err != nil { 242 return err 243 } 244 } else if i.ExportDeclaration.ExportClause != nil && !set { 245 for _, es := range i.ExportDeclaration.ExportClause.ExportList { 246 if es.IdentifierName != nil { 247 global.addBinding(es.IdentifierName, BindingRef) 248 } 249 } 250 } 251 } 252 } 253 return nil 254 } 255 256 func processStatementListItem(s *javascript.StatementListItem, scope *Scope, set bool) error { 257 if s.Statement != nil { 258 return processStatement(s.Statement, scope, set) 259 } else if s.Declaration != nil { 260 return processDeclaration(s.Declaration, scope, set) 261 } 262 return nil 263 } 264 265 func processStatement(s *javascript.Statement, scope *Scope, set bool) error { 266 if s.BlockStatement != nil { 267 return processBlockStatement(s.BlockStatement, scope.newLexicalScope(s.BlockStatement), set) 268 } else if s.VariableStatement != nil { 269 return processVariableStatement(s.VariableStatement, scope, set) 270 } else if s.ExpressionStatement != nil { 271 return processExpression(s.ExpressionStatement, scope, set) 272 } else if s.IfStatement != nil { 273 return processIfStatement(s.IfStatement, scope, set) 274 } else if s.IterationStatementDo != nil { 275 return processIterationStatementDo(s.IterationStatementDo, scope, set) 276 } else if s.IterationStatementWhile != nil { 277 return processIterationStatementWhile(s.IterationStatementWhile, scope, set) 278 } else if s.IterationStatementFor != nil { 279 return processIterationStatementFor(s.IterationStatementFor, scope, set) 280 } else if s.SwitchStatement != nil { 281 return processSwitchStatement(s.SwitchStatement, scope, set) 282 } else if s.WithStatement != nil { 283 return processWithStatement(s.WithStatement, scope, set) 284 } else if s.LabelIdentifier != nil { 285 if s.LabelledItemFunction != nil { 286 return processFunctionDeclaration(s.LabelledItemFunction, scope, set, false) 287 } else if s.LabelledItemStatement != nil { 288 return processStatement(s.LabelledItemStatement, scope, set) 289 } 290 } else if s.TryStatement != nil { 291 return processTryStatement(s.TryStatement, scope, set) 292 } 293 return nil 294 } 295 296 func processDeclaration(d *javascript.Declaration, scope *Scope, set bool) error { 297 if d.ClassDeclaration != nil { 298 if err := processClassDeclaration(d.ClassDeclaration, scope, set, false); err != nil { 299 return err 300 } 301 } else if d.FunctionDeclaration != nil { 302 if err := processFunctionDeclaration(d.FunctionDeclaration, scope, set, false); err != nil { 303 return err 304 } 305 } else if d.LexicalDeclaration != nil { 306 if err := processLexicalDeclaration(d.LexicalDeclaration, scope, set); err != nil { 307 return err 308 } 309 } 310 return nil 311 } 312 313 func processBlockStatement(b *javascript.Block, scope *Scope, set bool) error { 314 for n := range b.StatementList { 315 if err := processStatementListItem(&b.StatementList[n], scope, set); err != nil { 316 return err 317 } 318 } 319 return nil 320 } 321 322 func processVariableStatement(v *javascript.VariableStatement, scope *Scope, set bool) error { 323 for n := range v.VariableDeclarationList { 324 if err := processVariableDeclaration(&v.VariableDeclarationList[n], scope, set); err != nil { 325 return err 326 } 327 } 328 return nil 329 } 330 331 func processExpression(e *javascript.Expression, scope *Scope, set bool) error { 332 for n := range e.Expressions { 333 if err := processAssignmentExpression(&e.Expressions[n], scope, set); err != nil { 334 return err 335 } 336 } 337 return nil 338 } 339 340 func processIfStatement(i *javascript.IfStatement, scope *Scope, set bool) error { 341 if err := processExpression(&i.Expression, scope, set); err != nil { 342 return err 343 } 344 if err := processStatement(&i.Statement, scope, set); err != nil { 345 return err 346 } 347 if i.ElseStatement != nil { 348 if err := processStatement(i.ElseStatement, scope, set); err != nil { 349 return err 350 } 351 } 352 return nil 353 } 354 355 func processIterationStatementDo(d *javascript.IterationStatementDo, scope *Scope, set bool) error { 356 if err := processStatement(&d.Statement, scope, set); err != nil { 357 return err 358 } 359 if err := processExpression(&d.Expression, scope, set); err != nil { 360 return err 361 } 362 return nil 363 } 364 365 func processIterationStatementWhile(w *javascript.IterationStatementWhile, scope *Scope, set bool) error { 366 if err := processExpression(&w.Expression, scope, set); err != nil { 367 return err 368 } 369 if err := processStatement(&w.Statement, scope, set); err != nil { 370 return err 371 } 372 return nil 373 } 374 375 func processIterationStatementFor(f *javascript.IterationStatementFor, scope *Scope, set bool) error { 376 scope = scope.newLexicalScope(f) 377 switch f.Type { 378 case javascript.ForNormal: 379 case javascript.ForNormalVar: 380 for n := range f.InitVar { 381 if err := processVariableDeclaration(&f.InitVar[n], scope, set); err != nil { 382 return err 383 } 384 } 385 case javascript.ForNormalLexicalDeclaration: 386 if f.InitLexical != nil { 387 if err := processLexicalDeclaration(f.InitLexical, scope, set); err != nil { 388 return err 389 } 390 } 391 case javascript.ForNormalExpression: 392 if f.InitExpression != nil { 393 if err := processExpression(f.InitExpression, scope, set); err != nil { 394 return err 395 } 396 } 397 case javascript.ForInLeftHandSide, javascript.ForOfLeftHandSide, javascript.ForAwaitOfLeftHandSide: 398 if f.LeftHandSideExpression != nil { 399 if err := processLeftHandSideExpression(f.LeftHandSideExpression, scope, set); err != nil { 400 return err 401 } 402 } 403 default: 404 bindingType := BindingBare 405 switch f.Type { 406 case javascript.ForInLet, javascript.ForOfLet, javascript.ForAwaitOfLet: 407 bindingType = BindingLexicalLet 408 case javascript.ForInConst, javascript.ForOfConst, javascript.ForAwaitOfConst: 409 bindingType = BindingLexicalConst 410 case javascript.ForInVar, javascript.ForOfVar, javascript.ForAwaitOfVar: 411 bindingType = BindingVar 412 } 413 if f.ForBindingPatternObject != nil { 414 if err := processObjectBindingPattern(f.ForBindingPatternObject, scope, set, bindingType); err != nil { 415 return err 416 } 417 } else if f.ForBindingPatternArray != nil { 418 if err := processArrayBindingPattern(f.ForBindingPatternArray, scope, set, bindingType); err != nil { 419 return err 420 } 421 } else if f.ForBindingIdentifier != nil { 422 if bindingType == BindingBare && !set { 423 scope.addBinding(f.ForBindingIdentifier, BindingBare) 424 } else if bindingType != BindingBare && set { 425 if err := scope.setBinding(f.ForBindingIdentifier, bindingType); err != nil { 426 return err 427 } 428 } 429 } 430 } 431 switch f.Type { 432 case javascript.ForNormal, javascript.ForNormalVar, javascript.ForNormalLexicalDeclaration, javascript.ForNormalExpression: 433 if f.Conditional != nil { 434 if err := processExpression(f.Conditional, scope, set); err != nil { 435 return err 436 } 437 } 438 if f.Afterthought != nil { 439 if err := processExpression(f.Afterthought, scope, set); err != nil { 440 return err 441 } 442 } 443 case javascript.ForInLeftHandSide, javascript.ForInVar, javascript.ForInLet, javascript.ForInConst: 444 if f.In != nil { 445 if err := processExpression(f.In, scope, set); err != nil { 446 return err 447 } 448 } 449 case javascript.ForOfLeftHandSide, javascript.ForOfVar, javascript.ForOfLet, javascript.ForOfConst, javascript.ForAwaitOfLeftHandSide, javascript.ForAwaitOfVar, javascript.ForAwaitOfLet, javascript.ForAwaitOfConst: 450 if f.Of != nil { 451 if err := processAssignmentExpression(f.Of, scope, set); err != nil { 452 return err 453 } 454 } 455 } 456 if err := processStatement(&f.Statement, scope, set); err != nil { 457 return err 458 } 459 return nil 460 } 461 462 func processSwitchStatement(s *javascript.SwitchStatement, scope *Scope, set bool) error { 463 if err := processExpression(&s.Expression, scope, set); err != nil { 464 return err 465 } 466 scope = scope.newLexicalScope(s) 467 for n := range s.CaseClauses { 468 c := &s.CaseClauses[n] 469 if err := processExpression(&c.Expression, scope, set); err != nil { 470 return err 471 } 472 for m := range c.StatementList { 473 if err := processStatementListItem(&c.StatementList[m], scope, set); err != nil { 474 return err 475 } 476 } 477 } 478 for n := range s.DefaultClause { 479 if err := processStatementListItem(&s.DefaultClause[n], scope, set); err != nil { 480 return err 481 } 482 } 483 for n := range s.PostDefaultCaseClauses { 484 c := &s.PostDefaultCaseClauses[n] 485 if err := processExpression(&c.Expression, scope, set); err != nil { 486 return err 487 } 488 for m := range c.StatementList { 489 if err := processStatementListItem(&c.StatementList[m], scope, set); err != nil { 490 return err 491 } 492 } 493 } 494 return nil 495 } 496 497 func processWithStatement(w *javascript.WithStatement, scope *Scope, set bool) error { 498 if err := processExpression(&w.Expression, scope, set); err != nil { 499 return err 500 } 501 if err := processStatement(&w.Statement, scope, set); err != nil { 502 return err 503 } 504 return nil 505 } 506 507 func processFunctionDeclaration(f *javascript.FunctionDeclaration, scope *Scope, set, expression bool) error { 508 if f.BindingIdentifier != nil && set && !expression { 509 if err := scope.setBinding(f.BindingIdentifier, BindingHoistable); err != nil { 510 return err 511 } 512 } 513 scope = scope.newFunctionScope(f) 514 if err := processFormalParameters(&f.FormalParameters, scope, set); err != nil { 515 return err 516 } 517 if err := processBlockStatement(&f.FunctionBody, scope, set); err != nil { 518 return err 519 } 520 return nil 521 } 522 523 func processTryStatement(t *javascript.TryStatement, scope *Scope, set bool) error { 524 if err := processBlockStatement(&t.TryBlock, scope.newLexicalScope(&t.TryBlock), set); err != nil { 525 return err 526 } 527 if t.CatchBlock != nil { 528 scope = scope.newLexicalScope(t.CatchBlock) 529 if t.CatchParameterArrayBindingPattern != nil { 530 if err := processArrayBindingPattern(t.CatchParameterArrayBindingPattern, scope, set, BindingCatch); err != nil { 531 return err 532 } 533 } else if t.CatchParameterObjectBindingPattern != nil { 534 if err := processObjectBindingPattern(t.CatchParameterObjectBindingPattern, scope, set, BindingCatch); err != nil { 535 return err 536 } 537 } else if t.CatchParameterBindingIdentifier != nil && set { 538 if err := scope.setBinding(t.CatchParameterBindingIdentifier, BindingCatch); err != nil { 539 return err 540 } 541 } 542 if err := processBlockStatement(t.CatchBlock, scope, set); err != nil { 543 return err 544 } 545 } 546 if t.FinallyBlock != nil { 547 if err := processBlockStatement(t.FinallyBlock, scope.newLexicalScope(t.FinallyBlock), set); err != nil { 548 return err 549 } 550 } 551 return nil 552 } 553 554 func processClassDeclaration(c *javascript.ClassDeclaration, scope *Scope, set, expression bool) error { 555 if c.BindingIdentifier != nil && set && !expression { 556 if err := scope.setBinding(c.BindingIdentifier, BindingHoistable); err != nil { 557 return err 558 } 559 } 560 if c.ClassHeritage != nil { 561 if err := processLeftHandSideExpression(c.ClassHeritage, scope, set); err != nil { 562 return err 563 } 564 } 565 for n := range c.ClassBody { 566 if err := processClassElement(&c.ClassBody[n], scope, set); err != nil { 567 return err 568 } 569 } 570 return nil 571 } 572 573 func processClassElement(c *javascript.ClassElement, scope *Scope, set bool) error { 574 if c.MethodDefinition != nil { 575 if err := processMethodDefinition(c.MethodDefinition, scope, set); err != nil { 576 return err 577 } 578 } else if c.FieldDefinition != nil { 579 if err := processFieldDefinition(c.FieldDefinition, scope, set); err != nil { 580 return err 581 } 582 } else if c.ClassStaticBlock != nil { 583 scope = scope.newLexicalScope(c.ClassStaticBlock) 584 if err := processBlockStatement(c.ClassStaticBlock, scope, set); err != nil { 585 return err 586 } 587 } 588 return nil 589 } 590 591 func processFieldDefinition(f *javascript.FieldDefinition, scope *Scope, set bool) error { 592 if err := processClassElementName(&f.ClassElementName, scope, set); err != nil { 593 return err 594 } 595 if f.Initializer != nil { 596 if err := processAssignmentExpression(f.Initializer, scope, set); err != nil { 597 return err 598 } 599 } 600 return nil 601 } 602 603 func processClassElementName(c *javascript.ClassElementName, scope *Scope, set bool) error { 604 if c.PropertyName != nil { 605 if err := processPropertyName(c.PropertyName, scope, set); err != nil { 606 return err 607 } 608 } 609 return nil 610 } 611 612 func processLexicalDeclaration(l *javascript.LexicalDeclaration, scope *Scope, set bool) error { 613 typ := BindingLexicalLet 614 if l.LetOrConst == javascript.Const { 615 typ = BindingLexicalConst 616 } 617 for n := range l.BindingList { 618 if err := processLexicalBinding(&l.BindingList[n], scope, set, typ); err != nil { 619 return err 620 } 621 } 622 return nil 623 } 624 625 func processVariableDeclaration(v *javascript.VariableDeclaration, scope *Scope, set bool) error { 626 if v.ArrayBindingPattern != nil { 627 if err := processArrayBindingPattern(v.ArrayBindingPattern, scope, set, BindingVar); err != nil { 628 return err 629 } 630 } else if v.ObjectBindingPattern != nil { 631 if err := processObjectBindingPattern(v.ObjectBindingPattern, scope, set, BindingVar); err != nil { 632 return err 633 } 634 } else if v.BindingIdentifier != nil && set { 635 if err := scope.setBinding(v.BindingIdentifier, BindingVar); err != nil { 636 return err 637 } 638 } 639 if v.Initializer != nil { 640 if err := processAssignmentExpression(v.Initializer, scope, set); err != nil { 641 return err 642 } 643 } 644 return nil 645 } 646 647 func processAssignmentExpression(a *javascript.AssignmentExpression, scope *Scope, set bool) error { 648 if a.ConditionalExpression != nil { 649 if err := processConditionalExpression(a.ConditionalExpression, scope, set); err != nil { 650 return err 651 } 652 } else if a.ArrowFunction != nil { 653 if err := processArrowFunction(a.ArrowFunction, scope, set); err != nil { 654 return err 655 } 656 } else if a.LeftHandSideExpression != nil { 657 if a.LeftHandSideExpression.NewExpression != nil && a.LeftHandSideExpression.NewExpression.News == 0 && a.LeftHandSideExpression.NewExpression.MemberExpression.PrimaryExpression != nil && a.LeftHandSideExpression.NewExpression.MemberExpression.PrimaryExpression.IdentifierReference != nil { 658 if !set { 659 scope.addBinding(a.LeftHandSideExpression.NewExpression.MemberExpression.PrimaryExpression.IdentifierReference, BindingBare) 660 } 661 } else if err := processLeftHandSideExpression(a.LeftHandSideExpression, scope, set); err != nil { 662 return err 663 } 664 } else if a.AssignmentPattern != nil { 665 if err := processAssignmentPattern(a.AssignmentPattern, scope, set); err != nil { 666 return err 667 } 668 } 669 if a.AssignmentExpression != nil { 670 if err := processAssignmentExpression(a.AssignmentExpression, scope, set); err != nil { 671 return err 672 } 673 } 674 return nil 675 } 676 677 func processLeftHandSideExpression(l *javascript.LeftHandSideExpression, scope *Scope, set bool) error { 678 if l.NewExpression != nil { 679 if err := processNewExpression(l.NewExpression, scope, set); err != nil { 680 return err 681 } 682 } else if l.CallExpression != nil { 683 if err := processCallExpression(l.CallExpression, scope, set); err != nil { 684 return err 685 } 686 } else if l.OptionalExpression != nil { 687 if err := processOptionalExpression(l.OptionalExpression, scope, set); err != nil { 688 return err 689 } 690 } 691 return nil 692 } 693 694 func processAssignmentPattern(a *javascript.AssignmentPattern, scope *Scope, set bool) error { 695 if a.ArrayAssignmentPattern != nil { 696 if err := processArrayAssignmentPattern(a.ArrayAssignmentPattern, scope, set); err != nil { 697 return err 698 } 699 } else if a.ObjectAssignmentPattern != nil { 700 if err := processObjectAssignmentPattern(a.ObjectAssignmentPattern, scope, set); err != nil { 701 return err 702 } 703 } 704 return nil 705 } 706 707 func processObjectAssignmentPattern(o *javascript.ObjectAssignmentPattern, scope *Scope, set bool) error { 708 for n := range o.AssignmentPropertyList { 709 if err := processAssignmentProperty(&o.AssignmentPropertyList[n], scope, set); err != nil { 710 return err 711 } 712 } 713 if o.AssignmentRestElement != nil { 714 if err := processLeftHandSideExpression(o.AssignmentRestElement, scope, set); err != nil { 715 return err 716 } 717 } 718 return nil 719 } 720 721 func processAssignmentProperty(a *javascript.AssignmentProperty, scope *Scope, set bool) error { 722 if err := processPropertyName(&a.PropertyName, scope, set); err != nil { 723 return err 724 } 725 if a.DestructuringAssignmentTarget != nil { 726 if err := processDestructuringAssignmentTarget(a.DestructuringAssignmentTarget, scope, set); err != nil { 727 return err 728 } 729 } 730 if a.Initializer != nil { 731 if err := processAssignmentExpression(a.Initializer, scope, set); err != nil { 732 return err 733 } 734 } 735 return nil 736 } 737 738 func processDestructuringAssignmentTarget(d *javascript.DestructuringAssignmentTarget, scope *Scope, set bool) error { 739 if d.LeftHandSideExpression != nil { 740 if err := processLeftHandSideExpression(d.LeftHandSideExpression, scope, set); err != nil { 741 return err 742 } 743 } 744 if d.AssignmentPattern != nil { 745 if err := processAssignmentPattern(d.AssignmentPattern, scope, set); err != nil { 746 return err 747 } 748 } 749 return nil 750 } 751 752 func processAssignmentElement(a *javascript.AssignmentElement, scope *Scope, set bool) error { 753 if err := processDestructuringAssignmentTarget(&a.DestructuringAssignmentTarget, scope, set); err != nil { 754 return err 755 } 756 if a.Initializer != nil { 757 if err := processAssignmentExpression(a.Initializer, scope, set); err != nil { 758 return err 759 } 760 } 761 return nil 762 } 763 764 func processArrayAssignmentPattern(a *javascript.ArrayAssignmentPattern, scope *Scope, set bool) error { 765 for n := range a.AssignmentElements { 766 if err := processAssignmentElement(&a.AssignmentElements[n], scope, set); err != nil { 767 return err 768 } 769 } 770 if a.AssignmentRestElement != nil { 771 if err := processLeftHandSideExpression(a.AssignmentRestElement, scope, set); err != nil { 772 return err 773 } 774 } 775 return nil 776 } 777 778 func processObjectBindingPattern(o *javascript.ObjectBindingPattern, scope *Scope, set bool, bindingType BindingType) error { 779 for n := range o.BindingPropertyList { 780 if err := processBindingProperty(&o.BindingPropertyList[n], scope, set, bindingType); err != nil { 781 return err 782 } 783 } 784 if o.BindingRestProperty != nil { 785 if bindingType == BindingBare && !set { 786 scope.addBinding(o.BindingRestProperty, BindingBare) 787 } else if bindingType != BindingBare && set { 788 if err := scope.setBinding(o.BindingRestProperty, bindingType); err != nil { 789 return err 790 } 791 } 792 } 793 return nil 794 } 795 796 func processArrayBindingPattern(a *javascript.ArrayBindingPattern, scope *Scope, set bool, bindingType BindingType) error { 797 for n := range a.BindingElementList { 798 if err := processBindingElement(&a.BindingElementList[n], scope, set, bindingType); err != nil { 799 return err 800 } 801 } 802 if a.BindingRestElement != nil { 803 if err := processBindingElement(a.BindingRestElement, scope, set, bindingType); err != nil { 804 return err 805 } 806 } 807 return nil 808 } 809 810 func processFormalParameters(f *javascript.FormalParameters, scope *Scope, set bool) error { 811 for n := range f.FormalParameterList { 812 if err := processBindingElement(&f.FormalParameterList[n], scope, set, BindingFunctionParam); err != nil { 813 return err 814 } 815 } 816 if f.ArrayBindingPattern != nil { 817 if err := processArrayBindingPattern(f.ArrayBindingPattern, scope, set, BindingFunctionParam); err != nil { 818 return err 819 } 820 } else if f.ObjectBindingPattern != nil { 821 if err := processObjectBindingPattern(f.ObjectBindingPattern, scope, set, BindingFunctionParam); err != nil { 822 return err 823 } 824 } else if f.BindingIdentifier != nil && set { 825 if err := scope.setBinding(f.BindingIdentifier, BindingFunctionParam); err != nil { 826 return err 827 } 828 } 829 return nil 830 } 831 832 func processMethodDefinition(m *javascript.MethodDefinition, scope *Scope, set bool) error { 833 if err := processClassElementName(&m.ClassElementName, scope, set); err != nil { 834 return err 835 } 836 scope = scope.newFunctionScope(m) 837 if err := processFormalParameters(&m.Params, scope, set); err != nil { 838 return err 839 } 840 if err := processBlockStatement(&m.FunctionBody, scope, set); err != nil { 841 return err 842 } 843 return nil 844 } 845 846 func processLexicalBinding(l *javascript.LexicalBinding, scope *Scope, set bool, typ BindingType) error { 847 if l.ArrayBindingPattern != nil { 848 if err := processArrayBindingPattern(l.ArrayBindingPattern, scope, set, typ); err != nil { 849 return err 850 } 851 } else if l.ObjectBindingPattern != nil { 852 if err := processObjectBindingPattern(l.ObjectBindingPattern, scope, set, typ); err != nil { 853 return err 854 } 855 } else if l.BindingIdentifier != nil && set { 856 if err := scope.setBinding(l.BindingIdentifier, typ); err != nil { 857 return err 858 } 859 } 860 if l.Initializer != nil { 861 if err := processAssignmentExpression(l.Initializer, scope, set); err != nil { 862 return err 863 } 864 } 865 return nil 866 } 867 868 func processConditionalExpression(c *javascript.ConditionalExpression, scope *Scope, set bool) error { 869 if c.LogicalORExpression != nil { 870 if err := processLogicalORExpression(c.LogicalORExpression, scope, set); err != nil { 871 return err 872 } 873 } else if c.CoalesceExpression != nil { 874 if err := processCoalesceExpression(c.CoalesceExpression, scope, set); err != nil { 875 return err 876 } 877 } 878 if c.True != nil { 879 if err := processAssignmentExpression(c.True, scope, set); err != nil { 880 return err 881 } 882 } 883 if c.False != nil { 884 if err := processAssignmentExpression(c.False, scope, set); err != nil { 885 return err 886 } 887 } 888 return nil 889 } 890 891 func processArrowFunction(a *javascript.ArrowFunction, scope *Scope, set bool) error { 892 scope = scope.newArrowFunctionScope(a) 893 if a.FormalParameters != nil { 894 if err := processFormalParameters(a.FormalParameters, scope, set); err != nil { 895 return err 896 } 897 } else if a.BindingIdentifier != nil && set { 898 if err := scope.setBinding(a.BindingIdentifier, BindingFunctionParam); err != nil { 899 return err 900 } 901 } 902 if a.AssignmentExpression != nil { 903 if err := processAssignmentExpression(a.AssignmentExpression, scope, set); err != nil { 904 return err 905 } 906 } else if a.FunctionBody != nil { 907 if err := processBlockStatement(a.FunctionBody, scope, set); err != nil { 908 return err 909 } 910 } 911 return nil 912 } 913 914 func processNewExpression(n *javascript.NewExpression, scope *Scope, set bool) error { 915 if err := processMemberExpression(&n.MemberExpression, scope, set); err != nil { 916 return err 917 } 918 return nil 919 } 920 921 func processCallExpression(c *javascript.CallExpression, scope *Scope, set bool) error { 922 if c.MemberExpression != nil { 923 if err := processMemberExpression(c.MemberExpression, scope, set); err != nil { 924 return err 925 } 926 } else if c.ImportCall != nil { 927 if err := processAssignmentExpression(c.ImportCall, scope, set); err != nil { 928 return err 929 } 930 } else if c.CallExpression != nil { 931 if err := processCallExpression(c.CallExpression, scope, set); err != nil { 932 return err 933 } 934 } 935 if c.Arguments != nil { 936 if err := processArguments(c.Arguments, scope, set); err != nil { 937 return err 938 } 939 } else if c.Expression != nil { 940 if err := processExpression(c.Expression, scope, set); err != nil { 941 return err 942 } 943 } else if c.TemplateLiteral != nil { 944 if err := processTemplateLiteral(c.TemplateLiteral, scope, set); err != nil { 945 return err 946 } 947 } 948 return nil 949 } 950 951 func processOptionalExpression(o *javascript.OptionalExpression, scope *Scope, set bool) error { 952 if o.MemberExpression != nil { 953 if err := processMemberExpression(o.MemberExpression, scope, set); err != nil { 954 return err 955 } 956 } else if o.CallExpression != nil { 957 if err := processCallExpression(o.CallExpression, scope, set); err != nil { 958 return err 959 } 960 } else if o.OptionalExpression != nil { 961 if err := processOptionalExpression(o.OptionalExpression, scope, set); err != nil { 962 return err 963 } 964 } 965 if err := processOptionalChain(&o.OptionalChain, scope, set); err != nil { 966 return err 967 } 968 return nil 969 } 970 971 func processBindingProperty(b *javascript.BindingProperty, scope *Scope, set bool, bindingType BindingType) error { 972 if err := processPropertyName(&b.PropertyName, scope, set); err != nil { 973 return err 974 } 975 if err := processBindingElement(&b.BindingElement, scope, set, bindingType); err != nil { 976 return err 977 } 978 return nil 979 } 980 981 func processBindingElement(b *javascript.BindingElement, scope *Scope, set bool, bindingType BindingType) error { 982 if b.SingleNameBinding != nil { 983 if bindingType == BindingBare && !set { 984 scope.addBinding(b.SingleNameBinding, BindingBare) 985 } else if bindingType != BindingBare && set { 986 if err := scope.setBinding(b.SingleNameBinding, bindingType); err != nil { 987 return err 988 } 989 } 990 } else if b.ArrayBindingPattern != nil { 991 if err := processArrayBindingPattern(b.ArrayBindingPattern, scope, set, bindingType); err != nil { 992 return err 993 } 994 } else if b.ObjectBindingPattern != nil { 995 if err := processObjectBindingPattern(b.ObjectBindingPattern, scope, set, bindingType); err != nil { 996 return err 997 } 998 } 999 if b.Initializer != nil { 1000 if err := processAssignmentExpression(b.Initializer, scope, set); err != nil { 1001 return err 1002 } 1003 } 1004 return nil 1005 } 1006 1007 func processPropertyName(p *javascript.PropertyName, scope *Scope, set bool) error { 1008 if p.ComputedPropertyName != nil { 1009 if err := processAssignmentExpression(p.ComputedPropertyName, scope, set); err != nil { 1010 return err 1011 } 1012 } 1013 return nil 1014 } 1015 1016 func processLogicalORExpression(l *javascript.LogicalORExpression, scope *Scope, set bool) error { 1017 if l.LogicalORExpression != nil { 1018 if err := processLogicalORExpression(l.LogicalORExpression, scope, set); err != nil { 1019 return err 1020 } 1021 } 1022 if err := processLogicalANDExpression(&l.LogicalANDExpression, scope, set); err != nil { 1023 return err 1024 } 1025 return nil 1026 } 1027 1028 func processCoalesceExpression(c *javascript.CoalesceExpression, scope *Scope, set bool) error { 1029 if c.CoalesceExpressionHead != nil { 1030 if err := processCoalesceExpression(c.CoalesceExpressionHead, scope, set); err != nil { 1031 return err 1032 } 1033 } 1034 if err := processBitwiseORExpression(&c.BitwiseORExpression, scope, set); err != nil { 1035 return err 1036 } 1037 return nil 1038 } 1039 1040 func processParenthesizedExpression(c *javascript.ParenthesizedExpression, scope *Scope, set bool) error { 1041 for n := range c.Expressions { 1042 if err := processAssignmentExpression(&c.Expressions[n], scope, set); err != nil { 1043 return err 1044 } 1045 } 1046 return nil 1047 } 1048 1049 func processMemberExpression(m *javascript.MemberExpression, scope *Scope, set bool) error { 1050 if m.PrimaryExpression != nil { 1051 if err := processPrimaryExpression(m.PrimaryExpression, scope, set); err != nil { 1052 return err 1053 } 1054 } else if m.MemberExpression != nil { 1055 if err := processMemberExpression(m.MemberExpression, scope, set); err != nil { 1056 return err 1057 } 1058 if m.Expression != nil { 1059 if err := processExpression(m.Expression, scope, set); err != nil { 1060 return err 1061 } 1062 } else if m.TemplateLiteral != nil { 1063 if err := processTemplateLiteral(m.TemplateLiteral, scope, set); err != nil { 1064 return err 1065 } 1066 } else if m.Arguments != nil { 1067 if err := processArguments(m.Arguments, scope, set); err != nil { 1068 return err 1069 } 1070 } 1071 } 1072 return nil 1073 } 1074 1075 func processArguments(a *javascript.Arguments, scope *Scope, set bool) error { 1076 for n := range a.ArgumentList { 1077 if err := processAssignmentExpression(&a.ArgumentList[n].AssignmentExpression, scope, set); err != nil { 1078 return err 1079 } 1080 } 1081 return nil 1082 } 1083 1084 func processTemplateLiteral(t *javascript.TemplateLiteral, scope *Scope, set bool) error { 1085 for n := range t.Expressions { 1086 if err := processExpression(&t.Expressions[n], scope, set); err != nil { 1087 return err 1088 } 1089 } 1090 return nil 1091 } 1092 1093 func processOptionalChain(o *javascript.OptionalChain, scope *Scope, set bool) error { 1094 if o.OptionalChain != nil { 1095 if err := processOptionalChain(o.OptionalChain, scope, set); err != nil { 1096 return err 1097 } 1098 } 1099 if o.Arguments != nil { 1100 if err := processArguments(o.Arguments, scope, set); err != nil { 1101 return err 1102 } 1103 } else if o.Expression != nil { 1104 if err := processExpression(o.Expression, scope, set); err != nil { 1105 return err 1106 } 1107 } else if o.TemplateLiteral != nil { 1108 if err := processTemplateLiteral(o.TemplateLiteral, scope, set); err != nil { 1109 return err 1110 } 1111 } 1112 return nil 1113 } 1114 1115 func processLogicalANDExpression(l *javascript.LogicalANDExpression, scope *Scope, set bool) error { 1116 if l.LogicalANDExpression != nil { 1117 if err := processLogicalANDExpression(l.LogicalANDExpression, scope, set); err != nil { 1118 return err 1119 } 1120 } 1121 if err := processBitwiseORExpression(&l.BitwiseORExpression, scope, set); err != nil { 1122 return err 1123 } 1124 return nil 1125 } 1126 1127 func processBitwiseORExpression(b *javascript.BitwiseORExpression, scope *Scope, set bool) error { 1128 if b.BitwiseORExpression != nil { 1129 if err := processBitwiseORExpression(b.BitwiseORExpression, scope, set); err != nil { 1130 return err 1131 } 1132 } 1133 if err := processBitwiseXORExpression(&b.BitwiseXORExpression, scope, set); err != nil { 1134 return err 1135 } 1136 return nil 1137 } 1138 1139 func processPrimaryExpression(p *javascript.PrimaryExpression, scope *Scope, set bool) error { 1140 if p.ArrayLiteral != nil { 1141 if err := processArrayLiteral(p.ArrayLiteral, scope, set); err != nil { 1142 return err 1143 } 1144 } else if p.ObjectLiteral != nil { 1145 if err := processObjectLiteral(p.ObjectLiteral, scope, set); err != nil { 1146 return err 1147 } 1148 } else if p.FunctionExpression != nil { 1149 if err := processFunctionDeclaration(p.FunctionExpression, scope, set, true); err != nil { 1150 return err 1151 } 1152 } else if p.ClassExpression != nil { 1153 if err := processClassDeclaration(p.ClassExpression, scope, set, true); err != nil { 1154 return err 1155 } 1156 } else if p.TemplateLiteral != nil { 1157 if err := processTemplateLiteral(p.TemplateLiteral, scope, set); err != nil { 1158 return err 1159 } 1160 } else if p.ParenthesizedExpression != nil { 1161 if err := processParenthesizedExpression(p.ParenthesizedExpression, scope, set); err != nil { 1162 return err 1163 } 1164 } else if p.This != nil && !set { 1165 scope.addBinding(p.This, BindingRef) 1166 } else if p.IdentifierReference != nil && !set { 1167 scope.addBinding(p.IdentifierReference, BindingRef) 1168 } 1169 return nil 1170 } 1171 1172 func processBitwiseXORExpression(b *javascript.BitwiseXORExpression, scope *Scope, set bool) error { 1173 if b.BitwiseXORExpression != nil { 1174 if err := processBitwiseXORExpression(b.BitwiseXORExpression, scope, set); err != nil { 1175 return err 1176 } 1177 } 1178 if err := processBitwiseANDExpression(&b.BitwiseANDExpression, scope, set); err != nil { 1179 return err 1180 } 1181 return nil 1182 } 1183 1184 func processArrayElement(a *javascript.ArrayElement, scope *Scope, set bool) error { 1185 return processAssignmentExpression(&a.AssignmentExpression, scope, set) 1186 } 1187 1188 func processArrayLiteral(a *javascript.ArrayLiteral, scope *Scope, set bool) error { 1189 for n := range a.ElementList { 1190 if err := processArrayElement(&a.ElementList[n], scope, set); err != nil { 1191 return err 1192 } 1193 } 1194 return nil 1195 } 1196 1197 func processObjectLiteral(o *javascript.ObjectLiteral, scope *Scope, set bool) error { 1198 for n := range o.PropertyDefinitionList { 1199 if err := processPropertyDefinition(&o.PropertyDefinitionList[n], scope, set); err != nil { 1200 return err 1201 } 1202 } 1203 return nil 1204 } 1205 1206 func processBitwiseANDExpression(b *javascript.BitwiseANDExpression, scope *Scope, set bool) error { 1207 if b.BitwiseANDExpression != nil { 1208 if err := processBitwiseANDExpression(b.BitwiseANDExpression, scope, set); err != nil { 1209 return err 1210 } 1211 } 1212 if err := processEqualityExpression(&b.EqualityExpression, scope, set); err != nil { 1213 return err 1214 } 1215 return nil 1216 } 1217 1218 func processPropertyDefinition(p *javascript.PropertyDefinition, scope *Scope, set bool) error { 1219 if p.PropertyName != nil { 1220 if err := processPropertyName(p.PropertyName, scope, set); err != nil { 1221 return err 1222 } 1223 } 1224 if p.AssignmentExpression != nil { 1225 if err := processAssignmentExpression(p.AssignmentExpression, scope, set); err != nil { 1226 return err 1227 } 1228 } 1229 if p.MethodDefinition != nil { 1230 if err := processMethodDefinition(p.MethodDefinition, scope, set); err != nil { 1231 return err 1232 } 1233 } 1234 return nil 1235 } 1236 1237 func processEqualityExpression(e *javascript.EqualityExpression, scope *Scope, set bool) error { 1238 if e.EqualityExpression != nil { 1239 if err := processEqualityExpression(e.EqualityExpression, scope, set); err != nil { 1240 return err 1241 } 1242 } 1243 if err := processRelationalExpression(&e.RelationalExpression, scope, set); err != nil { 1244 return err 1245 } 1246 return nil 1247 } 1248 1249 func processRelationalExpression(r *javascript.RelationalExpression, scope *Scope, set bool) error { 1250 if r.RelationalExpression != nil { 1251 if err := processRelationalExpression(r.RelationalExpression, scope, set); err != nil { 1252 return err 1253 } 1254 } 1255 if err := processShiftExpression(&r.ShiftExpression, scope, set); err != nil { 1256 return err 1257 } 1258 return nil 1259 } 1260 1261 func processShiftExpression(s *javascript.ShiftExpression, scope *Scope, set bool) error { 1262 if s.ShiftExpression != nil { 1263 if err := processShiftExpression(s.ShiftExpression, scope, set); err != nil { 1264 return err 1265 } 1266 } 1267 if err := processAdditiveExpression(&s.AdditiveExpression, scope, set); err != nil { 1268 return err 1269 } 1270 return nil 1271 } 1272 1273 func processAdditiveExpression(a *javascript.AdditiveExpression, scope *Scope, set bool) error { 1274 if a.AdditiveExpression != nil { 1275 if err := processAdditiveExpression(a.AdditiveExpression, scope, set); err != nil { 1276 return err 1277 } 1278 } 1279 if err := processMultiplicativeExpression(&a.MultiplicativeExpression, scope, set); err != nil { 1280 return err 1281 } 1282 return nil 1283 } 1284 1285 func processMultiplicativeExpression(m *javascript.MultiplicativeExpression, scope *Scope, set bool) error { 1286 if m.MultiplicativeExpression != nil { 1287 if err := processMultiplicativeExpression(m.MultiplicativeExpression, scope, set); err != nil { 1288 return err 1289 } 1290 } 1291 if err := processExponentiationExpression(&m.ExponentiationExpression, scope, set); err != nil { 1292 return err 1293 } 1294 return nil 1295 } 1296 1297 func processExponentiationExpression(e *javascript.ExponentiationExpression, scope *Scope, set bool) error { 1298 if e.ExponentiationExpression != nil { 1299 if err := processExponentiationExpression(e.ExponentiationExpression, scope, set); err != nil { 1300 return err 1301 } 1302 } 1303 if err := processUnaryExpression(&e.UnaryExpression, scope, set); err != nil { 1304 return err 1305 } 1306 return nil 1307 } 1308 1309 func processUnaryExpression(u *javascript.UnaryExpression, scope *Scope, set bool) error { 1310 if err := processUpdateExpression(&u.UpdateExpression, scope, set); err != nil { 1311 return err 1312 } 1313 return nil 1314 } 1315 1316 func processUpdateExpression(u *javascript.UpdateExpression, scope *Scope, set bool) error { 1317 if u.LeftHandSideExpression != nil { 1318 if err := processLeftHandSideExpression(u.LeftHandSideExpression, scope, set); err != nil { 1319 return err 1320 } 1321 } 1322 if u.UnaryExpression != nil { 1323 if err := processUnaryExpression(u.UnaryExpression, scope, set); err != nil { 1324 return err 1325 } 1326 } 1327 return nil 1328 } 1329