Skip to content

Function Registry Contract

1. Purpose

This document defines the minimal contract for a function registry in the dtcg-formulas ecosystem.

A registry is responsible for:

  • storing function definitions parsed from .module.scssdef files
  • resolving function names to their declarations
  • supporting both built-in and custom function families

2. Registry interface

A registry must support the following operations:

2.1 Register a function

register(modulePath: string, functionName: string, declaration: FunctionDeclaration): void

Associates a parsed function declaration with a modulePath#functionName key.

2.2 Resolve a function

resolve(definitionRef: string): FunctionDeclaration | null

Accepts a definition reference in the format path/to/module.module.scssdef#functionName and returns the declaration, or null if not found.

2.3 List functions

list(): DefinitionRef[]

Returns all registered definition references.


3. Function declaration shape

A FunctionDeclaration must include at minimum:

FieldTypeDescription
namestringFunction name
parametersParameter[]Ordered list of parameters
returnExpressionstringThe symbolic @return expression body
metadataFunctionMetadataParsed doc comment metadata

A Parameter must include:

FieldTypeDescription
namestringParameter name (without $)
typestring | nullDocumented type annotation
defaultstring | nullDefault value expression
descriptionstringParameter description

4. Custom function adapters

The registry should support custom functions that are not defined in .module.scssdef files but are implemented by external adapters.

A custom adapter must provide:

evaluate(args: ResolvedArgument[]): ResolvedValue

Custom functions must be:

  • scalar and one-to-one (one input set, one output value)
  • pure and deterministic
  • optionally namespaced (e.g., leonardo.color)

5. Built-in functions

The following functions should be available in any compliant registry without explicit registration:

FunctionDescription
roundRound a value to a step with mode (nearest/floor/ceil)
clampConstrain a value between min and max
minReturn the smaller of two values
maxReturn the larger of two values

These correspond to CSS/Sass math built-ins and require no .module.scssdef definition.


6. Summary

The registry contract is intentionally minimal. It connects parsed definitions to the resolver without prescribing storage, caching, or lifecycle concerns. Implementations may add indexing, validation, or hot-reload as needed.

Released under the MIT License.