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.

analyze: give the reason bridge keeps a non-JS unit's exports, not JS-shaped (#1436)

jakalijey4fa0a809717b
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5d01aab4..9e4d01a1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -278,6 +278,10 @@
- PowerShell: a parameter typed `[decimal]`, `[uint64]` or `[ulong]` keeps its function in
PowerShell, with the reasons C# gives for the same types, instead of bridging it as `f64` or
`i64`. `[uint32]`, `[double]` and `[single]` bridge as before (#1341 by @jakalijey).
+- `analyze` no longer calls a Python, Go, Java or other non-JS unit "JS-shaped" when every export
+ stays in its language for another reason: the note gives the reason `bridge` gives for the first
+ kept export (`stamp` reads the clock, ...), and "Start here" no longer says such units expose
+ nothing a port could call (#1402 by @jakalijey).
- Ruby: `Dir.tmpdir`, `Dir.mktmpdir`, `Socket.gethostname`, `Process.pid`, `Process.uid` and
`Gem.user_home` count as reading the environment, as `Dir.home` already did, in `analyze` and
`bridge` (#1411 by @jakalijey).
diff --git a/src/analyze.rs b/src/analyze.rs
index c16f41e5..081d3513 100644
--- a/src/analyze.rs
+++ b/src/analyze.rs
@@ -533,11 +533,17 @@ fn check_bridgeable(root: &Path, report: &mut Report, lang: Lang) {
// The same call chain `bridge` follows: a helper in another unit carries the state it reads
// (issue #1042), so `bridgeable` has to count the same exports as `bridge`.
let outside = crate::bridge::outside_state_calls(lang, root, &mut outside_index, &foreign);
- bridgeable += foreign
+ // The reason `bridge` gives each kept export, in order: the first one names the note.
+ let kept: Vec<(String, String)> = foreign
.iter()
.zip(crate::bridge::state_through_calls_with(&foreign, &outside))
- .filter(|(f, state)| state.is_none() && crate::bridge::js_only_reason(f, &[]).is_none())
- .count();
+ .filter_map(|(f, state)| {
+ state
+ .or_else(|| crate::bridge::js_only_reason(f, &[]))
+ .map(|why| (f.name.clone(), why))
+ })
+ .collect();
+ bridgeable += foreign.len() - kept.len();
unit.exports = exports;
unit.bridgeable = bridgeable;
// A unit that stays in the original keeps its status, but its exports are still there.
@@ -546,18 +552,27 @@ fn check_bridgeable(root: &Path, report: &mut Report, lang: Lang) {
}
if exports > 0 && bridgeable == 0 {
unit.status = Status::Caution;
- unit.notes.insert(
- 0,
- if lang == Lang::C {
- "No export can cross the C ABI bridge (contexts, function pointers, structs by \
- value, pointers it cannot pair with a length): bridge a wrapper with plain \
- arguments, an API change."
- } else {
- "Every export is JS-shaped (functions, plugin objects, iterators): porting the \
- logic means adding a data-only function, an API change."
- }
- .into(),
- );
+ let note = match lang {
+ Lang::C => "No export can cross the C ABI bridge (contexts, function pointers, \
+ structs by value, pointers it cannot pair with a length): bridge a wrapper \
+ with plain arguments, an API change."
+ .to_string(),
+ Lang::Ts => "Every export is JS-shaped (functions, plugin objects, iterators): \
+ porting the logic means adding a data-only function, an API change."
+ .to_string(),
+ // Other languages keep an export for the reason `bridge` gives (#1402): the
+ // clock, the environment, app state, a type the wrapper cannot carry.
+ _ => match kept.first() {
+ Some((name, why)) => format!(
+ "No export can be bridged: `{name}` {why}. Pass what it reads in as a \
+ parameter, or change the signature, an API change."
+ ),
+ None => "No export can be bridged as it is: `bridge` names each export's \
+ reason; porting means an API change."
+ .to_string(),
+ },
+ };
+ unit.notes.insert(0, note);
}
}
let summary = &mut report.summary;
diff --git a/src/report.rs b/src/report.rs
index cf58caec..a84074c4 100644
--- a/src/report.rs
+++ b/src/report.rs
@@ -148,6 +148,28 @@ fn render(out: &mut String, r: &Report, title: &str) -> std::fmt::Result {
out,
"No unit to port first: every unit stays in the source language.\n"
)?;
+ } else if r
+ .units
+ .iter()
+ .any(|u| u.status == Status::Caution && u.exports > 0 && u.bridgeable == 0)
+ {
+ // The units export functions, but `bridge` keeps each one (#1402): say why here, as
+ // the per-unit details are only rendered for `start_here`.
+ writeln!(
+ out,
+ "No unit can be bridged as it is: these units export functions that stay in the source language.\n"
+ )?;
+ for u in r
+ .units
+ .iter()
+ .filter(|u| u.status == Status::Caution && u.exports > 0 && u.bridgeable == 0)
+ {
+ match u.notes.first() {
+ Some(note) => writeln!(out, "- `{}`: {note}", u.id)?,
+ None => writeln!(out, "- `{}`", u.id)?,
+ }
+ }
+ writeln!(out)?;
} else {
writeln!(
out,
diff --git a/tests/analyze.rs b/tests/analyze.rs
index a36286c4..05a4a55d 100644
--- a/tests/analyze.rs
+++ b/tests/analyze.rs
@@ -1337,6 +1337,43 @@ fn bridgeable_exports_are_counted_as_bridge_judges_them() {
assert_eq!((sum.exports, sum.bridgeable), (1, 1), "{sum:?}");
}
+#[test]
+fn a_unit_kept_for_the_clock_says_so_instead_of_js_shaped() {
+ // #1402: a Python function that reads the clock is a plain function, kept for the clock.
+ let root = std::env::temp_dir().join(format!("ra-analyze-kept-{}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&root);
+ std::fs::create_dir_all(root.join("stamp")).unwrap();
+ std::fs::write(
+ root.join("stamp/__init__.py"),
+ "import time\n\n\ndef stamp():\n return time.time()\n",
+ )
+ .unwrap();
+ std::fs::write(
+ root.join("pyproject.toml"),
+ "[project]\nname = \"stamp\"\nversion = \"0.1.0\"\n",
+ )
+ .unwrap();
+ let report = analyze(&root).unwrap();
+ let text = rust_anything::report::markdown(&report, "t");
+ std::fs::remove_dir_all(&root).unwrap();
+ let u = &report.units[0];
+ assert_eq!(u.status, Status::Caution, "{u:?}");
+ assert_eq!((u.exports, u.bridgeable), (1, 0), "{u:?}");
+ let note = &u.notes[0];
+ assert!(note.contains("`stamp`"), "{note}");
+ assert!(note.contains("time.time"), "{note}");
+ assert!(!note.contains("JS-shaped"), "{note}");
+ assert!(
+ !text.contains("constants, types or nothing a port could call"),
+ "{text}"
+ );
+ // The report itself names the reason, not only the JSON note.
+ assert!(
+ text.contains("`stamp`") && text.contains("time.time"),
+ "{text}"
+ );
+}
+
#[test]
fn powershell_modules_get_their_bridge_notes_and_import_order() {
let root =