Skip to content

Rewrite your app in Rust, piece by piece, checked against the original at every step. Works with TypeScript, JavaScript, Python, Go, Ruby, Java, C, PHP, C#, Kotlin, PowerShell and Swift.

fix(powershell): keep character parameters in PowerShell (#1440)

Atharva Dahakebb896a480cdb
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ec50f257..688aa82b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,8 @@
## Unreleased
+- PowerShell keeps functions with `[char]`, `[System.Char]`, or character array parameters in
+ PowerShell. Bridging those parameters as strings produced inputs that failed to bind (#1342).
- Recorded SMTP attachments preserve literal percent escapes in plain pieces of a split filename
(#1314).
- C# wrapper headers, generated `README.md`, and `shadow` refusals now name the unit's actual `<namespace>.Rust` namespace(s) and first real class alias instead of `<namespace>.Rust` and `Acme.Text.Rust.Slug` (#1381 by @m3lonxd).
diff --git a/docs/types.md b/docs/types.md
index a0a8b7bc..14c76b2d 100644
--- a/docs/types.md
+++ b/docs/types.md
@@ -336,7 +336,7 @@ Types are read from the `[type]` before each parameter in `param()`, and the res
| Source type | Port type | Notes |
|---|---|---|
-| `[string]`, `[char]` | `String` | |
+| `[string]` | `String` | |
| `[int]`, `[long]`, `[byte]`, `[uint32]` | `i64` | |
| `[double]`, `[float]`, `[single]` | `f64` | |
| `[bool]`, `[switch]` | `bool` | |
@@ -345,6 +345,7 @@ Types are read from the `[type]` before each parameter in `param()`, and the res
| `[hashtable]`, `[ordered]`, `[pscustomobject]`, `[object]` | `serde_json::Value` | |
| `[uint64]`, `[ulong]` | refused | wider than a JSON-safe integer |
| `[decimal]` | refused | a 128-bit decimal no JSON number keeps exactly |
+| `[char]`, `[char[]]` | refused | `char` is a UTF-16 code unit |
| `[datetime]`, `[guid]` | refused | takes a .NET object |
| `[scriptblock]` | refused | takes a script block |
diff --git a/src/lang/powershell.rs b/src/lang/powershell.rs
index 9c7556db..ba92687e 100644
--- a/src/lang/powershell.rs
+++ b/src/lang/powershell.rs
@@ -539,7 +539,7 @@ pub fn ps_to_ts(t: &str) -> Option<String> {
let lower = lower.strip_prefix("system.").unwrap_or(&lower);
Some(
match lower {
- "string" | "char" => "string",
+ "string" => "string",
"int" | "int32" | "long" | "int64" | "int16" | "short" | "byte" | "sbyte"
| "uint16" | "ushort" | "uint32" | "uint" => "int",
"double" | "single" | "float" => "number",
@@ -701,6 +701,8 @@ fn parameter_blocker(name: &str, t: &str) -> String {
format!("takes `${name}` (`[{t}]`), wider than a JSON-safe integer")
} else if element == "decimal" {
format!("takes `${name}` (`[{t}]`), a 128-bit decimal no JSON number keeps exactly")
+ } else if element == "char" {
+ format!("takes `${name}` (`[{t}]`), a UTF-16 code unit")
} else if lower == "scriptblock" || lower.ends_with(".scriptblock") {
format!("takes a script block `${name}`")
} else if lower == "ref" {
@@ -1071,6 +1073,7 @@ mod tests {
);
assert_eq!(ps_to_ts("switch").as_deref(), Some("boolean"));
assert_eq!(ps_to_ts("DateTime"), None);
+ assert_eq!(ps_to_ts("char"), None);
assert_eq!(ps_to_ts("decimal"), None);
assert_eq!(ps_to_ts("System.UInt64"), None);
assert_eq!(ps_to_ts("ulong"), None);
@@ -1080,6 +1083,26 @@ mod tests {
assert_eq!(ps_to_ts("single").as_deref(), Some("number"));
}
+ #[test]
+ fn char_parameters_stay_in_powershell() {
+ for ty in ["char", "System.Char", "char[]"] {
+ let text = format!("function Get-Code {{ param([{ty}]$C) return [int]$C }}");
+ let fns = exports("Text.psm1", &text);
+ assert_eq!(
+ find(&fns, "Get-Code").blocker,
+ Some(format!("takes `$C` (`[{ty}]`), a UTF-16 code unit"))
+ );
+ }
+
+ let fns = exports(
+ "Text.psm1",
+ "function Get-Text { param([string]$Text) $Text }",
+ );
+ let export = find(&fns, "Get-Text");
+ assert_eq!(export.blocker, None);
+ assert_eq!(export.params[0].ts_type, "string");
+ }
+
#[test]
fn decimal_and_uint64_stay_in_powershell_with_the_csharp_reasons() {
let text = "function Add-Tax {\n param([decimal]$Amount, [decimal]$Rate)\n return $Amount * (1 + $Rate)\n}\n\nfunction Get-Next {\n param([uint64]$Id)\n return $Id + 1\n}\n\nfunction Get-Big {\n param([System.UInt64[]]$Ids)\n $Ids\n}\n\nfunction Get-Half {\n param([uint32]$N, [double]$X, [single]$Y)\n $N / 2\n}\n";