Symbols and Declarations

Linking between a node and a symbol is performed by a few functions. One function that is used to bind the SourceFile node to the source file Symbol (in case of an external module) is the addDeclarationToSymbol function

Note : the Symbol for an external module source file is setup as flags : SymbolFlags.ValueModule and name: '"' + removeFileExtension(file.fileName) + '"').

  1. function addDeclarationToSymbol(symbol: Symbol, node: Declaration, symbolFlags: SymbolFlags) {
  2. symbol.flags |= symbolFlags;
  3. node.symbol = symbol;
  4. if (!symbol.declarations) {
  5. symbol.declarations = [];
  6. }
  7. symbol.declarations.push(node);
  8. if (symbolFlags & SymbolFlags.HasExports && !symbol.exports) {
  9. symbol.exports = {};
  10. }
  11. if (symbolFlags & SymbolFlags.HasMembers && !symbol.members) {
  12. symbol.members = {};
  13. }
  14. if (symbolFlags & SymbolFlags.Value && !symbol.valueDeclaration) {
  15. symbol.valueDeclaration = node;
  16. }
  17. }

The important linking portions:

  • Creates a link to the Symbol from the AST node (node.symbol).
  • Adds the node as one of the declarations of the Symbol (symbol.declarations).

Declaration

Declaration is just a node with an optional name. In types.ts

  1. interface Declaration extends Node {
  2. _declarationBrand: any;
  3. name?: DeclarationName;
  4. }