forked from swiftwasm/JavaScriptKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype-smith.mjs
More file actions
118 lines (105 loc) · 3.61 KB
/
Copy pathtype-smith.mjs
File metadata and controls
118 lines (105 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// @ts-check
/**
* @typedef {import('../types.mjs').BridgeType} BridgeType
*/
import { Random } from './random.mjs';
/** @type {BridgeType[]} */
const LEAF_TYPES = [
{ kind: 'int' },
{ kind: 'float' },
{ kind: 'double' },
{ kind: 'string' },
{ kind: 'bool' },
];
/**
* Generates random BridgeType objects with depth-limited recursion.
*/
export class TypeSmith {
/** @type {Random} */
#rng;
/** @type {number} */
#maxDepth;
/**
* @param {Random} rng
* @param {{ maxDepth?: number }} [options]
*/
constructor(rng, options = {}) {
this.#rng = rng;
this.#maxDepth = options.maxDepth ?? 3;
}
/**
* Generate a random type (depth-limited recursive).
* @param {number} [depth]
* @param {{ insideNullable?: boolean }} [constraints]
* @returns {BridgeType}
*/
randomType(depth = 0, constraints = {}) {
/** @type {BridgeType[]} */
const candidates = [...LEAF_TYPES, { kind: 'jsObject' }];
if (depth < this.#maxDepth) {
// Add wrapper type placeholders — we'll generate the inner type lazily
/** @type {string[]} */
const wrapperKinds = ['array', 'dictionary'];
// Prevent nested optionals (not supported by BridgeJS)
if (!constraints.insideNullable) wrapperKinds.push('nullable');
const chosen = this.#rng.pick([...candidates.map(() => 'leaf'), ...wrapperKinds]);
if (chosen === 'nullable') {
return { kind: 'nullable', wrapped: this.randomType(depth + 1, { insideNullable: true }) };
}
if (chosen === 'array') {
return { kind: 'array', element: this.randomType(depth + 1) };
}
if (chosen === 'dictionary') {
return { kind: 'dictionary', value: this.randomType(depth + 1) };
}
}
return this.#rng.pick(candidates);
}
/**
* Generate a random type suitable for function parameters.
* No void; importedClass only if availableClasses is non-empty.
* @param {number} [depth]
* @param {string[]} [availableClasses]
* @param {{ insideNullable?: boolean }} [constraints]
* @returns {BridgeType}
*/
randomParamType(depth = 0, availableClasses = [], constraints = {}) {
/** @type {BridgeType[]} */
const candidates = [...LEAF_TYPES, { kind: 'jsObject' }];
if (availableClasses.length > 0) {
candidates.push({ kind: 'importedClass', name: this.#rng.pick(availableClasses) });
}
if (depth < this.#maxDepth) {
/** @type {string[]} */
const wrapperKinds = ['array', 'dictionary'];
// Prevent nested optionals (not supported by BridgeJS)
if (!constraints.insideNullable) wrapperKinds.push('nullable');
const allChoices = [...candidates.map(() => 'leaf'), ...wrapperKinds];
const chosen = this.#rng.pick(allChoices);
if (chosen === 'nullable') {
return { kind: 'nullable', wrapped: this.randomParamType(depth + 1, availableClasses, { insideNullable: true }) };
}
if (chosen === 'array') {
return { kind: 'array', element: this.randomParamType(depth + 1, availableClasses) };
}
if (chosen === 'dictionary') {
return { kind: 'dictionary', value: this.randomParamType(depth + 1, availableClasses) };
}
}
return this.#rng.pick(candidates);
}
/**
* Generate a random type suitable for function return types.
* Includes void as a possibility.
* @param {number} [depth]
* @param {string[]} [availableClasses]
* @returns {BridgeType}
*/
randomReturnType(depth = 0, availableClasses = []) {
// 20% chance of void
if (this.#rng.next() < 0.2) {
return { kind: 'void' };
}
return this.randomParamType(depth, availableClasses);
}
}