(&self, start: TextSize)
| 197 | } |
| 198 | |
| 199 | fn node_range(&self, start: TextSize) -> TextRange { |
| 200 | // It's possible during error recovery that the parsing didn't consume any tokens. In that |
| 201 | // case, `last_token_end` still points to the end of the previous token but `start` is the |
| 202 | // start of the current token. Calling `TextRange::new(start, self.last_token_end)` would |
| 203 | // panic in that case because `start > end`. This path "detects" this case and creates an |
| 204 | // empty range instead. |
| 205 | // |
| 206 | // The reason it's `<=` instead of just `==` is because there could be whitespaces between |
| 207 | // the two tokens. For example: |
| 208 | // |
| 209 | // ```python |
| 210 | // # last token end |
| 211 | // # | current token (newline) start |
| 212 | // # v v |
| 213 | // def foo \n |
| 214 | // # ^ |
| 215 | // # assume there's trailing whitespace here |
| 216 | // ``` |
| 217 | // |
| 218 | // Or, there could tokens that are considered "trivia" and thus aren't emitted by the token |
| 219 | // source. These are comments and non-logical newlines. For example: |
| 220 | // |
| 221 | // ```python |
| 222 | // # last token end |
| 223 | // # v |
| 224 | // def foo # comment\n |
| 225 | // # ^ current token (newline) start |
| 226 | // ``` |
| 227 | // |
| 228 | // In either of the above cases, there's a "gap" between the end of the last token and start |
| 229 | // of the current token. |
| 230 | if self.prev_token_end <= start { |
| 231 | // We need to create an empty range at the last token end instead of the start because |
| 232 | // otherwise this node range will fall outside the range of it's parent node. Taking |
| 233 | // the above example: |
| 234 | // |
| 235 | // ```python |
| 236 | // if True: |
| 237 | // # function start |
| 238 | // # | function end |
| 239 | // # v v |
| 240 | // def foo # comment |
| 241 | // # ^ current token start |
| 242 | // ``` |
| 243 | // |
| 244 | // Here, the current token start is the start of parameter range but the function ends |
| 245 | // at `foo`. Even if there's a function body, the range of parameters would still be |
| 246 | // before the comment. |
| 247 | |
| 248 | // test_err node_range_with_gaps |
| 249 | // def foo # comment |
| 250 | // def bar(): ... |
| 251 | // def baz |
| 252 | TextRange::empty(self.prev_token_end) |
| 253 | } else { |
| 254 | TextRange::new(start, self.prev_token_end) |
| 255 | } |
| 256 | } |
no test coverage detected