From 61cc2f431d4664443a79057f76fdf7685eb191ea Mon Sep 17 00:00:00 2001 From: David Sherret Date: Thu, 22 Aug 2024 10:45:18 -0400 Subject: [PATCH] fix(emit): surface diagnostic for invalid left hand side assignment (#271) --- Cargo.lock | 25 +++++++++++++------------ src/transpiling/mod.rs | 16 ++++++++++++++++ 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e17ff7d..eca79a2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1024,9 +1024,9 @@ dependencies = [ [[package]] name = "swc_common" -version = "0.37.1" +version = "0.37.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4d6c716bb706926e22edc992565c98a8f00c0cfa983e97f525f473f9ce2f93f" +checksum = "46741b5a4ff3e821f6bb8d6c1289549272e71a5e0d163dbbae9e16e771d8da76" dependencies = [ "ast_node", "better_scoped_tls", @@ -1077,9 +1077,9 @@ dependencies = [ [[package]] name = "swc_ecma_ast" -version = "0.118.0" +version = "0.118.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d28b2b3ac8af922e693ee784881b2a32ca4604e1088072b66116003c225a44d9" +checksum = "ed6c1b94abbaf080a4e4ae47101a83d4eedef90d733dd98e32b361356d3f5e4b" dependencies = [ "bitflags", "is-macro", @@ -1510,9 +1510,9 @@ dependencies = [ [[package]] name = "swc_ecma_transforms_typescript" -version = "0.195.0" +version = "0.195.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05edb03338cd1ce3986b459eb14f7b4b24eae85ebae7d96138884903748c56ec" +checksum = "f814b32ec83fde097df19e7346c429825390d156d0015f321f1f6434b6a06c0c" dependencies = [ "ryu-js", "serde", @@ -1527,9 +1527,9 @@ dependencies = [ [[package]] name = "swc_ecma_utils" -version = "0.134.0" +version = "0.134.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5a5eb7b88fdc944a056c61daa0cc5a7099a00ee484bbeb21a7a033fe5584cd8" +checksum = "cde8f1ef3f7bd53340c7bd679f1ec563a45225ac8fb63f22d6de1ff4b345475d" dependencies = [ "indexmap", "num_cpus", @@ -1546,9 +1546,9 @@ dependencies = [ [[package]] name = "swc_ecma_visit" -version = "0.104.1" +version = "0.104.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e23f41588c8a08e44d900c079f8445ac4760d8006222bd7bae66e064b03f655" +checksum = "c71f5f97db49b96208805104b381c5e117f55fad5f3d178e626c92934a4d0e36" dependencies = [ "new_debug_unreachable", "num-bigint", @@ -1619,11 +1619,12 @@ dependencies = [ [[package]] name = "swc_visit" -version = "0.6.1" +version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e194d14f94121fd08b823d3379eedb3ce455785d9e0c3d2742c59377e283207" +checksum = "1ceb044142ba2719ef9eb3b6b454fce61ab849eb696c34d190f04651955c613d" dependencies = [ "either", + "new_debug_unreachable", ] [[package]] diff --git a/src/transpiling/mod.rs b/src/transpiling/mod.rs index a0bcbdf..3b7caf6 100644 --- a/src/transpiling/mod.rs +++ b/src/transpiling/mod.rs @@ -594,6 +594,8 @@ fn is_fatal_syntax_error(error_kind: &SyntaxError) -> bool { SyntaxError::LegacyDecimal | // expected expression SyntaxError::TS1109 | + // invalid left hand side of assignment + SyntaxError::TS2406 | // unterminated string literal SyntaxError::UnterminatedStrLit | // nullish coalescing with logical op @@ -1442,6 +1444,20 @@ for (let i = 0; i < testVariable >> 1; i++) callCount++; )); } + #[test] + fn diagnostic_invalid_left_hand_side_of_assignment() { + assert_eq!(get_diagnostic("(true ? a : b) = 1;"), concat!( + "The left-hand side of an assignment expression must be a variable or a property access. at https://deno.land/x/mod.ts:1:1\n\n", + " (true ? a : b) = 1;\n", + " ~~~~~~~~~~~~~~\n", + "\n", + // for some reason, swc does the same diagnostic twice + "The left-hand side of an assignment expression must be a variable or a property access. at https://deno.land/x/mod.ts:1:1\n\n", + " (true ? a : b) = 1;\n", + " ~~~~~~~~~~~~~~", + )); + } + fn get_diagnostic(source: &str) -> String { let specifier = ModuleSpecifier::parse("https://deno.land/x/mod.ts").unwrap();