From d8c093126dca62e89715424aee6d6de1acd14ef8 Mon Sep 17 00:00:00 2001 From: dalaoshu Date: Sat, 23 Nov 2024 23:18:36 +0800 Subject: [PATCH] feat(oxc_transformer): use better diagnostic message for `ReplaceGlobalDefinesPlugin` (#7439) When I was handling plugin-related diagnostics in `rolldown`, I encountered this issue. If there is a define key like `a.6.b`, the error message states that \`a.6.b\` is not an identifier. However, it should actually be that \`6\` is not an identifier. --- .../src/plugins/replace_global_defines.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/crates/oxc_transformer/src/plugins/replace_global_defines.rs b/crates/oxc_transformer/src/plugins/replace_global_defines.rs index 6bd29ce8e..fbb5f38f4 100644 --- a/crates/oxc_transformer/src/plugins/replace_global_defines.rs +++ b/crates/oxc_transformer/src/plugins/replace_global_defines.rs @@ -137,7 +137,9 @@ impl ReplaceGlobalDefinesConfig { if parts.len() == 1 { if !is_identifier_name(parts[0]) { - return Err(vec![OxcDiagnostic::error(format!("`{key}` is not an identifier."))]); + return Err(vec![OxcDiagnostic::error(format!( + "The define key `{key}` is not an identifier." + ))]); } return Ok(IdentifierType::Identifier); } @@ -148,7 +150,9 @@ impl ReplaceGlobalDefinesConfig { for part in &parts[0..normalized_parts_len] { if !is_identifier_name(part) { - return Err(vec![OxcDiagnostic::error(format!("`{key}` is not an identifier."))]); + return Err(vec![OxcDiagnostic::error(format!( + "The define key `{key}` contains an invalid identifier `{part}`." + ))]); } } if is_import_meta { @@ -167,7 +171,7 @@ impl ReplaceGlobalDefinesConfig { // StaticMemberExpression with postfix wildcard } else if normalized_parts_len != parts.len() { Err(vec![OxcDiagnostic::error( - "postfix wildcard is only allowed for `import.meta`.".to_string(), + "The postfix wildcard is only allowed for `import.meta`.".to_string(), )]) } else { Ok(IdentifierType::DotDefines {