swc-project / swc

Rust-based platform for the Web
https://swc.rs
Apache License 2.0
31.25k stars 1.23k forks source link

Cannot get KV trailing comments #7152

Open antoniomuso opened 1 year ago

antoniomuso commented 1 year ago

Describe the bug

I cannot get comments like:

{
        amount: property.bla,
        icon: {
          name: "something", // My comment to take
          spriteUrl: `url`,
        },
},

Using a Visitor like this:

impl<C: Comments + Debug> Visit for CommentsExtractor<C> {
    noop_visit_type!();

    // Here we get all comments
    fn visit_span(&mut self, span: &swc_core::common::Span) {
        let comments_opt = self.extractor.extract_leading_comments_span(span);

        if let Some(comments) = comments_opt {
            for c in comments {
                self.parsed_comments.insert(c);
            }
        }

        let comments_opt = self.extractor.extract_trailing_comments_span(span);

        if let Some(comments) = comments_opt {
            for c in comments {
                self.parsed_comments.insert(c);
            }
        }
    }
}

the extract_leading_comments_span and extract_trailing_comments_span use respectively these two methods:

let comments_opt = self.comments.get_leading(span.lo());
let comments_opt = self.comments.get_trailing(span.hi());

I note that the comment is contained in the Comments data structure but using these two methods I cannon get it.

Input code

{
        amount: property.bla,
        icon: {
          name: "something", // My comment to take
          spriteUrl: `url`,
        },
},

Expected behavior

The Comment should be getting as a Trailing comment in KV span.

Actual behavior

The commit is not returned

Version

swc_core = 0.69.17

Additional context

I can get the comment only If the comment is as follows:

{
        amount: property.bla,
        icon: {
          name: "something", /* My comment to take */
          spriteUrl: `url`,
        },
},
antoniomuso commented 1 year ago

I did some tests, and I found that if I add +1 to the span as follow:

        let mut hi = span.hi();
        hi.0 += 1;

        // Get trailing comments
        let comments_opt = self.comments.get_trailing(hi);

I get the comment.