-
-
Notifications
You must be signed in to change notification settings - Fork 861
feat(linter/plugins): serialize rust-parsed tokens #17025
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| use oxc_allocator::CloneIn; | ||
| use oxc_ast_macros::{ast, ast_meta}; | ||
| use oxc_estree::ESTree; | ||
| use oxc_span::{Atom, ContentEq, GetSpan, GetSpanMut, Span}; | ||
|
|
||
| #[ast] | ||
| #[generate_derive(CloneIn, ContentEq, ESTree, GetSpan, GetSpanMut)] | ||
| #[estree(add_fields(value = TokenValue), no_type, no_ts_def, no_parent)] | ||
| #[derive(Debug)] | ||
| /// Represents a token in the source code. | ||
| pub struct Token<'a> { | ||
| /// Span. | ||
| #[span] | ||
| pub span: Span, | ||
| /// Type. | ||
| pub r#type: Atom<'a>, | ||
| /// Flags. | ||
| pub flags: Option<Atom<'a>>, | ||
| /// Pattern. | ||
| pub pattern: Option<Atom<'a>>, | ||
| } | ||
|
|
||
| /// Custom deserializer for `value` field of `Token`. | ||
| #[ast_meta] | ||
| #[generate_derive(CloneIn, ContentEq, ESTree)] | ||
| #[estree(ts_type = "string", raw_deser = "SOURCE_TEXT.slice(THIS.start, THIS.end)")] | ||
| pub struct TokenValue<'a, 'b>(pub &'b Token<'a>); | ||
|
Comment on lines
+1
to
+27
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We shouldn't need this. We want JS side code to read the original Instead:
But you should be able to use The conversion from No doubt, you didn't need me to explain every one of the steps above. I've just included them for clarity, to (hopefully) save you a little time. Please excuse me if I'm "teaching grandpa to suck eggs". I've laid a bit of groundwork for this in #17050 and #17052.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh actually maybe we can get codegen to generate everything for us by:
/// Dummy type to communicate the content of `Token` to `oxc_ast_tools`.
#[ast(foreign = Token)]
#[generate_derive(ESTree)]
#[expect(dead_code)]
struct TokenAlias {
pub span: Span,
pub kind: Kind,
#[estree(skip)]
pub _align: U128Align,
}
/// Zero-sized type which has alignment of `u128`
#[repr(transparent)]
struct U128Align([u128; 0]);This tells the codegen "treat You'd need to add a "special case" to codegen for EDIT: No this is a bad idea. Need to implement |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we avoid adding this field to
Program? Tokens can be returned inParserReturninstead.