Script checker: setting a File's length, hidden or readonly counts as a file write

Setting a File object's length truncates or pads the file, and setting
readonly or hidden changes it, but script.check reported no outside
category for those assignments. It now reports file.write and
filesWritten for them: plain and compound assignment, ++ and -- before
or after, and the property name as a string literal in brackets.

A new integration test lists every global and member the script host
adds to the JavaScript engine (the own properties of the global object,
of each object or function it holds and of each function's prototype,
compared with a plain engine) and fails when one has no row. Each name
that reaches outside the document has a probe script, and the test
checks that script.check gives it the expected outside id. vc is listed
as not checked: script.check reads scripts written for the reference
app's scripting, and what vc.run does depends on the command. The test
also checks that the checker lists every preprocessor directive the
host reads, through the new DirectiveName::ALL.

File object properties:
https://extendscript.docsforadobe.dev/file-system-access/file-object/
This commit is contained in:
dotCooCoo
2026-11-20 18:36:13 -08:00
parent 063f550802
commit 417ed94431
8 changed files with 273 additions and 19 deletions
+4
View File
@@ -239,6 +239,10 @@ enum Pending<'s> {
MenuString(Option<usize>, &'s str),
/// `.open(`: a string literal with the open mode may follow.
Open(Option<usize>),
/// A File object's `length`, `hidden` or `readonly` (`f.length`, `f['length']`): an assignment
/// or an update (`++`, `--`) that sets it may follow. With `true`, a prefix update came before
/// the receiver, and it sets the property unless a member or a call follows it.
Setter(Option<usize>, bool),
/// A platform name where a property key or a label may stand (first, or after `{`, `,`, `;` or
/// `}`): it is a key or a label when `:` follows.
Name(&'s str, Option<usize>),
+49 -6
View File
@@ -1,6 +1,6 @@
//! The platform features, menu commands and later syntax the analysis recognises.
use super::{Analyzer, FeatureId, NEWER, OutsideId, Pending, Sig, T, catalog, is, member_mark, newer_number, report, unquote, word};
use super::{Analyzer, COMPOUND, FeatureId, NEWER, OutsideId, Pending, Sig, T, catalog, is, member_mark, newer_number, report, unquote, word};
/// A global name of the platform the report looks for (`$`, `File`, `Socket`, `alert`, `eval`…).
pub(super) fn platform_name(n: &str) -> bool {
@@ -72,6 +72,17 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> {
self.open_mode(&unquote(t.text, 2), l);
}
}
Pending::Setter(l, prefix) => {
let sets = if prefix {
!t.is(".") && !t.is("[") && !t.is("(")
} else {
// An update operator after a line break belongs to the next line's operand.
t.kind == T::Punct && (t.text == "=" || COMPOUND.contains(&t.text) || (matches!(t.text, "++" | "--") && !t.nl))
};
if sets {
self.file_setter(l);
}
}
}
match t.kind {
T::Ident => self.name(t, line),
@@ -113,7 +124,29 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> {
match pending {
Pending::Window(l) | Pending::WindowType(l) => self.acc.feature(FeatureId::ScriptuiWindow, l),
Pending::Menu(l) | Pending::MenuString(l, _) => self.menu(None, l),
Pending::Open(_) | Pending::Name(..) | Pending::None => {}
Pending::Setter(l, true) => self.file_setter(l),
Pending::Open(_) | Pending::Setter(_, false) | Pending::Name(..) | Pending::None => {}
}
}
/// A File object's `length`, `hidden` or `readonly` set at `line`: the file changes.
fn file_setter(&mut self, line: Option<usize>) {
self.acc.feature(FeatureId::FileWrite, line);
self.acc.outside(OutsideId::FilesWritten, line, None);
}
/// `name`, a property of `receiver` read with `.` or as a string literal in brackets, `before`
/// coming before the receiver: when it is a File object's setter, the next token says whether it
/// is set ([`Pending::Setter`]). An update operator in `before` is a prefix (`++f.length`) when
/// the receiver is on its line, or when it starts a line or comes where an expression may start;
/// otherwise it ends the operand before it (`i++` and a line break, then `f.length`).
fn setter(&mut self, name: &str, receiver: Option<Sig<'s>>, before: Option<Sig<'s>>, line: Option<usize>) {
if catalog::FILE_SETTERS.contains(&name) && self.file_receiver(receiver, before) {
// Before a receiver that ends in `)` (`new File(i++).length`), the operator is inside the
// parentheses.
let update = before.filter(|b| (b.is("++") || b.is("--")) && receiver.is_some_and(|r| r.kind == T::Ident));
let prefix = update.is_some_and(|b| b.nl || b.expr || receiver.is_some_and(|r| !r.nl));
self.pending = Pending::Setter(line, prefix);
}
}
@@ -170,6 +203,7 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> {
if n == catalog::REFLECT {
self.acc.feature(FeatureId::Reflection, line);
}
self.setter(n, h1, h2, line);
return;
}
if member_mark(h0) {
@@ -231,6 +265,11 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> {
fn punct(&mut self, t: &Sig<'s>, line: Option<usize>) {
let [h0, h1, h2, h3] = self.h;
match t.text {
// `f['length']`: a File object's setter may follow.
"]" if is(h1, "[") && h0.is_some_and(|s| s.kind == T::Str) => {
let name = h0.map(|s| unquote(s.text, 16)).unwrap_or_default();
self.setter(&name, h2, h3, line);
}
"(" => self.call(line),
// `Name.prototype["+"] = …` defines an operator.
"=" => {
@@ -251,10 +290,7 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> {
let Some(callee) = h0.filter(|s| s.kind == T::Ident) else { return };
let n = callee.text;
if is(h1, ".") {
// The receiver is a variable holding a File or Folder object (not a property of the same
// name), or a File or Folder object made in place or held in grouping parentheses
// ([`Sig::file`]).
let file = h2.is_some_and(|r| r.file || (r.kind == T::Ident && !member_mark(h3) && self.files.contains(r.text)));
let file = self.file_receiver(h2, h3);
if n == catalog::MENU_COMMAND {
self.pending = Pending::Menu(line);
} else if n == catalog::OPEN && file {
@@ -288,6 +324,13 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> {
}
}
/// Whether `receiver`, which `before` comes before, is a variable holding a File or Folder object
/// (not a property of the same name), or a File or Folder object made in place or held in
/// grouping parentheses ([`Sig::file`]).
fn file_receiver(&self, receiver: Option<Sig<'s>>, before: Option<Sig<'s>>) -> bool {
receiver.is_some_and(|r| r.file || (r.kind == T::Ident && !member_mark(before) && self.files.contains(r.text)))
}
fn string(&mut self, t: &Sig<'s>, line: Option<usize>) {
let [h0, h1, h2, _] = self.h;
if t.text.contains(catalog::ACTION_TEXT) {
+3
View File
@@ -32,6 +32,9 @@ pub(crate) const FOLDER: &str = "Folder";
pub(crate) const FILE_READS: &[&str] = &["read", "readch", "readln"];
pub(crate) const FOLDER_READS: &[&str] = &["getFiles"];
pub(crate) const FILE_WRITES: &[&str] = &["write", "writeln", "remove", "rename", "copy", "createAlias", "create"];
/// The properties of a File object whose setting changes the file: its length, and its hidden and
/// read-only flags.
pub(crate) const FILE_SETTERS: &[&str] = &["length", "hidden", "readonly"];
pub(crate) const FILE_DIALOGS: &[&str] = &["openDialog", "saveDialog"];
pub(crate) const FOLDER_DIALOGS: &[&str] = &["selectDialog"];
pub(crate) const INSTANCE_DIALOGS: &[&str] = &["openDlg", "saveDlg", "selectDlg"];
+34
View File
@@ -530,6 +530,40 @@ fn the_scripts_own_platform_names_are_not_the_platforms() {
assert_eq!(r.outside.iter().map(|o| o.id).collect::<Vec<_>>(), ["filesWritten"]);
}
/// 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]
fn setting_a_files_length_or_flags_writes_it() {
let r = report(concat!(
"var f = new File(p);\n",
"f.length = 0;\n",
"f.readonly = true;\n",
"f.hidden |= 1;\n",
"f.length++;\n",
"new File(q).length = 4;\n",
"if (x) --f.length;\n",
"f['readonly'] = false;\n",
"++\n",
"f.length;\n",
"var k = ++\n",
"f['length'];\n",
"++f.length.x; var z = new File(i++).length;\n",
"var n = f.length;\n",
"if (f.readonly == true) {}\n",
"o.length = 0;\n",
"o.f.length = 0;\n",
"var s = [1]; s.length = 0;\n",
"var m = f['length'];\n",
"i++\n",
"f.length;\n",
"f.length\n",
"++i;\n",
));
let outside: Vec<(&str, &[usize])> = r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect();
assert_eq!(outside, [("filesWritten", &[2, 3, 4, 5, 6, 7, 8, 10, 12][..])]);
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[2, 3, 4, 5, 6, 7, 8, 10, 12][..]));
}
#[test]
fn a_function_after_a_line_that_ends_a_statement_is_a_declaration() {
let r = report("var n = 1\nfunction File() {}\nFile('x');\nvar m = 2 /* a\n */ function Socket() {}\nnew Socket();\n");