denoland / deno_ast

Source text parsing, lexing, and AST related functionality for Deno
https://crates.io/crates/deno_ast
MIT License
155 stars 46 forks source link

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

Closed dsherret closed 2 years ago

dsherret commented 2 years ago

After https://github.com/swc-project/swc/pull/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.

dsherret commented 2 years ago

swc won't be using u32::MAX as a magic value https://github.com/swc-project/swc/pull/4650

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

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.