@tsonic/frontend 0.0.1
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/package.json +53 -0
- package/src/dependency-graph.ts +18 -0
- package/src/dotnet-metadata.ts +121 -0
- package/src/graph/builder.ts +81 -0
- package/src/graph/circular.ts +58 -0
- package/src/graph/extraction/exports.ts +55 -0
- package/src/graph/extraction/imports.ts +81 -0
- package/src/graph/extraction/index.ts +7 -0
- package/src/graph/extraction/orchestrator.ts +99 -0
- package/src/graph/extraction.ts +10 -0
- package/src/graph/helpers.ts +51 -0
- package/src/graph/index.ts +17 -0
- package/src/graph/types.ts +13 -0
- package/src/index.ts +80 -0
- package/src/ir/binding-resolution.test.ts +585 -0
- package/src/ir/builder/exports.ts +78 -0
- package/src/ir/builder/helpers.ts +27 -0
- package/src/ir/builder/imports.ts +153 -0
- package/src/ir/builder/index.ts +10 -0
- package/src/ir/builder/orchestrator.ts +178 -0
- package/src/ir/builder/statements.ts +55 -0
- package/src/ir/builder/types.ts +8 -0
- package/src/ir/builder/validation.ts +129 -0
- package/src/ir/builder.test.ts +581 -0
- package/src/ir/builder.ts +14 -0
- package/src/ir/converters/expressions/access.ts +99 -0
- package/src/ir/converters/expressions/calls.ts +137 -0
- package/src/ir/converters/expressions/collections.ts +84 -0
- package/src/ir/converters/expressions/functions.ts +62 -0
- package/src/ir/converters/expressions/helpers.ts +264 -0
- package/src/ir/converters/expressions/index.ts +43 -0
- package/src/ir/converters/expressions/literals.ts +22 -0
- package/src/ir/converters/expressions/operators.ts +147 -0
- package/src/ir/converters/expressions/other.ts +60 -0
- package/src/ir/converters/statements/control/blocks.ts +22 -0
- package/src/ir/converters/statements/control/conditionals.ts +67 -0
- package/src/ir/converters/statements/control/exceptions.ts +43 -0
- package/src/ir/converters/statements/control/index.ts +17 -0
- package/src/ir/converters/statements/control/loops.ts +99 -0
- package/src/ir/converters/statements/control.ts +17 -0
- package/src/ir/converters/statements/declarations/classes/constructors.ts +120 -0
- package/src/ir/converters/statements/declarations/classes/index.ts +12 -0
- package/src/ir/converters/statements/declarations/classes/methods.ts +61 -0
- package/src/ir/converters/statements/declarations/classes/orchestrator.ts +166 -0
- package/src/ir/converters/statements/declarations/classes/override-detection.ts +116 -0
- package/src/ir/converters/statements/declarations/classes/properties.ts +63 -0
- package/src/ir/converters/statements/declarations/classes.ts +6 -0
- package/src/ir/converters/statements/declarations/enums.ts +29 -0
- package/src/ir/converters/statements/declarations/functions.ts +39 -0
- package/src/ir/converters/statements/declarations/index.ts +14 -0
- package/src/ir/converters/statements/declarations/interfaces.ts +131 -0
- package/src/ir/converters/statements/declarations/registry.ts +45 -0
- package/src/ir/converters/statements/declarations/type-aliases.ts +25 -0
- package/src/ir/converters/statements/declarations/variables.ts +60 -0
- package/src/ir/converters/statements/declarations.ts +16 -0
- package/src/ir/converters/statements/helpers.ts +174 -0
- package/src/ir/converters/statements/index.ts +40 -0
- package/src/ir/expression-converter.ts +207 -0
- package/src/ir/generic-validator.ts +100 -0
- package/src/ir/hierarchical-bindings-e2e.test.ts +163 -0
- package/src/ir/index.ts +6 -0
- package/src/ir/statement-converter.ts +128 -0
- package/src/ir/type-converter/arrays.ts +20 -0
- package/src/ir/type-converter/converter.ts +10 -0
- package/src/ir/type-converter/functions.ts +22 -0
- package/src/ir/type-converter/index.ts +11 -0
- package/src/ir/type-converter/inference.ts +122 -0
- package/src/ir/type-converter/literals.ts +40 -0
- package/src/ir/type-converter/objects.ts +107 -0
- package/src/ir/type-converter/orchestrator.ts +85 -0
- package/src/ir/type-converter/patterns.ts +73 -0
- package/src/ir/type-converter/primitives.ts +57 -0
- package/src/ir/type-converter/references.ts +64 -0
- package/src/ir/type-converter/unions-intersections.ts +34 -0
- package/src/ir/type-converter.ts +13 -0
- package/src/ir/types/expressions.ts +215 -0
- package/src/ir/types/guards.ts +39 -0
- package/src/ir/types/helpers.ts +135 -0
- package/src/ir/types/index.ts +108 -0
- package/src/ir/types/ir-types.ts +96 -0
- package/src/ir/types/module.ts +57 -0
- package/src/ir/types/statements.ts +238 -0
- package/src/ir/types.ts +97 -0
- package/src/metadata/bindings-loader.test.ts +144 -0
- package/src/metadata/bindings-loader.ts +357 -0
- package/src/metadata/index.ts +15 -0
- package/src/metadata/library-loader.ts +153 -0
- package/src/metadata/loader.test.ts +156 -0
- package/src/metadata/loader.ts +382 -0
- package/src/program/bindings.test.ts +512 -0
- package/src/program/bindings.ts +253 -0
- package/src/program/config.ts +30 -0
- package/src/program/creation.ts +249 -0
- package/src/program/dependency-graph.ts +245 -0
- package/src/program/diagnostics.ts +103 -0
- package/src/program/index.ts +19 -0
- package/src/program/metadata.ts +68 -0
- package/src/program/queries.ts +18 -0
- package/src/program/types.ts +38 -0
- package/src/program.ts +13 -0
- package/src/resolver/dotnet-import-resolver.ts +226 -0
- package/src/resolver/import-resolution.ts +177 -0
- package/src/resolver/index.ts +18 -0
- package/src/resolver/namespace.test.ts +86 -0
- package/src/resolver/namespace.ts +42 -0
- package/src/resolver/naming.ts +38 -0
- package/src/resolver/path-resolution.ts +22 -0
- package/src/resolver/types.ts +15 -0
- package/src/resolver.test.ts +155 -0
- package/src/resolver.ts +14 -0
- package/src/symbol-table/builder.ts +114 -0
- package/src/symbol-table/creation.ts +42 -0
- package/src/symbol-table/helpers.ts +18 -0
- package/src/symbol-table/index.ts +13 -0
- package/src/symbol-table/queries.ts +42 -0
- package/src/symbol-table/types.ts +28 -0
- package/src/symbol-table.ts +14 -0
- package/src/types/bindings.ts +172 -0
- package/src/types/diagnostic.test.ts +164 -0
- package/src/types/diagnostic.ts +153 -0
- package/src/types/explicit-views.test.ts +113 -0
- package/src/types/explicit-views.ts +218 -0
- package/src/types/metadata.ts +229 -0
- package/src/types/module.ts +99 -0
- package/src/types/nested-types.test.ts +194 -0
- package/src/types/nested-types.ts +215 -0
- package/src/types/parameter-modifiers.ts +173 -0
- package/src/types/ref-parameters.test.ts +192 -0
- package/src/types/ref-parameters.ts +268 -0
- package/src/types/result.test.ts +157 -0
- package/src/types/result.ts +48 -0
- package/src/types/support-types.test.ts +81 -0
- package/src/types/support-types.ts +288 -0
- package/src/types/test-harness.ts +180 -0
- package/src/validation/exports.ts +98 -0
- package/src/validation/features.ts +89 -0
- package/src/validation/generics.ts +40 -0
- package/src/validation/helpers.ts +31 -0
- package/src/validation/imports.ts +97 -0
- package/src/validation/index.ts +11 -0
- package/src/validation/orchestrator.ts +51 -0
- package/src/validation/static-safety.ts +267 -0
- package/src/validator.test.ts +468 -0
- package/src/validator.ts +15 -0
- package/tsconfig.json +13 -0
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ref/Out Parameter Handling - Generate C# ref/out/in keywords from TSByRef wrapper.
|
|
3
|
+
*
|
|
4
|
+
* C# supports pass-by-reference parameters (ref, out, in) which don't have direct
|
|
5
|
+
* JavaScript equivalents. TypeScript code uses TSByRef<T> wrapper:
|
|
6
|
+
*
|
|
7
|
+
* TypeScript: const result = { value: 0 }; TryParse("42", result);
|
|
8
|
+
* C#: int result; TryParse("42", out result);
|
|
9
|
+
*
|
|
10
|
+
* @see spec/ref-out-parameters.md for complete documentation
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import * as ts from "typescript";
|
|
14
|
+
import type { ParameterMetadata } from "./metadata.js";
|
|
15
|
+
import { isTSByRef, getTSByRefWrappedType } from "./support-types.js";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Parameter modifier for C# emission.
|
|
19
|
+
*/
|
|
20
|
+
export type ParameterModifier = "ref" | "out" | "in" | "none";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Information about a ref/out/in parameter.
|
|
24
|
+
*/
|
|
25
|
+
export type RefParameterInfo = {
|
|
26
|
+
readonly isRef: boolean;
|
|
27
|
+
readonly isOut: boolean;
|
|
28
|
+
readonly isIn: boolean;
|
|
29
|
+
readonly modifier: ParameterModifier;
|
|
30
|
+
readonly wrappedType: ts.Type;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Get ref/out/in information from parameter metadata.
|
|
35
|
+
*
|
|
36
|
+
* @param paramMetadata - Parameter metadata from .metadata.json
|
|
37
|
+
* @returns Parameter modifier to use in C# emission
|
|
38
|
+
*/
|
|
39
|
+
export const getParameterModifier = (
|
|
40
|
+
paramMetadata: ParameterMetadata
|
|
41
|
+
): ParameterModifier => {
|
|
42
|
+
if (paramMetadata.isOut) {
|
|
43
|
+
return "out";
|
|
44
|
+
}
|
|
45
|
+
if (paramMetadata.isRef) {
|
|
46
|
+
return "ref";
|
|
47
|
+
}
|
|
48
|
+
if (paramMetadata.isIn) {
|
|
49
|
+
return "in";
|
|
50
|
+
}
|
|
51
|
+
return "none";
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Check if parameter requires TSByRef wrapper based on metadata.
|
|
56
|
+
*
|
|
57
|
+
* @param paramMetadata - Parameter metadata from .metadata.json
|
|
58
|
+
* @returns True if parameter should use TSByRef<T> in TypeScript
|
|
59
|
+
*/
|
|
60
|
+
export const requiresTSByRef = (paramMetadata: ParameterMetadata): boolean => {
|
|
61
|
+
return (
|
|
62
|
+
paramMetadata.isRef || paramMetadata.isOut || (paramMetadata.isIn ?? false)
|
|
63
|
+
);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Get ref parameter info from TypeScript type and metadata.
|
|
68
|
+
*
|
|
69
|
+
* @param paramType - TypeScript parameter type
|
|
70
|
+
* @param paramMetadata - Parameter metadata from .metadata.json
|
|
71
|
+
* @param checker - TypeScript type checker
|
|
72
|
+
* @returns Ref parameter info if applicable, undefined otherwise
|
|
73
|
+
*/
|
|
74
|
+
export const getRefParameterInfo = (
|
|
75
|
+
paramType: ts.Type,
|
|
76
|
+
paramMetadata: ParameterMetadata,
|
|
77
|
+
checker: ts.TypeChecker
|
|
78
|
+
): RefParameterInfo | undefined => {
|
|
79
|
+
// Check if type is TSByRef<T>
|
|
80
|
+
if (!isTSByRef(paramType, checker)) {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Extract wrapped type
|
|
85
|
+
const wrappedType = getTSByRefWrappedType(paramType, checker);
|
|
86
|
+
if (!wrappedType) {
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
isRef: paramMetadata.isRef,
|
|
92
|
+
isOut: paramMetadata.isOut,
|
|
93
|
+
isIn: paramMetadata.isIn ?? false,
|
|
94
|
+
modifier: getParameterModifier(paramMetadata),
|
|
95
|
+
wrappedType,
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Extract variable name from TSByRef argument expression.
|
|
101
|
+
*
|
|
102
|
+
* TypeScript: TryParse("42", result) // where result = { value: 0 }
|
|
103
|
+
* C#: TryParse("42", out result)
|
|
104
|
+
*
|
|
105
|
+
* @param argumentNode - Argument AST node
|
|
106
|
+
* @returns Variable identifier name, or undefined if not extractable
|
|
107
|
+
*/
|
|
108
|
+
export const extractRefArgumentVariable = (
|
|
109
|
+
argumentNode: ts.Expression
|
|
110
|
+
): string | undefined => {
|
|
111
|
+
// Handle simple identifier
|
|
112
|
+
if (ts.isIdentifier(argumentNode)) {
|
|
113
|
+
return argumentNode.text;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Handle property access (unlikely for ref params, but possible)
|
|
117
|
+
if (ts.isPropertyAccessExpression(argumentNode)) {
|
|
118
|
+
return argumentNode.name.text;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Cannot extract variable name from complex expressions
|
|
122
|
+
return undefined;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Check if argument expression is a TSByRef wrapper object literal.
|
|
127
|
+
*
|
|
128
|
+
* Detects: { value: 0 } or { value: initialValue }
|
|
129
|
+
*
|
|
130
|
+
* @param argumentNode - Argument AST node
|
|
131
|
+
* @returns True if argument is object literal with 'value' property
|
|
132
|
+
*/
|
|
133
|
+
export const isTSByRefObjectLiteral = (
|
|
134
|
+
argumentNode: ts.Expression
|
|
135
|
+
): boolean => {
|
|
136
|
+
if (!ts.isObjectLiteralExpression(argumentNode)) {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Check if object has exactly one property named 'value'
|
|
141
|
+
const properties = argumentNode.properties;
|
|
142
|
+
if (properties.length !== 1) {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const prop = properties[0];
|
|
147
|
+
if (!prop || !ts.isPropertyAssignment(prop)) {
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (!ts.isIdentifier(prop.name)) {
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return prop.name.text === "value";
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Generate C# parameter declaration with modifier.
|
|
160
|
+
*
|
|
161
|
+
* @param modifier - Parameter modifier (ref/out/in/none)
|
|
162
|
+
* @param typeName - C# type name
|
|
163
|
+
* @param paramName - Parameter name
|
|
164
|
+
* @returns C# parameter declaration
|
|
165
|
+
*/
|
|
166
|
+
export const generateCSharpParameter = (
|
|
167
|
+
modifier: ParameterModifier,
|
|
168
|
+
typeName: string,
|
|
169
|
+
paramName: string
|
|
170
|
+
): string => {
|
|
171
|
+
if (modifier === "none") {
|
|
172
|
+
return `${typeName} ${paramName}`;
|
|
173
|
+
}
|
|
174
|
+
return `${modifier} ${typeName} ${paramName}`;
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Generate C# argument with modifier for method call.
|
|
179
|
+
*
|
|
180
|
+
* @param modifier - Parameter modifier (ref/out/in/none)
|
|
181
|
+
* @param argumentExpression - C# argument expression
|
|
182
|
+
* @returns C# argument with modifier
|
|
183
|
+
*/
|
|
184
|
+
export const generateCSharpArgument = (
|
|
185
|
+
modifier: ParameterModifier,
|
|
186
|
+
argumentExpression: string
|
|
187
|
+
): string => {
|
|
188
|
+
if (modifier === "none") {
|
|
189
|
+
return argumentExpression;
|
|
190
|
+
}
|
|
191
|
+
return `${modifier} ${argumentExpression}`;
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Check if variable declaration is needed for out parameter.
|
|
196
|
+
*
|
|
197
|
+
* For out parameters in TypeScript:
|
|
198
|
+
* const result = { value: 0 }; // Declaration
|
|
199
|
+
* TryParse("42", result); // Usage
|
|
200
|
+
*
|
|
201
|
+
* For out parameters in C#:
|
|
202
|
+
* TryParse("42", out int result); // Inline declaration
|
|
203
|
+
*
|
|
204
|
+
* @param argumentNode - Argument AST node
|
|
205
|
+
* @returns True if variable should be declared before the call
|
|
206
|
+
*/
|
|
207
|
+
export const needsOutVariableDeclaration = (
|
|
208
|
+
argumentNode: ts.Expression
|
|
209
|
+
): boolean => {
|
|
210
|
+
// If argument is already a simple identifier, it's already declared
|
|
211
|
+
if (ts.isIdentifier(argumentNode)) {
|
|
212
|
+
return false;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// If argument is an object literal { value: ... }, we need to declare the variable
|
|
216
|
+
if (isTSByRefObjectLiteral(argumentNode)) {
|
|
217
|
+
return true;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return false;
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Extract initial value from TSByRef object literal.
|
|
225
|
+
*
|
|
226
|
+
* { value: 42 } → 42
|
|
227
|
+
* { value: getDefault() } → getDefault()
|
|
228
|
+
*
|
|
229
|
+
* @param argumentNode - Argument AST node (must be object literal)
|
|
230
|
+
* @returns Initial value expression, or undefined if no initializer
|
|
231
|
+
*/
|
|
232
|
+
export const extractTSByRefInitialValue = (
|
|
233
|
+
argumentNode: ts.Expression
|
|
234
|
+
): ts.Expression | undefined => {
|
|
235
|
+
if (!ts.isObjectLiteralExpression(argumentNode)) {
|
|
236
|
+
return undefined;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const properties = argumentNode.properties;
|
|
240
|
+
if (properties.length !== 1) {
|
|
241
|
+
return undefined;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const prop = properties[0];
|
|
245
|
+
if (!prop || !ts.isPropertyAssignment(prop)) {
|
|
246
|
+
return undefined;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
return prop.initializer;
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Determine if ref/out parameter should use inline declaration in C#.
|
|
254
|
+
*
|
|
255
|
+
* C# 7.0+ supports inline out var:
|
|
256
|
+
* if (int.TryParse("42", out var result)) { ... }
|
|
257
|
+
*
|
|
258
|
+
* We use this when the TypeScript code declares the wrapper inline.
|
|
259
|
+
*
|
|
260
|
+
* @param argumentNode - Argument AST node
|
|
261
|
+
* @returns True if should use inline declaration
|
|
262
|
+
*/
|
|
263
|
+
export const shouldUseInlineDeclaration = (
|
|
264
|
+
argumentNode: ts.Expression
|
|
265
|
+
): boolean => {
|
|
266
|
+
// Use inline declaration if argument is an object literal
|
|
267
|
+
return isTSByRefObjectLiteral(argumentNode);
|
|
268
|
+
};
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for Result type
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, it } from "mocha";
|
|
6
|
+
import { expect } from "chai";
|
|
7
|
+
import {
|
|
8
|
+
ok,
|
|
9
|
+
error,
|
|
10
|
+
map,
|
|
11
|
+
flatMap,
|
|
12
|
+
mapError,
|
|
13
|
+
unwrapOr,
|
|
14
|
+
unwrapOrElse,
|
|
15
|
+
isOk,
|
|
16
|
+
isError,
|
|
17
|
+
} from "./result.js";
|
|
18
|
+
|
|
19
|
+
describe("Result", () => {
|
|
20
|
+
describe("ok and error constructors", () => {
|
|
21
|
+
it("should create ok result", () => {
|
|
22
|
+
const result = ok<number, string>(42);
|
|
23
|
+
expect(result.ok).to.equal(true);
|
|
24
|
+
if (result.ok) {
|
|
25
|
+
expect(result.value).to.equal(42);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("should create error result", () => {
|
|
30
|
+
const result = error<number, string>("Something went wrong");
|
|
31
|
+
expect(result.ok).to.equal(false);
|
|
32
|
+
if (!result.ok) {
|
|
33
|
+
expect(result.error).to.equal("Something went wrong");
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe("map", () => {
|
|
39
|
+
it("should map ok value", () => {
|
|
40
|
+
const result = ok<number, string>(5);
|
|
41
|
+
const mapped = map(result, (x) => x * 2);
|
|
42
|
+
|
|
43
|
+
expect(mapped.ok).to.equal(true);
|
|
44
|
+
if (mapped.ok) {
|
|
45
|
+
expect(mapped.value).to.equal(10);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("should pass through error", () => {
|
|
50
|
+
const result = error<number, string>("Error");
|
|
51
|
+
const mapped = map(result, (x) => x * 2);
|
|
52
|
+
|
|
53
|
+
expect(mapped.ok).to.equal(false);
|
|
54
|
+
if (!mapped.ok) {
|
|
55
|
+
expect(mapped.error).to.equal("Error");
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe("flatMap", () => {
|
|
61
|
+
it("should flatMap ok value", () => {
|
|
62
|
+
const result = ok<number, string>(5);
|
|
63
|
+
const mapped = flatMap(result, (x) => ok(x.toString()));
|
|
64
|
+
|
|
65
|
+
expect(mapped.ok).to.equal(true);
|
|
66
|
+
if (mapped.ok) {
|
|
67
|
+
expect(mapped.value).to.equal("5");
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("should handle flatMap returning error", () => {
|
|
72
|
+
const result = ok<number, string>(5);
|
|
73
|
+
const mapped = flatMap(result, (x) =>
|
|
74
|
+
x > 10 ? ok(x) : error("Too small")
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
expect(mapped.ok).to.equal(false);
|
|
78
|
+
if (!mapped.ok) {
|
|
79
|
+
expect(mapped.error).to.equal("Too small");
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("should pass through original error", () => {
|
|
84
|
+
const result = error<number, string>("Original error");
|
|
85
|
+
const mapped = flatMap(result, (x) => ok(x * 2));
|
|
86
|
+
|
|
87
|
+
expect(mapped.ok).to.equal(false);
|
|
88
|
+
if (!mapped.ok) {
|
|
89
|
+
expect(mapped.error).to.equal("Original error");
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
describe("mapError", () => {
|
|
95
|
+
it("should map error value", () => {
|
|
96
|
+
const result = error<number, string>("Error");
|
|
97
|
+
const mapped = mapError(result, (e) => e.toUpperCase());
|
|
98
|
+
|
|
99
|
+
expect(mapped.ok).to.equal(false);
|
|
100
|
+
if (!mapped.ok) {
|
|
101
|
+
expect(mapped.error).to.equal("ERROR");
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("should pass through ok value", () => {
|
|
106
|
+
const result = ok<number, string>(42);
|
|
107
|
+
const mapped = mapError(result, (e) => e.toUpperCase());
|
|
108
|
+
|
|
109
|
+
expect(mapped.ok).to.equal(true);
|
|
110
|
+
if (mapped.ok) {
|
|
111
|
+
expect(mapped.value).to.equal(42);
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
describe("unwrapOr", () => {
|
|
117
|
+
it("should return value for ok", () => {
|
|
118
|
+
const result = ok<number, string>(42);
|
|
119
|
+
expect(unwrapOr(result, 0)).to.equal(42);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("should return default for error", () => {
|
|
123
|
+
const result = error<number, string>("Error");
|
|
124
|
+
expect(unwrapOr(result, 0)).to.equal(0);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
describe("unwrapOrElse", () => {
|
|
129
|
+
it("should return value for ok", () => {
|
|
130
|
+
const result = ok<number, string>(42);
|
|
131
|
+
expect(unwrapOrElse(result, (_e) => 0)).to.equal(42);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("should call function for error", () => {
|
|
135
|
+
const result = error<number, string>("Error");
|
|
136
|
+
expect(unwrapOrElse(result, () => 0)).to.equal(0);
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
describe("isOk and isError", () => {
|
|
141
|
+
it("should identify ok results", () => {
|
|
142
|
+
const okResult = ok<number, string>(42);
|
|
143
|
+
const errorResult = error<number, string>("Error");
|
|
144
|
+
|
|
145
|
+
expect(isOk(okResult)).to.equal(true);
|
|
146
|
+
expect(isOk(errorResult)).to.equal(false);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("should identify error results", () => {
|
|
150
|
+
const okResult = ok<number, string>(42);
|
|
151
|
+
const errorResult = error<number, string>("Error");
|
|
152
|
+
|
|
153
|
+
expect(isError(okResult)).to.equal(false);
|
|
154
|
+
expect(isError(errorResult)).to.equal(true);
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Result type for functional error handling
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export type Result<T, E> =
|
|
6
|
+
| { readonly ok: true; readonly value: T }
|
|
7
|
+
| { readonly ok: false; readonly error: E };
|
|
8
|
+
|
|
9
|
+
export const ok = <T, E>(value: T): Result<T, E> => ({
|
|
10
|
+
ok: true,
|
|
11
|
+
value,
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
export const error = <T, E>(error: E): Result<T, E> => ({
|
|
15
|
+
ok: false,
|
|
16
|
+
error,
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
export const map = <T, U, E>(
|
|
20
|
+
result: Result<T, E>,
|
|
21
|
+
fn: (value: T) => U
|
|
22
|
+
): Result<U, E> => (result.ok ? ok(fn(result.value)) : result);
|
|
23
|
+
|
|
24
|
+
export const flatMap = <T, U, E>(
|
|
25
|
+
result: Result<T, E>,
|
|
26
|
+
fn: (value: T) => Result<U, E>
|
|
27
|
+
): Result<U, E> => (result.ok ? fn(result.value) : result);
|
|
28
|
+
|
|
29
|
+
export const mapError = <T, E, F>(
|
|
30
|
+
result: Result<T, E>,
|
|
31
|
+
fn: (error: E) => F
|
|
32
|
+
): Result<T, F> => (result.ok ? result : error(fn(result.error)));
|
|
33
|
+
|
|
34
|
+
export const unwrapOr = <T, E>(result: Result<T, E>, defaultValue: T): T =>
|
|
35
|
+
result.ok ? result.value : defaultValue;
|
|
36
|
+
|
|
37
|
+
export const unwrapOrElse = <T, E>(
|
|
38
|
+
result: Result<T, E>,
|
|
39
|
+
fn: (error: E) => T
|
|
40
|
+
): T => (result.ok ? result.value : fn(result.error));
|
|
41
|
+
|
|
42
|
+
export const isOk = <T, E>(
|
|
43
|
+
result: Result<T, E>
|
|
44
|
+
): result is { readonly ok: true; readonly value: T } => result.ok;
|
|
45
|
+
|
|
46
|
+
export const isError = <T, E>(
|
|
47
|
+
result: Result<T, E>
|
|
48
|
+
): result is { readonly ok: false; readonly error: E } => !result.ok;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for support types recognition using real TypeScript types.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, it, before, after } from "mocha";
|
|
6
|
+
import { strict as assert } from "assert";
|
|
7
|
+
import { checkUnsupportedSupportType } from "./support-types.js";
|
|
8
|
+
import {
|
|
9
|
+
createTestHarness,
|
|
10
|
+
getSupportTypes,
|
|
11
|
+
type TestHarness,
|
|
12
|
+
} from "./test-harness.js";
|
|
13
|
+
|
|
14
|
+
describe("Support Types", () => {
|
|
15
|
+
let harness: TestHarness;
|
|
16
|
+
let types: ReturnType<typeof getSupportTypes>;
|
|
17
|
+
|
|
18
|
+
before(() => {
|
|
19
|
+
// Create test harness with real TypeScript program
|
|
20
|
+
harness = createTestHarness();
|
|
21
|
+
types = getSupportTypes(harness);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
after(() => {
|
|
25
|
+
// Clean up temporary files
|
|
26
|
+
harness.cleanup();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe("checkUnsupportedSupportType", () => {
|
|
30
|
+
it("should detect TSUnsafePointer as unsupported", () => {
|
|
31
|
+
const error = checkUnsupportedSupportType(
|
|
32
|
+
types.unsafePointer,
|
|
33
|
+
harness.checker
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
assert.ok(error);
|
|
37
|
+
assert.match(error!, /unsafe pointer/i);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("should detect TSFixed as unsupported", () => {
|
|
41
|
+
const error = checkUnsupportedSupportType(types.fixed, harness.checker);
|
|
42
|
+
|
|
43
|
+
assert.ok(error);
|
|
44
|
+
assert.match(error!, /fixed-size buffer/i);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("should detect TSStackAlloc as unsupported", () => {
|
|
48
|
+
const error = checkUnsupportedSupportType(
|
|
49
|
+
types.stackAlloc,
|
|
50
|
+
harness.checker
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
assert.ok(error);
|
|
54
|
+
assert.match(error!, /stackalloc/i);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("should allow TSByRef (supported)", () => {
|
|
58
|
+
const error = checkUnsupportedSupportType(types.byRef, harness.checker);
|
|
59
|
+
|
|
60
|
+
assert.equal(error, undefined);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("should allow TSNullable (supported)", () => {
|
|
64
|
+
const error = checkUnsupportedSupportType(
|
|
65
|
+
types.nullable,
|
|
66
|
+
harness.checker
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
assert.equal(error, undefined);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("should allow TSDelegate (supported)", () => {
|
|
73
|
+
const error = checkUnsupportedSupportType(
|
|
74
|
+
types.delegate,
|
|
75
|
+
harness.checker
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
assert.equal(error, undefined);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
});
|