Skip to content

Commit

Permalink
feat: add BindingPattern as an edge case.
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Jun 25, 2024
1 parent d9256ee commit 9c7d50f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
13 changes: 6 additions & 7 deletions crates/oxc_ast/src/generated/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -925,13 +925,6 @@ impl GetSpan for DebuggerStatement {
}
}

impl<'a> GetSpan for BindingPattern<'a> {
#[inline]
fn span(&self) -> Span {
self.span
}
}

impl<'a> GetSpan for BindingPatternKind<'a> {
fn span(&self) -> Span {
match self {
Expand Down Expand Up @@ -2174,3 +2167,9 @@ impl<'a> GetSpan for JSXText<'a> {
self.span
}
}

impl<'a> GetSpan for BindingPattern<'a> {
fn span(&self) -> Span {
self.kind.span()
}
}
38 changes: 33 additions & 5 deletions tasks/ast_codegen/src/generators/impl_get_span.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::collections::HashMap;

use itertools::Itertools;
use lazy_static::lazy_static;
use proc_macro2::TokenStream;
use quote::quote;
use syn::{parse_quote, Attribute, Variant};
Expand All @@ -12,6 +15,23 @@ use super::generated_header;

pub struct ImplGetSpanGenerator;

const EDGE_CASES: [&str; 1] = ["BindingPattern"];

fn edge_case(it: &std::cell::Ref<RType>) -> bool {
!it.ident().is_some_and(|it| EDGE_CASES.contains(&it.to_string().as_str()))
}

fn edge_case_impls() -> TokenStream {
quote! {
endl!();
impl<'a> GetSpan for BindingPattern<'a> {
fn span(&self) -> Span {
self.kind.span()
}
}
}
}

impl Generator for ImplGetSpanGenerator {
fn name(&self) -> &'static str {
"ImplGetSpanGenerator"
Expand All @@ -21,14 +41,19 @@ impl Generator for ImplGetSpanGenerator {
let impls: Vec<TokenStream> = ctx
.ty_table
.iter()
.filter(|it| it.borrow().visitable())
.filter_map(|maybe_kind| match &*maybe_kind.borrow() {
RType::Enum(it) => Some(impl_enum(it)),
RType::Struct(it) => Some(impl_struct(it)),
_ => None,
.map(|it| it.borrow())
.filter(|it| it.visitable())
.filter(|it| matches!(&**it, RType::Enum(_) | RType::Struct(_)))
.filter(edge_case)
.map(|kind| match &*kind {
RType::Enum(it) => impl_enum(it),
RType::Struct(it) => impl_struct(it),
_ => unreachable!("already filtered out!"),
})
.collect();

let edge_impls = edge_case_impls();

let header = generated_header!();

GeneratorOutput::One(quote! {
Expand All @@ -40,6 +65,9 @@ impl Generator for ImplGetSpanGenerator {
use oxc_span::{GetSpan, Span};

#(#impls)*

#edge_impls

})
}
}
Expand Down
1 change: 0 additions & 1 deletion tasks/ast_codegen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
span_file.write_all(span_content.as_bytes())?;
}


// NOTE: Print AstKind
// println!(
// "{}",
Expand Down

0 comments on commit 9c7d50f

Please sign in to comment.