Skip to content

Commit 3e79484

Browse files
committed
without emitDetailedErrors, skip pushing and popping the path.
Since this was a bit of copy/paste code, I refactored it into a single helper function.
1 parent db57d7c commit 3e79484

File tree

1 file changed

+85
-144
lines changed

1 file changed

+85
-144
lines changed

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

Lines changed: 85 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,63 @@ function visitDateType(type: ts.ObjectType, visitorContext: VisitorContext) {
7272
});
7373
}
7474

75+
function createRecursiveCall(functionName: string, functionArgument: ts.Expression, pathExpression: ts.Expression, visitorContext: VisitorContext): ts.Statement[] {
76+
const errorIdentifier = ts.createIdentifier('error');
77+
const emitDetailedErrors = !!visitorContext.options.emitDetailedErrors;
78+
79+
const statements: ts.Statement[] = [];
80+
if (emitDetailedErrors) {
81+
statements.push(ts.createExpressionStatement(
82+
ts.createCall(
83+
ts.createPropertyAccess(VisitorUtils.pathIdentifier, 'push'),
84+
undefined,
85+
[
86+
VisitorUtils.createBinaries(
87+
[
88+
pathExpression
89+
],
90+
ts.SyntaxKind.PlusToken
91+
)
92+
]
93+
)
94+
));
95+
}
96+
statements.push(ts.createVariableStatement(
97+
[ts.createModifier(ts.SyntaxKind.ConstKeyword)],
98+
[
99+
ts.createVariableDeclaration(
100+
errorIdentifier,
101+
undefined,
102+
ts.createCall(
103+
ts.createIdentifier(functionName),
104+
undefined,
105+
[functionArgument]
106+
)
107+
)
108+
]
109+
));
110+
if (emitDetailedErrors) {
111+
statements.push(ts.createExpressionStatement(
112+
ts.createCall(
113+
ts.createPropertyAccess(VisitorUtils.pathIdentifier, 'pop'),
114+
undefined,
115+
undefined
116+
)
117+
));
118+
}
119+
statements.push(ts.createIf(
120+
errorIdentifier,
121+
ts.createReturn(errorIdentifier)
122+
));
123+
return statements;
124+
}
125+
75126
function visitTupleObjectType(type: ts.TupleType, visitorContext: VisitorContext) {
76127
const name = VisitorTypeName.visitType(type, visitorContext, { type: 'type-check' });
77128
return VisitorUtils.setFunctionIfNotExists(name, visitorContext, () => {
78129
const functionNames = type.typeArguments ?
79130
type.typeArguments.map((type) => visitType(type, visitorContext))
80131
: [];
81-
const errorIdentifier = ts.createIdentifier('error');
82132

83133
const maxLength = functionNames.length;
84134
let minLength = functionNames.length;
@@ -134,40 +184,12 @@ function visitTupleObjectType(type: ts.TupleType, visitorContext: VisitorContext
134184
ts.createReturn(VisitorUtils.createErrorObject({ type: 'tuple', minLength, maxLength }, visitorContext))
135185
),
136186
...functionNames.map((functionName, index) =>
137-
ts.createBlock([
138-
ts.createExpressionStatement(
139-
ts.createCall(
140-
ts.createPropertyAccess(VisitorUtils.pathIdentifier, 'push'),
141-
undefined,
142-
[ts.createStringLiteral(`[${index}]`)]
143-
)
144-
),
145-
ts.createVariableStatement(
146-
[ts.createModifier(ts.SyntaxKind.ConstKeyword)],
147-
[
148-
ts.createVariableDeclaration(
149-
errorIdentifier,
150-
undefined,
151-
ts.createCall(
152-
ts.createIdentifier(functionName),
153-
undefined,
154-
[ts.createElementAccess(VisitorUtils.objectIdentifier, index)]
155-
)
156-
)
157-
]
158-
),
159-
ts.createExpressionStatement(
160-
ts.createCall(
161-
ts.createPropertyAccess(VisitorUtils.pathIdentifier, 'pop'),
162-
undefined,
163-
undefined
164-
)
165-
),
166-
ts.createIf(
167-
errorIdentifier,
168-
ts.createReturn(errorIdentifier)
169-
)
170-
])
187+
ts.createBlock(createRecursiveCall(
188+
functionName,
189+
ts.createElementAccess(VisitorUtils.objectIdentifier, index),
190+
ts.createStringLiteral(`[${index}]`),
191+
visitorContext
192+
))
171193
),
172194
ts.createReturn(ts.createNull())
173195
])
@@ -184,7 +206,7 @@ function visitArrayObjectType(type: ts.ObjectType, visitorContext: VisitorContex
184206
}
185207
const functionName = visitType(numberIndexType, visitorContext);
186208
const indexIdentifier = ts.createIdentifier('i');
187-
const errorIdentifier = ts.createIdentifier('error');
209+
188210
return ts.createFunctionDeclaration(
189211
undefined,
190212
undefined,
@@ -218,49 +240,21 @@ function visitArrayObjectType(type: ts.ObjectType, visitorContext: VisitorContex
218240
ts.createPropertyAccess(VisitorUtils.objectIdentifier, 'length')
219241
),
220242
ts.createPostfixIncrement(indexIdentifier),
221-
ts.createBlock([
222-
ts.createExpressionStatement(
223-
ts.createCall(
224-
ts.createPropertyAccess(VisitorUtils.pathIdentifier, 'push'),
225-
undefined,
243+
ts.createBlock(
244+
createRecursiveCall(
245+
functionName,
246+
ts.createElementAccess(VisitorUtils.objectIdentifier, indexIdentifier),
247+
VisitorUtils.createBinaries(
226248
[
227-
VisitorUtils.createBinaries(
228-
[
229-
ts.createStringLiteral('['),
230-
indexIdentifier,
231-
ts.createStringLiteral(']')
232-
],
233-
ts.SyntaxKind.PlusToken
234-
)
235-
]
236-
)
237-
),
238-
ts.createVariableStatement(
239-
[ts.createModifier(ts.SyntaxKind.ConstKeyword)],
240-
[
241-
ts.createVariableDeclaration(
242-
errorIdentifier,
243-
undefined,
244-
ts.createCall(
245-
ts.createIdentifier(functionName),
246-
undefined,
247-
[ts.createElementAccess(VisitorUtils.objectIdentifier, indexIdentifier)]
248-
)
249-
)
250-
]
251-
),
252-
ts.createExpressionStatement(
253-
ts.createCall(
254-
ts.createPropertyAccess(VisitorUtils.pathIdentifier, 'pop'),
255-
undefined,
256-
undefined
257-
)
258-
),
259-
ts.createIf(
260-
errorIdentifier,
261-
ts.createReturn(errorIdentifier)
249+
ts.createStringLiteral('['),
250+
indexIdentifier,
251+
ts.createStringLiteral(']')
252+
],
253+
ts.SyntaxKind.PlusToken
254+
),
255+
visitorContext
262256
)
263-
])
257+
)
264258
),
265259
ts.createReturn(ts.createNull())
266260
])
@@ -275,7 +269,6 @@ function visitRegularObjectType(type: ts.ObjectType, visitorContext: VisitorCont
275269
const stringIndexType = visitorContext.checker.getIndexTypeOfType(type, ts.IndexKind.String);
276270
const stringIndexFunctionName = stringIndexType ? visitType(stringIndexType, visitorContext) : undefined;
277271
const keyIdentifier = ts.createIdentifier('key');
278-
const errorIdentifier = ts.createIdentifier('error');
279272
return ts.createFunctionDeclaration(
280273
undefined,
281274
undefined,
@@ -329,40 +322,14 @@ function visitRegularObjectType(type: ts.ObjectType, visitorContext: VisitorCont
329322
ts.SyntaxKind.InKeyword,
330323
VisitorUtils.objectIdentifier
331324
),
332-
ts.createBlock([
333-
ts.createExpressionStatement(
334-
ts.createCall(
335-
ts.createPropertyAccess(VisitorUtils.pathIdentifier, 'push'),
336-
undefined,
337-
[ts.createStringLiteral(propertyInfo.name)]
338-
)
339-
),
340-
ts.createVariableStatement(
341-
[ts.createModifier(ts.SyntaxKind.ConstKeyword)],
342-
[
343-
ts.createVariableDeclaration(
344-
errorIdentifier,
345-
undefined,
346-
ts.createCall(
347-
ts.createIdentifier(functionName),
348-
undefined,
349-
[ts.createElementAccess(VisitorUtils.objectIdentifier, ts.createStringLiteral(propertyInfo.name))]
350-
)
351-
)
352-
]
353-
),
354-
ts.createExpressionStatement(
355-
ts.createCall(
356-
ts.createPropertyAccess(VisitorUtils.pathIdentifier, 'pop'),
357-
undefined,
358-
undefined
359-
)
360-
),
361-
ts.createIf(
362-
errorIdentifier,
363-
ts.createReturn(errorIdentifier)
325+
ts.createBlock(
326+
createRecursiveCall(
327+
functionName,
328+
ts.createElementAccess(VisitorUtils.objectIdentifier, ts.createStringLiteral(propertyInfo.name)),
329+
ts.createStringLiteral(propertyInfo.name),
330+
visitorContext
364331
)
365-
]),
332+
),
366333
propertyInfo.optional
367334
? undefined
368335
: ts.createReturn(VisitorUtils.createErrorObject({ type: 'missing-property', property: propertyInfo.name }, visitorContext))
@@ -384,40 +351,14 @@ function visitRegularObjectType(type: ts.ObjectType, visitorContext: VisitorCont
384351
ts.NodeFlags.Const
385352
),
386353
ts.createCall(ts.createPropertyAccess(ts.createIdentifier('Object'), 'keys'), undefined, [VisitorUtils.objectIdentifier]),
387-
ts.createBlock([
388-
ts.createExpressionStatement(
389-
ts.createCall(
390-
ts.createPropertyAccess(VisitorUtils.pathIdentifier, 'push'),
391-
undefined,
392-
[keyIdentifier]
393-
)
394-
),
395-
ts.createVariableStatement(
396-
[ts.createModifier(ts.SyntaxKind.ConstKeyword)],
397-
[
398-
ts.createVariableDeclaration(
399-
errorIdentifier,
400-
undefined,
401-
ts.createCall(
402-
ts.createIdentifier(stringIndexFunctionName),
403-
undefined,
404-
[ts.createElementAccess(VisitorUtils.objectIdentifier, keyIdentifier)]
405-
)
406-
)
407-
]
408-
),
409-
ts.createExpressionStatement(
410-
ts.createCall(
411-
ts.createPropertyAccess(VisitorUtils.pathIdentifier, 'pop'),
412-
undefined,
413-
undefined
414-
)
415-
),
416-
ts.createIf(
417-
errorIdentifier,
418-
ts.createReturn(errorIdentifier)
354+
ts.createBlock(
355+
createRecursiveCall(
356+
stringIndexFunctionName,
357+
ts.createElementAccess(VisitorUtils.objectIdentifier, keyIdentifier),
358+
keyIdentifier,
359+
visitorContext
419360
)
420-
])
361+
)
421362
)
422363
]
423364
: []

0 commit comments

Comments
 (0)