Commit 0651404
committed
Handle multi-character binary operators split by SWC's lexer
The SWC lexer can tokenize multi-character binary operators as separate
tokens when the operator follows a TypeScript type position. For
example, in `0 as number <= 1`, after parsing the type `number` the
lexer doesn't merge `<` with the trailing `=` and emits them as two
tokens — even though the parser's expression layer correctly treats
the whole thing as a single `<=` BinExpr.
`get_flattened_bin_expr` searched for a single token whose text equals
the binary operator (`<=`), found none, and panicked on the `unwrap()`:
thread 'main' panicked at flatten_binary_expr.rs:25:
called `Option::unwrap()` on a `None` value
This affected at least `<=` and `>=` after `as`-expressions; the
existing `<<` test in BinaryExpression_All hints the lexer has split
multi-char operators in the past too.
Move the operator-token lookup into a small helper and, when the full
operator-text match fails, fall back to matching just the operator's
first character. The fallback is sufficient because callers only need
the token's start position (e.g. `start_line_fast` for operator
position decisions); for the split case, the leading `<` token
carries the same start position as the conceptual `<=` operator.
Refs denoland/deno#31988.1 parent c2aef70 commit 0651404
2 files changed
Lines changed: 36 additions & 1 deletion
File tree
- src/generation/swc
- tests/specs/expressions/BinaryExpression
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
| 25 | + | |
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| |||
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
71 | 94 | | |
72 | 95 | | |
73 | 96 | | |
| |||
Lines changed: 12 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
286 | 286 | | |
287 | 287 | | |
288 | 288 | | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
0 commit comments