This release fixes a few bugs in the structure of the module import and export nodes. This also introduces a spanned
module which requires the user to provide a human friendly start and end position for each element of each node. For example, an empty object literal would now be defined as below.
Expr::Obj(ObjExpr {
open_brace: Slice {
source: Cow::Borrowed("{"),
loc: SourceLocation {
start: Position { line: 1, column: 1 },
end: Position { line: 1, column: 2 },
},
},
props: Vec::new(),
close_brace: Slice {
source: Cow::Borrowed("}"),
loc: SourceLocation {
start: Position { line: 1, column: 2 },
end: Position { line: 1, column: 3 },
},
},
});
While that is pretty verbose, it provides all of the context needed for performing things like source maps with 100% accuracy.
This release also introduces the Node
trait which has 2 methods, loc
which will provide the SourceLocation
and also source
for accessing the string slice for that node. Using the above Expr
we can see how this can see how this could be useful.
let obj = Expr::Obj(/*see above*/);
let full_loc = obj.loc();
assert_eq!(full_loc.start.line, 1);
assert_eq!(full_loc.start.column, 1);
assert_eq!(full_loc.end.line, 1);
assert_eq!(full_loc.end.column, 2);
if let Expr::Obj(inner) = &obj {
let start_loc = inner.open_brace.loc();
assert_eq!(start_loc.start, full_loc.start);
let end_loc = inner.close_brace.loc();
assert_eq!(end_loc.end, full_loc.end);
}