From d0dadd364c63899c0bbe3cd8a2db957db6b44c11 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Wed, 25 Oct 2023 23:59:52 +0800 Subject: [PATCH] fix(linter): noTemplateLiterals configuration in no_string_refs rule not working --- .../src/rules/react/no_string_refs.rs | 50 +++++++++++------ .../src/snapshots/no_string_refs.snap | 53 +++++++++++++++++-- 2 files changed, 84 insertions(+), 19 deletions(-) diff --git a/crates/oxc_linter/src/rules/react/no_string_refs.rs b/crates/oxc_linter/src/rules/react/no_string_refs.rs index 4d10868a8e7cc..62ed6a158e87d 100644 --- a/crates/oxc_linter/src/rules/react/no_string_refs.rs +++ b/crates/oxc_linter/src/rules/react/no_string_refs.rs @@ -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
Hello {this.props.name}
; + } + }); + ", + 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
Hello {this.props.name}
; } - } - ", - Some(serde_json::json!([{ "noTemplateLiterals": true }])), + } + ", + Some(serde_json::json!({ "noTemplateLiterals": true })), ), ]; diff --git a/crates/oxc_linter/src/snapshots/no_string_refs.snap b/crates/oxc_linter/src/snapshots/no_string_refs.snap index a211552826727..9e92c80711f0a 100644 --- a/crates/oxc_linter/src/snapshots/no_string_refs.snap +++ b/crates/oxc_linter/src/snapshots/no_string_refs.snap @@ -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
Hello {this.props.name}
; + · ───────────── + 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
Hello {this.props.name}
; + · ───────────────────── + 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
Hello {this.props.name}
; + · ───────────────────── + 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
Hello {this.props.name}
; + · ───────────────────── + 8 │ } + ╰──── + help: Using reference callback instead +