Skip to content

Commit 89ae2d8

Browse files
GearsDatapackslpil
authored andcommitted
Add doc comments
1 parent 240349d commit 89ae2d8

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

compiler-core/src/javascript/decision.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,20 @@ pub fn case<'a>(
4242
Ok(docvec![assignments_to_doc(assignments), decision.into_doc()].force_break())
4343
}
4444

45+
/// The generated code for a decision tree.
4546
enum CaseBody<'a> {
47+
/// A JavaScript `if`` statement by itself. This can be merged with any
48+
/// preceding `else` statements to form an `else if` construct.
4649
If {
4750
check: Document<'a>,
4851
body: Document<'a>,
4952
},
53+
/// A sequence of statements. This must be wrapped as the body of an `if` or
54+
/// `else` statement.
5055
Statements(Document<'a>),
56+
57+
/// A JavaScript `if` statement followed by one or more `else` clauses. This
58+
/// can sometimes be merged with preceding `else` statements.
5159
IfElse(Document<'a>),
5260
}
5361

@@ -59,10 +67,14 @@ impl<'a> CaseBody<'a> {
5967
}
6068
}
6169

70+
/// Convert this value into the required document to put directly after an
71+
/// `else` keyword.
6272
fn document_after_else(self) -> Document<'a> {
6373
match self {
74+
// `if` and `if-else` statements can come directly after an `else` keyword
6475
CaseBody::If { .. } => self.into_doc(),
6576
CaseBody::IfElse(document) => document,
77+
// Lists of statements must be wrapped in a block
6678
CaseBody::Statements(document) => break_block(document),
6779
}
6880
}
@@ -229,6 +241,21 @@ impl<'a> CasePrinter<'_, '_, 'a> {
229241
assignments.append(&mut segment_assignments);
230242

231243
let (check_doc, body) = match body? {
244+
// If we have a statement like this:
245+
// ```javascript
246+
// if (x) {
247+
// if (y) {
248+
// ...
249+
// }
250+
// }
251+
// ```
252+
//
253+
// We can transform it into:
254+
// ```javascript
255+
// if (x && y) {
256+
// ...
257+
// }
258+
// ```
232259
CaseBody::If { check, body } => {
233260
(docvec!["(", check_doc, ") && (", check, ")"], body)
234261
}
@@ -238,10 +265,12 @@ impl<'a> CasePrinter<'_, '_, 'a> {
238265
};
239266

240267
if_ = match if_ {
268+
// The first statement will always be an `if`
241269
_ if i == 0 => CaseBody::If {
242270
check: check_doc,
243271
body,
244272
},
273+
// If this is the second check, the `if` becomes `else if`
245274
CaseBody::If { .. } => CaseBody::IfElse(docvec![
246275
if_.into_doc(),
247276
" else if (",
@@ -346,7 +375,7 @@ impl<'a> CasePrinter<'_, '_, 'a> {
346375
.inside_new_scope(|this| this.decision(if_false))?
347376
.into_doc();
348377

349-
// We can now piece everything together into a single document!
378+
// We can now piece everything together into a case body!
350379
let if_ = CaseBody::If {
351380
check,
352381
body: if_true,

0 commit comments

Comments
 (0)