typescript-extract-shared.ts

Shared utilities for the per-declaration extractors in typescript-extract-*.ts.

Holds helpers used across function, type, and class extraction: signature parameter extraction, overload detection, generic parsing, modifier extraction, location reporting, intersection-property filtering, and runes detection.

@see typescript-extract-function.ts, typescript-extract-type.ts, typescript-extract-class.ts for the per-declaration extractors that build on these helpers

Declarations
#

11 declarations

view source

detectReactivity
#

typescript-extract-shared.ts view source

(initializer: Expression | undefined): "$state" | "$state.raw" | "$derived" | "$derived.by" | undefined

Detect a Svelte 5 reactivity rune from a variable or property initializer.

Inspects the AST since runes erase to their inner type after type-checking. Returns undefined for any non-rune expression. See the Reactivity enum in types.ts for the rationale on running this on every file regardless of extension.

initializer

type Expression | undefined

returns

"$state" | "$state.raw" | "$derived" | "$derived.by" | undefined

emitCallOrConstructSignature
#

typescript-extract-shared.ts view source

(getSignatures: () => readonly Signature[], signatureKind: "call" | "construct", resolveTsdocNode: (sig: Signature) => Node | undefined, paramValidationFallbackNode: Node, declaration: DeclarationJsonBuild, checker: TypeChecker, diagnostics: ({ ...; } | ... 12 more ... | { ...; })[], errorContext: { ...; }): void

Append a (call) or (construct) signature member to a declaration.

Captures the extraction logic shared by interface processing (extractTypeInfo) and type-alias property processing (extractTypeAliasProperties): type signature, parameters, generics, overloads, and TSDoc. The TSDoc source node is supplied by the caller — interfaces look it up in node.members (skipping TSDoc when no inline signature is declared, even if one is inherited), type aliases use sig.getDeclaration().

getSignatures

thunk to retrieve getCallSignatures() / getConstructSignatures(); called inside the try so checker errors are captured as diagnostics

type () => readonly Signature[]

signatureKind

'call' (member kind: function, includes returnType) or 'construct' (member kind: constructor, no returnType)

type "call" | "construct"

resolveTsdocNode

callback returning the AST node to parse TSDoc from, or undefined to skip TSDoc resolution

type (sig: Signature) => Node | undefined

paramValidationFallbackNode

location used by validateParamKeys when resolveTsdocNode returns undefined

type Node

declaration

parent declaration (mutated; appended to members)

checker

type TypeChecker

diagnostics

type ({ symbolName: string; file: string; message: string; severity: "error" | "warning"; kind: "type_extraction_failed"; line?: number | undefined; column?: number | undefined; } | { functionName: string; ... 5 more ...; column?: number | undefined; } | ... 11 more ... | { ...; })[]

errorContext

type { node: Node; kindLabel: string; }

returns

void

extractModifiers
#

typescript-extract-shared.ts view source

(modifiers: readonly ModifierLike[] | undefined): ("public" | "protected" | "readonly" | "static" | "abstract" | "getter" | "setter")[]

Extract modifier keywords from a node's modifiers.

Returns an array of modifier strings like ['public', 'readonly', 'static'].

modifiers

type readonly ModifierLike[] | undefined

returns

("public" | "protected" | "readonly" | "static" | "abstract" | "getter" | "setter")[]

extractSignatureParameters
#

typescript-extract-shared.ts view source

(sig: Signature, checker: TypeChecker, tsdocParams: Record<string, string> | undefined): { name: string; type: string; optional: boolean; rest: boolean; description?: string | undefined; defaultValue?: string | undefined; }[]

Extract parameters from a TypeScript signature with TSDoc descriptions and default values.

Shared helper for extracting parameter information from both standalone functions and class methods/constructors.

sig

the TypeScript signature to extract parameters from

type Signature

checker

TypeScript type checker for type resolution

type TypeChecker

tsdocParams

record of parameter names to TSDoc descriptions (from TsdocParsedComment.params)

type Record<string, string> | undefined

returns

{ name: string; type: string; optional: boolean; rest: boolean; description?: string | undefined; defaultValue?: string | undefined; }[]

array of ParameterJson objects

filterIntersectionProperties
#

typescript-extract-shared.ts view source

(type: Type, typeNode: Node, checker: TypeChecker, isExternalFile: IsExternalFile): { properties: Symbol[]; intersectionTypes: string[]; } | null

Filter intersection type properties to exclude those from external sources, and extract type names for branches whose properties are all external.

Returns null for non-intersection types (caller should use unfiltered properties). For intersection types, returns filtered properties and external type names.

Per-property filtering works through utility types (Partial, Pick, Readonly) because TypeScript preserves original declaration sources on mapped type properties.

type

type Type

typeNode

type Node

checker

type TypeChecker

isExternalFile

returns

{ properties: Symbol[]; intersectionTypes: string[]; } | null

getNodeLocation
#

typescript-extract-shared.ts view source

(node: Node): { file: string; line: number; column: number; }

Extract line and column from a TypeScript node. Returns 1-based line and column numbers.

node

type Node

returns

{ file: string; line: number; column: number; }

inferDeclarationKind
#

typescript-extract-shared.ts view source

(symbol: Symbol, node: Node): "function" | "type" | "variable" | "class" | "interface" | "enum" | "component" | "snippet" | "namespace"

Infer declaration kind from symbol and node.

Maps TypeScript constructs to DeclarationKind: - Classes → 'class' - Functions (declarations, expressions, arrows) → 'function' - Interfaces → 'interface' - Type aliases → 'type' - Enums (regular and const) → 'enum' - Variables → 'variable' (unless function-valued → 'function')

Note: namespace re-exports (export * as ns from './x') have no inline declaration form in TypeScript and are caught upstream in analyzeExports via classifyNamespaceReExport. They never reach this function. A direct call here on a ValueModule symbol would fall through to 'variable' and leak typeof import("/abs/path") into the output — keep the namespace dispatch in analyzeExports.

symbol

type Symbol

node

type Node

returns

"function" | "type" | "variable" | "class" | "interface" | "enum" | "component" | "snippet" | "namespace"

isExternalIntersectionBranch
#

typescript-extract-shared.ts view source

(branchNode: TypeNode, checker: TypeChecker, isExternalFile: IsExternalFile): boolean

Determine whether an intersection branch refers to a type whose declarations all live in external files (e.g., HTMLAttributes<HTMLDivElement> from svelte's d.ts). Inline object-literal branches and unrecognized node shapes return false (treated as local).

branchNode

type TypeNode

checker

type TypeChecker

isExternalFile

returns

boolean

parseGenericParam
#

typescript-extract-shared.ts view source

(param: TypeParameterDeclaration): { name: string; constraint?: string | undefined; defaultType?: string | undefined; }

Parse a TypeScript generic type parameter declaration into structured info.

param

the TypeScript type parameter declaration node

type TypeParameterDeclaration

returns

{ name: string; constraint?: string | undefined; defaultType?: string | undefined; }

structured GenericParamJson with name, constraint, and default type

populateCallableMember
#

typescript-extract-shared.ts view source

(target: MemberJsonBuild | DeclarationJsonBuild, signatures: readonly Signature[], checker: TypeChecker, tsdoc: TsdocParsedComment | undefined, paramValidationNode: Node, name: string, diagnostics: ({ ...; } | ... 12 more ... | { ...; })[], includeReturn?: boolean): void

Populate the callable fields of a declaration or member from its call/construct signatures: typeSignature, parameters, overloads, and (unless includeReturn is false) returnType / returnDescription.

The shared core of every named-callable extractor — standalone functions, interface methods, class methods/constructors, and type-alias function properties. Callers differ in how they obtain signatures (symbol type, constructor declarations, property call signatures) and in their own try/catch + diagnostic kind, so those stay at the callsite; this captures only the identical projection from a resolved signature list onto the build target. No-op when signatures is empty.

target

declaration or member build object (mutated)

type MemberJsonBuild | DeclarationJsonBuild

signatures

public call/construct signatures (signatures[0] is primary)

type readonly Signature[]

checker

type TypeChecker

tsdoc

parsed TSDoc for the target (supplies @param/@returns)

type TsdocParsedComment | undefined

paramValidationNode

node validateParamKeys reports unknown_param against

type Node

name

target name, for diagnostic messages

type string

diagnostics

type ({ symbolName: string; file: string; message: string; severity: "error" | "warning"; kind: "type_extraction_failed"; line?: number | undefined; column?: number | undefined; } | { functionName: string; ... 5 more ...; column?: number | undefined; } | ... 11 more ... | { ...; })[]

includeReturn

set false for constructors (no return type/description)

type boolean
default true

returns

void

resolveIntersectionTypeNode
#

typescript-extract-shared.ts view source

(type: Type, typeNode: Node): IntersectionTypeNode | undefined

Resolve the IntersectionTypeNode for a type node, walking through type alias indirection. Returns undefined when the underlying type is an intersection but no AST node is recoverable (rare — synthesized intersections).

type

type Type

typeNode

type Node

returns

IntersectionTypeNode | undefined

Depends on
#

Imported by
#