Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions crates/oxc_ast_visit/src/generated/utf8_to_utf16_converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,12 +398,6 @@ impl<'a> VisitMut<'a> for Utf8ToUtf16Converter<'_> {
self.convert_offset(&mut it.span.end);
}

fn visit_binding_rest_element(&mut self, it: &mut BindingRestElement<'a>) {
self.convert_offset(&mut it.span.start);
walk_mut::walk_binding_rest_element(self, it);
self.convert_offset(&mut it.span.end);
}

fn visit_function(&mut self, it: &mut Function<'a>, flags: ScopeFlags) {
self.convert_offset(&mut it.span.start);
walk_mut::walk_function(self, it, flags);
Expand Down Expand Up @@ -1094,6 +1088,44 @@ impl<'a> VisitMut<'a> for Utf8ToUtf16Converter<'_> {
self.convert_offset(&mut prop.span.end);
}

fn visit_binding_pattern(&mut self, pattern: &mut BindingPattern<'a>) {
let span_end = match &mut pattern.kind {
BindingPatternKind::BindingIdentifier(ident) => {
self.convert_offset(&mut ident.span.start);
walk_mut::walk_binding_identifier(self, ident);
&mut ident.span.end
}
BindingPatternKind::ObjectPattern(obj_pattern) => {
self.convert_offset(&mut obj_pattern.span.start);
walk_mut::walk_object_pattern(self, obj_pattern);
&mut obj_pattern.span.end
}
BindingPatternKind::ArrayPattern(arr_pattern) => {
self.convert_offset(&mut arr_pattern.span.start);
walk_mut::walk_array_pattern(self, arr_pattern);
&mut arr_pattern.span.end
}
BindingPatternKind::AssignmentPattern(assign_pattern) => {
self.convert_offset(&mut assign_pattern.span.start);
walk_mut::walk_assignment_pattern(self, assign_pattern);
&mut assign_pattern.span.end
}
};
if let Some(type_annotation) = &mut pattern.type_annotation {
self.visit_ts_type_annotation(type_annotation);
}
self.convert_offset(span_end);
}

fn visit_binding_rest_element(&mut self, rest_element: &mut BindingRestElement<'a>) {
self.convert_offset(&mut rest_element.span.start);
self.visit_binding_pattern_kind(&mut rest_element.argument.kind);
if let Some(type_annotation) = &mut rest_element.argument.type_annotation {
self.visit_ts_type_annotation(type_annotation);
}
self.convert_offset(&mut rest_element.span.end);
}

fn visit_binding_property(&mut self, prop: &mut BindingProperty<'a>) {
self.convert_offset(&mut prop.span.start);
match (prop.shorthand, &mut prop.key, &mut prop.value) {
Expand Down
52 changes: 52 additions & 0 deletions tasks/ast_tools/src/generators/utf8_to_utf16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ impl Generator for Utf8ToUtf16ConverterGenerator {
/// 3. `TemplateLiteral`s, where `quasis` and `expressions` are interleaved.
/// 4. Decorators before `export` in `@dec export class C {}` / `@dec export default class {}`
/// have span before the start of `ExportNamedDeclaration` / `ExportDefaultDeclaration` span.
/// 5. `BindingPattern` where `type_annotation` has span within `BindingPatternKind`.
/// Except for `BindingRestElement`, where `type_annotation`'s span is after `BindingPatternKind`.
///
/// Define custom visitors for these types, which ensure `convert_offset` is always called with offsets
/// in ascending order.
Expand All @@ -57,6 +59,7 @@ fn generate(schema: &Schema, codegen: &Codegen) -> TokenStream {
"ExportSpecifier",
"WithClause",
"TemplateLiteral",
"BindingRestElement",
"Comment",
]
.map(|type_name| schema.type_names[type_name]);
Expand Down Expand Up @@ -114,6 +117,55 @@ fn generate(schema: &Schema, codegen: &Codegen) -> TokenStream {
self.convert_offset(&mut prop.span.end);
}

///@@line_break
fn visit_binding_pattern(&mut self, pattern: &mut BindingPattern<'a>) {
// Span of `type_annotation` is within the span of `kind`,
// so visit `type_annotation` before exiting span of `kind`
let span_end = match &mut pattern.kind {
BindingPatternKind::BindingIdentifier(ident) => {
self.convert_offset(&mut ident.span.start);
walk_mut::walk_binding_identifier(self, ident);
&mut ident.span.end
}
BindingPatternKind::ObjectPattern(obj_pattern) => {
self.convert_offset(&mut obj_pattern.span.start);
walk_mut::walk_object_pattern(self, obj_pattern);
&mut obj_pattern.span.end
}
BindingPatternKind::ArrayPattern(arr_pattern) => {
self.convert_offset(&mut arr_pattern.span.start);
walk_mut::walk_array_pattern(self, arr_pattern);
&mut arr_pattern.span.end
}
BindingPatternKind::AssignmentPattern(assign_pattern) => {
self.convert_offset(&mut assign_pattern.span.start);
walk_mut::walk_assignment_pattern(self, assign_pattern);
&mut assign_pattern.span.end
}
};

if let Some(type_annotation) = &mut pattern.type_annotation {
self.visit_ts_type_annotation(type_annotation);
}

self.convert_offset(span_end);
}

///@@line_break
fn visit_binding_rest_element(&mut self, rest_element: &mut BindingRestElement<'a>) {
// `BindingRestElement` contains a `BindingPattern`, but in this case span of
// `type_annotation` is after span of `kind`.
// So avoid calling `visit_binding_pattern` above.
self.convert_offset(&mut rest_element.span.start);

self.visit_binding_pattern_kind(&mut rest_element.argument.kind);
if let Some(type_annotation) = &mut rest_element.argument.type_annotation {
self.visit_ts_type_annotation(type_annotation);
}

self.convert_offset(&mut rest_element.span.end);
}

///@@line_break
fn visit_binding_property(&mut self, prop: &mut BindingProperty<'a>) {
self.convert_offset(&mut prop.span.start);
Expand Down
Loading