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

feat: add jsx-no-comment-text-nodes rule #1352

Merged
merged 4 commits into from
Dec 10, 2024
Merged
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 docs/rules/jsx_no_comment_text_nodes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Prevent comment strings being accidentally passed as text in JSX.

### Invalid:

```tsx
const foo = <div>// comment</div>;
const foo = <div>/* comment */</div>;
```

### Valid:

```tsx
const foo = <div>{/* comment */}</div>;
```
1 change: 1 addition & 0 deletions schemas/rules.v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"jsx-curly-braces",
"jsx-key",
"jsx-no-children-prop",
"jsx-no-comment-text-nodes",
"jsx-no-danger-with-children",
"jsx-no-duplicate-props",
"jsx-no-useless-fragment",
Expand Down
2 changes: 2 additions & 0 deletions src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub mod jsx_boolean_value;
pub mod jsx_curly_braces;
pub mod jsx_key;
pub mod jsx_no_children_prop;
pub mod jsx_no_comment_text_nodes;
pub mod jsx_no_danger_with_children;
pub mod jsx_no_duplicate_props;
pub mod jsx_no_useless_fragment;
Expand Down Expand Up @@ -274,6 +275,7 @@ fn get_all_rules_raw() -> Vec<Box<dyn LintRule>> {
Box::new(jsx_curly_braces::JSXCurlyBraces),
Box::new(jsx_key::JSXKey),
Box::new(jsx_no_children_prop::JSXNoChildrenProp),
Box::new(jsx_no_comment_text_nodes::JSXNoCommentTextNodes),
Box::new(jsx_no_danger_with_children::JSXNoDangerWithChildren),
Box::new(jsx_no_duplicate_props::JSXNoDuplicateProps),
Box::new(jsx_no_useless_fragment::JSXNoUselessFragment),
Expand Down
87 changes: 87 additions & 0 deletions src/rules/jsx_no_comment_text_nodes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use super::{Context, LintRule};
use crate::handler::{Handler, Traverse};
use crate::tags::Tags;
use crate::{tags, Program};
use deno_ast::view::JSXText;
use deno_ast::SourceRanged;

#[derive(Debug)]
pub struct JSXNoCommentTextNodes;

const CODE: &str = "jsx-no-comment-text-nodes";

impl LintRule for JSXNoCommentTextNodes {
fn tags(&self) -> Tags {
&[tags::RECOMMENDED, tags::REACT, tags::JSX, tags::FRESH]
}

fn code(&self) -> &'static str {
CODE
}

fn lint_program_with_ast_view(
&self,
context: &mut Context,
program: Program,
) {
JSXNoCommentTextNodesHandler.traverse(program, context);
}

#[cfg(feature = "docs")]
fn docs(&self) -> &'static str {
include_str!("../../docs/rules/jsx_no_comment_text_nodes.md")
}
}

const MESSAGE: &str =
"Comments inside children should be placed inside curly braces";

struct JSXNoCommentTextNodesHandler;

impl Handler for JSXNoCommentTextNodesHandler {
fn jsx_text(&mut self, node: &JSXText, ctx: &mut Context) {
let value = &node.inner.value;
if value.starts_with("//") || value.starts_with("/*") {
Copy link
Member

Choose a reason for hiding this comment

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

Should these be stricter checks? Eg. ensuring there are only single lines or for block comments that it ends with */?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is too difficult to do, because of expression nodes. In practice this is imo good enough.

a = <p>/* foo {text}</p>

ctx.add_diagnostic(node.range(), CODE, MESSAGE);
}
}
}

// most tests are taken from ESlint, commenting those
// requiring code path support
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn jsx_no_comment_text_nodes_valid() {
assert_lint_ok! {
JSXNoCommentTextNodes,
filename: "file:///foo.jsx",
// non derived classes.
r#"<div>{/* comment */}</div>"#,
};
}

#[test]
fn jsx_no_comment_text_nodes_invalid() {
assert_lint_err! {
JSXNoCommentTextNodes,
filename: "file:///foo.jsx",
"<div>// comment</div>": [
{
col: 5,
message: MESSAGE,
}
],
r#"<div>/* comment */</div>"#: [
{
col: 5,
message: MESSAGE,
}
],
};
}
}
10 changes: 10 additions & 0 deletions www/static/docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,16 @@
"fresh"
]
},
{
"code": "jsx-no-comment-text-nodes",
"docs": "Prevent comment strings being accidentally passed as text in JSX.\n\n### Invalid:\n\n```tsx\nconst foo = <div>// comment</div>;\nconst foo = <div>/* comment */</div>;\n```\n\n### Valid:\n\n```tsx\nconst foo = <div>{/* comment */}</div>;\n```\n",
"tags": [
"recommended",
"react",
"jsx",
"fresh"
]
},
{
"code": "jsx-no-danger-with-children",
"docs": "Using JSX children together with `dangerouslySetInnerHTML` is invalid as they\nwill be ignored.\n\n### Invalid:\n\n```tsx\n<div dangerouslySetInnerHTML={{ __html: \"<h1>hello</h1>\" }}>\n <h1>this will never be rendered</h1>\n</div>;\n```\n\n### Valid:\n\n```tsx\n<div dangerouslySetInnerHTML={{ __html: \"<h1>hello</h1>\" }} />;\n```\n",
Expand Down
Loading