-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add jsx-no-comment-text-nodes rule
- Loading branch information
1 parent
5a7ce21
commit 5f4940a
Showing
4 changed files
with
111 additions
and
0 deletions.
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
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::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) -> &'static [&'static str] { | ||
&["react", "jsx", "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