diff --git a/crates/scriptcheck/src/analyze/features.rs b/crates/scriptcheck/src/analyze/features.rs index 858e8b652e..69b101540d 100644 --- a/crates/scriptcheck/src/analyze/features.rs +++ b/crates/scriptcheck/src/analyze/features.rs @@ -605,6 +605,10 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> { None }; if let Some(name) = rebound { + // In a function body: the function may run after a later literal gives the name methods. + if !self.scopes.is_empty() { + self.reassigned_later(&name); + } self.forget_writer(&name); } else if global_index_closed { // `$.global[key] = …`: the key may name any of them. diff --git a/crates/scriptcheck/src/tests.rs b/crates/scriptcheck/src/tests.rs index 28812081de..f57792a5b4 100644 --- a/crates/scriptcheck/src/tests.rs +++ b/crates/scriptcheck/src/tests.rs @@ -933,8 +933,7 @@ fn a_hoisted_function_that_changes_or_shares_the_variable_ends_the_own_save_as() /// A function body that declares the variable's name itself can still give the variable another /// value through the global object: the variable's methods are not put back when the body closes. -/// An assignment under a computed key of the global object may give any variable another value, so -/// after one anywhere in the file every call is reported. +/// A body that only declares the name leaves the methods the script's own. #[test] fn a_global_assignment_under_a_shadowing_declaration_ends_the_own_save_as() { let r = report(concat!( @@ -947,11 +946,48 @@ fn a_global_assignment_under_a_shadowing_declaration_ends_the_own_save_as() { "function keep() { var calm = 1; }\n", "keep();\n", "calm.saveAs(f);\n", + )); + assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[5][..])); +} + +/// An assignment under a computed key of the global object may give any variable another value, so +/// after one anywhere in the file every call is reported. +#[test] +fn a_computed_global_assignment_ends_every_own_save_as() { + let r = report(concat!( + "var f = new File(p);\n", "var other = {saveAs: function () {}};\n", "function any() { var other = 1; $.global[k] = app.activeDocument; }\n", "any();\n", "other.saveAs(f);\n", )); + assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[5][..])); +} + +/// A function declared before the literal may run after it and give the variable another value +/// through any form of a global object property: `$.global.name`, `globalThis.name` and +/// `$.global['name']`. The literal's later calls keep reporting the write. An assignment in the +/// global code runs once, before a later literal, and leaves that literal's methods the script's own. +#[test] +fn a_hoisted_global_property_assignment_ends_a_later_own_save_as() { + let r = report(concat!( + "var f = new File(p);\n", + "function replace() { $.global.local = app.activeDocument; }\n", + "var local = {saveAs: function () {}};\n", + "replace();\n", + "local.saveAs(f);\n", + "function swap() { globalThis.other = app.activeDocument; }\n", + "var other = {saveAs: function () {}};\n", + "swap();\n", + "other.saveAs(f);\n", + "function put() { $.global['third'] = app.activeDocument; }\n", + "var third = {saveAs: function () {}};\n", + "put();\n", + "third.saveAs(f);\n", + "$.global.fresh = 1;\n", + "var fresh = {saveAs: function () {}};\n", + "fresh.saveAs(f);\n", + )); assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[5, 9, 13][..])); }