You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#7211 brought up a problem. Most of our literal types (NumericLiteral etc) have raw field.
This makes sense when the AST has come from the parser, but little sense when the node is generated (e.g. in transformer), because the node has no "raw" representation.
Having to provide "0" here feels like a hack to me, for example:
The exception is StringLiteral which doesn't have a raw field. This feels like an omission - if the other literal types have a raw field, it should have one too. But adding one would be a pain, as there are a lot of places we generate strings in transformer etc.
Prior art
Babel's types have the raw field under extra which is optional:
Change raw field on NumericLiteral etc to Option<&'a str>.
Add a raw: Option<&'a str> field to StringLiteral.
Remove all raw fields
Where an AST node has a non-empty span, the raw value can be obtained by slicing source text. So remove all the raw fields, and use methods to get raw value where required:
Personally I prefer the 2nd. I don't think the raw fields are used much, so they're bloating the AST for little value. The methods to get raw value from Span are pretty trivial and cheap.
The text was updated successfully, but these errors were encountered:
The problem
#7211 brought up a problem. Most of our literal types (
NumericLiteral
etc) haveraw
field.This makes sense when the AST has come from the parser, but little sense when the node is generated (e.g. in transformer), because the node has no "raw" representation.
Having to provide
"0"
here feels like a hack to me, for example:oxc/crates/oxc_ast/src/ast_builder_impl.rs
Lines 209 to 211 in 44375a5
The exception is
StringLiteral
which doesn't have araw
field. This feels like an omission - if the other literal types have araw
field, it should have one too. But adding one would be a pain, as there are a lot of places we generate strings in transformer etc.Prior art
Babel's types have the
raw
field underextra
which is optional:StringLiteral BaseNode
Acorn's types also have the
raw
field as optional:source
Possible solutions
I can see 2 options:
Make
raw
fields optionalraw
field onNumericLiteral
etc toOption<&'a str>
.raw: Option<&'a str>
field toStringLiteral
.Remove all
raw
fieldsWhere an AST node has a non-empty span, the raw value can be obtained by slicing source text. So remove all the
raw
fields, and use methods to get raw value where required:(as suggested in #5522)
Which?
Personally I prefer the 2nd. I don't think the
raw
fields are used much, so they're bloating the AST for little value. The methods to get raw value fromSpan
are pretty trivial and cheap.The text was updated successfully, but these errors were encountered: