validate
On this page

validate()

Function in Diagnostics

Check an authored module against the structural rules every emit depends on, and throw a ValidationError when any of them fails.

import { validate } from 'typeshade'

The signature, the description and the examples come from the compiler's own source at commit 26de7be8.

Syntax

function validate(m: ModuleDecl): void

Parameters

m ModuleDecl

the authored module, as built, before any pass has rewritten its nodes.

Return value

void

Exceptions

@link ValidationError

carrying every error-severity diagnostic, with code SD0020.

ValidationError

Check an authored module against the structural rules every emit depends on, and throw a ValidationError when any of them fails. emitModule, emitGlslModule and compileModule each run this check first, before they rewrite the module for their target, so a structurally invalid module is reported at the line that authored it and never reaches the driver as invalid WGSL or GLSL.

Description

emitModule, emitGlslModule and compileModule each run this check first, before they rewrite the module for their target, so a structurally invalid module is reported at the line that authored it and never reaches the driver as invalid WGSL or GLSL. Call it directly when you want that answer without emitting.

One error reports every failure. validate collects every error-severity diagnostic, and the throw carries them all on err.diagnostics; the message renders the same list as text. Each diagnostic names its code (SD0107 and its siblings), the ruleId that raised it, the fn it was found in, and, with setSourceTracing on, the file:line:col that authored the node.

It runs the core rules: duplicate struct or function names, binding collisions, a function path that ends without returning a value, mixed scalar types in one expression, a call whose arity or argument types disagree with the function it names, a fragment-only builtin read outside a fragment entry, a portable kernel that cannot emit on both backends, and a local that shadows another in the same function. These hold for every module, including modules assembled with composeModule and compute kernels that return early on purpose. Style rules are lint-only; run lintModule or diagnose for those.

Examples

Example

import { validate, ValidationError } from 'typeshade'
try {
validate(MODULE)
} catch (e) {
if (e instanceof ValidationError) for (const d of e.diagnostics) report(d.code, d.message)
else throw e
}

In the guide

See also

Source

src/core/passes/validate.ts, line 119, at commit 26de7be8

Edit this page Report a problem