Skip to content

Commit db57d7c

Browse files
committed
createErrorObject: return empty object if emitDetailedErrors is false.
Handle empty objects in TypeGuardError. This requires passing visitorContext through a few more functions.
1 parent c10f9d0 commit db57d7c

File tree

4 files changed

+34
-16
lines changed

4 files changed

+34
-16
lines changed

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ function inputObjectAtPath(path, inputObject) {
1919
}
2020

2121
function appendInputToErrorMessage(message, path, inputObject) {
22+
if (message === undefined)
23+
return 'validation error';
2224
const foundInputObject = inputObjectAtPath(path, inputObject);
2325
try {
2426
return message + ', found: ' + require('util').inspect(foundInputObject);

src/transform-inline/visitor-keyof.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ function visitRegularObjectType(type: ts.ObjectType, visitorContext: VisitorCont
6060
return VisitorUtils.createAssertionFunction(
6161
condition,
6262
{ type: 'object-keyof', properties: names },
63-
name
63+
name,
64+
visitorContext
6465
);
6566
});
6667
}

src/transform-inline/visitor-type-check.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function visitDateType(type: ts.ObjectType, visitorContext: VisitorContext) {
6363
ts.createIdentifier('nativeDateObject')
6464
)
6565
),
66-
ts.createReturn(VisitorUtils.createErrorObject({ type: 'date' })),
66+
ts.createReturn(VisitorUtils.createErrorObject({ type: 'date' }, visitorContext)),
6767
ts.createReturn(ts.createNull())
6868
)],
6969
true
@@ -131,7 +131,7 @@ function visitTupleObjectType(type: ts.TupleType, visitorContext: VisitorContext
131131
],
132132
ts.SyntaxKind.BarBarToken
133133
),
134-
ts.createReturn(VisitorUtils.createErrorObject({ type: 'tuple', minLength, maxLength }))
134+
ts.createReturn(VisitorUtils.createErrorObject({ type: 'tuple', minLength, maxLength }, visitorContext))
135135
),
136136
...functionNames.map((functionName, index) =>
137137
ts.createBlock([
@@ -205,7 +205,7 @@ function visitArrayObjectType(type: ts.ObjectType, visitorContext: VisitorContex
205205
[VisitorUtils.objectIdentifier]
206206
)
207207
),
208-
ts.createReturn(VisitorUtils.createErrorObject({ type: 'array' }))
208+
ts.createReturn(VisitorUtils.createErrorObject({ type: 'array' }, visitorContext))
209209
),
210210
ts.createFor(
211211
ts.createVariableDeclarationList(
@@ -307,7 +307,7 @@ function visitRegularObjectType(type: ts.ObjectType, visitorContext: VisitorCont
307307
],
308308
ts.SyntaxKind.BarBarToken
309309
),
310-
ts.createReturn(VisitorUtils.createErrorObject({ type: 'object' }))
310+
ts.createReturn(VisitorUtils.createErrorObject({ type: 'object' }, visitorContext))
311311
),
312312
...propertyInfos.map((propertyInfo) => {
313313
if (propertyInfo.isSymbol) {
@@ -365,13 +365,13 @@ function visitRegularObjectType(type: ts.ObjectType, visitorContext: VisitorCont
365365
]),
366366
propertyInfo.optional
367367
? undefined
368-
: ts.createReturn(VisitorUtils.createErrorObject({ type: 'missing-property', property: propertyInfo.name }))
368+
: ts.createReturn(VisitorUtils.createErrorObject({ type: 'missing-property', property: propertyInfo.name }, visitorContext))
369369
)
370370
]);
371371
}),
372372
...(
373373
visitorContext.options.disallowSuperfluousObjectProperties && stringIndexFunctionName === undefined
374-
? [VisitorUtils.createSuperfluousPropertiesLoop(propertyInfos.map((propertyInfo) => propertyInfo.name))]
374+
? [VisitorUtils.createSuperfluousPropertiesLoop(propertyInfos.map((propertyInfo) => propertyInfo.name), visitorContext)]
375375
: []
376376
),
377377
...(
@@ -518,6 +518,7 @@ function visitLiteralType(type: ts.LiteralType, visitorContext: VisitorContext)
518518
),
519519
{ type: 'string-literal', value },
520520
name,
521+
visitorContext,
521522
VisitorUtils.createStrictNullCheckStatement(VisitorUtils.objectIdentifier, visitorContext)
522523
);
523524
});
@@ -532,6 +533,7 @@ function visitLiteralType(type: ts.LiteralType, visitorContext: VisitorContext)
532533
),
533534
{ type: 'number-literal', value },
534535
name,
536+
visitorContext,
535537
VisitorUtils.createStrictNullCheckStatement(VisitorUtils.objectIdentifier, visitorContext)
536538
);
537539
});
@@ -558,7 +560,7 @@ function visitUnionOrIntersectionType(type: ts.UnionOrIntersectionType, visitorC
558560
// Check object keys at intersection type level. https://github.com/woutervh-/typescript-is/issues/21
559561
const keys = VisitorIsStringKeyof.visitType(type, visitorContext);
560562
if (keys instanceof Set) {
561-
const loop = VisitorUtils.createSuperfluousPropertiesLoop(sliceSet(keys));
563+
const loop = VisitorUtils.createSuperfluousPropertiesLoop(sliceSet(keys), visitorContext);
562564
return VisitorUtils.createConjunctionFunction(functionNames, name, [loop]);
563565
}
564566
}
@@ -580,6 +582,7 @@ function visitBooleanLiteral(type: ts.Type, visitorContext: VisitorContext) {
580582
),
581583
{ type: 'boolean-literal', value: true },
582584
name,
585+
visitorContext,
583586
VisitorUtils.createStrictNullCheckStatement(VisitorUtils.objectIdentifier, visitorContext)
584587
);
585588
});
@@ -593,6 +596,7 @@ function visitBooleanLiteral(type: ts.Type, visitorContext: VisitorContext) {
593596
),
594597
{ type: 'boolean-literal', value: false },
595598
name,
599+
visitorContext,
596600
VisitorUtils.createStrictNullCheckStatement(VisitorUtils.objectIdentifier, visitorContext)
597601
);
598602
});
@@ -633,6 +637,7 @@ function visitNonPrimitiveType(type: ts.Type, visitorContext: VisitorContext) {
633637
ts.createLogicalNot(condition),
634638
{ type: 'non-primitive' },
635639
name,
640+
visitorContext,
636641
VisitorUtils.createStrictNullCheckStatement(VisitorUtils.objectIdentifier, visitorContext)
637642
);
638643
});
@@ -700,7 +705,7 @@ function visitTemplateLiteralType(type: ts.TemplateLiteralType, visitorContext:
700705
const templateLiteralTypeError = VisitorUtils.createErrorObject({
701706
type: 'template-literal',
702707
value: typePairs
703-
})
708+
}, visitorContext)
704709
return VisitorUtils.setFunctionIfNotExists(name, visitorContext, () => ts.factory.createFunctionDeclaration(
705710
undefined,
706711
undefined,

src/transform-inline/visitor-utils.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ export function getFunctionFunction(visitorContext: VisitorContext) {
203203
),
204204
{ type: 'function' },
205205
name,
206+
visitorContext,
206207
createStrictNullCheckStatement(objectIdentifier, visitorContext)
207208
);
208209
});
@@ -218,6 +219,7 @@ export function getStringFunction(visitorContext: VisitorContext) {
218219
),
219220
{ type: 'string' },
220221
name,
222+
visitorContext,
221223
createStrictNullCheckStatement(objectIdentifier, visitorContext)
222224
);
223225
});
@@ -233,6 +235,7 @@ export function getBooleanFunction(visitorContext: VisitorContext) {
233235
),
234236
{ type: 'boolean' },
235237
name,
238+
visitorContext,
236239
createStrictNullCheckStatement(objectIdentifier, visitorContext)
237240
);
238241
});
@@ -248,6 +251,7 @@ export function getBigIntFunction(visitorContext: VisitorContext) {
248251
),
249252
{ type: 'big-int' },
250253
name,
254+
visitorContext,
251255
createStrictNullCheckStatement(objectIdentifier, visitorContext)
252256
);
253257
});
@@ -263,6 +267,7 @@ export function getNumberFunction(visitorContext: VisitorContext) {
263267
),
264268
{ type: 'number' },
265269
name,
270+
visitorContext,
266271
createStrictNullCheckStatement(objectIdentifier, visitorContext)
267272
);
268273
});
@@ -278,6 +283,7 @@ export function getUndefinedFunction(visitorContext: VisitorContext) {
278283
),
279284
{ type: 'undefined' },
280285
name,
286+
visitorContext,
281287
createStrictNullCheckStatement(objectIdentifier, visitorContext)
282288
);
283289
});
@@ -301,6 +307,7 @@ export function getNullFunction(visitorContext: VisitorContext) {
301307
),
302308
{ type: 'null' },
303309
name,
310+
visitorContext,
304311
createStrictNullCheckStatement(objectIdentifier, visitorContext)
305312
);
306313
});
@@ -320,7 +327,7 @@ export function getNeverFunction(visitorContext: VisitorContext) {
320327
],
321328
undefined,
322329
ts.createBlock([
323-
ts.createReturn(createErrorObject({ type: 'never' }))
330+
ts.createReturn(createErrorObject({ type: 'never' }, visitorContext))
324331
])
325332
);
326333
});
@@ -492,7 +499,7 @@ export function createDisjunctionFunction(functionNames: string[], functionName:
492499
)
493500
])
494501
),
495-
ts.createReturn(createErrorObject({ type: 'union' }))
502+
ts.createReturn(createErrorObject({ type: 'union' }, visitorContext))
496503
])
497504
);
498505
}
@@ -537,7 +544,7 @@ export function createStrictNullCheckStatement(identifier: ts.Identifier, visito
537544
}
538545
}
539546

540-
export function createAssertionFunction(failureCondition: ts.Expression, expected: Reason, functionName: string, ...otherStatements: ts.Statement[]) {
547+
export function createAssertionFunction(failureCondition: ts.Expression, expected: Reason, functionName: string, visitorContext: VisitorContext, ...otherStatements: ts.Statement[]) {
541548
return ts.createFunctionDeclaration(
542549
undefined,
543550
undefined,
@@ -552,14 +559,14 @@ export function createAssertionFunction(failureCondition: ts.Expression, expecte
552559
...otherStatements,
553560
ts.createIf(
554561
failureCondition,
555-
ts.createReturn(createErrorObject(expected)),
562+
ts.createReturn(createErrorObject(expected, visitorContext)),
556563
ts.createReturn(ts.createNull())
557564
)
558565
])
559566
);
560567
}
561568

562-
export function createSuperfluousPropertiesLoop(propertyNames: string[]) {
569+
export function createSuperfluousPropertiesLoop(propertyNames: string[], visitorContext: VisitorContext) {
563570
return ts.createForOf(
564571
undefined,
565572
ts.createVariableDeclarationList(
@@ -574,7 +581,7 @@ export function createSuperfluousPropertiesLoop(propertyNames: string[]) {
574581
ts.SyntaxKind.AmpersandAmpersandToken,
575582
ts.createTrue()
576583
),
577-
ts.createReturn(createErrorObject({ type: 'superfluous-property' }))
584+
ts.createReturn(createErrorObject({ type: 'superfluous-property' }, visitorContext))
578585
)
579586
])
580587
);
@@ -625,7 +632,10 @@ function createAssertionString(reason: string | ts.Expression): ts.Expression {
625632
}
626633
}
627634

628-
export function createErrorObject(reason: Reason): ts.Expression {
635+
export function createErrorObject(reason: Reason, visitorContext: VisitorContext): ts.Expression {
636+
if (visitorContext.options.emitDetailedErrors === false) {
637+
return ts.createObjectLiteral([]);
638+
}
629639
return ts.createObjectLiteral([
630640
ts.createPropertyAssignment('message', createErrorMessage(reason)),
631641
ts.createPropertyAssignment('path', ts.createCall(ts.createPropertyAccess(pathIdentifier, 'slice'), undefined, undefined)),

0 commit comments

Comments
 (0)