denoland / deno_ast

Source text parsing, lexing, and AST related functionality for Deno

Home Page:https://crates.io/crates/deno_ast

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

We can no longer start files with `BytePos(0)`

dsherret opened this issue · comments

After swc-project/swc#4616 we can no longer start files at BytePos(0) and therefore now have to offset all our node byte positions. This is a very unfortunate change because byte positions of nodes no longer align with byte positions in the text.

I think we should deprecate using swc's BytePos and instead start using an opaque type that you can perform operations on, but need to provide a parsed source to get the index in the file.

swc won't be using u32::MAX as a magic value swc-project/swc#4650

For this change, we're going to have to ban the use of the following swc structs and methods:

  • BytePos
  • Span
  • span() - use ctxt() only

https://stackoverflow.com/a/69484643/188246

I'm thinking we can add a new methods to Spanned like start(), end() and range() that return a new SourceIndex type that wraps BytePos and offsets the position be a certain value.

pub struct SourceIndex(BytePos);

impl SourceIndex {
  pub fn new(val: usize) {
    SourceIndex(BytePos(val as u32 + 1000))
  }

  pub fn as_usize(&self) -> usize {
    (self.0.0 - 1000) as usize
  }
}

This is super unfortunate we have to do this though.