Typescript
  • Introducão
  • Primeiros Passos
    • Por que TypeScript
  • JavaScript
    • Igualdade
    • Referências
    • Null vs. Undefined
    • this
    • Closure
    • Number
    • Truthy
  • Futuro JavaScript Agora
    • Classes
      • Classes Emit
    • Arrow Functions
    • Rest Parameters
    • let
    • const
    • Destructuring
    • Spread Operator
    • for...of
    • Iterators
    • Template Strings
    • Promise
    • Generators
    • Async Await
  • Project
    • Compilation Context
      • tsconfig.json
      • Which Files?
    • Declaration Spaces
    • Modules
      • File Module Details
      • globals.d.ts
    • Namespaces
    • Dynamic Import Expressions
  • Node.js QuickStart
  • Browser QuickStart
  • TypeScript's Type System
    • JS Migration Guide
    • @types
    • Ambient Declarations
      • Declaration Files
      • Variables
    • Interfaces
    • Enums
    • lib.d.ts
    • Functions
    • Callable
    • Type Assertion
    • Freshness
    • Type Guard
    • Literal Types
    • Readonly
    • Generics
    • Type Inference
    • Type Compatibility
    • Never Type
    • Discriminated Unions
    • Index Signatures
    • Moving Types
    • Exception Handling
    • Mixins
  • JSX
    • React
    • Non React JSX
  • Options
    • noImplicitAny
    • strictNullChecks
  • Errors in TypeScript
    • Interpreting Errors
    • Common Errors
  • NPM
  • Testing
    • Jest
    • Cypress
  • Tools
    • Prettier
    • Husky
    • Changelog
  • TIPs
    • String Based Enums
    • Nominal Typing
    • Stateful Functions
    • Bind is Bad
    • Currying
    • Type Instantiation
    • Lazy Object Literal Initialization
    • Classes are Useful
    • Avoid Export Default
    • Limit Property Setters
    • outFile caution
    • JQuery tips
    • static constructors
    • singleton pattern
    • Function parameters
    • Build Toggles
    • Barrel
    • Create Arrays
    • Typesafe Event Emitter
  • StyleGuide
  • TypeScript Compiler Internals
    • Program
    • AST
      • TIP: Visit Children
      • TIP: SyntaxKind enum
      • Trivia
    • Scanner
    • Parser
      • Parser Functions
    • Binder
      • Binder Functions
      • Binder Declarations
      • Binder Container
      • Binder SymbolTable
      • Binder Error Reporting
    • Checker
      • Checker Diagnostics
      • Checker Error Reporting
    • Emitter
      • Emitter Functions
      • Emitter SourceMaps
    • Contributing
Powered by GitBook
On this page
  • bindSourceFile
  • bind
  • bindWorker
  • bindFoo functions

Was this helpful?

  1. TypeScript Compiler Internals
  2. Binder

Binder Functions

Two critical binder functions are bindSourceFile and mergeSymbolTable. We will take a look at these next.

bindSourceFile

Basically checks if file.locals is defined, if not it hands over to (a local function) bind.

Note: locals is defined on Node and is of type SymbolTable. Note that SourceFile is also a Node (in fact a root node in the AST).

TIP: local functions are used heavily within the TypeScript compiler. A local function very likely uses variables from the parent function (captured by closure). In the case of bind (a local function within bindSourceFile) it (or a function it calls) will setup the symbolCount and classifiableNames among others, that are then stored on the returned SourceFile.

bind

Bind takes any Node (not just SourceFile). First thing it does is assign the node.parent (if parent variable has been setup ... which again is something the binder does during its processing within the bindChildren function), then hands off to bindWorker which does the heavy lifting. Finally it calls bindChildren (a function that simply stores the binder state e.g. current parent within its function local vars, then calls bind on each child, and then restores the binder state). Now let's look at bindWorker which is the more interesting function.

bindWorker

This function switches on node.kind (of type SyntaxKind) and delegates work to the appropriate bindFoo function (also defined within binder.ts). For example if the node is a SourceFile it calls (eventually and only if its an external file module) bindAnonymousDeclaration

bindFoo functions

There are a few patterns common to bindFoo functions as well as some utility functions that these use. One function that is almost always used is the createSymbol function. It is presented in its entirety below:

function createSymbol(flags: SymbolFlags, name: string): Symbol {
    symbolCount++;
    return new Symbol(flags, name);
}

As you can see it is simply keeping the symbolCount (a local to bindSourceFile) up to date and creating the symbol with the specified parameters.

PreviousBinderNextBinder Declarations

Last updated 6 years ago

Was this helpful?