tjs-lang 0.6.13 → 0.6.15
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 +40 -17
- package/demo/docs.json +1 -1
- package/dist/index.js +119 -116
- package/dist/index.js.map +8 -8
- package/dist/src/lang/parser-transforms.d.ts +15 -0
- package/dist/src/lang/runtime.d.ts +4 -2
- package/dist/src/types/Type.d.ts +41 -0
- package/dist/src/types/index.d.ts +1 -1
- package/dist/tjs-full.js +119 -116
- package/dist/tjs-full.js.map +8 -8
- package/dist/tjs-vm.js +37 -37
- package/dist/tjs-vm.js.map +4 -4
- package/docs/function-predicate-design.md +180 -0
- package/package.json +1 -1
- package/src/cli/tjs.ts +1 -1
- package/src/lang/emitters/dts.test.ts +58 -0
- package/src/lang/emitters/dts.ts +84 -0
- package/src/lang/emitters/from-ts.ts +217 -6
- package/src/lang/function-predicate.test.ts +188 -0
- package/src/lang/parser-transforms.ts +103 -0
- package/src/lang/parser.ts +2 -0
- package/src/lang/runtime.ts +4 -0
- package/src/lang/typescript-syntax.test.ts +154 -5
- package/src/types/Type.ts +148 -0
- package/src/types/index.ts +5 -0
|
@@ -91,6 +91,21 @@ export declare function transformEqualityToStructural(source: string): string;
|
|
|
91
91
|
* When predicate + example: auto-generate type guard from example
|
|
92
92
|
*/
|
|
93
93
|
export declare function transformTypeDeclarations(source: string): string;
|
|
94
|
+
/**
|
|
95
|
+
* Transform FunctionPredicate declarations
|
|
96
|
+
*
|
|
97
|
+
* Block form:
|
|
98
|
+
* FunctionPredicate Callback {
|
|
99
|
+
* params: { x: 0, y: '' }
|
|
100
|
+
* returns: false
|
|
101
|
+
* }
|
|
102
|
+
* → const Callback = FunctionPredicate('Callback', { params: { x: 0, y: '' }, returns: false })
|
|
103
|
+
*
|
|
104
|
+
* Function form:
|
|
105
|
+
* FunctionPredicate Handler(existingFn, 'description')
|
|
106
|
+
* → const Handler = FunctionPredicate('description', existingFn)
|
|
107
|
+
*/
|
|
108
|
+
export declare function transformFunctionPredicateDeclarations(source: string): string;
|
|
94
109
|
/**
|
|
95
110
|
* Transform Generic block declarations
|
|
96
111
|
*
|
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
* This runtime is attached to globalThis.__tjs and shared across modules.
|
|
11
11
|
*/
|
|
12
12
|
import { validate } from 'tosijs-schema';
|
|
13
|
-
import { Type, isRuntimeType, Union, Generic, Enum, 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, Nullable, Optional, TArray, TString, TNumber, TBoolean, TInteger, TPositiveInt, TNonEmptyString, TEmail, TUrl, TUuid, Timestamp, LegalDate, TPair, TRecord, isValidUrl, isValidTimestamp, isValidLegalDate, };
|
|
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
15
|
export declare const TJS_VERSION: string;
|
|
16
16
|
/**
|
|
17
17
|
* Well-known symbol for custom equality.
|
|
@@ -332,6 +332,7 @@ export declare function createRuntime(): {
|
|
|
332
332
|
Union: typeof Union;
|
|
333
333
|
Generic: typeof Generic;
|
|
334
334
|
Enum: typeof Enum;
|
|
335
|
+
FunctionPredicate: typeof FunctionPredicate;
|
|
335
336
|
Nullable: typeof Nullable;
|
|
336
337
|
Optional: typeof Optional;
|
|
337
338
|
TArray: typeof TArray;
|
|
@@ -393,6 +394,7 @@ export declare const runtime: {
|
|
|
393
394
|
Union: typeof Union;
|
|
394
395
|
Generic: typeof Generic;
|
|
395
396
|
Enum: typeof Enum;
|
|
397
|
+
FunctionPredicate: typeof FunctionPredicate;
|
|
396
398
|
Nullable: typeof Nullable;
|
|
397
399
|
Optional: typeof Optional;
|
|
398
400
|
TArray: typeof TArray;
|
package/dist/src/types/Type.d.ts
CHANGED
|
@@ -172,4 +172,45 @@ export interface EnumType<T extends Record<string, string | number> = Record<str
|
|
|
172
172
|
* Color.members.Red // 'red'
|
|
173
173
|
*/
|
|
174
174
|
export declare function Enum<T extends Record<string, string | number>>(description: string, members: T): EnumType<T>;
|
|
175
|
+
/** Return contract levels in order of strictness */
|
|
176
|
+
export type ReturnContract = 'assertReturns' | 'returns' | 'checkedReturns';
|
|
177
|
+
/** Specification for a FunctionPredicate */
|
|
178
|
+
export interface FunctionPredicateSpec {
|
|
179
|
+
/** Parameter types as example values */
|
|
180
|
+
params?: Record<string, any>;
|
|
181
|
+
/** Return type as example value */
|
|
182
|
+
returns?: any;
|
|
183
|
+
/** Return contract level */
|
|
184
|
+
returnContract?: ReturnContract;
|
|
185
|
+
}
|
|
186
|
+
/** A runtime type that validates function signatures */
|
|
187
|
+
export interface FunctionPredicateType extends RuntimeType<Function> {
|
|
188
|
+
/** Parameter specification */
|
|
189
|
+
readonly params: Record<string, any>;
|
|
190
|
+
/** Return type specification */
|
|
191
|
+
readonly returns?: any;
|
|
192
|
+
/** Return contract level */
|
|
193
|
+
readonly returnContract: ReturnContract;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Create a runtime type for function signatures.
|
|
197
|
+
*
|
|
198
|
+
* Forms:
|
|
199
|
+
* FunctionPredicate(name, spec) - from a specification object
|
|
200
|
+
* FunctionPredicate(name, fn) - from an existing typed function
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* const Callback = FunctionPredicate('Callback', {
|
|
204
|
+
* params: { x: 0, y: 0 },
|
|
205
|
+
* returns: 0,
|
|
206
|
+
* })
|
|
207
|
+
* Callback.check((a, b) => a + b) // true (typeof === 'function')
|
|
208
|
+
* Callback.check(42) // false
|
|
209
|
+
*
|
|
210
|
+
* @example
|
|
211
|
+
* function add(a: 0, b: 0) -> 0 { return a + b }
|
|
212
|
+
* const Adder = FunctionPredicate('Adder', add)
|
|
213
|
+
* // Extracts params/returns from add.__tjs
|
|
214
|
+
*/
|
|
215
|
+
export declare function FunctionPredicate(name: string, specOrFn: FunctionPredicateSpec | Function): FunctionPredicateType;
|
|
175
216
|
export {};
|
|
@@ -3,6 +3,6 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Runtime types with descriptions and validation.
|
|
5
5
|
*/
|
|
6
|
-
export { Type, isRuntimeType, type RuntimeType, TString, TNumber, TBoolean, TInteger, TPositiveInt, TNonEmptyString, TEmail, TUrl, TUuid, Timestamp as TimestampType, LegalDate as LegalDateType, isValidUrl, isValidTimestamp, isValidLegalDate, Nullable, Optional, Union, TArray, Enum, type EnumType, Generic, TPair, TRecord, type GenericType, type TypeParam, } from './Type';
|
|
6
|
+
export { Type, isRuntimeType, type RuntimeType, TString, TNumber, TBoolean, TInteger, TPositiveInt, TNonEmptyString, TEmail, TUrl, TUuid, Timestamp as TimestampType, LegalDate as LegalDateType, isValidUrl, isValidTimestamp, isValidLegalDate, Nullable, Optional, Union, TArray, Enum, type EnumType, Generic, TPair, TRecord, type GenericType, type TypeParam, FunctionPredicate, type FunctionPredicateType, type FunctionPredicateSpec, type ReturnContract, } from './Type';
|
|
7
7
|
export { Timestamp, type TimestampString } from './Timestamp';
|
|
8
8
|
export { LegalDate, type LegalDateString } from './LegalDate';
|