Scripts: a global property assignment in a function ends a later literal's own saveAs

A function body that assigns `$.global.name`, `globalThis.name` or
`$.global['name']` may run after a later literal gives `name` its methods,
as `this.name` already did. The assignment now records the name as
reassigned later, so the literal's later calls keep reporting the write.
An assignment in the global code runs once and still leaves a later
literal's methods the script's own.

The shadowing test is split so the named-global case and the computed-key
case each check their own line.
This commit is contained in:
dotCooCoo committed 2026-12-01 08:47:08 -08:00
1 parent 406a612d7b
commit 00cf8a148f
2 files changed
+42 -2

No files matched your search

@@ -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.
+38 -2
View File
@@ -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][..]));
}