buildScopes
re-export| Package | @formscript/scope |
|---|---|
| Specification | 2.4, 6.3, 6.11 |
| Depends on | ast, 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
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 arepeat, its declaredmax;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. Astatethat 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.
createScope
re-exportDeclaration
interfaceDeclarationKind
typeDuplicateDeclaration
interfacefindDeclaration
re-exportisDataBearing
re-exportresolveReference
re-exportScope
interfacescopeChain
re-exportScopeResult
interface