|
| 1 | +use std::cmp::max; |
| 2 | + |
| 3 | +use oxc_allocator::Box; |
| 4 | +use oxc_ast::ast::Expression; |
| 5 | +use oxc_ast::ast::ObjectExpression; |
| 6 | +use oxc_ast::ast::ObjectPropertyKind; |
| 7 | +use oxc_ast::ast::PropertyKind; |
| 8 | +use oxc_ast::AstKind; |
| 9 | +use oxc_diagnostics::OxcDiagnostic; |
| 10 | +use oxc_macros::declare_oxc_lint; |
| 11 | +use oxc_span::GetSpan; |
| 12 | +use oxc_span::Span; |
| 13 | + |
| 14 | +use crate::{ast_util::is_method_call, context::LintContext, rule::Rule, AstNode}; |
| 15 | + |
| 16 | +fn prefer_object_spread_diagnostic(span: Span, for_use_literal: bool) -> OxcDiagnostic { |
| 17 | + let help_message = if for_use_literal { |
| 18 | + "Use an object literal instead of `Object.assign`. eg: `{ foo: bar }`." |
| 19 | + } else { |
| 20 | + "Use an object spread instead of `Object.assign` eg: `{ ...foo }`." |
| 21 | + }; |
| 22 | + OxcDiagnostic::warn("Disallow using `Object.assign` with an object literal as the first argument and prefer the use of object spread instead") |
| 23 | + .with_help(help_message) |
| 24 | + .with_label(span) |
| 25 | +} |
| 26 | + |
| 27 | +#[derive(Debug, Default, Clone)] |
| 28 | +pub struct PreferObjectSpread; |
| 29 | + |
| 30 | +declare_oxc_lint!( |
| 31 | + /// ### What it does |
| 32 | + /// Disallow using `Object.assign` with an object literal as the first argument and prefer the use of object spread instead |
| 33 | + /// |
| 34 | + /// ### Why is this bad? |
| 35 | + /// When `Object.assign` is called using an object literal as the first argument, this rule requires using the object spread syntax instead. This rule also warns on cases where an `Object.assign` call is made using a single argument that is an object literal, in this case, the `Object.assign` call is not needed. |
| 36 | + /// |
| 37 | + /// ### Examples |
| 38 | + /// |
| 39 | + /// Examples of **incorrect** code for this rule: |
| 40 | + /// ```js |
| 41 | + /// Object.assign({}, foo); |
| 42 | + /// |
| 43 | + /// Object.assign({}, {foo: 'bar'}); |
| 44 | + /// |
| 45 | + /// Object.assign({ foo: 'bar'}, baz); |
| 46 | + /// |
| 47 | + /// Object.assign({}, baz, { foo: 'bar' }); |
| 48 | + /// |
| 49 | + /// Object.assign({}, { ...baz }); |
| 50 | + /// |
| 51 | + /// // Object.assign with a single argument that is an object literal |
| 52 | + /// Object.assign({}); |
| 53 | + /// |
| 54 | + /// Object.assign({ foo: bar }); |
| 55 | + /// ``` |
| 56 | + /// |
| 57 | + /// Examples of **correct** code for this rule: |
| 58 | + /// ```js |
| 59 | + /// ({ ...foo }); |
| 60 | + /// |
| 61 | + /// ({ ...baz, foo: 'bar' }); |
| 62 | + /// |
| 63 | + /// // Any Object.assign call without an object literal as the first argument |
| 64 | + /// Object.assign(foo, { bar: baz }); |
| 65 | + /// |
| 66 | + /// Object.assign(foo, bar); |
| 67 | + /// |
| 68 | + /// Object.assign(foo, { bar, baz }); |
| 69 | + /// |
| 70 | + /// Object.assign(foo, { ...baz }); |
| 71 | + /// ``` |
| 72 | + PreferObjectSpread, |
| 73 | + eslint, |
| 74 | + style, |
| 75 | + fix |
| 76 | +); |
| 77 | + |
| 78 | +impl Rule for PreferObjectSpread { |
| 79 | + fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { |
| 80 | + let AstKind::CallExpression(call_expr) = node.kind() else { |
| 81 | + return; |
| 82 | + }; |
| 83 | + |
| 84 | + if !is_method_call(call_expr, None, Some(&["assign"]), Some(1), None) { |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + let Some(callee) = call_expr.callee.as_member_expression() else { |
| 89 | + return; |
| 90 | + }; |
| 91 | + |
| 92 | + match callee.object().get_inner_expression() { |
| 93 | + Expression::Identifier(ident) => { |
| 94 | + if ident.name != "Object" || !ctx.semantic().is_reference_to_global_variable(ident) |
| 95 | + { |
| 96 | + return; |
| 97 | + } |
| 98 | + } |
| 99 | + Expression::StaticMemberExpression(member_expr) => { |
| 100 | + if let Expression::Identifier(ident) = member_expr.object.get_inner_expression() { |
| 101 | + if ident.name != "globalThis" |
| 102 | + || !ctx.semantic().is_reference_to_global_variable(ident) |
| 103 | + { |
| 104 | + return; |
| 105 | + } |
| 106 | + } else { |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + if member_expr.property.name != "Object" { |
| 111 | + return; |
| 112 | + } |
| 113 | + } |
| 114 | + _ => return, |
| 115 | + } |
| 116 | + |
| 117 | + let arguments_len = call_expr.arguments.len(); |
| 118 | + |
| 119 | + for (idx, arg) in call_expr.arguments.iter().enumerate() { |
| 120 | + let Some(Expression::ObjectExpression(obj_expr)) = |
| 121 | + arg.as_expression().map(oxc_ast::ast::Expression::get_inner_expression) |
| 122 | + else { |
| 123 | + if idx == 0 { |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + if arg.is_spread() { |
| 128 | + return; |
| 129 | + } |
| 130 | + |
| 131 | + continue; |
| 132 | + }; |
| 133 | + |
| 134 | + if arguments_len > 1 && has_get_or_set_property(obj_expr) { |
| 135 | + return; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + ctx.diagnostic_with_fix( |
| 140 | + prefer_object_spread_diagnostic(call_expr.span(), arguments_len == 1), |
| 141 | + |fixer| { |
| 142 | + let fixer = fixer.for_multifix(); |
| 143 | + let mut rule_fixes = fixer.new_fix_with_capacity(2 + call_expr.arguments.len() * 5); |
| 144 | + |
| 145 | + let needs_paren = !matches!( |
| 146 | + ctx.nodes().parent_kind(node.id()), |
| 147 | + Some( |
| 148 | + AstKind::VariableDeclarator(_) |
| 149 | + | AstKind::ArrayExpressionElement(_) |
| 150 | + | AstKind::ReturnStatement(_) |
| 151 | + | AstKind::Argument(_) |
| 152 | + | AstKind::ObjectProperty(_) |
| 153 | + | AstKind::AssignmentExpression(_) |
| 154 | + ) |
| 155 | + ); |
| 156 | + |
| 157 | + let Some(callee_left_paren_span) = find_char_span(ctx, call_expr, b'(') else { |
| 158 | + return fixer.noop(); |
| 159 | + }; |
| 160 | + |
| 161 | + let (left, right) = if needs_paren { ("({", "})") } else { ("{", "}") }; |
| 162 | + |
| 163 | + rule_fixes.push( |
| 164 | + fixer |
| 165 | + .replace(Span::new(call_expr.span.start, callee_left_paren_span.end), left), |
| 166 | + ); |
| 167 | + rule_fixes.push( |
| 168 | + fixer.replace(Span::new(call_expr.span.end - 1, call_expr.span.end), right), |
| 169 | + ); |
| 170 | + |
| 171 | + for arg in &call_expr.arguments { |
| 172 | + let Some(expression) = arg.as_expression() else { |
| 173 | + return fixer.noop(); |
| 174 | + }; |
| 175 | + |
| 176 | + if let Expression::ObjectExpression(obj_expr) = expression { |
| 177 | + let delete_span_of_left = get_delete_span_of_left(obj_expr, ctx); |
| 178 | + |
| 179 | + let delete_span_of_right = Span::new( |
| 180 | + max( |
| 181 | + get_delete_span_start_of_right(obj_expr, ctx), |
| 182 | + delete_span_of_left.end, |
| 183 | + ), |
| 184 | + obj_expr.span.end, |
| 185 | + ); |
| 186 | + |
| 187 | + rule_fixes.push(fixer.delete_range(delete_span_of_left)); |
| 188 | + rule_fixes.push(fixer.delete_range(delete_span_of_right)); |
| 189 | + |
| 190 | + if obj_expr.properties.is_empty() |
| 191 | + || ctx.source_range(get_last_char_span(expression, 1, ctx).unwrap()) |
| 192 | + == "," |
| 193 | + { |
| 194 | + if let Some(maybe_arg_comma_span) = get_char_span_after(expression, ctx) |
| 195 | + { |
| 196 | + if ctx.source_range(maybe_arg_comma_span) == "," { |
| 197 | + rule_fixes.push(fixer.delete_range(maybe_arg_comma_span)); |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + } else { |
| 202 | + let span = expression.span(); |
| 203 | + let replacement = if matches!( |
| 204 | + expression, |
| 205 | + Expression::ArrowFunctionExpression(_) |
| 206 | + | Expression::AssignmentExpression(_) |
| 207 | + | Expression::ConditionalExpression(_) |
| 208 | + ) { |
| 209 | + format!("...({})", ctx.source_range(span)) |
| 210 | + } else { |
| 211 | + format!("...{}", ctx.source_range(span)) |
| 212 | + }; |
| 213 | + |
| 214 | + rule_fixes.push(fixer.replace(span, replacement)); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + rule_fixes |
| 219 | + }, |
| 220 | + ); |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +fn has_get_or_set_property(obj_expr: &ObjectExpression) -> bool { |
| 225 | + obj_expr.properties.iter().any(|p| { |
| 226 | + let ObjectPropertyKind::ObjectProperty(p) = p else { |
| 227 | + return false; |
| 228 | + }; |
| 229 | + |
| 230 | + p.kind == PropertyKind::Get || p.kind == PropertyKind::Set |
| 231 | + }) |
| 232 | +} |
| 233 | + |
| 234 | +/** |
| 235 | + * Find the span of the first character matches with target_char in the expression |
| 236 | + */ |
| 237 | +fn find_char_span(ctx: &LintContext, expr: &dyn GetSpan, target_char: u8) -> Option<Span> { |
| 238 | + let span = expr.span(); |
| 239 | + for idx in memchr::memchr_iter(target_char, ctx.source_range(span).as_bytes()) { |
| 240 | + let idx = u32::try_from(idx).unwrap(); |
| 241 | + |
| 242 | + let current_span = Span::sized(span.start + idx, 1); |
| 243 | + |
| 244 | + if ctx.comments().iter().any(|comment| comment.span.contains_inclusive(current_span)) { |
| 245 | + continue; |
| 246 | + } |
| 247 | + |
| 248 | + return Some(current_span); |
| 249 | + } |
| 250 | + |
| 251 | + None |
| 252 | +} |
| 253 | + |
| 254 | +/** |
| 255 | + * Find the span of the first non-whitespace character before the expression. |
| 256 | + * (Includes character in the comment) |
| 257 | + */ |
| 258 | +fn get_char_span_before(start_char_span: Span, ctx: &LintContext) -> Option<Span> { |
| 259 | + let skip_count = start_char_span.start; |
| 260 | + let mut span_start = skip_count; |
| 261 | + for c in ctx.source_text()[..skip_count as usize].chars().rev() { |
| 262 | + let c_size = u32::try_from(c.len_utf8()).unwrap(); |
| 263 | + span_start -= c_size; |
| 264 | + |
| 265 | + if c.is_whitespace() { |
| 266 | + continue; |
| 267 | + } |
| 268 | + |
| 269 | + let current_span = Span::sized(span_start, c_size); |
| 270 | + |
| 271 | + return Some(current_span); |
| 272 | + } |
| 273 | + |
| 274 | + None |
| 275 | +} |
| 276 | + |
| 277 | +fn get_last_char_span(expr: &Expression, last_from: u32, ctx: &LintContext) -> Option<Span> { |
| 278 | + let expr_span = expr.span(); |
| 279 | + let mut count: u32 = 0; |
| 280 | + let mut span_start = expr_span.end; |
| 281 | + for c in ctx.source_range(expr_span).chars().rev() { |
| 282 | + let c_size = u32::try_from(c.len_utf8()).unwrap(); |
| 283 | + span_start -= c_size; |
| 284 | + |
| 285 | + if c.is_whitespace() { |
| 286 | + continue; |
| 287 | + } |
| 288 | + |
| 289 | + let current_span = Span::sized(span_start, c_size); |
| 290 | + |
| 291 | + if ctx.comments().iter().any(|comment| comment.span.contains_inclusive(current_span)) { |
| 292 | + continue; |
| 293 | + } |
| 294 | + |
| 295 | + count += 1; |
| 296 | + if count > last_from { |
| 297 | + return Some(current_span); |
| 298 | + } |
| 299 | + } |
| 300 | + |
| 301 | + None |
| 302 | +} |
| 303 | + |
| 304 | +/** |
| 305 | + * Find the span of the first non-whitespace character after the expression. |
| 306 | + * And ignore characters in the comment. |
| 307 | + */ |
| 308 | +fn get_char_span_after(expr: &Expression, ctx: &LintContext) -> Option<Span> { |
| 309 | + let skip_count = expr.span().end; |
| 310 | + let mut span_end = skip_count; |
| 311 | + for c in ctx.source_text()[skip_count as usize..].chars() { |
| 312 | + let c_size = u32::try_from(c.len_utf8()).unwrap(); |
| 313 | + span_end += c_size; |
| 314 | + |
| 315 | + if c.is_whitespace() { |
| 316 | + continue; |
| 317 | + } |
| 318 | + |
| 319 | + let current_span = Span::new(span_end - c_size, span_end); |
| 320 | + |
| 321 | + if ctx.comments().iter().any(|comment| comment.span.contains_inclusive(current_span)) { |
| 322 | + continue; |
| 323 | + } |
| 324 | + |
| 325 | + return Some(current_span); |
| 326 | + } |
| 327 | + |
| 328 | + None |
| 329 | +} |
| 330 | + |
| 331 | +fn get_delete_span_of_left(obj_expr: &Box<'_, ObjectExpression<'_>>, ctx: &LintContext) -> Span { |
| 332 | + let mut span_end = obj_expr.span.start; |
| 333 | + for (i, c) in ctx.source_range(obj_expr.span).char_indices() { |
| 334 | + if i != 0 && !c.is_whitespace() { |
| 335 | + break; |
| 336 | + } |
| 337 | + |
| 338 | + let c_size = u32::try_from(c.len_utf8()).unwrap(); |
| 339 | + span_end += c_size; |
| 340 | + } |
| 341 | + |
| 342 | + Span::new(obj_expr.span.start, span_end) |
| 343 | +} |
| 344 | + |
| 345 | +fn get_delete_span_start_of_right( |
| 346 | + obj_expr: &Box<'_, ObjectExpression<'_>>, |
| 347 | + ctx: &LintContext, |
| 348 | +) -> u32 { |
| 349 | + let obj_expr_last_char_span = Span::new(obj_expr.span.end - 1, obj_expr.span.end); |
| 350 | + let Some(prev_token_span) = get_char_span_before(obj_expr_last_char_span, ctx) else { |
| 351 | + return obj_expr_last_char_span.start; |
| 352 | + }; |
| 353 | + |
| 354 | + let has_line_comment = if let Some(comment) = |
| 355 | + ctx.comments().iter().find(|&c| c.span.contains_inclusive(prev_token_span)) |
| 356 | + { |
| 357 | + comment.is_line() |
| 358 | + } else { |
| 359 | + false |
| 360 | + }; |
| 361 | + |
| 362 | + if has_line_comment { |
| 363 | + return obj_expr_last_char_span.start; |
| 364 | + } |
| 365 | + |
| 366 | + let mut span_start: u32 = obj_expr.span.end; |
| 367 | + for (i, c) in ctx.source_range(obj_expr.span).chars().rev().enumerate() { |
| 368 | + if i != 0 && !c.is_whitespace() { |
| 369 | + break; |
| 370 | + } |
| 371 | + |
| 372 | + let c_size = u32::try_from(c.len_utf8()).unwrap(); |
| 373 | + span_start -= c_size; |
| 374 | + } |
| 375 | + |
| 376 | + span_start |
| 377 | +} |
| 378 | + |
| 379 | +#[test] |
| 380 | +fn test() { |
| 381 | + use crate::tester::Tester; |
| 382 | + |
| 383 | + let pass = vec![ |
| 384 | + "Object.assign()", |
| 385 | + "let a = Object.assign(a, b)", |
| 386 | + "Object.assign(a, b)", |
| 387 | + "let a = Object.assign(b, { c: 1 })", |
| 388 | + "const bar = { ...foo }", |
| 389 | + "Object.assign(...foo)", |
| 390 | + "Object.assign(foo, { bar: baz })", |
| 391 | + "Object.assign/** comment😀 */(foo, { 'key😀': '😀😀' })", // with multi byte characters |
| 392 | + "Object.assign({}, ...objects)", |
| 393 | + "foo({ foo: 'bar' })", |
| 394 | + " |
| 395 | + const Object = {}; |
| 396 | + Object.assign({}, foo); |
| 397 | + ", |
| 398 | + // " |
| 399 | + // Object = {}; |
| 400 | + // Object.assign({}, foo); |
| 401 | + // ", |
| 402 | + " |
| 403 | + const Object = {}; |
| 404 | + Object.assign({ foo: 'bar' }); |
| 405 | + ", |
| 406 | + // " |
| 407 | + // Object = {}; |
| 408 | + // Object.assign({ foo: 'bar' }); |
| 409 | + // ", |
| 410 | + " |
| 411 | + const Object = require('foo'); |
| 412 | + Object.assign({ foo: 'bar' }); |
| 413 | + ", |
| 414 | + " |
| 415 | + import Object from 'foo'; |
| 416 | + Object.assign({ foo: 'bar' }); |
| 417 | + ", |
| 418 | + " |
| 419 | + import { Something as Object } from 'foo'; |
| 420 | + Object.assign({ foo: 'bar' }); |
| 421 | + ", |
| 422 | + " |
| 423 | + import { Object, Array } from 'globals'; |
| 424 | + Object.assign({ foo: 'bar' }); |
| 425 | + ", |
| 426 | + " |
| 427 | + var globalThis = foo; |
| 428 | + globalThis.Object.assign({}, foo) |
| 429 | + ", // { "ecmaVersion": 2020 }, |
| 430 | + "class C { #assign; foo() { Object.#assign({}, foo); } }", // { "ecmaVersion": 2022 }, |
| 431 | + "Object.assign({ get a() {} }, {})", |
| 432 | + "Object.assign({ set a(val) {} }, {})", |
| 433 | + "Object.assign({ get a() {} }, foo)", |
| 434 | + "Object.assign({ set a(val) {} }, foo)", |
| 435 | + "Object.assign({ foo: 'bar', get a() {}, baz: 'quux' }, quuux)", |
| 436 | + "Object.assign({ foo: 'bar', set a(val) {} }, { baz: 'quux' })", |
| 437 | + "Object.assign({}, { get a() {} })", |
| 438 | + "Object.assign({}, { set a(val) {} })", |
| 439 | + "Object.assign({}, { foo: 'bar', get a() {} }, {})", |
| 440 | + "Object.assign({ foo }, bar, {}, { baz: 'quux', set a(val) {}, quuux }, {})", |
| 441 | + ]; |
| 442 | + |
| 443 | + let fail = vec![ |
| 444 | + "Object.assign({}, foo)", |
| 445 | + "Object.assign ({}, foo)", |
| 446 | + "Object.assign({}, { foo: 'bar' })", |
| 447 | + "Object.assign({}, baz, { foo: 'bar' })", |
| 448 | + "Object.assign({}, { foo: 'bar', baz: 'foo' })", |
| 449 | + "Object.assign/** comment😀 */({}, { '😀': '😀', '😆': '💪' })", // with multi byte characters |
| 450 | + "Object.assign({ foo: 'bar' }, baz)", |
| 451 | + "Object.assign({ foo: 'bar' }, cats, dogs, trees, birds)", |
| 452 | + "Object.assign({ foo: 'bar' }, Object.assign({ bar: 'foo' }, baz))", |
| 453 | + "Object.assign({ foo: 'bar' }, Object.assign({ bar: 'foo' }, Object.assign({}, { superNested: 'butwhy' })))", |
| 454 | + "Object.assign({foo: 'bar', ...bar}, baz)", |
| 455 | + "Object.assign({}, { foo, bar, baz })", |
| 456 | + "Object.assign({}, { [bar]: 'foo' })", |
| 457 | + "Object.assign({ ...bar }, { ...baz })", |
| 458 | + r#" |
| 459 | + Object.assign({ ...bar }, { |
| 460 | + // this is a bar |
| 461 | + foo: 'bar', |
| 462 | + baz: "cats" |
| 463 | + }) |
| 464 | + "#, |
| 465 | + r#" |
| 466 | + Object.assign({ |
| 467 | + boo: "lol", |
| 468 | + // I'm a comment |
| 469 | + dog: "cat" |
| 470 | + }, { |
| 471 | + // this is a bar |
| 472 | + foo: 'bar', |
| 473 | + baz: "cats" |
| 474 | + }) |
| 475 | + "#, |
| 476 | + r#" |
| 477 | + const test = Object.assign({ ...bar }, { |
| 478 | + <!-- html comment |
| 479 | + foo: 'bar', |
| 480 | + baz: "cats" |
| 481 | + --> weird |
| 482 | + }) |
| 483 | + "#, // { "sourceType": "script" }, |
| 484 | + r#" |
| 485 | + const test = Object.assign({ ...bar }, { |
| 486 | + foo: 'bar', // inline comment |
| 487 | + baz: "cats" |
| 488 | + }) |
| 489 | + "#, |
| 490 | + r#" |
| 491 | + const test = Object.assign({ ...bar }, { |
| 492 | + /** |
| 493 | + * foo |
| 494 | + */ |
| 495 | + foo: 'bar', |
| 496 | + baz: "cats" |
| 497 | + }) |
| 498 | + "#, |
| 499 | + "Object.assign({})", |
| 500 | + "Object.assign({ foo: bar })", |
| 501 | + " |
| 502 | + const foo = 'bar'; |
| 503 | + Object.assign({ foo: bar }) |
| 504 | + ", |
| 505 | + " |
| 506 | + foo = 'bar'; |
| 507 | + Object.assign({ foo: bar }) |
| 508 | + ", |
| 509 | + "let a = Object.assign({})", |
| 510 | + "let a = Object.assign({}, a)", |
| 511 | + "let a = Object.assign ({}, a)", |
| 512 | + "let a = Object.assign({ a: 1 }, b)", |
| 513 | + "Object.assign( {}, a, b, )", |
| 514 | + "Object.assign({}, a ? b : {}, b => c, a = 2)", |
| 515 | + " |
| 516 | + const someVar = 'foo'; |
| 517 | + Object.assign({}, a ? b : {}, b => c, a = 2) |
| 518 | + ", |
| 519 | + " |
| 520 | + someVar = 'foo'; |
| 521 | + Object.assign({}, a ? b : {}, b => c, a = 2) |
| 522 | + ", |
| 523 | + "[1, 2, Object.assign({}, a)]", |
| 524 | + "const foo = Object.assign({}, a)", |
| 525 | + "function foo() { return Object.assign({}, a) }", |
| 526 | + "foo(Object.assign({}, a));", |
| 527 | + "const x = { foo: 'bar', baz: Object.assign({}, a) }", |
| 528 | + " |
| 529 | + import Foo from 'foo'; |
| 530 | + Object.assign({ foo: Foo });", |
| 531 | + " |
| 532 | + import Foo from 'foo'; |
| 533 | + Object.assign({}, Foo);", |
| 534 | + " |
| 535 | + const Foo = require('foo'); |
| 536 | + Object.assign({ foo: Foo });", |
| 537 | + " |
| 538 | + import { Something as somethingelse } from 'foo'; |
| 539 | + Object.assign({}, somethingelse); |
| 540 | + ", |
| 541 | + " |
| 542 | + import { foo } from 'foo'; |
| 543 | + Object.assign({ foo: Foo }); |
| 544 | + ", |
| 545 | + " |
| 546 | + const Foo = require('foo'); |
| 547 | + Object.assign({}, Foo); |
| 548 | + ", |
| 549 | + " |
| 550 | + const actions = Object.assign( |
| 551 | + { |
| 552 | + onChangeInput: this.handleChangeInput, |
| 553 | + }, |
| 554 | + this.props.actions |
| 555 | + ); |
| 556 | + ", |
| 557 | + " |
| 558 | + const actions = Object.assign( |
| 559 | + { |
| 560 | + onChangeInput: this.handleChangeInput, // |
| 561 | + }, |
| 562 | + this.props.actions |
| 563 | + ); |
| 564 | + ", |
| 565 | + " |
| 566 | + const actions = Object.assign( |
| 567 | + { |
| 568 | + onChangeInput: this.handleChangeInput // |
| 569 | + }, |
| 570 | + this.props.actions |
| 571 | + ); |
| 572 | + ", |
| 573 | + " |
| 574 | + const actions = Object.assign( |
| 575 | + ( |
| 576 | + { |
| 577 | + onChangeInput: this.handleChangeInput |
| 578 | + } |
| 579 | + ), |
| 580 | + ( |
| 581 | + this.props.actions |
| 582 | + ) |
| 583 | + ); |
| 584 | + ", |
| 585 | + "eventData = Object.assign({}, eventData, { outsideLocality: `${originLocality} - ${destinationLocality}` })", |
| 586 | + "Object.assign({ });", |
| 587 | + "Object.assign({\n});", |
| 588 | + "globalThis.Object.assign({ });", // { "ecmaVersion": 2020 }, |
| 589 | + "globalThis.Object.assign({\n});", // { "ecmaVersion": 2020 }, |
| 590 | + "globalThis.Object.assign({}, foo)", |
| 591 | + "globalThis.Object.assign({}, { foo: 'bar' })", // { "ecmaVersion": 6 }, |
| 592 | + "globalThis.Object.assign({}, baz, { foo: 'bar' })", // { "ecmaVersion": 2017 }, |
| 593 | + " |
| 594 | + function foo () { var globalThis = bar; } |
| 595 | + globalThis.Object.assign({ }); |
| 596 | + ", // { "ecmaVersion": 2020 }, |
| 597 | + " |
| 598 | + const Foo = require('foo'); |
| 599 | + globalThis.Object.assign({ foo: Foo }); |
| 600 | + ", // { "ecmaVersion": 2020 }, |
| 601 | + "Object.assign({ get a() {}, set b(val) {} })", |
| 602 | + "const obj = Object.assign<{}, Record<string, string[]>>({}, getObject());", // { "parser": require("../../fixtures/parsers/typescript-parsers/object-assign-with-generic/object-assign-with-generic-1") }, |
| 603 | + "Object.assign<{}, A>({}, foo);", // { "parser": require("../../fixtures/parsers/typescript-parsers/object-assign-with-generic/object-assign-with-generic-2") } |
| 604 | + ]; |
| 605 | + |
| 606 | + let fix = vec![ |
| 607 | + ("Object.assign({}, foo)", "({ ...foo})", None), |
| 608 | + ("Object.assign ({}, foo)", "({ ...foo})", None), |
| 609 | + ("Object.assign({}, { foo: 'bar' })", "({ foo: 'bar'})", None), |
| 610 | + ("Object.assign({}, baz, { foo: 'bar' })", "({ ...baz, foo: 'bar'})", None), |
| 611 | + ("Object.assign({}, { foo: 'bar', baz: 'foo' })", "({ foo: 'bar', baz: 'foo'})", None), |
| 612 | + (r"Object.assign/** comment with multi byte 😀 */({}, { '😀': '😀', '😆': '💪' })", r"({ '😀': '😀', '😆': '💪'})", None), |
| 613 | + ("Object.assign({ foo: 'bar' }, baz)", "({foo: 'bar', ...baz})", None), |
| 614 | + ("Object.assign({ foo: 'bar' }, cats, dogs, trees, birds)", "({foo: 'bar', ...cats, ...dogs, ...trees, ...birds})", None), |
| 615 | + ("Object.assign({ foo: 'bar' }, Object.assign({ bar: 'foo' }, baz))", "({foo: 'bar', ...Object.assign({ bar: 'foo' }, baz)})", None), |
| 616 | + ("Object.assign({ foo: 'bar' }, Object.assign({ bar: 'foo' }, Object.assign({}, { superNested: 'butwhy' })))", "({foo: 'bar', ...Object.assign({ bar: 'foo' }, Object.assign({}, { superNested: 'butwhy' }))})", None), |
| 617 | + ("Object.assign({foo: 'bar', ...bar}, baz)", "({foo: 'bar', ...bar, ...baz})", None), |
| 618 | + ("Object.assign({}, { foo, bar, baz })", "({ foo, bar, baz})", None), |
| 619 | + ("Object.assign({}, { [bar]: 'foo' })", "({ [bar]: 'foo'})", None), |
| 620 | + ("Object.assign({ ...bar }, { ...baz })", "({...bar, ...baz})", None), |
| 621 | + ( |
| 622 | + r#" |
| 623 | + Object.assign({ ...bar }, { |
| 624 | + // this is a bar |
| 625 | + foo: 'bar', |
| 626 | + baz: "cats" |
| 627 | + }) |
| 628 | + "#, |
| 629 | + r#" |
| 630 | + ({...bar, // this is a bar |
| 631 | + foo: 'bar', |
| 632 | + baz: "cats"}) |
| 633 | + "#, |
| 634 | + None |
| 635 | + ), |
| 636 | + ( |
| 637 | + r#" |
| 638 | + Object.assign({ |
| 639 | + boo: "lol", |
| 640 | + // I'm a comment |
| 641 | + dog: "cat" |
| 642 | + }, { |
| 643 | + // this is a bar |
| 644 | + foo: 'bar', |
| 645 | + baz: "cats" |
| 646 | + }) |
| 647 | + "#, |
| 648 | + r#" |
| 649 | + ({boo: "lol", |
| 650 | + // I'm a comment |
| 651 | + dog: "cat", // this is a bar |
| 652 | + foo: 'bar', |
| 653 | + baz: "cats"}) |
| 654 | + "#, |
| 655 | + None |
| 656 | + ), |
| 657 | + ( |
| 658 | + r#" |
| 659 | + const test = Object.assign({ ...bar }, { |
| 660 | + /* comment |
| 661 | + foo: 'bar', |
| 662 | + baz: "cats" |
| 663 | + */ weird |
| 664 | + }) |
| 665 | + "#, |
| 666 | + r#" |
| 667 | + const test = {...bar, /* comment |
| 668 | + foo: 'bar', |
| 669 | + baz: "cats" |
| 670 | + */ weird} |
| 671 | + "#, |
| 672 | + None |
| 673 | + ), |
| 674 | + ( |
| 675 | + r#" |
| 676 | + const test = Object.assign({ ...bar }, { |
| 677 | + foo: 'bar', // inline comment |
| 678 | + baz: "cats" |
| 679 | + }) |
| 680 | + "#, |
| 681 | + r#" |
| 682 | + const test = {...bar, foo: 'bar', // inline comment |
| 683 | + baz: "cats"} |
| 684 | + "#, |
| 685 | + None |
| 686 | + ), |
| 687 | + ( |
| 688 | + r#" |
| 689 | + const test = Object.assign({ ...bar }, { |
| 690 | + /** |
| 691 | + * foo |
| 692 | + */ |
| 693 | + foo: 'bar', |
| 694 | + baz: "cats" |
| 695 | + }) |
| 696 | + "#, |
| 697 | + r#" |
| 698 | + const test = {...bar, /** |
| 699 | + * foo |
| 700 | + */ |
| 701 | + foo: 'bar', |
| 702 | + baz: "cats"} |
| 703 | + "#, |
| 704 | + None |
| 705 | + ), |
| 706 | + ("Object.assign({})", "({})", None), |
| 707 | + ("Object.assign({ foo: bar })", "({foo: bar})", None), |
| 708 | + ( |
| 709 | + " |
| 710 | + const foo = 'bar'; |
| 711 | + Object.assign({ foo: bar }) |
| 712 | + ", |
| 713 | + " |
| 714 | + const foo = 'bar'; |
| 715 | + ({foo: bar}) |
| 716 | + ", |
| 717 | + None |
| 718 | + ), |
| 719 | + ( |
| 720 | + " |
| 721 | + foo = 'bar'; |
| 722 | + Object.assign({ foo: bar }) |
| 723 | + ", |
| 724 | + " |
| 725 | + foo = 'bar'; |
| 726 | + ({foo: bar}) |
| 727 | + ", |
| 728 | + None |
| 729 | + ), |
| 730 | + ("let a = Object.assign({})", "let a = {}", None), |
| 731 | + ("let a = Object.assign({}, a)", "let a = { ...a}", None), |
| 732 | + ("let a = Object.assign ({}, a)", "let a = { ...a}", None), |
| 733 | + ("let a = Object.assign({ a: 1 }, b)", "let a = {a: 1, ...b}", None), |
| 734 | + ("Object.assign( {}, a, b, )", "({ ...a, ...b, })", None), |
| 735 | + ("Object.assign({}, a ? b : {}, b => c, a = 2)", "({ ...(a ? b : {}), ...(b => c), ...(a = 2)})", None), |
| 736 | + ( |
| 737 | + " |
| 738 | + const someVar = 'foo'; |
| 739 | + Object.assign({}, a ? b : {}, b => c, a = 2) |
| 740 | + ", |
| 741 | + " |
| 742 | + const someVar = 'foo'; |
| 743 | + ({ ...(a ? b : {}), ...(b => c), ...(a = 2)}) |
| 744 | + ", |
| 745 | + None |
| 746 | + ), |
| 747 | + ( |
| 748 | + " |
| 749 | + someVar = 'foo'; |
| 750 | + Object.assign({}, a ? b : {}, b => c, a = 2) |
| 751 | + ", |
| 752 | + " |
| 753 | + someVar = 'foo'; |
| 754 | + ({ ...(a ? b : {}), ...(b => c), ...(a = 2)}) |
| 755 | + ", |
| 756 | + None |
| 757 | + ), |
| 758 | + ("[1, 2, Object.assign({}, a)]", "[1, 2, { ...a}]", None), |
| 759 | + ("const foo = Object.assign({}, a)", "const foo = { ...a}", None), |
| 760 | + ("function foo() { return Object.assign({}, a) }", "function foo() { return { ...a} }", None), |
| 761 | + ("foo(Object.assign({}, a));", "foo({ ...a});", None), |
| 762 | + ("const x = { foo: 'bar', baz: Object.assign({}, a) }", "const x = { foo: 'bar', baz: { ...a} }", None), |
| 763 | + ( |
| 764 | + " |
| 765 | + import Foo from 'foo'; |
| 766 | + Object.assign({ foo: Foo }); |
| 767 | + ", |
| 768 | + " |
| 769 | + import Foo from 'foo'; |
| 770 | + ({foo: Foo}); |
| 771 | + ", |
| 772 | + None |
| 773 | + ), |
| 774 | + ( |
| 775 | + " |
| 776 | + import Foo from 'foo'; |
| 777 | + Object.assign({}, Foo); |
| 778 | + ", |
| 779 | + " |
| 780 | + import Foo from 'foo'; |
| 781 | + ({ ...Foo}); |
| 782 | + ", |
| 783 | + None |
| 784 | + ), |
| 785 | + ( |
| 786 | + " |
| 787 | + const Foo = require('foo'); |
| 788 | + Object.assign({ foo: Foo }); |
| 789 | + ", |
| 790 | + " |
| 791 | + const Foo = require('foo'); |
| 792 | + ({foo: Foo}); |
| 793 | + ", |
| 794 | + None |
| 795 | + ), |
| 796 | + ( |
| 797 | + " |
| 798 | + import { Something as somethingelse } from 'foo'; |
| 799 | + Object.assign({}, somethingelse); |
| 800 | + ", |
| 801 | + " |
| 802 | + import { Something as somethingelse } from 'foo'; |
| 803 | + ({ ...somethingelse}); |
| 804 | + ", |
| 805 | + None |
| 806 | + ), |
| 807 | + ( |
| 808 | + " |
| 809 | + import { foo } from 'foo'; |
| 810 | + Object.assign({ foo: Foo }); |
| 811 | + ", |
| 812 | + " |
| 813 | + import { foo } from 'foo'; |
| 814 | + ({foo: Foo}); |
| 815 | + ", |
| 816 | + None |
| 817 | + ), |
| 818 | + ( |
| 819 | + " |
| 820 | + const Foo = require('foo'); |
| 821 | + Object.assign({}, Foo); |
| 822 | + ", |
| 823 | + " |
| 824 | + const Foo = require('foo'); |
| 825 | + ({ ...Foo}); |
| 826 | + ", |
| 827 | + None |
| 828 | + ), |
| 829 | + ( |
| 830 | + " |
| 831 | + const actions = Object.assign( |
| 832 | + { |
| 833 | + onChangeInput: this.handleChangeInput, |
| 834 | + }, |
| 835 | + this.props.actions |
| 836 | + ); |
| 837 | + ", |
| 838 | + " |
| 839 | + const actions = { |
| 840 | + onChangeInput: this.handleChangeInput, |
| 841 | + ...this.props.actions |
| 842 | + }; |
| 843 | + ", |
| 844 | + None |
| 845 | + ), |
| 846 | + ( |
| 847 | + " |
| 848 | + const actions = Object.assign( |
| 849 | + { |
| 850 | + onChangeInput: this.handleChangeInput, // |
| 851 | + }, // comment 2 |
| 852 | + this.props.actions |
| 853 | + ); |
| 854 | + ", |
| 855 | + " |
| 856 | + const actions = { |
| 857 | + onChangeInput: this.handleChangeInput, // |
| 858 | + // comment 2 |
| 859 | + ...this.props.actions |
| 860 | + }; |
| 861 | + ", |
| 862 | + None |
| 863 | + ), |
| 864 | + ( |
| 865 | + " |
| 866 | + const actions = Object.assign( |
| 867 | + { |
| 868 | + onChangeInput: this.handleChangeInput // |
| 869 | + }, |
| 870 | + this.props.actions |
| 871 | + ); |
| 872 | + ", |
| 873 | + " |
| 874 | + const actions = { |
| 875 | + onChangeInput: this.handleChangeInput // |
| 876 | + , |
| 877 | + ...this.props.actions |
| 878 | + }; |
| 879 | + ", |
| 880 | + None |
| 881 | + ), |
| 882 | + ( |
| 883 | + " |
| 884 | + const actions = Object.assign( |
| 885 | + ( |
| 886 | + { |
| 887 | + onChangeInput: this.handleChangeInput |
| 888 | + } |
| 889 | + ), |
| 890 | + ( |
| 891 | + this.props.actions |
| 892 | + ) |
| 893 | + ); |
| 894 | + ", |
| 895 | + " |
| 896 | + const actions = { |
| 897 | + ...( |
| 898 | + { |
| 899 | + onChangeInput: this.handleChangeInput |
| 900 | + } |
| 901 | + ), |
| 902 | + ...( |
| 903 | + this.props.actions |
| 904 | + ) |
| 905 | + }; |
| 906 | + ", |
| 907 | + None |
| 908 | + ), |
| 909 | + ( |
| 910 | + "eventData = Object.assign({}, eventData, { outsideLocality: `${originLocality} - ${destinationLocality}` })", |
| 911 | + "eventData = { ...eventData, outsideLocality: `${originLocality} - ${destinationLocality}`}", |
| 912 | + None |
| 913 | + ), |
| 914 | + ("Object.assign({ });", "({});", None), |
| 915 | + ("Object.assign({\n});", "({});", None), |
| 916 | + ("globalThis.Object.assign({ });", "({});", None), |
| 917 | + ("globalThis.Object.assign({\n});", "({});", None), |
| 918 | + ( |
| 919 | + " |
| 920 | + function foo () { var globalThis = bar; } |
| 921 | + globalThis.Object.assign({ }); |
| 922 | + ", |
| 923 | + " |
| 924 | + function foo () { var globalThis = bar; } |
| 925 | + ({}); |
| 926 | + ", |
| 927 | + None |
| 928 | + ), |
| 929 | + ( |
| 930 | + " |
| 931 | + const Foo = require('foo'); |
| 932 | + globalThis.Object.assign({ foo: Foo }); |
| 933 | + ", |
| 934 | + " |
| 935 | + const Foo = require('foo'); |
| 936 | + ({foo: Foo}); |
| 937 | + ", |
| 938 | + None |
| 939 | + ), |
| 940 | + ("Object.assign({ get a() {}, set b(val) {} })", "({get a() {}, set b(val) {}})", None), |
| 941 | + ("const obj = Object.assign<{}, Record<string, string[]>>({}, getObject());", "const obj = { ...getObject()};", None), |
| 942 | + ("Object.assign<{}, A>({}, foo);", "({ ...foo});", None) |
| 943 | + ]; |
| 944 | + Tester::new(PreferObjectSpread::NAME, PreferObjectSpread::PLUGIN, pass, fail) |
| 945 | + .expect_fix(fix) |
| 946 | + .test_and_snapshot(); |
| 947 | +} |
0 commit comments