From 6cdf0febd913e87f7874d4bfd3b0bee757f1322b Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 27 Nov 2026 18:40:45 -0800 Subject: [PATCH] Script check: a global rebind or a with body ends a literal's own saveAs - An assignment through the global object ($.global.local = d, and $.global[k] = d for every name) also clears the literal methods that an open function body or catch block keeps to restore for the name, so a body that declares the name does not bring the exemption back. - A call inside the body of a with statement is not treated as a call of a literal's own method, since the name may be a property of the statement's object. --- crates/scriptcheck/src/analyze.rs | 12 +++++ crates/scriptcheck/src/analyze/features.rs | 61 ++++++++++++++++++++-- crates/scriptcheck/src/tests.rs | 40 ++++++++++++++ 3 files changed, 109 insertions(+), 4 deletions(-) diff --git a/crates/scriptcheck/src/analyze.rs b/crates/scriptcheck/src/analyze.rs index 73b968c59e..6a8f628b00 100644 --- a/crates/scriptcheck/src/analyze.rs +++ b/crates/scriptcheck/src/analyze.rs @@ -663,6 +663,15 @@ struct Analyzer<'s, 'a, 'l> { global_indexes: Vec, /// The last token was the `]` that closes an index of the global object. global_index_closed: bool, + /// The `with` statement whose head is being read: the bracket depth of its `with`, and whether + /// its `)` was read. + with_head: Option<(usize, bool)>, + /// The bracket depths the bodies of the open `with` statements hold at, the innermost last, up + /// to [`crate::Limits::nesting`] of them ([`Analyzer::with_step`]). + withs: Vec, + /// A `with` statement had no room in [`Analyzer::withs`]: no call of a literal's own method is + /// known from then on. + withs_full: bool, /// This file or what the scan started with gives names values the analysis doesn't read /// ([`Declared::unsure`]): no object counts as a Folder object whatever the path names. unsure: bool, @@ -748,6 +757,9 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> { index_stable: false, global_indexes: vec![], global_index_closed: false, + with_head: None, + withs: vec![], + withs_full: false, unsure: known.unsure || declared.unsure, own_names: HashSet::new(), scopes: vec![], diff --git a/crates/scriptcheck/src/analyze/features.rs b/crates/scriptcheck/src/analyze/features.rs index 4e8ae90b61..43231690b6 100644 --- a/crates/scriptcheck/src/analyze/features.rs +++ b/crates/scriptcheck/src/analyze/features.rs @@ -113,6 +113,7 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> { } } } + self.with_step(t); self.writer_use(t); match t.kind { T::Ident => self.name(t, line), @@ -395,9 +396,61 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> { /// 0 for anything else. A receiver the check does not follow may be a document. /// Whether a call of `name`'s own method may be left out: the call is in the global code, or in a /// function body that declares `name` itself. A body that reads `name` from outside may run - /// after the variable was given another value, so its calls are not known. + /// after the variable was given another value, so its calls are not known. Inside the body of a + /// `with` statement the name may be a property of the statement's object, so no call is known + /// there ([`Analyzer::with_step`]). fn stable(&self, name: &str) -> bool { - self.scopes.last().is_none_or(|s| s.saved.contains_key(name)) + !self.withs_full && self.withs.is_empty() && self.scopes.last().is_none_or(|s| s.saved.contains_key(name)) + } + + /// Follows the `with` statements around `t`. A body in braces ends with its `}`; a body without + /// braces is taken to last until the block the statement is in closes. + fn with_step(&mut self, t: &Sig<'s>) { + let depth = self.depth(); + self.withs.retain(|d| *d <= depth); + match self.with_head { + Some((d, false)) if t.is(")") && depth == d => self.with_head = Some((d, true)), + Some((d, true)) => { + self.with_head = None; + let body = if t.is("{") { depth } else { d }; + if self.withs.len() < self.acc.limits.nesting { + self.withs.push(body); + } else { + self.withs_full = true; + } + } + _ => {} + } + if t.kind == T::Ident && t.text == "with" && !member_mark(self.h[0]) { + self.with_head = Some((depth, false)); + } + } + + /// `name` may have been given another value through the global object: neither the variable + /// nor the outer variables of that name that the open function bodies and `catch` blocks keep + /// to put back hold their literal's methods any longer. + fn forget_writer(&mut self, name: &str) { + self.writers.remove(name); + for s in &mut self.scopes { + if let Some(saved) = s.saved.get_mut(name) { + saved.writer = 0; + } + } + for c in self.catches.iter_mut().filter(|c| c.name == name) { + c.saved.writer = 0; + } + } + + /// Any variable may have been given another value through the global object + /// ([`Self::forget_writer`] for every name). + fn forget_writers(&mut self) { + self.writers.clear(); + for saved in self.scopes.iter_mut().flat_map(|s| s.saved.values_mut()) { + saved.writer = 0; + } + for c in &mut self.catches { + c.saved.writer = 0; + } } /// A function body uses `name`, which no literal has given methods and the body does not declare, @@ -510,10 +563,10 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> { None }; if let Some(name) = rebound { - self.writers.remove(&name); + self.forget_writer(&name); } else if global_index_closed { // `$.global[key] = …`: the key may name any of them. - self.writers.clear(); + self.forget_writers(); } } } diff --git a/crates/scriptcheck/src/tests.rs b/crates/scriptcheck/src/tests.rs index a3a9543936..add23af7cc 100644 --- a/crates/scriptcheck/src/tests.rs +++ b/crates/scriptcheck/src/tests.rs @@ -755,6 +755,46 @@ fn a_hoisted_function_that_changes_or_shares_the_variable_ends_the_own_save_as() assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[4, 7, 11, 13][..])); } +/// 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. +/// A body that declares the name and does not do that leaves them as they were. +#[test] +fn a_global_assignment_under_a_shadowing_declaration_ends_the_own_save_as() { + let r = report(concat!( + "var f = new File(p);\n", + "var local = {saveAs: function () {}};\n", + "function replace() { var local = 1; $.global.local = app.activeDocument; }\n", + "replace();\n", + "local.saveAs(f);\n", + "var calm = {saveAs: function () {}};\n", + "function keep() { var calm = 1; }\n", + "keep();\n", + "calm.saveAs(f);\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, 13][..])); +} + +/// Inside the body of a `with` statement a name may be a property of the statement's object, so a +/// call of a literal's method there is reported; after the body, and outside it, it is not. +#[test] +fn a_call_inside_a_with_body_is_not_the_own_save_as() { + let r = report(concat!( + "var f = new File(p);\n", + "var local = {saveAs: function () {}};\n", + "with ({local: app.activeDocument}) {\n", + " local.saveAs(f);\n", + "}\n", + "local.saveAs(f);\n", + "if (x) { with (o) local.saveAs(f); }\n", + "local.saveAs(f);\n", + )); + assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[4, 7][..])); +} + /// A table of shared names with no room left for one more means a function may share any name: no /// literal after it gives methods that are the script's own. #[test]