22
33use std:: slice:: Iter ;
44
5- use oxc_ast:: ast:: { Program , RegExpLiteral , TemplateElement } ;
5+ use oxc_ast:: ast:: {
6+ JSXText , PrivateIdentifier , Program , RegExpLiteral , StringLiteral , TemplateElement ,
7+ } ;
68use oxc_ast_visit:: utf8_to_utf16:: Utf8ToUtf16 ;
79use oxc_ast_visit:: { Visit , utf8_to_utf16:: Utf8ToUtf16Converter } ;
810use oxc_estree:: {
@@ -241,11 +243,18 @@ impl<'b, O: ESTreeTokenConfig, S: SequenceSerializer> JsonContext<'b, O, S> {
241243 self . serialize_safe_token ( token, token_type, value) ;
242244 }
243245
246+ /// Advance to the token at `start` and serialize it using provided `value` without JSON encoding.
244247 fn emit_safe_token_at ( & mut self , start : u32 , token_type : TokenType , value : & str ) {
245248 let token = self . advance_to ( start) ;
246249 self . serialize_safe_token ( token, token_type, value) ;
247250 }
248251
252+ /// Advance to the token at `start` and serialize it using raw source text with JSON encoding.
253+ fn emit_unsafe_token_at ( & mut self , start : u32 , token_type : TokenType ) {
254+ let token = self . advance_to ( start) ;
255+ self . emit_unsafe_token ( token, token_type) ;
256+ }
257+
249258 /// Serialize a token using its raw source text, with JSON encoding.
250259 ///
251260 /// Used for tokens whose values may contain backslashes, quotes, or control characters
@@ -365,19 +374,19 @@ impl<O: ESTreeTokenConfig, S: SequenceSerializer> Context for JsonContext<'_, O,
365374 self . emit_safe_token_at ( start, TokenType :: new ( "JSXIdentifier" ) , name) ;
366375 }
367376
368- /// Emit the token at `start` as ` PrivateIdentifier`.
369- fn emit_private_identifier_at ( & mut self , start : u32 , name : & str ) {
370- let token = self . advance_to ( start) ;
377+ /// Emit a ` PrivateIdentifier` token .
378+ fn emit_private_identifier ( & mut self , ident : & PrivateIdentifier < ' _ > ) {
379+ let token = self . advance_to ( ident . span . start ) ;
371380
372- // `identifier .name` has `#` stripped and escapes decoded by the parser, and is JSON-safe.
381+ // `ident .name` has `#` stripped and escapes decoded by the parser, and is JSON-safe.
373382 // Use it in most cases — if token is not marked as escaped, it's JSON-safe, so can skip JSON encoding.
374383 // When `self.is_js()` is `true`, token `value` should *always* be the unescaped version,
375384 // so can also use `name` from AST node and skip JSON encoding.
376385 // Only fall back to raw source text when the token contains escapes *and* decoding is disabled,
377386 // since escape sequences contain `\` which needs JSON escaping.
378387 // Escaped identifiers are extremely rare, so handle them in `#[cold]` branch.
379388 if self . is_js ( ) || !token. escaped ( ) {
380- self . serialize_safe_token ( token, TokenType :: new ( "PrivateIdentifier" ) , name) ;
389+ self . serialize_safe_token ( token, TokenType :: new ( "PrivateIdentifier" ) , & ident . name ) ;
381390 } else {
382391 #[ cold]
383392 #[ inline( never) ]
@@ -393,17 +402,19 @@ impl<O: ESTreeTokenConfig, S: SequenceSerializer> Context for JsonContext<'_, O,
393402 }
394403 }
395404
396- /// Emit the token at `start` as `JSXText`.
397- fn emit_jsx_text_at ( & mut self , start : u32 ) {
398- let token = self . advance_to ( start) ;
399- self . emit_unsafe_token ( token, TokenType :: new ( "JSXText" ) ) ;
405+ /// Emit a `StringLiteral` token.
406+ fn emit_string_literal ( & mut self , literal : & StringLiteral < ' _ > ) {
407+ self . emit_unsafe_token_at ( literal. span . start , TokenType :: new ( "String" ) ) ;
400408 }
401409
402- /// Emit the token at `start` as the specified token type,
403- /// where the token's `value` may not be JSON-safe.
404- fn emit_unsafe_token_at ( & mut self , start : u32 , token_type : TokenType ) {
405- let token = self . advance_to ( start) ;
406- self . emit_unsafe_token ( token, token_type) ;
410+ /// Emit a `StringLiteral` in a JSX attribute as `JSXText`.
411+ fn emit_string_literal_as_jsx_text ( & mut self , literal : & StringLiteral < ' _ > ) {
412+ self . emit_unsafe_token_at ( literal. span . start , TokenType :: new ( "JSXText" ) ) ;
413+ }
414+
415+ /// Emit a `JSXText` token.
416+ fn emit_jsx_text ( & mut self , jsx_text : & JSXText < ' _ > ) {
417+ self . emit_unsafe_token_at ( jsx_text. span . start , TokenType :: new ( "JSXText" ) ) ;
407418 }
408419
409420 /// Emit token for `RegExpLiteral`.
0 commit comments