FS FormScript
Package@formscript/scope
Specification2.4, 6.3, 6.11
Depends onast, types

@formscript/scope

The symbol table and lexical scoping of §6.3 and §6.11 of the specification.

Scoping is a property of a document, not of a parse: the same expression resolves differently in two blocks. This package therefore builds a scope tree from a finished AST, and leaves the parser free of scope concerns.

Usage

ts
import { buildScopes, resolveReference, scopeChain } from '@formscript/scope';

const { root, declarations, duplicates } = buildScopes(document);

resolveReference(root, ['household', 'members']);   // the declaration, or null
scopeChain(scope);                                   // the scope and its ancestors

What it reports

buildScopes returns:

  • root — the scope of the form body;
  • declarations — every declaration with its path (['household', 'members']), its kind, its declared type, and, for a repeat, its declared max;
  • duplicates — a name declared twice in the same scope. A declaration that shadows an outer one is not a duplicate; it is a shadow, and the scope chain resolves it. A state that shadows a field name is a duplicate (Spec-8.7), and the field keeps the name.

A state block declares its name in the form root scope and contributes no data level (Spec-7.1, Spec-8.7); its members are declarations of kind state-member in the scope of the state.

References under the $ namespace ($env, $user, $form) are resolved by the runtime and are never checked statically (§2.4).

Public API

11 exported symbols.

buildScopes re-export
createScope re-export
Declaration interface
DeclarationKind type
DuplicateDeclaration interface
findDeclaration re-export
isDataBearing re-export
resolveReference re-export
Scope interface
scopeChain re-export
ScopeResult interface