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(linter): noTemplateLiterals configuration in no_string_refs rule not working #1063

Merged
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
50 changes: 35 additions & 15 deletions crates/oxc_linter/src/rules/react/no_string_refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fn contains_string_literal(
) -> bool {
if let JSXExpression::Expression(expr) = &expr_container.expression {
matches!(expr, Expression::StringLiteral(_))
|| no_template_literals && matches!(expr, Expression::TemplateLiteral(_))
|| (no_template_literals && matches!(expr, Expression::TemplateLiteral(_)))
} else {
false
}
Expand All @@ -106,7 +106,7 @@ fn is_literal_ref_attribute(attr: &JSXAttribute, no_template_literals: bool) ->
impl Rule for NoStringRefs {
fn from_configuration(value: serde_json::Value) -> Self {
let no_template_literals =
value.get("allowExpressions").and_then(serde_json::Value::as_bool).unwrap_or(false);
value.get("noTemplateLiterals").and_then(serde_json::Value::as_bool).unwrap_or(false);

Self { no_template_literals }
}
Expand Down Expand Up @@ -229,7 +229,7 @@ fn test() {
}
});
",
Some(serde_json::json!([{ "noTemplateLiterals": true }])),
Some(serde_json::json!({ "noTemplateLiterals": true })),
),
(
"
Expand All @@ -242,30 +242,50 @@ fn test() {
}
});
",
Some(serde_json::json!([{ "noTemplateLiterals": true }])),
Some(serde_json::json!({ "noTemplateLiterals": true })),
),
(
"
class Hello extends React.Component {
componentDidMount() {
var Hello = createReactClass({
render: function() {
return <div ref={`hello${index}`}>Hello {this.props.name}</div>;
}
});
",
Some(serde_json::json!({ "noTemplateLiterals": true })),
),
(
"
class Hello extends React.Component {
componentDidMount() {
var component = this.refs.hello;
}
}
",
None,
),
(
"
class Hello extends React.Component {
componentDidMount() {
var component = this.refs.hello;
}
}
",
}
}
",
None,
),
(
"
class Hello extends React.PureComponent {
componentDidMount() {
class Hello extends React.PureComponent {
componentDidMount() {
var component = this.refs.hello;
}
}
render() {
return <div ref={`hello${index}`}>Hello {this.props.name}</div>;
}
}
",
Some(serde_json::json!([{ "noTemplateLiterals": true }])),
}
",
Some(serde_json::json!({ "noTemplateLiterals": true })),
),
];

Expand Down
53 changes: 49 additions & 4 deletions crates/oxc_linter/src/snapshots/no_string_refs.snap
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ expression: no_string_refs
╰────
help: Using this.xxx instead of this.refs.xxx

⚠ eslint-plugin-react(no-string-refs): Using string literals in ref attributes is deprecated.
╭─[no_string_refs.tsx:6:1]
6 │ render: function() {
7 │ return <div ref={`hello`}>Hello {this.props.name}</div>;
· ─────────────
8 │ }
╰────
help: Using reference callback instead

⚠ eslint-plugin-react(no-string-refs): Using this.refs is deprecated.
╭─[no_string_refs.tsx:3:1]
3 │ componentDidMount: function() {
Expand All @@ -65,22 +74,58 @@ expression: no_string_refs
╰────
help: Using this.xxx instead of this.refs.xxx

⚠ eslint-plugin-react(no-string-refs): Using string literals in ref attributes is deprecated.
╭─[no_string_refs.tsx:6:1]
6 │ render: function() {
7 │ return <div ref={`hello${index}`}>Hello {this.props.name}</div>;
· ─────────────────────
8 │ }
╰────
help: Using reference callback instead

⚠ eslint-plugin-react(no-string-refs): Using string literals in ref attributes is deprecated.
╭─[no_string_refs.tsx:3:1]
3 │ render: function() {
4 │ return <div ref={`hello${index}`}>Hello {this.props.name}</div>;
· ─────────────────────
5 │ }
╰────
help: Using reference callback instead

⚠ eslint-plugin-react(no-string-refs): Using this.refs is deprecated.
╭─[no_string_refs.tsx:3:1]
3 │ componentDidMount() {
4 │ var component = this.refs.hello;
· ─────────
5 │ }
╰────
help: Using this.xxx instead of this.refs.xxx

⚠ eslint-plugin-react(no-string-refs): Using this.refs is deprecated.
╭─[no_string_refs.tsx:3:1]
3 │ componentDidMount() {
3 │ componentDidMount() {
4 │ var component = this.refs.hello;
· ─────────
5 │ }
5 │ }
╰────
help: Using this.xxx instead of this.refs.xxx

⚠ eslint-plugin-react(no-string-refs): Using this.refs is deprecated.
╭─[no_string_refs.tsx:3:1]
3 │ componentDidMount() {
3 │ componentDidMount() {
4 │ var component = this.refs.hello;
· ─────────
5 │ }
5 │ }
╰────
help: Using this.xxx instead of this.refs.xxx

⚠ eslint-plugin-react(no-string-refs): Using string literals in ref attributes is deprecated.
╭─[no_string_refs.tsx:6:1]
6 │ render() {
7 │ return <div ref={`hello${index}`}>Hello {this.props.name}</div>;
· ─────────────────────
8 │ }
╰────
help: Using reference callback instead


Loading