fix(linter): noTemplateLiterals configuration in no_string_refs rule not working (#1063)

I didn't notice that the `noTemplateLiterals` configuration didn't work
due to the lack of test cases
This commit is contained in:
Dunqing 2023-10-25 22:03:37 -05:00 committed by GitHub
parent 5029dfde5a
commit 59660a53b8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 84 additions and 19 deletions

View file

@ -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
}
@ -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 }
}
@ -229,7 +229,7 @@ fn test() {
}
});
",
Some(serde_json::json!([{ "noTemplateLiterals": true }])),
Some(serde_json::json!({ "noTemplateLiterals": true })),
),
(
"
@ -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.PureComponent {
componentDidMount() {
class Hello extends React.Component {
componentDidMount() {
var component = this.refs.hello;
}
}
}
",
None,
),
(
"
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 })),
),
];

View file

@ -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() {
@ -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() {
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 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 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