Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .changeset/slick-paths-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
"@biomejs/biome": patch
---

Added the nursery rule [`noJsxLeakedComment`](https://biomejs.dev/linter/rules/no-jsx-leaked-comment), which prevents comments from being inserted as JSX text nodes.

**Invalid**:

```jsx
<div>
// This will be rendered as text
/* This will also be rendered as text */
</div>
```
24 changes: 24 additions & 0 deletions crates/biome_cli/src/execute/migrate/eslint_any_rule_to_biome.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/biome_configuration/src/analyzer/linter/rules.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/biome_configuration/src/generated/domain_selector.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/biome_diagnostics_categories/src/categories.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

130 changes: 130 additions & 0 deletions crates/biome_js_analyze/src/lint/nursery/no_jsx_leaked_comment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
use biome_analyze::{
Ast, Rule, RuleDiagnostic, RuleDomain, RuleSource, context::RuleContext, declare_lint_rule,
};
Comment thread
coderabbitai[bot] marked this conversation as resolved.
use biome_console::markup;
use biome_diagnostics::Severity;
use biome_js_syntax::JsxText;
use biome_rowan::{TextRange, TextSize};
use biome_rule_options::no_jsx_leaked_comment::NoJsxLeakedCommentOptions;

declare_lint_rule! {
/// Prevent comments from being inserted as JSX text nodes.
///
/// Comments inside JSX children must be wrapped in braces, otherwise they are rendered as text.
///
/// ## Examples
///
/// ### Invalid
///
/// ```jsx,expect_diagnostic
/// <div>// comment</div>
/// ```
///
/// ```jsx,expect_diagnostic
/// <div>/* comment */</div>
/// ```
///
/// ### Valid
///
/// ```jsx
/// <div>{/* comment */}</div>
/// ```
///
pub NoJsxLeakedComment {
version: "next",
name: "noJsxLeakedComment",
language: "jsx",
domains: &[RuleDomain::Qwik, RuleDomain::React],
sources: &[RuleSource::EslintReact("jsx-no-comment-textnodes").same(), RuleSource::EslintReactJsx("no-comment-textnodes").same(), RuleSource::EslintReactXyz("jsx-no-comment-textnodes").same()],
recommended: false,
severity: Severity::Warning,
}
}

impl Rule for NoJsxLeakedComment {
type Query = Ast<JsxText>;
type State = TextRange;
type Signals = Option<Self::State>;
type Options = NoJsxLeakedCommentOptions;

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let node = ctx.query();
let jsx_value = node.value_token().ok()?;
let node_range_start = jsx_value.text_range().start();
let jsx_value = jsx_value.text();
let bytes = jsx_value.as_bytes();
let mut bytes_iter = jsx_value.bytes().enumerate();

let is_comment_start = |index: usize| -> bool {
if index == 0 {
return true;
}
let prev_byte = bytes.get(index - 1);
// Allow comment if preceded by whitespace, but not if it's "://" (URL)
prev_byte.is_some_and(|&b| {
b.is_ascii_whitespace() && !(b == b':' && bytes.get(index + 1) == Some(&b'/'))
})
};

while let Some((index, byte)) = bytes_iter.next() {
if byte != b'/' {
continue;
}

match bytes_iter.next()? {
(_, b'/') => {
if is_comment_start(index) {
let end = bytes_iter
.find(|(_, current)| current == &b'\n')
.map_or(bytes.len(), |(idx, _)| idx);
return Some(TextRange::new(
node_range_start + TextSize::from(index as u32),
node_range_start + TextSize::from(end as u32),
));
}
}
(_, b'*') => {
if is_comment_start(index) {
let comment_start = index;
let mut end = bytes.len(); // Default to end of text if no closing found

while let Some((_, byte)) = bytes_iter.next() {
if byte != b'*' {
continue;
}

let Some((close_index, b'/')) = bytes_iter.next() else {
continue;
};

end = close_index + 1;
break;
}

return Some(TextRange::new(
node_range_start + TextSize::from(comment_start as u32),
node_range_start + TextSize::from(end as u32),
));
}
}
_ => {}
}
}

None
}

fn diagnostic(_ctx: &RuleContext<Self>, state: &Self::State) -> Option<RuleDiagnostic> {
Some(RuleDiagnostic::new(
rule_category!(),
state,
markup! {
"Unexpected comment syntax in a text node."
},
).note(
markup! {
"This comment syntax will be rendered as text. Wrap "<Emphasis>"comments"</Emphasis>" inside JSX children with "<Emphasis>"braces"</Emphasis>" or remove them entirely."
}
))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ declare_lint_rule! {
recommended: false,
fix_kind: FixKind::Unsafe,
severity: Severity::Warning,
domains: &[RuleDomain::React],
domains: &[RuleDomain::Qwik, RuleDomain::React],
sources: &[RuleSource::EslintReactJsx("no-leaked-dollar").same(), RuleSource::EslintReactXyz("jsx-no-leaked-dollar").same()],
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* should generate diagnostics */
const Invalid1 = () => <div>// invalid</div>;
const Invalid2 = () => <>// invalid</>;
const Invalid3 = () => <div>/* invalid */</div>;
const Invalid4 = () => (
<div>
// invalid
</div>
);
const Invalid5 = () => (
<div>
abcdef
/* invalid */
foo
</div>
);
const Invalid6 = () => (
<div>
{'abcdef'}
// invalid
{'foo'}
</div>
);
const Invalid7 = () => <span>/*</span>;
Loading
Loading