From af73147adb7b40744f1e30b282bbba85f2734365 Mon Sep 17 00:00:00 2001 From: Denis Bezrukov <6227442+denbezrukov@users.noreply.github.com> Date: Sun, 14 May 2023 19:32:20 +0300 Subject: [PATCH] fix(rome_js_analyze): Files with fn(() => (aborted = true)); cause - entered unreachable code - when --apply-unsafe #4464 --- CHANGELOG.md | 1 + crates/rome_cli/tests/commands/check.rs | 32 ++ .../apply_unsafe_no_assign_in_expression.snap | 18 + .../suspicious/no_assign_in_expressions.rs | 374 +++++++++++++++++- .../noAssignInExpressions/invalid.js | 5 +- .../noAssignInExpressions/invalid.js.snap | 352 +++++++++-------- 6 files changed, 611 insertions(+), 171 deletions(-) create mode 100644 crates/rome_cli/tests/snapshots/main_commands_check/apply_unsafe_no_assign_in_expression.snap diff --git a/CHANGELOG.md b/CHANGELOG.md index bbc823b96e0..ae8877fc64e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ ### Editors ### Formatter ### Linter +- Fixed an issue where the `noAssignInExpressions` rule replaced the operator with an invalid token, which caused other lint rules to crash. [#4464](https://github.com/rome/tools/issues/4464) ### Parser ### VSCode ### JavaScript APIs diff --git a/crates/rome_cli/tests/commands/check.rs b/crates/rome_cli/tests/commands/check.rs index e830cd92d6e..754d066a1b8 100644 --- a/crates/rome_cli/tests/commands/check.rs +++ b/crates/rome_cli/tests/commands/check.rs @@ -2080,3 +2080,35 @@ fn should_apply_correct_file_source() { result, )); } + +#[test] +fn apply_unsafe_no_assign_in_expression() { + let mut fs = MemoryFileSystem::default(); + let mut console = BufferConsole::default(); + + let file_path = Path::new("fix.js"); + fs.insert( + file_path.into(), + "res.onAborted(() => (aborted = true));".as_bytes(), + ); + + let result = run_cli( + DynRef::Borrowed(&mut fs), + &mut console, + Args::from(&[ + ("check"), + file_path.as_os_str().to_str().unwrap(), + ("--apply-unsafe"), + ]), + ); + + assert!(result.is_ok(), "run_cli returned {result:?}"); + + assert_cli_snapshot(SnapshotPayload::new( + module_path!(), + "apply_unsafe_no_assign_in_expression", + fs, + console, + result, + )); +} diff --git a/crates/rome_cli/tests/snapshots/main_commands_check/apply_unsafe_no_assign_in_expression.snap b/crates/rome_cli/tests/snapshots/main_commands_check/apply_unsafe_no_assign_in_expression.snap new file mode 100644 index 00000000000..ea1ea59d5ac --- /dev/null +++ b/crates/rome_cli/tests/snapshots/main_commands_check/apply_unsafe_no_assign_in_expression.snap @@ -0,0 +1,18 @@ +--- +source: crates/rome_cli/tests/snap_test.rs +expression: content +--- +## `fix.js` + +```js +res.onAborted(() => aborted === true); + +``` + +# Emitted Messages + +```block +Fixed 1 file(s) in