-
Notifications
You must be signed in to change notification settings - Fork 174
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("/*") { | ||
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, | ||
} | ||
], | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
*/
?There was a problem hiding this comment.
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.