From 5709055a97ac68510221d0dc92b5c4eef54d576c Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 24 Nov 2026 23:59:59 -0800 Subject: [PATCH] Scripts: the checker finds print on documents.index() and open through a variable that holds app `documents.index()` and `app.documents.index()` now count as chains that give a document, so `app.documents.index(0).print(o)` is listed as a file write. A document method named in brackets (`app.documents['index'](0)`) gives a document as its dotted form does. `app.open(f)` is found from the member chain, so `var a = app; a.open(f)` reads a file, while `x.app.open(f)` and an `app` the script declares still do not. Member chains hold at least 64 characters while they are read, and the report cuts the listed chains to the echo limit, so a short echo limit no longer hides these calls. --- crates/scriptcheck/src/analyze.rs | 12 +++++++- crates/scriptcheck/src/analyze/features.rs | 36 +++++++++++++++++++--- crates/scriptcheck/src/catalog.rs | 5 ++- crates/scriptcheck/src/report.rs | 5 +-- crates/scriptcheck/src/tests.rs | 30 ++++++++++++++---- 5 files changed, 73 insertions(+), 15 deletions(-) diff --git a/crates/scriptcheck/src/analyze.rs b/crates/scriptcheck/src/analyze.rs index 11361f733a..04ebe4b0f2 100644 --- a/crates/scriptcheck/src/analyze.rs +++ b/crates/scriptcheck/src/analyze.rs @@ -410,6 +410,10 @@ struct Chain { group: Option, } +/// Characters a member chain holds at least, more than the longest chain the checks compare +/// (`app.documents.addDocumentNoUI().print()` has 39). +const CHAIN_CHARS: usize = 64; + impl Chain { fn new(root: String, bare: bool, line: usize) -> Self { let (chars, cut) = (root.chars().count(), root.ends_with('…')); @@ -779,10 +783,16 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> { // ---------- member chains ---------- + /// The most characters a member chain holds while it is read: [`crate::Limits::echo`], and at least + /// [`CHAIN_CHARS`], so a short echo limit cuts only the chains the report lists. + fn chain_cap(&self) -> usize { + self.acc.limits.echo.max(CHAIN_CHARS) + } + /// Whether the active chain takes `t` (a `.`, a member's name, `[` or `(`); any other token ends /// it. fn continue_chain(&mut self, t: &Sig<'s>) -> bool { - let cap = self.acc.limits.echo; + let cap = self.chain_cap(); let Some(c) = self.active.as_mut() else { return false }; if c.after_dot { c.after_dot = false; diff --git a/crates/scriptcheck/src/analyze/features.rs b/crates/scriptcheck/src/analyze/features.rs index 8e252c4dcf..b8a022d550 100644 --- a/crates/scriptcheck/src/analyze/features.rs +++ b/crates/scriptcheck/src/analyze/features.rs @@ -291,8 +291,31 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> { /// set aside until its `)` is one of [`catalog::DOM_DOCUMENTS`] followed by `called`, the method /// as the chain writes it (`.print()`, or `[]()` when brackets name it). fn document_print(&self, called: &str) -> bool { + self.called_on(called).is_some_and(|receiver| catalog::DOM_DOCUMENTS.contains(&receiver)) + } + + /// Whether this `(` calls a method of the application object: `receiver`, which `before` comes + /// before, is the application object ([`Self::app_receiver`]), or the document-model member chain + /// the `(` has just set aside until its `)` is `app` followed by `called`. The chain starts at `app` + /// or at a variable that holds it, never at a property named `app` or at an `app` the script declared. + fn app_call(&self, called: &str, receiver: Option>, before: Option>) -> bool { + self.app_receiver(receiver, before) || self.called_on(called) == Some(catalog::DOM_ROOTS[0]) + } + + /// Whether the method `name`, which a string literal in brackets named, gives a document: the + /// member chain this `(` has just set aside, with `name` called on its receiver, is one of + /// [`catalog::DOM_DOCUMENTS`]. + fn gives_document(&self, name: &str) -> bool { + let named = + |receiver: &str, d: &str| d.strip_prefix(receiver).and_then(|m| m.strip_prefix('.')).and_then(|m| m.strip_suffix("()")) == Some(name); + self.called_on("[]()").is_some_and(|receiver| catalog::DOM_DOCUMENTS.iter().any(|d| named(receiver, d))) + } + + /// What the document-model member chain that this `(` has just set aside until its `)` holds before + /// `called`, when the chain ends with `called`. + fn called_on(&self, called: &str) -> Option<&str> { let chain = self.suspended.last().filter(|c| c.wait.saturating_add(1) == self.depth()); - chain.and_then(|c| c.path.strip_suffix(called)).is_some_and(|receiver| catalog::DOM_DOCUMENTS.contains(&receiver)) + chain.and_then(|c| c.path.strip_suffix(called)) } /// `(` after a name: a call. @@ -306,10 +329,13 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> { if catalog::DOM_FILE_WRITES.contains(&name.as_str()) || (name == catalog::DOM_PRINT && self.document_print("[]()")) { self.acc.feature(FeatureId::FileWrite, line); self.acc.outside(OutsideId::FilesWritten, line, None); - } else if catalog::DOM_FILE_READS.contains(&name.as_str()) && self.app_receiver(h3, self.h4) { + } else if catalog::DOM_FILE_READS.contains(&name.as_str()) && self.app_call("[]()", h3, self.h4) { self.acc.feature(FeatureId::FileRead, line); - // `app['open'](f)` gives a document, as `app.open(f)` does. - let (depth, cap) = (self.depth(), self.acc.limits.echo); + } + // `app['open'](f)` and `app.documents['index'](0)` give a document, as `app.open(f)` and + // `app.documents.index(0)` do. + if self.gives_document(&name) { + let (depth, cap) = (self.depth(), self.chain_cap()); if let Some(c) = self.suspended.last_mut().filter(|c| c.wait.saturating_add(1) == depth) { c.name_call(&name, cap); } @@ -330,7 +356,7 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> { self.acc.feature(FeatureId::FileDialog, line); } else if catalog::FOLDER_READS.contains(&n) || (file && catalog::FILE_READS.contains(&n)) - || (catalog::DOM_FILE_READS.contains(&n) && self.app_receiver(h2, h3)) + || (catalog::DOM_FILE_READS.contains(&n) && self.app_call(&format!(".{n}()"), h2, h3)) { self.acc.feature(FeatureId::FileRead, line); } else if (file && catalog::FILE_WRITES.contains(&n)) diff --git a/crates/scriptcheck/src/catalog.rs b/crates/scriptcheck/src/catalog.rs index e747bcca71..832990368d 100644 --- a/crates/scriptcheck/src/catalog.rs +++ b/crates/scriptcheck/src/catalog.rs @@ -86,7 +86,8 @@ pub(crate) const DOM_PRINT: &str = "print"; // The member chains that give a document, whole and as the chains are written (`[]` for an index, // `()` for a call): https://ai-scripting.docsforadobe.dev/jsobjref/Application/ (`activeDocument`, // `documents`, `open`) and https://ai-scripting.docsforadobe.dev/jsobjref/Documents/ (`add`, `addDocument`, -// `addDocumentNoUI`, `getByName`). +// `addDocumentNoUI`, `getByName`). `index` is not on that page; it comes from VectorCraft's own binding of +// the collection (`crates/engine/src/script/dom.rs`), which returns the document at a position or with a name. pub(crate) const DOM_DOCUMENTS: &[&str] = &[ "activeDocument", "app.activeDocument", @@ -100,6 +101,8 @@ pub(crate) const DOM_DOCUMENTS: &[&str] = &[ "app.documents.addDocumentNoUI()", "documents.getByName()", "app.documents.getByName()", + "documents.index()", + "app.documents.index()", "app.open()", ]; diff --git a/crates/scriptcheck/src/report.rs b/crates/scriptcheck/src/report.rs index b21feb20e6..6142045a3e 100644 --- a/crates/scriptcheck/src/report.rs +++ b/crates/scriptcheck/src/report.rs @@ -342,10 +342,11 @@ impl<'l> Acc<'l> { } pub fn member(&mut self, path: &str) { - if let Some(n) = self.members.get_mut(path) { + let key = self.echoed(path); + if let Some(n) = self.members.get_mut(&*key) { *n = n.saturating_add(1); } else if self.room(self.members.len()) { - self.members.insert(path.to_string(), 1); + self.members.insert(key.into_owned(), 1); } } diff --git a/crates/scriptcheck/src/tests.rs b/crates/scriptcheck/src/tests.rs index bb63f00918..1be9aeddbf 100644 --- a/crates/scriptcheck/src/tests.rs +++ b/crates/scriptcheck/src/tests.rs @@ -559,8 +559,8 @@ fn save_as_and_export_file_in_brackets_write_a_file() { } /// `app.open` reads the file it is given, called by name or with its name as a string literal in -/// brackets; a document has no `open` method, and a property or a variable named `app` is not the -/// application. +/// brackets, also through a variable that holds the application; a document has no `open` method, and +/// a property or a variable named `app` is not the application. #[test] fn app_open_reads_a_file() { let r = report(concat!( @@ -570,16 +570,28 @@ fn app_open_reads_a_file() { "app.activeDocument['open'](f);\n", "x.app.open(f);\n", "x.app['open'](f);\n", + "var a = app; a.open(f);\n", + "a['open'](f);\n", + "var b = x.app; b.open(f); b['open'](f);\n", )); - assert_eq!(feature(&r, "file.read").map(|f| f.lines.as_slice()), Some(&[2, 3][..])); + assert_eq!(feature(&r, "file.read").map(|f| f.lines.as_slice()), Some(&[2, 3, 7, 8][..])); assert!(r.outside.is_empty()); let r = report("var app = {};\napp.open(f);\napp['open'](f);\n"); assert!(feature(&r, "file.read").is_none()); + // A short echo limit cuts the member chains the report lists, not what the check finds. + let short = Limits { echo: 4, ..Limits::default() }; + let src = + "app.open(f);\napp['open'](f);\nvar a = app; a.open(f);\nvar d = app['open'](f); d.print(o);\napp.documents.addDocumentNoUI('P').print(o);\n"; + let r = check("t.jsx", src.as_bytes(), None, &short); + assert_eq!(feature(&r, "file.read").map(|f| f.lines.as_slice()), Some(&[1, 2, 3, 4][..])); + assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[4, 5][..])); + assert!(!r.members.is_empty()); + assert!(r.members.iter().all(|m| m.name.chars().count() <= short.echo)); } /// A document's `print` writes the file its options' `jobOptions.file` names, called with a dot or -/// with its name in brackets, on a document (also one `add`, `addDocument`, `addDocumentNoUI` or -/// `app.open`, with or without brackets, returns) or a variable that holds one; reading the method without +/// with its name in brackets, on a document (also one `add`, `addDocument`, `addDocumentNoUI`, `getByName`, +/// `index` or `app.open`, with or without brackets, returns) or a variable that holds one; reading the method without /// calling it, and a `print` method of another object, do not. #[test] fn print_writes_a_file() { @@ -600,8 +612,14 @@ fn print_writes_a_file() { "app.documents.addDocumentNoUI('Print').print(o);\n", "var g = app['open'](new File(p)); g.print(o);\n", "var h = app['activeDocument'](new File(p)); h.print(o);\n", + "app.documents.index(0).print(o);\n", + "documents.index('Print')['print'](o);\n", + "var i = app.documents.index(1); i.print(o);\n", + "app.documents['index'](0).print(o);\n", + "var j = documents['getByName']('Print'); j['print'](o);\n", + "app.documents['length'](0).print(o);\n", )); - let lines = &[2, 3, 5, 9, 10, 11, 12, 13, 14, 15][..]; + let lines = &[2, 3, 5, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21][..]; assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(lines)); assert_eq!(r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect::>(), [("filesWritten", lines)]); }