tjs-lang 0.6.19 → 0.6.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLAUDE.md +29 -24
- package/bin/dev.ts +9 -0
- package/demo/docs.json +49 -1
- package/demo/src/playground-shared.ts +8 -39
- package/demo/src/tjs-playground.ts +8 -0
- package/demo/src/tjs-runtime-iframe.ts +10 -0
- package/demo/src/ts-playground.ts +8 -0
- package/package.json +1 -1
- package/src/cli/commands/run.ts +4 -1
- package/src/cli/tjs.ts +1 -1
- package/src/lang/codegen.test.ts +26 -27
- package/src/lang/emitters/dts.test.ts +22 -0
- package/src/lang/emitters/dts.ts +5 -1
- package/src/lang/emitters/js-tests.ts +13 -19
- package/src/lang/emitters/js.ts +39 -7
- package/src/lang/parser-transforms.ts +18 -5
- package/src/lang/roundtrip.test.ts +1 -1
- package/src/lang/runtime.ts +88 -2
- package/src/lang/wasm.ts +1 -1
- package/dist/bin/benchmarks.d.ts +0 -9
- package/dist/bin/dev.d.ts +0 -1
- package/dist/bin/docs.d.ts +0 -1
- package/dist/bin/select-local-models.d.ts +0 -1
- package/dist/examples/modules/dist/main.d.ts +0 -34
- package/dist/examples/modules/dist/math.d.ts +0 -120
- package/dist/index.js +0 -426
- package/dist/index.js.map +0 -45
- package/dist/scripts/build-demo.d.ts +0 -2
- package/dist/scripts/build.d.ts +0 -11
- package/dist/scripts/seed-blog-rules.d.ts +0 -2
- package/dist/scripts/seed-stored-function.d.ts +0 -2
- package/dist/src/atoms/batteries.d.ts +0 -4
- package/dist/src/atoms/browser.d.ts +0 -4
- package/dist/src/atoms/index.d.ts +0 -4
- package/dist/src/batteries/audit.d.ts +0 -9
- package/dist/src/batteries/index.d.ts +0 -28
- package/dist/src/batteries/llm.d.ts +0 -18
- package/dist/src/batteries/models.d.ts +0 -19
- package/dist/src/batteries/store.d.ts +0 -13
- package/dist/src/builder.d.ts +0 -127
- package/dist/src/bun-plugin/tjs-plugin.d.ts +0 -12
- package/dist/src/cli/commands/check.d.ts +0 -4
- package/dist/src/cli/commands/convert.d.ts +0 -20
- package/dist/src/cli/commands/emit.d.ts +0 -34
- package/dist/src/cli/commands/run.d.ts +0 -6
- package/dist/src/cli/commands/test.d.ts +0 -21
- package/dist/src/cli/commands/types.d.ts +0 -4
- package/dist/src/cli/create-app.d.ts +0 -9
- package/dist/src/cli/playground.d.ts +0 -9
- package/dist/src/cli/tjs.d.ts +0 -15
- package/dist/src/cli/tjsx.d.ts +0 -16
- package/dist/src/index.d.ts +0 -11
- package/dist/src/inference.types.d.ts +0 -1
- package/dist/src/lang/core.d.ts +0 -56
- package/dist/src/lang/docs.d.ts +0 -69
- package/dist/src/lang/emitters/ast.d.ts +0 -24
- package/dist/src/lang/emitters/dts.d.ts +0 -48
- package/dist/src/lang/emitters/from-ts.d.ts +0 -94
- package/dist/src/lang/emitters/js-tests.d.ts +0 -70
- package/dist/src/lang/emitters/js-wasm.d.ts +0 -15
- package/dist/src/lang/emitters/js.d.ts +0 -175
- package/dist/src/lang/eval.d.ts +0 -63
- package/dist/src/lang/index.d.ts +0 -216
- package/dist/src/lang/inference.d.ts +0 -40
- package/dist/src/lang/linter.d.ts +0 -53
- package/dist/src/lang/metadata-cache.d.ts +0 -172
- package/dist/src/lang/parser-params.d.ts +0 -37
- package/dist/src/lang/parser-transforms.d.ts +0 -281
- package/dist/src/lang/parser-types.d.ts +0 -175
- package/dist/src/lang/parser.d.ts +0 -56
- package/dist/src/lang/runtime.d.ts +0 -454
- package/dist/src/lang/schema.d.ts +0 -35
- package/dist/src/lang/tests.d.ts +0 -94
- package/dist/src/lang/transpiler.d.ts +0 -25
- package/dist/src/lang/types.d.ts +0 -147
- package/dist/src/lang/wasm.d.ts +0 -83
- package/dist/src/rbac/index.d.ts +0 -85
- package/dist/src/rbac/rules.d.ts +0 -184
- package/dist/src/runtime.d.ts +0 -4
- package/dist/src/store/index.d.ts +0 -3
- package/dist/src/store/indexeddb.d.ts +0 -6
- package/dist/src/store/interface.d.ts +0 -90
- package/dist/src/store/memory.d.ts +0 -10
- package/dist/src/test-examples.d.ts +0 -41
- package/dist/src/test-utils.d.ts +0 -86
- package/dist/src/transpiler/index.d.ts +0 -6
- package/dist/src/transpiler/parser.d.ts +0 -4
- package/dist/src/transpiler/transformer.d.ts +0 -4
- package/dist/src/transpiler/type-system/inference.d.ts +0 -1
- package/dist/src/transpiler/types.d.ts +0 -4
- package/dist/src/types/LegalDate.d.ts +0 -241
- package/dist/src/types/Timestamp.d.ts +0 -233
- package/dist/src/types/Type.d.ts +0 -234
- package/dist/src/types/index.d.ts +0 -8
- package/dist/src/vm/atoms/batteries.d.ts +0 -6
- package/dist/src/vm/atoms/browser.d.ts +0 -18
- package/dist/src/vm/atoms/index.d.ts +0 -10
- package/dist/src/vm/index.d.ts +0 -12
- package/dist/src/vm/runtime.d.ts +0 -333
- package/dist/src/vm/vm.d.ts +0 -28
- package/dist/src/vm.d.ts +0 -4
- package/dist/test-preprocess.d.ts +0 -1
- package/dist/tjs-batteries.js +0 -4
- package/dist/tjs-batteries.js.map +0 -15
- package/dist/tjs-full.js +0 -426
- package/dist/tjs-full.js.map +0 -45
- package/dist/tjs-src/runtime.d.ts +0 -1
- package/dist/tjs-transpiler.js +0 -3
- package/dist/tjs-transpiler.js.map +0 -11
- package/dist/tjs-vm.js +0 -54
- package/dist/tjs-vm.js.map +0 -22
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Parser types and interfaces
|
|
3
|
-
*
|
|
4
|
-
* Shared type definitions used across parser modules.
|
|
5
|
-
*/
|
|
6
|
-
/** Parser options */
|
|
7
|
-
export interface ParseOptions {
|
|
8
|
-
/** Filename for error messages */
|
|
9
|
-
filename?: string;
|
|
10
|
-
/** Enable colon shorthand syntax preprocessing */
|
|
11
|
-
colonShorthand?: boolean;
|
|
12
|
-
/**
|
|
13
|
-
* Target is the VM (AJS code).
|
|
14
|
-
* When true, skips == to Is() transformation since the VM handles == correctly.
|
|
15
|
-
*/
|
|
16
|
-
vmTarget?: boolean;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* A WASM block extracted from source
|
|
20
|
-
*
|
|
21
|
-
* Simple form (body is both WASM source and JS fallback):
|
|
22
|
-
* wasm {
|
|
23
|
-
* for (let i = 0; i < arr.length; i++) { arr[i] *= 2 }
|
|
24
|
-
* }
|
|
25
|
-
*
|
|
26
|
-
* With explicit fallback (when WASM and JS need different code):
|
|
27
|
-
* wasm {
|
|
28
|
-
* // WASM-optimized path
|
|
29
|
-
* } fallback {
|
|
30
|
-
* // JS fallback using different approach
|
|
31
|
-
* }
|
|
32
|
-
*
|
|
33
|
-
* Variables are captured from scope automatically.
|
|
34
|
-
*/
|
|
35
|
-
export interface WasmBlock {
|
|
36
|
-
/** Unique ID for this block */
|
|
37
|
-
id: string;
|
|
38
|
-
/** The body (JS subset that compiles to WASM, also used as fallback) */
|
|
39
|
-
body: string;
|
|
40
|
-
/** Explicit fallback body (only if different from body) */
|
|
41
|
-
fallback?: string;
|
|
42
|
-
/** Variables captured from enclosing scope (auto-detected) */
|
|
43
|
-
captures: string[];
|
|
44
|
-
/** Start position in original source */
|
|
45
|
-
start: number;
|
|
46
|
-
/** End position in original source */
|
|
47
|
-
end: number;
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* A test block extracted from source
|
|
51
|
-
*
|
|
52
|
-
* Syntax:
|
|
53
|
-
* test { body }
|
|
54
|
-
* test 'description' { body }
|
|
55
|
-
*
|
|
56
|
-
* Tests run at transpile time and are stripped from output.
|
|
57
|
-
*/
|
|
58
|
-
export interface TestBlock {
|
|
59
|
-
/** Optional description */
|
|
60
|
-
description?: string;
|
|
61
|
-
/** The test body code */
|
|
62
|
-
body: string;
|
|
63
|
-
/** Start position in original source */
|
|
64
|
-
start: number;
|
|
65
|
-
/** End position in original source */
|
|
66
|
-
end: number;
|
|
67
|
-
/** Source line number (1-indexed) */
|
|
68
|
-
line?: number;
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Preprocess options
|
|
72
|
-
*/
|
|
73
|
-
export interface PreprocessOptions {
|
|
74
|
-
/** Skip test execution (tests still stripped from output) */
|
|
75
|
-
dangerouslySkipTests?: boolean;
|
|
76
|
-
/**
|
|
77
|
-
* Skip == to Is() transformation.
|
|
78
|
-
* Set to true for AJS code that runs in the VM, which already handles == correctly.
|
|
79
|
-
* Default: false (transform == to Is() for TJS code running in regular JS)
|
|
80
|
-
*/
|
|
81
|
-
vmTarget?: boolean;
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Tokenizer state for tracking context during source transformation
|
|
85
|
-
*/
|
|
86
|
-
export type TokenizerState = 'normal' | 'single-string' | 'double-string' | 'template-string' | 'line-comment' | 'block-comment' | 'regex';
|
|
87
|
-
/**
|
|
88
|
-
* Structural context for tracking where we are in the code
|
|
89
|
-
* This enables proper handling of class methods vs function calls
|
|
90
|
-
*/
|
|
91
|
-
export type StructuralContext = 'top-level' | 'class-body' | 'function-body' | 'block';
|
|
92
|
-
export interface ContextFrame {
|
|
93
|
-
type: StructuralContext;
|
|
94
|
-
braceDepth: number;
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Unified paren expression transformer using state machine tokenizer
|
|
98
|
-
*
|
|
99
|
-
* Model: opening paren can be ( or (? or (!, closing can be ) or )->type or )-?type or )-!type
|
|
100
|
-
*
|
|
101
|
-
* This unifies handling of:
|
|
102
|
-
* - Function declaration params: function foo(x: type) -> returnType { }
|
|
103
|
-
* - Arrow function params: (x: type) => expr
|
|
104
|
-
* - Safe/unsafe markers: function foo(?) or function foo(!)
|
|
105
|
-
* - Return type annotations: ) -> type or ) -? type or ) -! type
|
|
106
|
-
*
|
|
107
|
-
* @param source The source code to transform
|
|
108
|
-
* @param ctx Context for tracking required params, safe/unsafe functions, etc.
|
|
109
|
-
* @returns Transformed source and extracted metadata
|
|
110
|
-
*/
|
|
111
|
-
/** TJS mode flags for opt-in language improvements */
|
|
112
|
-
export interface TjsModes {
|
|
113
|
-
/** TjsEquals: == and != use structural equality */
|
|
114
|
-
tjsEquals: boolean;
|
|
115
|
-
/** TjsClass: classes callable without new, explicit new is banned */
|
|
116
|
-
tjsClass: boolean;
|
|
117
|
-
/** TjsDate: Date is banned, use Timestamp/LegalDate */
|
|
118
|
-
tjsDate: boolean;
|
|
119
|
-
/** TjsNoeval: eval() and new Function() are banned */
|
|
120
|
-
tjsNoeval: boolean;
|
|
121
|
-
/** TjsStandard: newlines as statement terminators (prevents ASI footguns) */
|
|
122
|
-
tjsStandard: boolean;
|
|
123
|
-
/** TjsSafeEval: include Eval/SafeFunction in runtime for dynamic code execution */
|
|
124
|
-
tjsSafeEval: boolean;
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Extension info for a single extend block
|
|
128
|
-
*/
|
|
129
|
-
export interface ExtensionInfo {
|
|
130
|
-
/** The type name being extended (e.g., 'String', 'Array', 'MyClass') */
|
|
131
|
-
typeName: string;
|
|
132
|
-
/** Method names defined in this extend block */
|
|
133
|
-
methods: string[];
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Transform `extend TypeName { ... }` blocks into `const __ext_TypeName = { ... }` objects
|
|
137
|
-
* and runtime registration calls.
|
|
138
|
-
*
|
|
139
|
-
* extend String {
|
|
140
|
-
* capitalize() { return this[0].toUpperCase() + this.slice(1) }
|
|
141
|
-
* }
|
|
142
|
-
*
|
|
143
|
-
* becomes:
|
|
144
|
-
*
|
|
145
|
-
* const __ext_String = {
|
|
146
|
-
* capitalize: function() { return this[0].toUpperCase() + this.slice(1) }
|
|
147
|
-
* }
|
|
148
|
-
* if (__tjs?.registerExtension) {
|
|
149
|
-
* __tjs.registerExtension('String', 'capitalize', __ext_String.capitalize)
|
|
150
|
-
* }
|
|
151
|
-
*/
|
|
152
|
-
export interface PolyVariant {
|
|
153
|
-
/** Index (1-based) for renaming */
|
|
154
|
-
index: number;
|
|
155
|
-
/** Start position in source */
|
|
156
|
-
start: number;
|
|
157
|
-
/** End position in source (after closing brace) */
|
|
158
|
-
end: number;
|
|
159
|
-
/** The full function source text */
|
|
160
|
-
text: string;
|
|
161
|
-
/** Whether it was exported */
|
|
162
|
-
exported: boolean;
|
|
163
|
-
/** Whether it was async */
|
|
164
|
-
isAsync: boolean;
|
|
165
|
-
/** Parsed parameter info: [name, defaultValue][] */
|
|
166
|
-
params: {
|
|
167
|
-
name: string;
|
|
168
|
-
defaultValue: string;
|
|
169
|
-
required: boolean;
|
|
170
|
-
}[];
|
|
171
|
-
}
|
|
172
|
-
/**
|
|
173
|
-
* Infer a type-check expression from a parameter's default value string.
|
|
174
|
-
* Returns a condition that checks if an argument matches this param's type.
|
|
175
|
-
*/
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* TJS Parser — Orchestration Layer
|
|
3
|
-
*
|
|
4
|
-
* This module contains the main entry points: preprocess() and parse().
|
|
5
|
-
* Transform functions are in parser-transforms.ts, param processing in parser-params.ts.
|
|
6
|
-
*/
|
|
7
|
-
import type { Program, FunctionDeclaration } from 'acorn';
|
|
8
|
-
export type { ParseOptions, WasmBlock, TestBlock, PreprocessOptions, TjsModes, } from './parser-types';
|
|
9
|
-
import type { ParseOptions, WasmBlock, TestBlock, PreprocessOptions, TjsModes } from './parser-types';
|
|
10
|
-
export { transformExtensionCalls } from './parser-transforms';
|
|
11
|
-
export declare function preprocess(source: string, options?: PreprocessOptions): {
|
|
12
|
-
source: string;
|
|
13
|
-
returnType?: string;
|
|
14
|
-
returnSafety?: 'safe' | 'unsafe';
|
|
15
|
-
moduleSafety?: 'none' | 'inputs' | 'all';
|
|
16
|
-
tjsModes: TjsModes;
|
|
17
|
-
originalSource: string;
|
|
18
|
-
requiredParams: Set<string>;
|
|
19
|
-
unsafeFunctions: Set<string>;
|
|
20
|
-
safeFunctions: Set<string>;
|
|
21
|
-
wasmBlocks: WasmBlock[];
|
|
22
|
-
tests: TestBlock[];
|
|
23
|
-
testErrors: string[];
|
|
24
|
-
polymorphicNames: Set<string>;
|
|
25
|
-
extensions: Map<string, Set<string>>;
|
|
26
|
-
};
|
|
27
|
-
/**
|
|
28
|
-
* Parse source code into an Acorn AST
|
|
29
|
-
*/
|
|
30
|
-
export declare function parse(source: string, options?: ParseOptions): {
|
|
31
|
-
ast: Program;
|
|
32
|
-
returnType?: string;
|
|
33
|
-
returnSafety?: 'safe' | 'unsafe';
|
|
34
|
-
moduleSafety?: 'none' | 'inputs' | 'all';
|
|
35
|
-
originalSource: string;
|
|
36
|
-
requiredParams: Set<string>;
|
|
37
|
-
unsafeFunctions: Set<string>;
|
|
38
|
-
safeFunctions: Set<string>;
|
|
39
|
-
wasmBlocks: WasmBlock[];
|
|
40
|
-
tests: TestBlock[];
|
|
41
|
-
testErrors: string[];
|
|
42
|
-
};
|
|
43
|
-
/**
|
|
44
|
-
* Validate that the source contains exactly one function declaration
|
|
45
|
-
*/
|
|
46
|
-
export declare function validateSingleFunction(ast: Program, filename?: string): FunctionDeclaration;
|
|
47
|
-
/**
|
|
48
|
-
* Extract TDoc comment from before a function
|
|
49
|
-
*
|
|
50
|
-
* TJS doc comments use /\*# ... \*\/ syntax and preserve full markdown content.
|
|
51
|
-
* Legacy JSDoc (/\*\* ... \*\/) is supported as a fallback.
|
|
52
|
-
*/
|
|
53
|
-
export declare function extractTDoc(source: string, func: FunctionDeclaration): {
|
|
54
|
-
description?: string;
|
|
55
|
-
params: Record<string, string>;
|
|
56
|
-
};
|
|
@@ -1,454 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* TJS Runtime
|
|
3
|
-
*
|
|
4
|
-
* Monadic type checking at runtime:
|
|
5
|
-
* - Functions validate inputs against __tjs metadata
|
|
6
|
-
* - If any input is an error, pass it through (no work)
|
|
7
|
-
* - Type mismatches return error objects
|
|
8
|
-
* - Errors propagate through call chains
|
|
9
|
-
*
|
|
10
|
-
* This runtime is attached to globalThis.__tjs and shared across modules.
|
|
11
|
-
*/
|
|
12
|
-
import { validate } from 'tosijs-schema';
|
|
13
|
-
import { Type, isRuntimeType, Union, Generic, Enum, FunctionPredicate, Nullable, Optional, TArray, TString, TNumber, TBoolean, TInteger, TPositiveInt, TNonEmptyString, TEmail, TUrl, TUuid, Timestamp, LegalDate, TPair, TRecord, isValidUrl, isValidTimestamp, isValidLegalDate } from '../types/Type';
|
|
14
|
-
export { Type, isRuntimeType, Union, Generic, Enum, FunctionPredicate, Nullable, Optional, TArray, TString, TNumber, TBoolean, TInteger, TPositiveInt, TNonEmptyString, TEmail, TUrl, TUuid, Timestamp, LegalDate, TPair, TRecord, isValidUrl, isValidTimestamp, isValidLegalDate, };
|
|
15
|
-
export declare const TJS_VERSION: string;
|
|
16
|
-
/**
|
|
17
|
-
* Well-known symbol for custom equality.
|
|
18
|
-
*
|
|
19
|
-
* Any object can implement `[tjsEquals](other)` to control how `==` / `Is()`
|
|
20
|
-
* compares it. Useful for Proxies that should delegate equality to their target.
|
|
21
|
-
*
|
|
22
|
-
* Priority: tjsEquals symbol → .Equals method → structural comparison
|
|
23
|
-
*/
|
|
24
|
-
export declare const tjsEquals: unique symbol;
|
|
25
|
-
/**
|
|
26
|
-
* Compare two version strings
|
|
27
|
-
* Returns: -1 if a < b, 0 if equal, 1 if a > b
|
|
28
|
-
*/
|
|
29
|
-
export declare function compareVersions(a: string, b: string): -1 | 0 | 1;
|
|
30
|
-
/**
|
|
31
|
-
* Check if two versions are compatible (same major version)
|
|
32
|
-
*/
|
|
33
|
-
export declare function versionsCompatible(a: string, b: string): boolean;
|
|
34
|
-
/**
|
|
35
|
-
* MonadicError - Internal error type for monadic error propagation
|
|
36
|
-
*
|
|
37
|
-
* This extends Error so:
|
|
38
|
-
* 1. It's a real Error with proper stack traces
|
|
39
|
-
* 2. User code can't accidentally process it as data (unlike { $error: true })
|
|
40
|
-
* 3. It flows through function calls via instanceof checks
|
|
41
|
-
*
|
|
42
|
-
* NOT exported to user code - they just see Error instances.
|
|
43
|
-
*/
|
|
44
|
-
export declare class MonadicError extends Error {
|
|
45
|
-
/** Path where the error occurred, e.g., "src/file.ts:42:greet.name" */
|
|
46
|
-
readonly path: string;
|
|
47
|
-
/** Expected type */
|
|
48
|
-
readonly expected?: string;
|
|
49
|
-
/** Actual type received */
|
|
50
|
-
readonly actual?: string;
|
|
51
|
-
/** TJS call stack (only in debug mode) - shows source locations */
|
|
52
|
-
readonly callStack?: string[];
|
|
53
|
-
constructor(message: string, path: string, expected?: string, actual?: string, callStack?: string[]);
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Create a type error for inline validation
|
|
57
|
-
*
|
|
58
|
-
* Called ONLY when a type check fails - no overhead on happy path.
|
|
59
|
-
* Returns a MonadicError that propagates through the call chain.
|
|
60
|
-
* In debug mode, captures the TJS call stack with source locations.
|
|
61
|
-
*
|
|
62
|
-
* @param path - Location of the error, e.g., "src/file.ts:42:greet.name"
|
|
63
|
-
* @param expected - Expected type, e.g., "string"
|
|
64
|
-
* @param value - The actual value that failed the check
|
|
65
|
-
*/
|
|
66
|
-
export declare function typeError(path: string, expected: string, value: unknown): MonadicError;
|
|
67
|
-
/**
|
|
68
|
-
* Check if a value is a MonadicError (for internal use)
|
|
69
|
-
*/
|
|
70
|
-
export declare function isMonadicError(value: unknown): value is MonadicError;
|
|
71
|
-
/**
|
|
72
|
-
* Error marker - identifies TJS error objects
|
|
73
|
-
* @deprecated Use MonadicError instead. This interface is kept for backward compatibility.
|
|
74
|
-
*/
|
|
75
|
-
export interface TJSError {
|
|
76
|
-
$error: true;
|
|
77
|
-
message: string;
|
|
78
|
-
/** Failure location - e.g., "greet.name" */
|
|
79
|
-
path?: string;
|
|
80
|
-
/** Call stack in debug mode - e.g., ["main", "processUser", "greet.name"] */
|
|
81
|
-
stack?: string[];
|
|
82
|
-
expected?: string;
|
|
83
|
-
actual?: string;
|
|
84
|
-
cause?: Error | TJSError;
|
|
85
|
-
/** Source location for error reporting */
|
|
86
|
-
loc?: {
|
|
87
|
-
start: number;
|
|
88
|
-
end: number;
|
|
89
|
-
};
|
|
90
|
-
/** Multiple errors (when composing parameter errors) */
|
|
91
|
-
errors?: TJSError[];
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* Safety levels for runtime validation
|
|
95
|
-
* - 'none': No validation unless explicitly forced with (?) or -?
|
|
96
|
-
* - 'inputs': Validate inputs only (default) - outputs only with explicit -> or -?
|
|
97
|
-
* - 'all': Validate both inputs and outputs unless explicitly skipped with (!) or -!
|
|
98
|
-
*/
|
|
99
|
-
export type SafetyLevel = 'none' | 'inputs' | 'all';
|
|
100
|
-
/**
|
|
101
|
-
* Runtime configuration
|
|
102
|
-
*/
|
|
103
|
-
export interface TJSConfig {
|
|
104
|
-
/** Enable debug mode - captures call stacks in errors */
|
|
105
|
-
debug?: boolean;
|
|
106
|
-
/** Safety level for validation (default: 'inputs') */
|
|
107
|
-
safety?: SafetyLevel;
|
|
108
|
-
/** Require explicit return types (error if -> not specified) */
|
|
109
|
-
requireReturnTypes?: boolean;
|
|
110
|
-
/** Maximum call stack size to prevent memory issues (default: 100) */
|
|
111
|
-
maxStackSize?: number;
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Enter unsafe mode - disables validation for all wrapped function calls
|
|
115
|
-
* Can be nested (uses depth counter)
|
|
116
|
-
*/
|
|
117
|
-
export declare function enterUnsafe(): void;
|
|
118
|
-
/**
|
|
119
|
-
* Exit unsafe mode - re-enables validation when depth returns to 0
|
|
120
|
-
*/
|
|
121
|
-
export declare function exitUnsafe(): void;
|
|
122
|
-
/**
|
|
123
|
-
* Check if currently in unsafe mode
|
|
124
|
-
*/
|
|
125
|
-
export declare function isUnsafeMode(): boolean;
|
|
126
|
-
/**
|
|
127
|
-
* Configure TJS runtime
|
|
128
|
-
*/
|
|
129
|
-
export declare function configure(options: TJSConfig): void;
|
|
130
|
-
/**
|
|
131
|
-
* Get current configuration
|
|
132
|
-
*/
|
|
133
|
-
export declare function getConfig(): TJSConfig;
|
|
134
|
-
/**
|
|
135
|
-
* Push a function onto the call stack (debug mode only)
|
|
136
|
-
* Respects maxStackSize to prevent unbounded memory growth
|
|
137
|
-
*/
|
|
138
|
-
export declare function pushStack(name: string): void;
|
|
139
|
-
/**
|
|
140
|
-
* Pop a function from the call stack (debug mode only)
|
|
141
|
-
*/
|
|
142
|
-
export declare function popStack(): void;
|
|
143
|
-
/**
|
|
144
|
-
* Get current call stack snapshot
|
|
145
|
-
*/
|
|
146
|
-
export declare function getStack(): string[];
|
|
147
|
-
/**
|
|
148
|
-
* Reset runtime state to defaults
|
|
149
|
-
*
|
|
150
|
-
* Resets: config, callStack, unsafeDepth
|
|
151
|
-
* Use this in test teardown to prevent state leaking between tests.
|
|
152
|
-
*/
|
|
153
|
-
export declare function resetRuntime(): void;
|
|
154
|
-
/**
|
|
155
|
-
* Structural equality - the == that works
|
|
156
|
-
*
|
|
157
|
-
* Rules:
|
|
158
|
-
* 1. If left has [tjsEquals], call left[tjsEquals](right)
|
|
159
|
-
* 2. If right has [tjsEquals], call right[tjsEquals](left)
|
|
160
|
-
* 3. If left has .Equals, call left.Equals(right)
|
|
161
|
-
* 4. If right has .Equals, call right.Equals(left)
|
|
162
|
-
* 5. Arrays/objects: recursive structural comparison
|
|
163
|
-
* 6. Primitives: strict equality (no coercion)
|
|
164
|
-
*
|
|
165
|
-
* Usage: `a Is b` transforms to `Is(a, b)`
|
|
166
|
-
*/
|
|
167
|
-
export declare function Is(a: unknown, b: unknown): boolean;
|
|
168
|
-
/**
|
|
169
|
-
* Structural inequality - the != that works
|
|
170
|
-
*
|
|
171
|
-
* Usage: `a IsNot b` transforms to `IsNot(a, b)`
|
|
172
|
-
*/
|
|
173
|
-
export declare function IsNot(a: unknown, b: unknown): boolean;
|
|
174
|
-
/**
|
|
175
|
-
* Check if a value is a TJS error
|
|
176
|
-
*/
|
|
177
|
-
export declare function isError(value: unknown): value is TJSError;
|
|
178
|
-
/**
|
|
179
|
-
* Create a TJS error
|
|
180
|
-
* In debug mode, captures the current call stack
|
|
181
|
-
*/
|
|
182
|
-
export declare function error(message: string, details?: Partial<Omit<TJSError, '$error' | 'message'>>): TJSError;
|
|
183
|
-
/**
|
|
184
|
-
* Compose multiple errors into a single error
|
|
185
|
-
* Used when multiple parameters have errors
|
|
186
|
-
*/
|
|
187
|
-
export declare function composeErrors(errors: TJSError[], funcName?: string): TJSError;
|
|
188
|
-
/**
|
|
189
|
-
* Get the type of a value
|
|
190
|
-
*
|
|
191
|
-
* Enhanced typeof that handles:
|
|
192
|
-
* - null (returns 'null' not 'object')
|
|
193
|
-
* - undefined (returns 'undefined')
|
|
194
|
-
* - arrays (returns 'array' not 'object')
|
|
195
|
-
* - native/platform types (returns constructor name for objects)
|
|
196
|
-
*
|
|
197
|
-
* For objects, returns the constructor name which enables pragmatic
|
|
198
|
-
* native type checking (e.g., 'HTMLElement', 'Buffer', 'Event')
|
|
199
|
-
*/
|
|
200
|
-
export declare function typeOf(value: unknown): string;
|
|
201
|
-
/**
|
|
202
|
-
* Check if a value is an instance of a native/platform type by constructor name
|
|
203
|
-
*
|
|
204
|
-
* This enables pragmatic native type checking without shipping type definitions:
|
|
205
|
-
* - isNativeType(el, 'HTMLElement') - DOM element check
|
|
206
|
-
* - isNativeType(buf, 'Buffer') - Node.js Buffer check
|
|
207
|
-
* - isNativeType(evt, 'Event') - DOM Event check
|
|
208
|
-
* - isNativeType(map, 'Map') - Map instance check
|
|
209
|
-
*
|
|
210
|
-
* @param value - The value to check
|
|
211
|
-
* @param typeName - The constructor name to match (e.g., 'HTMLElement', 'Buffer')
|
|
212
|
-
* @returns true if value's constructor.name matches or is in prototype chain
|
|
213
|
-
*/
|
|
214
|
-
export declare function isNativeType(value: unknown, typeName: string): boolean;
|
|
215
|
-
/**
|
|
216
|
-
* Check if a value matches an expected type
|
|
217
|
-
*
|
|
218
|
-
* @param value - The value to check
|
|
219
|
-
* @param expected - Either a string type name ('string', 'number', etc.)
|
|
220
|
-
* or a RuntimeType instance with .check() method
|
|
221
|
-
* @param path - Optional path for error messages
|
|
222
|
-
*/
|
|
223
|
-
export declare function checkType(value: unknown, expected: string | {
|
|
224
|
-
check: (v: unknown) => boolean;
|
|
225
|
-
description: string;
|
|
226
|
-
}, path?: string): TJSError | null;
|
|
227
|
-
/** Type specifier - either a string name or a RuntimeType */
|
|
228
|
-
type TypeSpec = string | {
|
|
229
|
-
check: (v: unknown) => boolean;
|
|
230
|
-
description: string;
|
|
231
|
-
};
|
|
232
|
-
/** Parameter metadata with optional location */
|
|
233
|
-
interface ParamMeta {
|
|
234
|
-
type: TypeSpec;
|
|
235
|
-
required: boolean;
|
|
236
|
-
default?: unknown;
|
|
237
|
-
loc?: {
|
|
238
|
-
start: number;
|
|
239
|
-
end: number;
|
|
240
|
-
};
|
|
241
|
-
}
|
|
242
|
-
/**
|
|
243
|
-
* Validate function arguments against __tjs metadata
|
|
244
|
-
* Returns first error found, or null if all valid
|
|
245
|
-
*/
|
|
246
|
-
export declare function validateArgs(args: Record<string, unknown>, meta: {
|
|
247
|
-
params: Record<string, ParamMeta>;
|
|
248
|
-
}, funcName?: string): TJSError | null;
|
|
249
|
-
/**
|
|
250
|
-
* Function metadata with safety flags
|
|
251
|
-
*/
|
|
252
|
-
export interface FunctionMeta {
|
|
253
|
-
params: Record<string, any>;
|
|
254
|
-
returns?: {
|
|
255
|
-
type: any;
|
|
256
|
-
safe?: boolean;
|
|
257
|
-
defaults?: Record<string, unknown>;
|
|
258
|
-
};
|
|
259
|
-
/** Function marked with (!) - never validate inputs */
|
|
260
|
-
unsafe?: boolean;
|
|
261
|
-
/** Function marked with (?) - always validate inputs */
|
|
262
|
-
safe?: boolean;
|
|
263
|
-
/** Return type marked with -! - never validate output */
|
|
264
|
-
unsafeReturn?: boolean;
|
|
265
|
-
/** Return type marked with -? - always validate output */
|
|
266
|
-
safeReturn?: boolean;
|
|
267
|
-
/** Explicit function name for stack tracking (used when fn.name is empty) */
|
|
268
|
-
name?: string;
|
|
269
|
-
/** Polymorphic dispatcher — skip wrapping, dispatch handles validation */
|
|
270
|
-
polymorphic?: boolean;
|
|
271
|
-
}
|
|
272
|
-
/**
|
|
273
|
-
* Wrap a function with monadic type checking
|
|
274
|
-
*
|
|
275
|
-
* @param fn - The original function
|
|
276
|
-
* @param meta - The __tjs metadata
|
|
277
|
-
* @returns Wrapped function that validates inputs and propagates errors
|
|
278
|
-
*/
|
|
279
|
-
export declare function wrap<T extends (...args: any[]) => any>(fn: T, meta: FunctionMeta): T;
|
|
280
|
-
/**
|
|
281
|
-
* Wrap a class to make it callable without `new`
|
|
282
|
-
*
|
|
283
|
-
* In TJS, classes can be instantiated without the `new` keyword:
|
|
284
|
-
* const obj = MyClass(args) // equivalent to new MyClass(args)
|
|
285
|
-
*
|
|
286
|
-
* This eliminates a common source of errors where developers forget `new`.
|
|
287
|
-
* Using explicit `new` still works but should be flagged by the linter.
|
|
288
|
-
*/
|
|
289
|
-
export declare function wrapClass<T extends new (...args: any[]) => any>(cls: T): T & ((...args: ConstructorParameters<T>) => InstanceType<T>);
|
|
290
|
-
/**
|
|
291
|
-
* Create an isolated TJS runtime instance
|
|
292
|
-
*
|
|
293
|
-
* Each call returns a fresh runtime with its own:
|
|
294
|
-
* - config (debug, safety, etc.)
|
|
295
|
-
* - callStack
|
|
296
|
-
* - unsafeDepth
|
|
297
|
-
*
|
|
298
|
-
* The new instance inherits the current global config at creation time,
|
|
299
|
-
* but subsequent changes are isolated.
|
|
300
|
-
*
|
|
301
|
-
* Use this to prevent state leaking between transpiled modules.
|
|
302
|
-
*/
|
|
303
|
-
export declare function createRuntime(): {
|
|
304
|
-
version: string;
|
|
305
|
-
MonadicError: typeof MonadicError;
|
|
306
|
-
typeError: (path: string, expected: string, value: unknown) => MonadicError;
|
|
307
|
-
isMonadicError: typeof isMonadicError;
|
|
308
|
-
isError: typeof isError;
|
|
309
|
-
error: (message: string, details?: Partial<Omit<TJSError, "$error" | "message">>) => TJSError;
|
|
310
|
-
composeErrors: typeof composeErrors;
|
|
311
|
-
typeOf: typeof typeOf;
|
|
312
|
-
isNativeType: typeof isNativeType;
|
|
313
|
-
checkType: typeof checkType;
|
|
314
|
-
validateArgs: typeof validateArgs;
|
|
315
|
-
wrap: typeof wrap;
|
|
316
|
-
wrapClass: typeof wrapClass;
|
|
317
|
-
compareVersions: typeof compareVersions;
|
|
318
|
-
versionsCompatible: typeof versionsCompatible;
|
|
319
|
-
configure: (options: TJSConfig) => void;
|
|
320
|
-
getConfig: () => TJSConfig;
|
|
321
|
-
pushStack: (name: string) => void;
|
|
322
|
-
popStack: () => void;
|
|
323
|
-
getStack: () => string[];
|
|
324
|
-
resetRuntime: () => void;
|
|
325
|
-
enterUnsafe: () => void;
|
|
326
|
-
exitUnsafe: () => void;
|
|
327
|
-
isUnsafeMode: () => boolean;
|
|
328
|
-
validate: typeof validate;
|
|
329
|
-
infer: (value: any) => import("tosijs-schema").Base<any>;
|
|
330
|
-
Type: typeof Type;
|
|
331
|
-
isRuntimeType: typeof isRuntimeType;
|
|
332
|
-
Union: typeof Union;
|
|
333
|
-
Generic: typeof Generic;
|
|
334
|
-
Enum: typeof Enum;
|
|
335
|
-
FunctionPredicate: typeof FunctionPredicate;
|
|
336
|
-
Nullable: typeof Nullable;
|
|
337
|
-
Optional: typeof Optional;
|
|
338
|
-
TArray: typeof TArray;
|
|
339
|
-
TString: import("..").RuntimeType<string>;
|
|
340
|
-
TNumber: import("..").RuntimeType<number>;
|
|
341
|
-
TBoolean: import("..").RuntimeType<boolean>;
|
|
342
|
-
TInteger: import("..").RuntimeType<number>;
|
|
343
|
-
TPositiveInt: import("..").RuntimeType<number>;
|
|
344
|
-
TNonEmptyString: import("..").RuntimeType<string>;
|
|
345
|
-
TEmail: import("..").RuntimeType<string>;
|
|
346
|
-
TUrl: import("..").RuntimeType<string>;
|
|
347
|
-
TUuid: import("..").RuntimeType<string>;
|
|
348
|
-
TPair: import("..").GenericType<string[]>;
|
|
349
|
-
TRecord: import("..").GenericType<string[]>;
|
|
350
|
-
Is: typeof Is;
|
|
351
|
-
IsNot: typeof IsNot;
|
|
352
|
-
tjsEquals: symbol;
|
|
353
|
-
registerExtension: (typeName: string, methodName: string, fn: (...args: any[]) => any) => void;
|
|
354
|
-
resolveExtension: (value: unknown, methodName: string) => ((...args: any[]) => any) | undefined;
|
|
355
|
-
};
|
|
356
|
-
/** Type for runtime instances */
|
|
357
|
-
export type TJSRuntime = ReturnType<typeof createRuntime>;
|
|
358
|
-
/**
|
|
359
|
-
* TJS Runtime object - attached to globalThis.__tjs
|
|
360
|
-
*
|
|
361
|
-
* NOTE: This is a shared global instance. For isolated execution,
|
|
362
|
-
* use createRuntime() instead.
|
|
363
|
-
*/
|
|
364
|
-
export declare const runtime: {
|
|
365
|
-
version: string;
|
|
366
|
-
MonadicError: typeof MonadicError;
|
|
367
|
-
typeError: typeof typeError;
|
|
368
|
-
isMonadicError: typeof isMonadicError;
|
|
369
|
-
isError: typeof isError;
|
|
370
|
-
error: typeof error;
|
|
371
|
-
composeErrors: typeof composeErrors;
|
|
372
|
-
typeOf: typeof typeOf;
|
|
373
|
-
isNativeType: typeof isNativeType;
|
|
374
|
-
checkType: typeof checkType;
|
|
375
|
-
validateArgs: typeof validateArgs;
|
|
376
|
-
wrap: typeof wrap;
|
|
377
|
-
wrapClass: typeof wrapClass;
|
|
378
|
-
compareVersions: typeof compareVersions;
|
|
379
|
-
versionsCompatible: typeof versionsCompatible;
|
|
380
|
-
configure: typeof configure;
|
|
381
|
-
getConfig: typeof getConfig;
|
|
382
|
-
pushStack: typeof pushStack;
|
|
383
|
-
popStack: typeof popStack;
|
|
384
|
-
getStack: typeof getStack;
|
|
385
|
-
resetRuntime: typeof resetRuntime;
|
|
386
|
-
enterUnsafe: typeof enterUnsafe;
|
|
387
|
-
exitUnsafe: typeof exitUnsafe;
|
|
388
|
-
isUnsafeMode: typeof isUnsafeMode;
|
|
389
|
-
createRuntime: typeof createRuntime;
|
|
390
|
-
validate: typeof validate;
|
|
391
|
-
infer: (value: any) => import("tosijs-schema").Base<any>;
|
|
392
|
-
Type: typeof Type;
|
|
393
|
-
isRuntimeType: typeof isRuntimeType;
|
|
394
|
-
Union: typeof Union;
|
|
395
|
-
Generic: typeof Generic;
|
|
396
|
-
Enum: typeof Enum;
|
|
397
|
-
FunctionPredicate: typeof FunctionPredicate;
|
|
398
|
-
Nullable: typeof Nullable;
|
|
399
|
-
Optional: typeof Optional;
|
|
400
|
-
TArray: typeof TArray;
|
|
401
|
-
TString: import("..").RuntimeType<string>;
|
|
402
|
-
TNumber: import("..").RuntimeType<number>;
|
|
403
|
-
TBoolean: import("..").RuntimeType<boolean>;
|
|
404
|
-
TInteger: import("..").RuntimeType<number>;
|
|
405
|
-
TPositiveInt: import("..").RuntimeType<number>;
|
|
406
|
-
TNonEmptyString: import("..").RuntimeType<string>;
|
|
407
|
-
TEmail: import("..").RuntimeType<string>;
|
|
408
|
-
TUrl: import("..").RuntimeType<string>;
|
|
409
|
-
TUuid: import("..").RuntimeType<string>;
|
|
410
|
-
Timestamp: import("..").RuntimeType<string>;
|
|
411
|
-
LegalDate: import("..").RuntimeType<string>;
|
|
412
|
-
TPair: import("..").GenericType<string[]>;
|
|
413
|
-
TRecord: import("..").GenericType<string[]>;
|
|
414
|
-
Is: typeof Is;
|
|
415
|
-
IsNot: typeof IsNot;
|
|
416
|
-
};
|
|
417
|
-
/**
|
|
418
|
-
* Install runtime globally (idempotent, version-checked)
|
|
419
|
-
*
|
|
420
|
-
* Version handling:
|
|
421
|
-
* - Same version: reuse existing (no warning)
|
|
422
|
-
* - Compatible (same major): reuse existing, log info
|
|
423
|
-
* - Incompatible (different major): warn, use newer version
|
|
424
|
-
*/
|
|
425
|
-
export declare function installRuntime(): typeof runtime;
|
|
426
|
-
/**
|
|
427
|
-
* Generate runtime wrapper code for emitted JS
|
|
428
|
-
* Skips wrapping for unsafe functions (marked with !)
|
|
429
|
-
*/
|
|
430
|
-
export declare function emitRuntimeWrapper(funcName: string): string;
|
|
431
|
-
/**
|
|
432
|
-
* Generate class wrapper code for emitted JS
|
|
433
|
-
* Makes classes callable without `new` keyword
|
|
434
|
-
* Emits standalone JS - no runtime dependency
|
|
435
|
-
*/
|
|
436
|
-
export declare function emitClassWrapper(className: string): string;
|
|
437
|
-
/**
|
|
438
|
-
* Questions/Notes:
|
|
439
|
-
*
|
|
440
|
-
* Q1: Should wrap() be automatic or opt-in?
|
|
441
|
-
* Current: Must be explicitly wrapped
|
|
442
|
-
* Could: Auto-wrap all functions with __tjs metadata
|
|
443
|
-
*
|
|
444
|
-
* Q2: Performance overhead of validation?
|
|
445
|
-
* Need benchmarks comparing wrapped vs unwrapped
|
|
446
|
-
* Could have 'production' mode that skips validation
|
|
447
|
-
*
|
|
448
|
-
* Q3: Async function handling?
|
|
449
|
-
* wrap() should detect async and handle Promise returns
|
|
450
|
-
*
|
|
451
|
-
* Q4: Deep object validation?
|
|
452
|
-
* Currently only checks top-level type
|
|
453
|
-
* Could recursively validate object shapes
|
|
454
|
-
*/
|