Scripts: a document named in brackets counts for print's file write

The script check now reads a string literal in index brackets as the
member it names when the document chains list that member as a
property, so app['activeDocument'].print(o) and
app['documents'][0].print(o) report a file write. Other names in the
brackets, such as app['preferences'], keep the index and do not give a
document.
This commit is contained in:
dotCooCoo committed 2026-11-25 15:21:27 -08:00
1 parent 3630e01602
commit 0359cc0b93
2 files changed
+52

No files matched your search

+33
View File
@@ -463,6 +463,29 @@ impl Chain {
self.append(if call { "()" } else { "[]" }, cap);
}
/// The index that ends the chain is of the member `name`, which a string literal in brackets
/// named: `[]` becomes `.name` when one of [`catalog::DOM_DOCUMENTS`] reads that member as a
/// property (`app['activeDocument']`, `app['documents'][0]`). Any other name keeps the index.
fn name_index(&mut self, name: &str, cap: usize) {
let Some(base) = self.path.strip_suffix("[]").filter(|_| !self.cut) else { return };
let property = |d: &&str| {
d.strip_prefix(base)
.and_then(|m| m.strip_prefix('.'))
.and_then(|m| m.strip_prefix(name))
.is_some_and(|rest| rest.is_empty() || rest.starts_with('.') || rest.starts_with('['))
};
let ident = !name.is_empty() && name.chars().all(|c| c.is_alphanumeric() || c == '_' || c == '$');
if !ident || !catalog::DOM_DOCUMENTS.iter().any(property) {
return;
}
self.path.truncate(self.path.len() - 2);
self.chars = self.chars.saturating_sub(2);
self.last_change = false;
for s in [".", name] {
self.append(s, cap);
}
}
/// The call that ends the chain is of the member `name`, which a string literal in brackets
/// named: `[]()` becomes `.name()`.
fn name_call(&mut self, name: &str, cap: usize) {
@@ -1177,6 +1200,16 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> {
}
if self.suspended.last().is_some_and(|c| c.wait == self.depth()) {
self.active = self.suspended.pop();
// `app['activeDocument']`: a string literal in the brackets names a member.
if t.text == "]"
&& is(self.h[1], "[")
&& let Some(name) = self.h[0].filter(|s| s.kind == T::Str).map(|s| unquote(s.text, 32))
{
let cap = self.chain_cap();
if let Some(c) = self.active.as_mut() {
c.name_index(&name, cap);
}
}
}
self.close_scopes();
let grouped = self.close_parens(t);
+19
View File
@@ -624,6 +624,25 @@ fn print_writes_a_file() {
assert_eq!(r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect::<Vec<_>>(), [("filesWritten", lines)]);
}
/// A string literal in brackets that names `activeDocument` or `documents` reads that property, so the
/// `print` of the document it gives writes a file; any other name in the brackets, a name that is not
/// a property's, and a computed name do not give a document.
#[test]
fn print_on_a_document_named_in_brackets_writes_a_file() {
let r = report(concat!(
"var o = new PrintOptions(); o.jobOptions.file = new File(p);\n",
"app['activeDocument'].print(o);\n",
"app[\"documents\"][0].print(o);\n",
"var d = app['documents'][1]; d.print(o);\n",
"app['documents']['add']().print(o);\n",
"app['preferences'].print(o); app['activeDocuments'].print(o); app['documents[]'].print(o);\n",
"app['documents'].print(o); app[k].print(o); x.app['activeDocument'].print(o);\n",
));
let lines = &[2, 3, 4, 5][..];
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::<Vec<_>>(), [("filesWritten", lines)]);
}
/// Setting a File object's length, hidden or read-only flag changes the file; reading them, comparing
/// them and setting them on another object does not.
#[test]