| 993 | |
| 994 | // https://tc39.es/ecma262/#prod-SwitchStatement |
| 995 | parseSwitchStatement(this: Parser, node: Undone<N.SwitchStatement>) { |
| 996 | this.next(); |
| 997 | node.discriminant = this.parseHeaderExpression(); |
| 998 | const cases: Undone<N.SwitchCase>[] = (node.cases = []); |
| 999 | this.expect(tt.braceL); |
| 1000 | this.state.labels.push(switchLabel); |
| 1001 | this.scope.enter(ScopeFlag.SWITCH); |
| 1002 | |
| 1003 | // Statements under must be grouped (by label) in SwitchCase |
| 1004 | // nodes. `cur` is used to keep the node that we are currently |
| 1005 | // adding statements to. |
| 1006 | |
| 1007 | let cur; |
| 1008 | for (let sawDefault; !this.match(tt.braceR);) { |
| 1009 | if (this.match(tt._case) || this.match(tt._default)) { |
| 1010 | const isCase = this.match(tt._case); |
| 1011 | if (cur) this.finishNode(cur, "SwitchCase"); |
| 1012 | cases.push((cur = this.startNode<N.SwitchCase>())); |
| 1013 | cur.consequent = []; |
| 1014 | this.next(); |
| 1015 | if (isCase) { |
| 1016 | cur.test = this.parseExpression(); |
| 1017 | } else { |
| 1018 | if (sawDefault) { |
| 1019 | this.raise( |
| 1020 | Errors.MultipleDefaultsInSwitch, |
| 1021 | this.state.lastTokStartLoc!, |
| 1022 | ); |
| 1023 | } |
| 1024 | sawDefault = true; |
| 1025 | cur.test = null; |
| 1026 | } |
| 1027 | this.expect(tt.colon); |
| 1028 | } else { |
| 1029 | if (cur) { |
| 1030 | cur.consequent.push(this.parseStatementListItem()); |
| 1031 | } else { |
| 1032 | this.unexpected(); |
| 1033 | } |
| 1034 | } |
| 1035 | } |
| 1036 | this.scope.exit(); |
| 1037 | if (cur) this.finishNode(cur, "SwitchCase"); |
| 1038 | this.next(); // Closing brace |
| 1039 | this.state.labels.pop(); |
| 1040 | return this.finishNode(node, "SwitchStatement"); |
| 1041 | } |
| 1042 | |
| 1043 | parseThrowStatement(this: Parser, node: Undone<N.ThrowStatement>) { |
| 1044 | this.next(); |