Skip to content

Commit 9295cd7

Browse files
authored
💥 Get rid of old namespace syntax for ObjectArbitrary (dubzzz#631) (dubzzz#755)
1 parent 0a2a52d commit 9295cd7

File tree

3 files changed

+75
-75
lines changed

3 files changed

+75
-75
lines changed

.eslintrc.cjs

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@ module.exports = {
44
'plugin:@typescript-eslint/eslint-recommended',
55
'plugin:@typescript-eslint/recommended',
66
'prettier',
7-
'prettier/@typescript-eslint'
7+
'prettier/@typescript-eslint',
88
],
99
rules: {
1010
'@typescript-eslint/explicit-function-return-type': 'off',
1111
'@typescript-eslint/interface-name-prefix': 'off',
1212
'@typescript-eslint/no-empty-function': 'warn',
1313
'@typescript-eslint/no-empty-interface': 'off',
1414
'@typescript-eslint/no-explicit-any': 'off',
15-
'@typescript-eslint/no-namespace': 'warn',
1615
'@typescript-eslint/no-this-alias': 'warn',
1716
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
18-
'@typescript-eslint/no-use-before-define': 'warn'
19-
}
17+
'@typescript-eslint/no-use-before-define': 'warn',
18+
},
2019
};

src/check/arbitrary/ObjectArbitrary.ts

+71-70
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,61 @@ import { string, unicodeString } from './StringArbitrary';
1515
import { tuple } from './TupleArbitrary';
1616
import { bigInt } from './BigIntArbitrary';
1717

18-
export class ObjectConstraints {
18+
/**
19+
* Constraints for `fc.anything` and `fc.object`
20+
*/
21+
export type ObjectConstraints = {
22+
/** Maximal depth allowed */
23+
maxDepth?: number;
24+
/** Maximal number of keys */
25+
maxKeys?: number;
26+
/**
27+
* Arbitrary for keys
28+
*
29+
* Default for `key` is: `fc.string()`
30+
*/
31+
key?: Arbitrary<string>;
32+
/**
33+
* Arbitrary for values
34+
*
35+
* Default for `values` are:
36+
* - `fc.boolean()`,
37+
* - `fc.integer()`,
38+
* - `fc.double()`,
39+
* - `fc.string()`
40+
* - constants among:
41+
* - `null`,
42+
* - `undefined`,
43+
* - `Number.NaN`,
44+
* - `+0`,
45+
* - `-0`,
46+
* - `Number.EPSILON`,
47+
* - `Number.MIN_VALUE`,
48+
* - `Number.MAX_VALUE`,
49+
* - `Number.MIN_SAFE_INTEGER`,
50+
* - `Number.MAX_SAFE_INTEGER`,
51+
* - `Number.POSITIVE_INFINITY`,
52+
* - `Number.NEGATIVE_INFINITY`
53+
*/
54+
values?: Arbitrary<unknown>[];
55+
/** Also generate boxed versions of values */
56+
withBoxedValues?: boolean;
57+
/** Also generate Set */
58+
withSet?: boolean;
59+
/** Also generate Map */
60+
withMap?: boolean;
61+
/** Also generate string representations of object instances */
62+
withObjectString?: boolean;
63+
/** Also generate object with null prototype */
64+
withNullPrototype?: boolean;
65+
/** Also generate BigInt */
66+
withBigInt?: boolean;
67+
};
68+
69+
/**
70+
* @internal
71+
*/
72+
class QualifiedObjectConstraints {
1973
constructor(
2074
readonly key: Arbitrary<string>,
2175
readonly values: Arbitrary<unknown>[],
@@ -29,7 +83,7 @@ export class ObjectConstraints {
2983
) {}
3084

3185
/**
32-
* Default value of ObjectConstraints.Settings.values field
86+
* Default value of ObjectConstraints.values field
3387
*/
3488
static defaultValues(): Arbitrary<unknown>[] {
3589
return [
@@ -54,7 +108,6 @@ export class ObjectConstraints {
54108
];
55109
}
56110

57-
/** @internal */
58111
private static boxArbitraries(arbs: Arbitrary<unknown>[]): Arbitrary<unknown>[] {
59112
return arbs.map((arb) =>
60113
arb.map((v) => {
@@ -75,19 +128,18 @@ export class ObjectConstraints {
75128
);
76129
}
77130

78-
/** @internal */
79131
private static boxArbitrariesIfNeeded(arbs: Arbitrary<unknown>[], boxEnabled: boolean): Arbitrary<unknown>[] {
80-
return boxEnabled ? ObjectConstraints.boxArbitraries(arbs).concat(arbs) : arbs;
132+
return boxEnabled ? QualifiedObjectConstraints.boxArbitraries(arbs).concat(arbs) : arbs;
81133
}
82134

83-
static from(settings?: ObjectConstraints.Settings): ObjectConstraints {
135+
static from(settings?: ObjectConstraints): QualifiedObjectConstraints {
84136
function getOr<T>(access: () => T | undefined, value: T): T {
85137
return settings != null && access() != null ? access()! : value;
86138
}
87-
return new ObjectConstraints(
139+
return new QualifiedObjectConstraints(
88140
getOr(() => settings!.key, string()),
89-
ObjectConstraints.boxArbitrariesIfNeeded(
90-
getOr(() => settings!.values, ObjectConstraints.defaultValues()),
141+
QualifiedObjectConstraints.boxArbitrariesIfNeeded(
142+
getOr(() => settings!.values, QualifiedObjectConstraints.defaultValues()),
91143
getOr(() => settings!.withBoxedValues, false)
92144
),
93145
getOr(() => settings!.maxDepth, 2),
@@ -101,59 +153,8 @@ export class ObjectConstraints {
101153
}
102154
}
103155

104-
export namespace ObjectConstraints {
105-
/** Constraints to be applied during object generation */
106-
export interface Settings {
107-
/** Maximal depth allowed */
108-
maxDepth?: number;
109-
/** Maximal number of keys */
110-
maxKeys?: number;
111-
/**
112-
* Arbitrary for keys
113-
*
114-
* Default for `key` is: `fc.string()`
115-
*/
116-
key?: Arbitrary<string>;
117-
/**
118-
* Arbitrary for values
119-
*
120-
* Default for `values` are:
121-
* - `fc.boolean()`,
122-
* - `fc.integer()`,
123-
* - `fc.double()`,
124-
* - `fc.string()`
125-
* - constants among:
126-
* - `null`,
127-
* - `undefined`,
128-
* - `Number.NaN`,
129-
* - `+0`,
130-
* - `-0`,
131-
* - `Number.EPSILON`,
132-
* - `Number.MIN_VALUE`,
133-
* - `Number.MAX_VALUE`,
134-
* - `Number.MIN_SAFE_INTEGER`,
135-
* - `Number.MAX_SAFE_INTEGER`,
136-
* - `Number.POSITIVE_INFINITY`,
137-
* - `Number.NEGATIVE_INFINITY`
138-
*/
139-
values?: Arbitrary<unknown>[];
140-
/** Also generate boxed versions of values */
141-
withBoxedValues?: boolean;
142-
/** Also generate Set */
143-
withSet?: boolean;
144-
/** Also generate Map */
145-
withMap?: boolean;
146-
/** Also generate string representations of object instances */
147-
withObjectString?: boolean;
148-
/** Also generate object with null prototype */
149-
withNullPrototype?: boolean;
150-
/** Also generate BigInt */
151-
withBigInt?: boolean;
152-
}
153-
}
154-
155156
/** @internal */
156-
const anythingInternal = (constraints: ObjectConstraints): Arbitrary<unknown> => {
157+
const anythingInternal = (constraints: QualifiedObjectConstraints): Arbitrary<unknown> => {
157158
const arbKeys = constraints.withObjectString
158159
? memo((n) =>
159160
frequency(
@@ -212,7 +213,7 @@ const anythingInternal = (constraints: ObjectConstraints): Arbitrary<unknown> =>
212213
};
213214

214215
/** @internal */
215-
const objectInternal = (constraints: ObjectConstraints): Arbitrary<Record<string, unknown>> => {
216+
const objectInternal = (constraints: QualifiedObjectConstraints): Arbitrary<Record<string, unknown>> => {
216217
return dictionary(constraints.key, anythingInternal(constraints));
217218
};
218219

@@ -248,11 +249,11 @@ function anything(): Arbitrary<unknown>;
248249
* // - [42,42,42]...
249250
* ```
250251
*
251-
* @param settings - Constraints to apply when building instances
252+
* @param constraints - Constraints to apply when building instances
252253
*/
253-
function anything(settings: ObjectConstraints.Settings): Arbitrary<unknown>;
254-
function anything(settings?: ObjectConstraints.Settings): Arbitrary<unknown> {
255-
return anythingInternal(ObjectConstraints.from(settings));
254+
function anything(constraints: ObjectConstraints): Arbitrary<unknown>;
255+
function anything(constraints?: ObjectConstraints): Arbitrary<unknown> {
256+
return anythingInternal(QualifiedObjectConstraints.from(constraints));
256257
}
257258

258259
/**
@@ -272,11 +273,11 @@ function object(): Arbitrary<Record<string, unknown>>;
272273
* @example
273274
* ```{} or {k: [{}, 1, 2]}```
274275
*
275-
* @param settings - Constraints to apply when building instances
276+
* @param constraints - Constraints to apply when building instances
276277
*/
277-
function object(settings: ObjectConstraints.Settings): Arbitrary<Record<string, unknown>>;
278-
function object(settings?: ObjectConstraints.Settings): Arbitrary<Record<string, unknown>> {
279-
return objectInternal(ObjectConstraints.from(settings));
278+
function object(constraints: ObjectConstraints): Arbitrary<Record<string, unknown>>;
279+
function object(constraints?: ObjectConstraints): Arbitrary<Record<string, unknown>> {
280+
return objectInternal(QualifiedObjectConstraints.from(constraints));
280281
}
281282

282283
/** @internal */

test/unit/check/arbitrary/ObjectArbitrary.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ describe('ObjectArbitrary', () => {
138138
assertShrinkedValue(originalValue, shrinkable.value);
139139
})
140140
));
141-
const checkProduce = (settings: ObjectConstraints.Settings, f: (v: any) => boolean) => {
141+
const checkProduce = (settings: ObjectConstraints, f: (v: any) => boolean) => {
142142
let numRuns = 0;
143143
const seed = 0;
144144
const mrng = new Random(prand.xorshift128plus(seed));

0 commit comments

Comments
 (0)