Skip to content

Commit 0f27ed5

Browse files
committed
Tidy up style of emitted code
1 parent bad6ec7 commit 0f27ed5

File tree

5 files changed

+6484
-3817
lines changed

5 files changed

+6484
-3817
lines changed

index.ts

Lines changed: 54 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -503,16 +503,17 @@ function toTypeScript(
503503
const values: Array<${this.func.returnType.unwrap().toCode()}> = [];
504504
const failedExpectations: runtime.FailedExpectation[] = [];
505505
let remainder = ${this.remainder.toCode()};
506-
let result;
507506
508-
do {
507+
while (
508+
${this.max !== undefined ? `&& values.length < ${this.max}` : `true`}
509+
) {
509510
let r = remainder;
510511
${
511512
this.delimiter
512513
? `
513514
if (values.length > 0) {
514-
result = ${this.delimiter.toCode()}(r);
515-
515+
const result = ${this.delimiter.toCode()}(r);
516+
failedExpectations.push(...result.failedExpectations);
516517
if (result.success === false) {
517518
break;
518519
}
@@ -522,23 +523,23 @@ function toTypeScript(
522523
: ``
523524
}
524525
525-
result = ${this.func.toCode()}(r);
526+
const result = ${this.func.toCode()}(r);
526527
failedExpectations.push(...result.failedExpectations);
527528
if (result.success === false) {
528529
break;
529530
}
530531
531532
remainder = result.remainder;
532533
values.push(result.value);
533-
} while (${this.max !== undefined ? `values.length < ${this.max}` : `true`});
534+
}
534535
535536
${
536537
this.min
537-
? `if (values.length < ${this.min} && result.success === false /* technically redundant */) {
538-
return { success: false, remainder: result.remainder, failedExpectations };
539-
} else {
540-
return { success: true, value: values, remainder, failedExpectations };
541-
}`
538+
? `if (values.length < ${this.min}) {
539+
return { success: false, remainder: ${this.remainder.toCode()}, failedExpectations };
540+
}
541+
542+
return { success: true, value: values, remainder, failedExpectations };`
542543
: `return { success: true, value: values, remainder, failedExpectations };`
543544
}`;
544545
}
@@ -2193,14 +2194,14 @@ function toTypeScript(
21932194
remainder: ${this.remainder.toCode()},
21942195
failedExpectations: [],
21952196
}
2196-
} else {
2197-
return {
2198-
success: true,
2199-
value: undefined,
2200-
remainder: ${this.remainder.toCode()},
2201-
failedExpectations: [],
2202-
};
22032197
}
2198+
2199+
return {
2200+
success: true,
2201+
value: undefined,
2202+
remainder: ${this.remainder.toCode()},
2203+
failedExpectations: [],
2204+
};
22042205
})()
22052206
`;
22062207
}
@@ -2265,12 +2266,12 @@ function toTypeScript(
22652266
remainder: ${this.value.toCode()}.slice(matches[0].length),
22662267
failedExpectations: [],
22672268
};
2268-
} else {
2269-
return {
2270-
success: false,
2271-
remainder: ${this.value.toCode()},
2272-
failedExpectations: [${failedExpectations.map((e) => e.toCode()).join()}],
2273-
}
2269+
}
2270+
2271+
return {
2272+
success: false,
2273+
remainder: ${this.value.toCode()},
2274+
failedExpectations: [${failedExpectations.map((e) => e.toCode()).join()}],
22742275
}
22752276
`;
22762277
}
@@ -2317,9 +2318,9 @@ function toTypeScript(
23172318
remainder: result.remainder,
23182319
failedExpectations: result.failedExpectations,
23192320
}
2320-
} else {
2321-
return result;
23222321
}
2322+
2323+
return result;
23232324
`;
23242325
}
23252326

@@ -2533,9 +2534,9 @@ function toTypeScript(
25332534
return `
25342535
if (${this.condition.toCode()}) {
25352536
${new Return(this.ifTrue).toCode()};
2536-
} else {
2537-
${new Return(this.elseFalse).toCode()};
25382537
}
2538+
2539+
${new Return(this.elseFalse).toCode()};
25392540
`;
25402541
}
25412542

@@ -3117,9 +3118,9 @@ function toTypeScript(
31173118
remainder: result${i}.remainder,
31183119
failedExpectations,
31193120
}
3120-
} else {
3121-
remainder = result${i}.remainder;
31223121
}
3122+
3123+
remainder = result${i}.remainder;
31233124
`;
31243125
} else {
31253126
const failedExpectations = e.expectations.map(
@@ -3136,9 +3137,9 @@ function toTypeScript(
31363137
remainder,
31373138
failedExpectations,
31383139
}
3139-
} else {
3140-
remainder = remainder.slice(result${i}[0].length);
31413140
}
3141+
3142+
remainder = remainder.slice(result${i}[0].length);
31423143
`;
31433144
}
31443145
})
@@ -3219,33 +3220,33 @@ function toTypeScript(
32193220
32203221
if (result.success === true) {
32213222
return result.value;
3222-
} else {
3223-
let remainder = input;
3224-
let failedExpectations: runtime.FailedExpectation[] = [];
3223+
}
32253224
3226-
for (const e of result.failedExpectations) {
3227-
if (e.remainder.length < remainder.length) {
3228-
remainder = e.remainder;
3229-
failedExpectations = [];
3230-
}
3225+
let remainder = input;
3226+
let failedExpectations: runtime.FailedExpectation[] = [];
32313227
3232-
if (e.remainder.length === remainder.length) {
3233-
failedExpectations.push(e);
3234-
}
3228+
for (const e of result.failedExpectations) {
3229+
if (e.remainder.length < remainder.length) {
3230+
remainder = e.remainder;
3231+
failedExpectations = [];
32353232
}
32363233
3237-
throw new SyntaxError(
3238-
failedExpectations.map(e => e.expectation),
3239-
remainder.slice(0, 1),
3240-
runtime.getLocation(
3241-
parse$source,
3242-
input,
3243-
remainder,
3244-
remainder
3245-
)
3246-
);
3234+
if (e.remainder.length === remainder.length) {
3235+
failedExpectations.push(e);
3236+
}
32473237
}
32483238
3239+
throw new ParseSyntaxError(
3240+
failedExpectations.map(e => e.expectation),
3241+
remainder.slice(0, 1),
3242+
runtime.getLocation(
3243+
parse$source,
3244+
input,
3245+
remainder,
3246+
remainder
3247+
)
3248+
);
3249+
32493250
${reusables
32503251
.filter((r) => r instanceof Code)
32513252
.filter((r) => referencedNodes.includes(r))

0 commit comments

Comments
 (0)