From 282420c0fc008407a61da58509d4ca129d269670 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Wed, 14 May 2025 01:22:54 +0000 Subject: [PATCH] fix(ast_visit): fix visitation order for `TSTemplateLiteralType` in `Utf8ToUtf16Converter` (#11007) Fix visitation order in `Utf8ToUtf16Converter` to process span offsets in ascending order in `TSTemplateLiteralType`. `quasis` and `types` are interleaved, so to visit them in source order, need to visit `quasis[0]`, `types[0]`, `quasis[1]`, `types[1]`, `quasis[2]`, etc. --- .../src/generated/utf8_to_utf16_converter.rs | 16 ++++++++++------ tasks/ast_tools/src/generators/utf8_to_utf16.rs | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/crates/oxc_ast_visit/src/generated/utf8_to_utf16_converter.rs b/crates/oxc_ast_visit/src/generated/utf8_to_utf16_converter.rs index 41afe92ea1907..2664df3afe630 100644 --- a/crates/oxc_ast_visit/src/generated/utf8_to_utf16_converter.rs +++ b/crates/oxc_ast_visit/src/generated/utf8_to_utf16_converter.rs @@ -989,12 +989,6 @@ impl<'a> VisitMut<'a> for Utf8ToUtf16Converter<'_> { self.convert_offset(&mut it.span.end); } - fn visit_ts_template_literal_type(&mut self, it: &mut TSTemplateLiteralType<'a>) { - self.convert_offset(&mut it.span.start); - walk_mut::walk_ts_template_literal_type(self, it); - self.convert_offset(&mut it.span.end); - } - fn visit_ts_as_expression(&mut self, it: &mut TSAsExpression<'a>) { self.convert_offset(&mut it.span.start); walk_mut::walk_ts_as_expression(self, it); @@ -1234,6 +1228,16 @@ impl<'a> VisitMut<'a> for Utf8ToUtf16Converter<'_> { self.visit_template_element(lit.quasis.last_mut().unwrap()); self.convert_offset(&mut lit.span.end); } + + fn visit_ts_template_literal_type(&mut self, lit: &mut TSTemplateLiteralType<'a>) { + self.convert_offset(&mut lit.span.start); + for (quasi, ts_type) in lit.quasis.iter_mut().zip(&mut lit.types) { + self.visit_template_element(quasi); + self.visit_ts_type(ts_type); + } + self.visit_template_element(lit.quasis.last_mut().unwrap()); + self.convert_offset(&mut lit.span.end); + } } impl Utf8ToUtf16Converter<'_> { diff --git a/tasks/ast_tools/src/generators/utf8_to_utf16.rs b/tasks/ast_tools/src/generators/utf8_to_utf16.rs index c2495535605a8..ff5c505c5bd5d 100644 --- a/tasks/ast_tools/src/generators/utf8_to_utf16.rs +++ b/tasks/ast_tools/src/generators/utf8_to_utf16.rs @@ -37,6 +37,7 @@ impl Generator for Utf8ToUtf16ConverterGenerator { /// 1. Types where a shorthand syntax means 2 nodes have same span e.g. `const {x} = y;`, `export {x}`. /// 2. `WithClause`, where `IdentifierName` for `with` keyword has span outside of the `WithClause`. /// 3. `TemplateLiteral`s, where `quasis` and `expressions` are interleaved. +/// Ditto `TSTemplateLiteralType`s where `quasis` and `types` 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`. @@ -59,6 +60,7 @@ fn generate(schema: &Schema, codegen: &Codegen) -> TokenStream { "ExportSpecifier", "WithClause", "TemplateLiteral", + "TSTemplateLiteralType", "BindingRestElement", "Comment", ] @@ -300,6 +302,20 @@ fn generate(schema: &Schema, codegen: &Codegen) -> TokenStream { self.convert_offset(&mut lit.span.end); } + + ///@@line_break + fn visit_ts_template_literal_type(&mut self, lit: &mut TSTemplateLiteralType<'a>) { + self.convert_offset(&mut lit.span.start); + + // Visit `quasis` and `types` in source order. The two `Vec`s are interleaved. + for (quasi, ts_type) in lit.quasis.iter_mut().zip(&mut lit.types) { + self.visit_template_element(quasi); + self.visit_ts_type(ts_type); + } + self.visit_template_element(lit.quasis.last_mut().unwrap()); + + self.convert_offset(&mut lit.span.end); + } } ///@@line_break