-
-
Notifications
You must be signed in to change notification settings - Fork 459
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make raw
field on literal AST types optional, or remove them
#7254
Comments
I did a try to remove raw fields, but there are some issues... I think the core problem is the mix purpose of AST, different requirement leads to different design. For example, for compiling, we do not need any origin data, so the AST could be abstract. But for linter or formatter, some abilities should not be enabled by default. Like, for this, they might not want 1_0_2_2 be converted to 1_022, at least there should be an option to control it. Roslyn(Biome and RA too, they refers Roslyn) keeps a lossless syntax tree for this purpose. This design makes their AST great for interreact, although it is not pretty memory friendly(because they need to maintain about 2 trees). So, I would suggest to choose option 1. Any thoughts? @overlookmotel @Boshen |
It is true that transformer/minifier and linter/formatter have different needs. However in this case, I don't think it much affects which option we go for. For example, in linter the numeric_separators_style rule does need the raw code string. But it can equally easily get that from a The |
@Boshen Do you have any opinion on which option? If we can agree, perhaps @ShuiRuTian is willing to work on it? |
Boshen prefers |
#7393 adds Still to do: Change |
@ShuiRuTian I wonder if you'd like to help out with this? Changing the |
@overlookmotel I would love to, but @Boshen already have a PR #7393 Not sure what I need to do now. |
Thanks for your willingness! Boshen's PR will add e.g. FYI, oxc/crates/oxc_span/src/atom.rs Line 20 in 199076b
Boshen said he ran into a problem using Does that make more sense? |
Yeah, it makes things much clearer, thanks |
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: