diff --git a/crates/engine/src/script/dom/save_open/screens.rs b/crates/engine/src/script/dom/save_open/screens.rs index 893127aab6..e179eae1dc 100644 --- a/crates/engine/src/script/dom/save_open/screens.rs +++ b/crates/engine/src/script/dom/save_open/screens.rs @@ -38,6 +38,7 @@ //! accepted at its default and throws at any other value, and a property the pages do not list //! throws an error that names it, unless it is `undefined`. The options object's class must be //! the one of `exportFormat`, and an omitted options object takes the defaults a new one holds. +//! `null` in place of the options, `itemToExport` or `fileNamePrefix` throws a TypeError. use std::rc::Rc; @@ -442,7 +443,10 @@ fn doc_export_for_screens(h: &Host, a: &[Data]) -> Answer { let doc = this_doc(h, a)?; let folder = folder_path(arg(a, 1))?; let (mut row, class) = format(arg(a, 2))?; - let options = given(arg(a, 3)).filter(|d| !matches!(d, Data::Null)); + let options = given(arg(a, 3)); + if matches!(options, Some(Data::Null)) { + return Err(HostError::Type("the options must be an object".into())); + } let mut q = params(class, options)?; if class.name == PNG24.name { background(&mut q); @@ -453,8 +457,8 @@ fn doc_export_for_screens(h: &Host, a: &[Data]) -> Answer { size(&mut q)?; } row.extend(q); - let exports = exports(given(arg(a, 4)).filter(|d| !matches!(d, Data::Null)))?; - let prefix = match given(arg(a, 5)).filter(|d| !matches!(d, Data::Null)) { + let exports = exports(given(arg(a, 4)))?; + let prefix = match given(arg(a, 5)) { None => None, Some(Data::String(s)) if s.len() > MAX_TEXT_BYTES => return Err(HostError::Range("fileNamePrefix is too long".into())), Some(Data::String(s)) => Some(s.clone()), diff --git a/crates/engine/src/tests/script_screens.rs b/crates/engine/src/tests/script_screens.rs index ed2a4fbe69..bf9887423b 100644 --- a/crates/engine/src/tests/script_screens.rs +++ b/crates/engine/src/tests/script_screens.rs @@ -466,6 +466,9 @@ fn bad_arguments_are_refused_with_an_error() { "has no option artboard", ), (format!("{e}({f}, ExportForScreensType.SE_PNG24, undefined, undefined, 5);"), "fileNamePrefix must be a string"), + (format!("{e}({f}, ExportForScreensType.SE_PNG24, null);"), "the options must be an object"), + (format!("{e}({f}, ExportForScreensType.SE_PNG24, undefined, null);"), "ExportForScreensItemToExport"), + (format!("{e}({f}, ExportForScreensType.SE_PNG24, undefined, undefined, null);"), "fileNamePrefix must be a string"), (format!("var i = new ExportForScreensItemToExport(); i.artboards = '3'; {e}({f}, ExportForScreensType.SE_PNG24, undefined, i);"), "range"), ]; for (case, wanted) in &cases { diff --git a/crates/scriptcheck/src/analyze.rs b/crates/scriptcheck/src/analyze.rs index 9088c26367..9621cc24a0 100644 --- a/crates/scriptcheck/src/analyze.rs +++ b/crates/scriptcheck/src/analyze.rs @@ -703,6 +703,9 @@ struct Analyzer<'s, 'a, 'l> { /// [`Analyzer::shared_early`] had no room for a name: no literal gives methods that are the /// script's own from then on. shared_full: bool, + /// The function body that the token being read closed (its `}`): how many bodies were open + /// in it and the names it declared. A value read just before the `}` is read in that body. + closed_body: Option<(usize, Vec)>, /// The object literals being assigned to a variable, the innermost last, up to /// [`crate::Limits::nesting`] of them. objects: Vec, @@ -823,6 +826,7 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> { writers: HashMap::new(), shared_early: HashMap::new(), shared_full: false, + closed_body: None, objects: vec![], closed: None, index_receiver: 0, @@ -917,6 +921,7 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> { fn token(&mut self, mut t: Sig<'s>) { self.acc.steps = self.acc.steps.saturating_add(1); + self.closed_body = None; // The name of a grouped value whose last `)` was read, or of a value without parentheses that // `t` ends, holds the value, or nothing the check follows, before `t` starts a member chain or // closes a function body. @@ -1240,7 +1245,11 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> { /// in them gets back what it held outside them, and the `var` statement a body was in goes on. fn close_scopes(&mut self) { while self.scopes.last().is_some_and(|s| self.depth() < s.depth) { + let open = self.scopes.len(); let Some(scope) = self.scopes.pop() else { return }; + if self.closed_body.is_none() { + self.closed_body = Some((open, scope.saved.keys().cloned().collect())); + } self.var_depth = scope.var_depth; for (name, saved) in scope.saved { self.restore(name, saved); diff --git a/crates/scriptcheck/src/analyze/features.rs b/crates/scriptcheck/src/analyze/features.rs index 6aa9c709be..46eda57b4b 100644 --- a/crates/scriptcheck/src/analyze/features.rs +++ b/crates/scriptcheck/src/analyze/features.rs @@ -536,12 +536,18 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> { /// [`Analyzer::share_early`], and with `global` set, for the global variable `name` read through /// the global object (`$.global.local`) even when the body declares a variable of that name. fn share_early_as(&mut self, name: &str, global: bool) { - let Some(body) = self.scopes.last() else { return }; - if (body.saved.contains_key(name) && !global) || self.writers.contains_key(name) { + // A value read just before the `}` that closes a function body is read in that body. + let (open, declared) = match &self.closed_body { + Some((open, names)) => (*open, names.iter().any(|n| n == name)), + None => match self.scopes.last() { + Some(body) => (self.scopes.len(), body.saved.contains_key(name)), + None => return, + }, + }; + if (declared && !global) || self.writers.contains_key(name) { return; } if self.shared_early.contains_key(name) || self.acc.room(self.shared_early.len()) { - let open = self.scopes.len(); let seen = self.shared_early.entry(name.to_string()).or_insert(open); *seen = (*seen).max(open); } else { diff --git a/crates/scriptcheck/src/tests.rs b/crates/scriptcheck/src/tests.rs index 41f15d5942..39851eecd6 100644 --- a/crates/scriptcheck/src/tests.rs +++ b/crates/scriptcheck/src/tests.rs @@ -1515,6 +1515,27 @@ fn a_bracketed_global_member_used_as_a_value_ends_the_own_save_as() { assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[5, 10][..])); } +/// A hoisted function body whose last value, read without a `;` before its closing `}`, is the +/// variable shares it with its caller, as one that ends with `;` does. +#[test] +fn a_variable_read_just_before_a_function_body_closes_ends_the_own_save_as() { + for read in ["$.global['local']", "$.global.local", "this.local", "local"] { + let r = report(&format!( + concat!( + "var f = new File(p);\n", + "function get() {{ return {} }}\n", + "var local = {{saveAs: function () {{}}}};\n", + "get().saveAs = function (f) {{\n", + " app.activeDocument.saveAs(f);\n", + "}};\n", + "local.saveAs(f);\n", + ), + read + )); + assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[5, 7][..]), "{read}"); + } +} + /// `$.global.local.saveAs = …` in a function whose parameter is named `local` replaces the global /// variable's method; the parameter alone leaves the outer literal's `saveAs` the script's own. #[test] diff --git a/docs/mcp.md b/docs/mcp.md index db4f400083..d535585121 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -3562,7 +3562,7 @@ through `$`: `SVGFontType.SVGFONT`, the default, writes text as SVG text elements. A listed option that is not mapped (`matteColor`) is accepted at its default and throws at any other value, a property the pages do not list throws an error that names it, an omitted options object takes the defaults a new one holds, and an options object of - another class throws. A bad option value throws an error before anything is written. Each export checks the scale of + another class throws. `null` in place of the options, `itemToExport` or `fileNamePrefix` throws a TypeError. A bad option value throws an error before anything is written. Each export checks the scale of each of its items before it writes, and an item too large to draw throws after the files before it are written. A size gives each region its own scale, so a size that only a later export's items make out of range throws after the earlier exports' files are written. The warnings of each export go to the console after `exportForScreens: `.