Skip to content

Commit

Permalink
fix(linter): eslint-plugin-unicorn prefer-spread wrong linter suggest…
Browse files Browse the repository at this point in the history
…ion on variables of type string (#5265)

fixes #5248.

Similar issue found in here:
sindresorhus/eslint-plugin-unicorn#1147
  • Loading branch information
Arian94 authored Aug 28, 2024
1 parent 582ce9e commit 76e86f8
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions crates/oxc_linter/src/rules/unicorn/prefer_spread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use phf::phf_set;

use crate::{context::LintContext, rule::Rule, AstNode};
use crate::{ast_util, context::LintContext, rule::Rule, AstNode};

fn prefer_spread_diagnostic(span: Span, x1: &str) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Prefer the spread operator (`...`) over {x1}"))
Expand Down Expand Up @@ -83,7 +83,7 @@ impl Rule for PreferSpread {
}
// `array.concat()`
"concat" => {
if is_not_array(member_expr.object().without_parenthesized()) {
if is_not_array(member_expr.object().without_parenthesized(), ctx) {
return;
}

Expand Down Expand Up @@ -181,7 +181,7 @@ const IGNORED_SLICE_CALLEE: phf::Set<&'static str> = phf_set! {
"this",
};

fn is_not_array(expr: &Expression) -> bool {
fn is_not_array(expr: &Expression, ctx: &LintContext) -> bool {
if matches!(
expr.without_parenthesized(),
Expression::TemplateLiteral(_) | Expression::BinaryExpression(_)
Expand All @@ -203,7 +203,20 @@ fn is_not_array(expr: &Expression) -> bool {
}

let ident = match expr.without_parenthesized() {
Expression::Identifier(ident) => ident.name.as_str(),
Expression::Identifier(ident) => {
if let Some(symbol_id) = ast_util::get_symbol_id_of_variable(ident, ctx) {
let symbol_table = ctx.semantic().symbols();
let node = ctx.nodes().get_node(symbol_table.get_declaration(symbol_id));

if let AstKind::VariableDeclarator(variable_declarator) = node.kind() {
if let Some(ref_expr) = &variable_declarator.init {
return is_not_array(ref_expr, ctx);
}
}
}

ident.name.as_str()
}
expr @ match_member_expression!(Expression) => {
if let Some(v) = expr.to_member_expression().static_property_name() {
v
Expand Down Expand Up @@ -324,6 +337,8 @@ fn test() {
r#""".split(string)"#,
r"string.split()",
r#"string.notSplit("")"#,
r#"const x = "foo"; x.concat(x);"#,
r#"const y = "foo"; const x = y; x.concat(x);"#,
];

let fail = vec![
Expand Down

0 comments on commit 76e86f8

Please sign in to comment.