Skip to content

Conversation

@samkearney
Copy link

This issue was seen when using Tsify's derive macros inside a declarative macro_rules! macro. For example:

use tsify::Tsify;

// Helper macro that adds derives
macro_rules! my_types {
    ($($decl:item)+) => {$(
        #[derive(Tsify)]
        $decl
    )+};
}

/// A comment
#[derive(Tsify)]
struct Direct {
    x: i32,
}

my_types! {
  /// A comment
  struct Macro {
      x: i32,
  }
}

fn main() {
    println!("{}", Direct::DECL);
    // /**
    //  * A comment
    //  */
    // export interface Direct { ... }

    println!("{}", Macro::DECL);
    // /**
    //  *r" A comment
    //  */
    // export interface Macro { ... }
}

In the declarative macro case, the generated TypeScript comments contain raw string literal syntax artifacts like r", r#", and "#.

It seems like when tokens pass through a declarative macro, the compiler may re-serialize string literals in doc attributes using raw string syntax (r"..." or r#"..."#). This then comes out in the source code representation that's produced by lit.to_string() in the extract_doc_comments function. Re-parsing the literal as a LitStr fixes the issue and removes the need to strip quotes later.

Makes the approach to parsing doc comments more robust to handle
situations where raw string literal artifacts are present in the parsed
token tree, which can happen when tsify macros are used inside a
declarative macro
@siefkenj
Copy link
Collaborator

@samkearney Thanks for this fix! It looks like the test failures might be due to a change in wasmbindgen and not you...Would you be able to rebuild the test artifacts?

@samkearney
Copy link
Author

@siefkenj sorry, looks like there were some more test failures lurking behind those initial ones - I should have time to take a look at that in the next day or two

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants