Skip to content

Commit ec46d1c

Browse files
Copilotstreamich
andcommitted
Run formatting fixes and update index.ts to use star export
Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
1 parent 3509dfd commit ec46d1c

File tree

4 files changed

+32
-36
lines changed

4 files changed

+32
-36
lines changed

src/codegen/capacity/__tests__/CapacityEstimatorCodegenContext.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,12 @@ describe('standalone codegen function', () => {
220220
test('generates capacity estimator equivalent to compileCapacityEstimator', () => {
221221
const system = new TypeSystem();
222222
const type = system.t.Array(system.t.str);
223-
223+
224224
// Compare standalone codegen function with the class method
225225
const {codegen} = require('../estimators');
226226
const standaloneEstimator = codegen(type, {});
227227
const classEstimator = type.compileCapacityEstimator({});
228-
228+
229229
const testData = ['hello', 'world', 'test'];
230230
expect(standaloneEstimator(testData)).toBe(classEstimator(testData));
231231
expect(standaloneEstimator(testData)).toBe(maxEncodingCapacity(testData));
@@ -235,13 +235,13 @@ describe('standalone codegen function', () => {
235235
const system = new TypeSystem();
236236
const type = system.t.Object(
237237
system.t.prop('name', system.t.str),
238-
system.t.prop('items', system.t.Array(system.t.num))
238+
system.t.prop('items', system.t.Array(system.t.num)),
239239
);
240-
240+
241241
const {codegen} = require('../estimators');
242242
const standaloneEstimator = codegen(type, {});
243243
const classEstimator = type.compileCapacityEstimator({});
244-
244+
245245
const testData = {name: 'test', items: [1, 2, 3, 4, 5]};
246246
expect(standaloneEstimator(testData)).toBe(classEstimator(testData));
247247
expect(standaloneEstimator(testData)).toBe(maxEncodingCapacity(testData));
@@ -250,10 +250,10 @@ describe('standalone codegen function', () => {
250250
test('works with const types', () => {
251251
const system = new TypeSystem();
252252
const type = system.t.Const('hello world');
253-
253+
254254
const {codegen} = require('../estimators');
255255
const standaloneEstimator = codegen(type, {});
256-
256+
257257
// For const types, the value doesn't matter
258258
expect(standaloneEstimator(null)).toBe(maxEncodingCapacity('hello world'));
259259
});

src/codegen/capacity/estimators.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import {JsExpression} from '@jsonjoy.com/util/lib/codegen/util/JsExpression';
22
import {MaxEncodingOverhead, maxEncodingCapacity} from '@jsonjoy.com/util/lib/json-size';
33
import {CapacityEstimatorCodegenContext} from './CapacityEstimatorCodegenContext';
4-
import type {CapacityEstimatorCodegenContextOptions, CompiledCapacityEstimator} from './CapacityEstimatorCodegenContext';
4+
import type {
5+
CapacityEstimatorCodegenContextOptions,
6+
CompiledCapacityEstimator,
7+
} from './CapacityEstimatorCodegenContext';
58
import type {Type} from '../../type';
69

710
type EstimatorFunction = (ctx: CapacityEstimatorCodegenContext, value: JsExpression, type: Type) => void;
@@ -102,7 +105,12 @@ export const tup = (ctx: CapacityEstimatorCodegenContext, value: JsExpression, t
102105
}
103106
};
104107

105-
export const obj = (ctx: CapacityEstimatorCodegenContext, value: JsExpression, type: Type, estimateCapacityFn: EstimatorFunction): void => {
108+
export const obj = (
109+
ctx: CapacityEstimatorCodegenContext,
110+
value: JsExpression,
111+
type: Type,
112+
estimateCapacityFn: EstimatorFunction,
113+
): void => {
106114
const codegen = ctx.codegen;
107115
const r = codegen.var(value.use());
108116
const objectType = type as any; // ObjectType
@@ -117,7 +125,7 @@ export const obj = (ctx: CapacityEstimatorCodegenContext, value: JsExpression, t
117125
for (const field of fields) {
118126
ctx.inc(maxEncodingCapacity(field.key));
119127
const accessor = normalizeAccessor(field.key);
120-
const isOptional = field.optional || (field.constructor?.name === 'ObjectOptionalFieldType');
128+
const isOptional = field.optional || field.constructor?.name === 'ObjectOptionalFieldType';
121129
const block = () => estimateCapacityFn(ctx, new JsExpression(() => `${r}${accessor}`), field.value);
122130
if (isOptional) {
123131
codegen.if(`${r}${accessor} !== undefined`, block);
@@ -156,7 +164,12 @@ export const ref = (ctx: CapacityEstimatorCodegenContext, value: JsExpression, t
156164
ctx.codegen.js(`size += ${d}(${value.use()});`);
157165
};
158166

159-
export const or = (ctx: CapacityEstimatorCodegenContext, value: JsExpression, type: Type, estimateCapacityFn: EstimatorFunction): void => {
167+
export const or = (
168+
ctx: CapacityEstimatorCodegenContext,
169+
value: JsExpression,
170+
type: Type,
171+
estimateCapacityFn: EstimatorFunction,
172+
): void => {
160173
const codegen = ctx.codegen;
161174
const orType = type as any; // OrType
162175
const discriminator = orType.discriminator();
@@ -179,7 +192,7 @@ export const or = (ctx: CapacityEstimatorCodegenContext, value: JsExpression, ty
179192
*/
180193
export const generate = (ctx: CapacityEstimatorCodegenContext, value: JsExpression, type: Type): void => {
181194
const kind = type.getTypeName();
182-
195+
183196
switch (kind) {
184197
case 'any':
185198
any(ctx, value, type);
@@ -239,4 +252,4 @@ export const codegen = (
239252
// Use the centralized router instead of the abstract method
240253
generate(ctx, value, type);
241254
return ctx.compile();
242-
};
255+
};

src/codegen/capacity/index.ts

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,7 @@
1-
export {
2-
generate,
3-
codegen,
4-
any,
5-
bool,
6-
num,
7-
str,
8-
bin,
9-
const_,
10-
arr,
11-
tup,
12-
obj,
13-
map,
14-
ref,
15-
or,
16-
} from './estimators';
1+
export * from './estimators';
172

18-
export { CapacityEstimatorCodegenContext } from './CapacityEstimatorCodegenContext';
19-
export type {
20-
CapacityEstimatorCodegenContextOptions,
21-
CompiledCapacityEstimator
22-
} from './CapacityEstimatorCodegenContext';
3+
export {CapacityEstimatorCodegenContext} from './CapacityEstimatorCodegenContext';
4+
export type {
5+
CapacityEstimatorCodegenContextOptions,
6+
CompiledCapacityEstimator,
7+
} from './CapacityEstimatorCodegenContext';

src/type/classes/AbstractType.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,6 @@ export abstract class AbstractType<S extends schema.Schema> implements BaseType<
271271
return ctx.compile();
272272
}
273273

274-
275-
276274
private __capacityEstimator: CompiledCapacityEstimator | undefined;
277275
public capacityEstimator(): CompiledCapacityEstimator {
278276
return (

0 commit comments

Comments
 (0)