Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: noCommentText does not work when any other text is a child(#3298) #3446

Merged
merged 7 commits into from
Jul 23, 2024

Conversation

ryo-ebata
Copy link
Contributor

@ryo-ebata ryo-ebata commented Jul 15, 2024

Summary

Changed logic to judge by regex, considering the case where a comment is inserted in the middle of a paragraph.

Expanded test cases and added explanation of rule.

The quick fix feature has also been developed to fix only the commented part.

Closes #3298

Test Plan

Invalid Case

Add comments, line breaks, etc. in sentences.

<>
    <div>// comment</div>
    <div>/* comment */</div>
    <div>/** comment */</div>
    <div>text /* comment */</div>
    <div>/* comment */ text</div>
    <div>
        text
        // comment
    </div>
    <div>
        /* comment */
        text
    </div>
    <div>
        // comment
        text
    </div>
    <div>
        /* comment
        comment */
    </div>
</>

Valid Case

<>
    <div>{/* comment */}</div>
    <div>{/** comment */}</div>
    <div className={"cls" /* comment */}></div>
    <div>text {/* comment */}</div>
    <div>{/* comment */} text</div>
</>

@github-actions github-actions bot added A-Linter Area: linter L-JavaScript Language: JavaScript and super languages labels Jul 15, 2024
Copy link

codspeed-hq bot commented Jul 15, 2024

CodSpeed Performance Report

Merging #3446 will not alter performance

Comparing ryo-ebata:fix/#3298 (8c470f4) with main (6f8eade)

Summary

✅ 104 untouched benchmarks

@ryo-ebata ryo-ebata marked this pull request as ready for review July 15, 2024 18:54
let node_text = node.text().to_string();

// Replace the comments with JSX comments
let new_jsx_value = COMMENT_REGEX.replace_all(&node_text, |caps: &regex::Captures| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just my preference (not a suggestion) for readability. What do you think?

let new_jsx_value = COMMENT_REGEX.replace_all(&node_text, |caps: &regex::Captures| {
    let comment = caps[0]
        .trim_start_matches("//")
        .trim_start_matches("/*")
        .trim_end_matches("*/")
        .trim();
    format!("{{/*{}*/}}", comment)
});

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chansuke
To accommodate Doc comments(like /** example */), the code has been changed as follows

let new_jsx_value = COMMENT_REGEX.replace_all(&node_text, |caps: &regex::Captures| {
    let comment = caps[0].trim();
    match comment {
        c if c.starts_with("//") => format!("{{/* {} */}}", c[2..].trim()),
        c if c.starts_with("/**") => format!("{{/** {} */}}", &c[3..c.len() - 2].trim()),
        c => format!("{{/* {} */}}", &c[2..c.len() - 2].trim()),
    }
});

If you have any suggestions for a better code, I welcome them!

Comment on lines 120 to 121
5 │ - ····<div>text·/*·comment·*/</div>
5 │ + ····<div>text·{/*comment*/}</div>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous suggestion was able to keep the spaces, but now we remove leading and trailing spaces between the word "comment".

Before: const·a4·=·<div>{/*·comment·*/}</div>;
Now: <div>text·{/*comment*/}</div>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ematipico
thanks your check, I had missed it...
I fixed in this commit!
9c6f601

@ematipico ematipico merged commit 8ed14cd into biomejs:main Jul 23, 2024
12 checks passed
@ryo-ebata ryo-ebata deleted the fix/#3298 branch July 23, 2024 09:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Linter Area: linter L-JavaScript Language: JavaScript and super languages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

💅 noCommentText does not work when any other text is a child
3 participants