Files
vectorcraft/crates/scriptcheck/src/tests.rs
T
dotCooCoo ccf5765419 Tests: assert counts and saved names in tool, path, 3D, brush and CAD tests
Tests whose all() passed on an empty list now check the list's length or
that it is not empty:
- huge_circles_bridge_with_finite_anchors_or_are_refused (pathops bridge)
- smooth_rounds_corners (pathops ops): the four corners stay, smoothed
- a_held_rotation_turns_the_art_around_its_pin (tools ARAP): one point
  per mesh vertex
- the_eraser_drag_area_matches_the_release_when_pressure_or_a_mode_starts_during_the_drag
  (tools gesture): one released point per press
- every_effect_casts_a_shadow, inflate_cells_across_a_hole_edge_are_cut_to_the_shape,
  a_plane_has_only_a_front_and_a_hollow_solid_none and
  perspective_runs_0_to_160_degrees_with_the_eye_outside_the_object (three-d)
- a_file_two_branches_include_is_followed_from_the_shallower_one
  (scriptcheck): one row per include line
- pens_pick_colors_and_widths (cad HP-GL): four widths
- junk_pressure_brushes_draw_finite_art (brush): all 12 cases draw

Round trips that hold by construction (parse finds the id in ALL) are
replaced with one saved name that must parse and one that must not:
follow_names_parse (pathops shared edge), the shape kinds (pathops
shapes), surfaces_parse_from_their_ids (three-d), the Quick Pen actions
(tools), names_parse_and_serialize_as_names (plugins) and the brush
variation modes, which also compare the serde names with the ids.

crop_keeps_an_instance_whose_art_reaches_the_region (cad) names the
entities written instead of checking that some were.
the_saved_engine_preference_is_found (app) pins the GPU choices the
preferences offer to the three the app reads.
timing_unite_1000_circles (pathops) gets an ignore reason and checks
that Unite gives one shape.

Each changed assertion fails when the code under test is broken (an id
renamed, a list emptied, a flush or instance dropped) and passes when it
is restored.
2026-12-01 17:35:21 -08:00

3005 lines
137 KiB
Rust

//! Tests on synthetic scripts, each written here from the language rules with invented names.
use proptest::prelude::*;
use crate::catalog::FeatureId;
use crate::lex::{Lexer, T};
use crate::*;
fn report(src: &str) -> Report {
check("test.jsx", src.as_bytes(), None, &Limits::default())
}
/// The kinds and texts of the tokens of `src`, blanks and line breaks left out.
fn tokens(src: &str) -> Vec<(T, &str)> {
Lexer::new(src, Limits::default().nesting).filter(|t| !matches!(t.kind, T::Space | T::Newline)).map(|t| (t.kind, t.text)).collect()
}
fn feature<'r>(r: &'r Report, id: &str) -> Option<&'r Feature> {
r.features.iter().find(|f| f.id == id)
}
fn problems(r: &Report) -> Vec<(&'static str, Option<usize>, Option<&str>)> {
r.problems.iter().map(|p| (p.code, p.line, p.detail.as_deref())).collect()
}
/// The script `src` with one feature, and one without it: `(id, with, without)`.
const FEATURE_CASES: &[(&str, &str, &str)] = &[
("actions", "app.doScript('Set', 'Group');", "doScript('Set', 'Group');"),
("actions", "var text = '/version 3 /actionCount 1';", "var text = '/version 3';"),
("bridgeTalk", "var bt = new BridgeTalk();", "var bt = new Bridge();"),
("directive.include", "#include 'a.jsxinc'", "var include = 'a.jsxinc';"),
("directive.include", "#includepath 'lib'", "// #includepath 'lib'"),
("directive.other", "#strict on", "var strict = 'on';"),
("directive.target", "#target acme", "// #target acme"),
("directive.target", " //@target acme", "x(); //@target acme"),
("directive.targetengine", "#targetengine session", "var targetengine = 'session';"),
("dollar", "$.writeln('hi');", "log.writeln('hi');"),
("dollar", "$.localize = true;", "obj.localize = true;"),
("dollar.env", "var home = $.getenv('HOME');", "var home = env.getenv('HOME');"),
("dollar.evalFile", "$.evalFile(f);", "loader.evalFile(f);"),
("dynamicCode", "eval(code);", "safe.eval(code);"),
("dynamicCode", "var f = new Function('a', 'return a');", "var f = function (a) { return a; };"),
("e4x", "var x = <root><item id=\"1\"/>text {v}</root>;", "var x = a < b;"),
("e4x", "var id = node.@id;", "var id = node.id;"),
("e4x", "var items = node..item;", "var items = node.item;"),
("e4x", "for each (var v in list) {}", "for (var k in list) {}"),
("e4x", "default xml namespace = ns;", "var namespace = ns;"),
("externalObject", "var lib = new ExternalObject('lib:' + name);", "var lib = new External(name);"),
("file.dialog", "var f = File.openDialog('Pick');", "var f = picker.openDialog('Pick');"),
("file.dialog", "var d = Folder.selectDialog();", "var d = Folder.select();"),
("file.dialog", "var f = target.saveDlg('Save');", "var f = target.save('Save');"),
("file.execute", "out.execute();", "out.executeAll();"),
("file.objects", "var f = new File(path);", "var f = new Filer(path);"),
("file.places", "var t = Folder.temp;", "var t = cache.temp;"),
("file.read", "var f = new File(p); f.open('r');", "var f = new File(p);"),
("file.read", "var f = new File(p); var s = f.read();", "var s = cache.read();"),
("file.read", "var all = folder.getFiles();", "var all = folder.files;"),
("file.read", "var s = File(p).read();", "var s = Cache(p).read();"),
("file.write", "var f = new File(p); f.open('w');", "var f = new File(p); f.open('r');"),
("file.write", "var f = File.saveDialog(); f.write(s);", "var f = picker(); f.write(s);"),
("file.write", "new File(p).remove();", "new Thing(p).remove();"),
("file.write", "var a = new File(p); var b = a; b.remove();", "var a = new File(p); var b = a.parent; b.remove();"),
("file.write", "var f = new File(p); f.remove();", "var f = new File(p); var o = {f: {remove: function () {}}}; o.f.remove();"),
("file.write", "var f = new File(p); f.open('\\167');", "var f = new File(p); f.open('\\162');"),
("file.write", "new File(p).open('a');", "var cache = {open: function (m) {}}; cache.open('w');"),
("file.write", "new Folder(p).create();", "new Folder(p).name;"),
("file.write", "File.saveDialog('Save').open('w');", "picker.saveDialog('Save').open('w');"),
("file.write", "Folder.selectDialog().create();", "Folder.select().create();"),
("file.write", "Folder.temp.remove();", "cache.temp.remove();"),
("hostCheck", "if (parseInt(app.version) < 20) {}", "if (parseInt(appVersion) < 20) {}"),
("localize", "var s = localize(text);", "var s = text.localize;"),
("menuCommand", "app.executeMenuCommand('acmecopyall');", "executeMenuCommand('acmecopyall');"),
("notify", "alert('done');", "panel.alert('done');"),
("notify", "if (confirm('Go?')) {}", "function confirm(m) {}"),
("operatorOverloading", "Vec.prototype['+'] = function (o) { return o; };", "Vec.prototype['plus'] = function (o) { return o; };"),
("preferences", "app.preferences.setAcmeFlag('k', 1);", "prefs.setAcmeFlag('k', 1);"),
("reflection", "var p = obj.reflect.properties;", "var p = obj.reflected;"),
("scriptui.dialog", "var w = new Window('dialog', 'Title');", "var w = new Window('palette');"),
("scriptui.dialog", "var w = new Window(\"dialog { text: 'T' }\");", "var w = new Window('window');"),
("scriptui.drawing", "g.onDraw = paint;", "g.onDrawn = paint;"),
("scriptui.drawing", "var pen = g.graphics.newPen(t, c, 1);", "var pen = g.graphics.pen;"),
("scriptui.palette", "var w = new Window('palette');", "var w = new Window('dialog');"),
("scriptui.window", "var w = new Window('window');", "var w = new Window('dialog');"),
("scriptui.window", "var w = new Window(kind);", "var w = new Panel(kind);"),
("selectTool", "app.selectTool('acme');", "selectTool('acme');"),
("socket", "var s = new Socket();", "var s = new Sock();"),
("unitValue", "var u = new UnitValue(1, 'in');", "var u = units.UnitValue;"),
("xml", "var x = new XML(s);", "var x = new Xml(s);"),
("xmp", "var m = new XMPMeta();", "var m = new Meta();"),
];
#[test]
fn each_feature_is_found_where_it_is_and_nowhere_else() {
for (id, with, without) in FEATURE_CASES {
let r = report(with);
assert_eq!(r.kind, Kind::Script, "{with}");
let f = feature(&r, id).unwrap_or_else(|| panic!("{id} not found in {with:?}: {:?}", r.features));
assert_eq!(f.lines, [1], "{with}");
let need = FeatureId::ALL.iter().find(|f| f.id() == *id).map(|f| f.need()).unwrap_or_else(|| panic!("{id} sets no need"));
assert!(r.needs.contains(&need), "{with}: {:?}", r.needs);
let r = report(without);
assert!(feature(&r, id).is_none(), "{id} found in {without:?}");
}
}
#[test]
fn the_dollar_objects_localize_property_is_not_the_global_localize_function() {
for src in ["$.localize = true;", "if ($.localize) {}", "var s = $.localize(text);"] {
let r = report(src);
assert!(feature(&r, "dollar").is_some(), "{src}: {:?}", r.features);
assert!(feature(&r, "localize").is_none(), "{src}: {:?}", r.features);
}
}
#[test]
fn features_are_listed_in_alphabetical_order_of_their_ids() {
let ids: Vec<&str> = FeatureId::ALL.iter().map(|f| f.id()).collect();
assert!(ids.windows(2).all(|w| w[0] < w[1]), "{ids:?}");
assert!(FeatureId::ALL.windows(2).all(|w| w[0] < w[1]), "the enum is declared in the order of its ids");
}
#[test]
fn every_feature_id_has_a_case() {
for f in FeatureId::ALL {
let id = f.id();
assert!(FEATURE_CASES.iter().any(|(case, ..)| *case == id), "{id} has no case");
}
for (id, ..) in FEATURE_CASES {
assert!(FeatureId::ALL.iter().any(|f| f.id() == *id), "{id} isn't a FeatureId");
}
let unique = |ids: &[&str]| ids.iter().collect::<std::collections::BTreeSet<_>>().len() == ids.len();
assert!(unique(PROBLEMS) && unique(OUTSIDE));
}
#[test]
fn compiled_scripts_are_reported_and_never_read() {
let limits = Limits::default();
for (name, bytes) in [
("x.jsxbin", b"#target acme\nalert(1);".as_slice()),
("x.JSXBIN", b"".as_slice()),
("x.jsx", b"@JSXBIN@[email protected]@AcmeBody".as_slice()),
("x.jsx", b"\xEF\xBB\xBF \r\n\t@JSXBIN@[email protected]@".as_slice()),
] {
let r = check(name, bytes, None, &limits);
assert_eq!(r.kind, Kind::Compiled, "{name} {bytes:?}");
assert_eq!(r.steps, 0, "a compiled script isn't read");
assert!(r.directives.is_empty() && r.needs.is_empty());
}
// The prefix elsewhere is text in a script.
assert_eq!(report("var s = '@JSXBIN@[email protected]@';").kind, Kind::Script);
}
#[test]
fn modules_are_not_scripts() {
for src in ["import Acme from 'acme';\nexport default function App() { return <div/>; }\n", "var a = 1;\nexport { a };\n", "export const b = 2;"]
{
let r = report(src);
assert_eq!(r.kind, Kind::Module, "{src}");
assert!(r.features.is_empty() && r.problems.is_empty() && r.needs.is_empty(), "{r:?}");
}
let r = report("var o = { import: 1 };\no.export(2);\nimport('x');\nexport.y = 3;\n");
assert_eq!(r.kind, Kind::Script);
}
#[test]
fn binary_and_oversized_files_are_not_checked() {
let limits = Limits::default();
assert_eq!(check("x.jsx", b"var a = 1;\0", None, &limits).kind, Kind::NotText);
assert_eq!(check("x.jsx", &[1, 2, 3, 4, 5, b'a', b'b'], None, &limits).kind, Kind::NotText);
assert_eq!(check("x.jsx", b"var a = '\x07';\nvar b = 2;\n", None, &limits).kind, Kind::Script, "a stray control character");
let small = Limits { file_bytes: 16, ..Limits::default() };
assert_eq!(check("x.jsx", &[b'a'; 17], None, &small).kind, Kind::TooLarge);
assert_eq!(check("x.jsx", &[b'a'; 16], None, &small).kind, Kind::Script);
}
#[test]
fn directives_are_read_at_the_start_of_a_line_in_both_forms() {
let r = report(concat!(
"#target \"acme_2-26.1-en_US\"\n",
" //@targetengine session\n",
"#include 'lib/a.jsxinc'; // helpers\n",
"#includepath \"lib;shared\"\n",
"#script \"Tidy\"\n",
"#strict on\n",
"var x = 1; //@target other\n",
"#pragma once\n",
"#!\n",
"#TARGET second\n",
));
let directives: Vec<_> = r.directives.iter().map(|d| (d.name, d.value.as_str(), d.line, d.form)).collect();
assert_eq!(
directives,
[
("target", "acme_2-26.1-en_US", 1, "#"),
("targetengine", "session", 2, "//@"),
("include", "lib/a.jsxinc", 3, "#"),
("includepath", "lib;shared", 4, "#"),
("script", "Tidy", 5, "#"),
("strict", "on", 6, "#"),
("target", "second", 10, "#"),
]
);
let t = &r.targets[0];
assert_eq!(
(t.value.as_str(), t.app.as_str(), t.instance.as_deref(), t.version.as_deref(), t.locale.as_deref(), t.line),
("acme_2-26.1-en_US", "acme", Some("2"), Some("26.1"), Some("en_US"), 1)
);
assert_eq!((r.targets[1].app.as_str(), r.targets[1].instance.as_deref(), r.targets[1].version.as_deref()), ("second", None, None));
let includes: Vec<_> = r.includes.iter().map(|i| (i.value.as_str(), i.line, i.status)).collect();
assert_eq!(includes, [("lib/a.jsxinc", 3, IncludeStatus::NotRead)]);
assert_eq!(problems(&r), [("unknownDirective", Some(8), Some("#pragma")), ("unknownDirective", Some(9), Some("#!"))]);
assert_eq!(r.needs, [Need::Language], "VectorCraft runs #targetengine");
}
#[test]
fn reflection_needs_only_the_language() {
let r = report("var p = obj.reflect.properties;\nvar m = $.reflect.find('write');\n");
assert_eq!(feature(&r, "reflection").map(|f| f.lines.clone()), Some(vec![1, 2]));
assert_eq!(r.needs, [Need::Language], "VectorCraft runs reflect");
}
#[test]
fn a_byte_order_mark_and_every_line_break_are_read() {
// U+2028 ends line 2.
let r = check("t.jsx", b"\xEF\xBB\xBF#target acme\nvar a = 1;\xE2\x80\xA8#target b\r\n", None, &Limits::default());
let lines: Vec<usize> = r.directives.iter().map(|d| d.line).collect();
assert_eq!(lines, [1, 3]);
assert_eq!(r.lines, 3);
assert_eq!(problems(&r), [("mixedLineEndings", None, None)]);
}
#[test]
fn invalid_utf8_is_noted_and_the_rest_checked() {
let r = check("t.jsx", b"var b = '\xff';\nalert(b);\n", None, &Limits::default());
assert_eq!(problems(&r), [("notUtf8", None, None)]);
assert!(feature(&r, "notify").is_some());
assert_eq!(r.lines, 2);
}
#[test]
fn member_chains_are_listed_as_written_with_variables_replaced() {
let r = report(concat!(
"var doc = app.activeDocument;\n",
"var shape = doc.acmeItems.add();\n",
"shape.filled = true;\n",
"for (var i = 0; i < doc.acmeLayers.length; i++) {\n",
" doc.acmeLayers[i].shown = false;\n",
"}\n",
"var n = selection.length;\n",
"var first = documents[0];\n",
));
let members: Vec<(&str, usize)> = r.members.iter().map(|m| (m.name.as_str(), m.count)).collect();
assert_eq!(
members,
[
("app.activeDocument", 1),
("app.activeDocument.acmeItems.add()", 1),
("app.activeDocument.acmeItems.add().filled", 1),
("app.activeDocument.acmeLayers.length", 1),
("app.activeDocument.acmeLayers[].shown", 1),
("documents[]", 1),
("selection.length", 1),
]
);
assert_eq!(r.needs, [Need::Dom]);
}
#[test]
fn names_written_with_escapes_are_read_as_the_names_they_spell() {
let r = report("\\u0061pp.activeDocument.acmeName;\n\\u0065val(code);\nvar s = '\\u0061pp.x', \\u002e;\n");
let members: Vec<&str> = r.members.iter().map(|m| m.name.as_str()).collect();
assert_eq!(members, ["app.activeDocument.acmeName"]);
assert!(feature(&r, "dynamicCode").is_some());
assert_eq!(r.outside.iter().map(|o| o.id).collect::<Vec<_>>(), ["unseenCode"]);
assert_eq!(r.lines, 3);
}
/// `src` with the escapes in its names written as the characters they stand for.
fn unescaped(src: &str) -> String {
let limits = Limits::default();
crate::analyze::unescaped(std::borrow::Cow::Borrowed(src), &mut crate::report::Acc::new(&limits)).0.into_owned()
}
#[test]
fn names_are_made_of_the_unicode_identifier_characters() {
// U+2118 (Other_ID_Start) starts a name, and U+203F (connector punctuation), U+00B7
// (Other_ID_Continue) and the zero width non-joiner and joiner continue one.
// Unicode 17.0 assigned the letter U+088F and the combining mark U+1ACF.
for name in ["\u{2118}a", "a\u{203F}b", "a\u{B7}b", "a\u{200C}b", "a\u{200D}b", "_\u{2118}", "$_a1", "\u{E9}t\u{E9}", "\u{88F}", "_\u{1ACF}"] {
assert_eq!(tokens(name), [(T::Ident, name)], "{name:?}");
}
for (src, want) in [
// Characters that only continue a name, and digits, don't start one.
("\u{203F}a", [(T::Other, "\u{203F}"), (T::Ident, "a")]),
("\u{1ACF}a", [(T::Other, "\u{1ACF}"), (T::Ident, "a")]),
("\u{B7}a", [(T::Other, "\u{B7}"), (T::Ident, "a")]),
("\u{200C}a", [(T::Other, "\u{200C}"), (T::Ident, "a")]),
("0a", [(T::Number, "0"), (T::Ident, "a")]),
// A superscript digit (category No) and a letter that is Pattern_Syntax are in no name.
("a\u{B2}", [(T::Ident, "a"), (T::Other, "\u{B2}")]),
("a\u{2E2F}", [(T::Ident, "a"), (T::Other, "\u{2E2F}")]),
("\u{2E2F}a", [(T::Other, "\u{2E2F}"), (T::Ident, "a")]),
// `#` and a name away from the start of a line, and a regular expression's flags.
("a #\u{2118}b", [(T::Ident, "a"), (T::Other, "#\u{2118}b")]),
("=/a/g\u{B2}", [(T::RegExp, "/a/g"), (T::Other, "\u{B2}")]),
] {
let mut got = tokens(src);
if src.starts_with('=') {
got.remove(0);
}
assert_eq!(got, want, "{src:?}");
}
assert_eq!(tokens("a #\u{B2}"), [(T::Ident, "a"), (T::Other, "#"), (T::Other, "\u{B2}")]);
assert_eq!(tokens("=/a/g\u{200D}"), [(T::Punct, "="), (T::RegExp, "/a/g\u{200D}")]);
// The code in an XML literal's braces reads names by the same rule.
assert!(tokens("var x = <a>{\u{2118}b + c\u{203F}d}</a>;").iter().filter(|t| t.0 == T::Ident).any(|t| t.1 == "c\u{203F}d"));
assert!(tokens("var x = <a>{\u{2118}b}</a>;").contains(&(T::Ident, "\u{2118}b")));
}
#[test]
fn an_escape_stands_for_a_character_only_where_a_name_may_hold_it() {
for (src, want) in [
("\\u2118a", "\u{2118}a"),
("a\\u203Fb", "a\u{203F}b"),
("a\\u00B7b", "a\u{B7}b"),
("a\\u200Cb", "a\u{200C}b"),
("a\\u200Db", "a\u{200D}b"),
("a\\u0030", "a0"),
("\\u0024\\u005F", "$_"),
("\\u088F", "\u{88F}"),
("_\\u1ACF", "_\u{1ACF}"),
// Not at the start of a name.
("\\u0030abc", "\\u0030abc"),
("\\u1ACFa", "\\u1ACFa"),
("\\u203Fa", "\\u203Fa"),
("\\u00B7a", "\\u00B7a"),
// In no name.
("a\\u00B2", "a\\u00B2"),
("a\\u2E2F", "a\\u2E2F"),
("\\u2E2F", "\\u2E2F"),
// Malformed escapes and a lone surrogate.
("\\u", "\\u"),
("a\\u12;", "a\\u12;"),
("a\\uZZZZ", "a\\uZZZZ"),
("a\\uD800", "a\\uD800"),
("", ""),
] {
assert_eq!(unescaped(src), want, "{src:?}");
}
let r = report("\\u0061pp.a\\u203Fb;\napp.\\u2118c;\n");
let members: Vec<&str> = r.members.iter().map(|m| m.name.as_str()).collect();
assert_eq!(members, ["app.a\u{203F}b", "app.\u{2118}c"]);
// An escape a name may not hold where it stands, or a malformed one, is not part of a name.
for (src, want) in [
("\\u0030a", [(T::Other, "\\"), (T::Ident, "u0030a")]),
("a\\u00B2", [(T::Ident, "a"), (T::Other, "\\")]),
("a\\u12", [(T::Ident, "a"), (T::Other, "\\")]),
("a\\uD800", [(T::Ident, "a"), (T::Other, "\\")]),
] {
assert_eq!(tokens(src).get(..2), Some(&want[..]), "{src:?}");
}
assert_eq!(tokens("a\\u0030\\u200C"), [(T::Ident, "a\\u0030\\u200C")]);
let r = report("app.\\u0030abc;\napp.a\\u00B2;\n");
assert!(r.members.iter().all(|m| !m.name.contains('\\') && !m.name.contains("0abc")), "{:?}", r.members);
}
#[test]
fn malformed_escapes_in_bulk_are_read_once_through() {
let src = format!("{}\n{}\nalert(1);\n", "\\u\\uD800\\u12\\u0030\\u2118".repeat(20_000), "a\u{203F}".repeat(20_000));
let r = report(&src);
assert_eq!(r.lines, 3);
assert_eq!(feature(&r, "notify").map(|f| f.lines.as_slice()), Some(&[3][..]));
assert!(r.steps <= 5 * src.len() + 16, "{} steps for {} bytes", r.steps, src.len());
// Braced escapes: long runs of digits, closed and not, and ones that end at once.
let zeros = "0".repeat(100_000);
let src = format!(
"a\\u{{{zeros}62}};\n\\u{{{}\n{}\n{}\nalert(1);\n",
"f".repeat(100_000),
"\\u{".repeat(20_000),
"\\u{0000000061}\\u{110000}\\u{}\\u{D800}#\\u{61}".repeat(10_000)
);
let r = report(&src);
assert_eq!(r.lines, 5);
assert_eq!(feature(&r, "notify").map(|f| f.lines.as_slice()), Some(&[5][..]));
assert!(r.steps <= 5 * src.len() + 16, "{} steps for {} bytes", r.steps, src.len());
}
#[test]
fn braced_escapes_in_names_are_read_by_their_value() {
for (src, want) in [
("\\u{61}pp", "app"),
("a\\u{62}c", "abc"),
("\\u{0000000061}", "a"),
("\\u{2118}\\u{203F}", "\u{2118}\u{203F}"),
("a\\u{1D7CE}", "a\u{1D7CE}"),
("\\u{1D49C}", "\u{1D49C}"),
// Not at the start of a name, in no name, out of range, empty, a surrogate and not closed.
("\\u{30}a", "\\u{30}a"),
("a\\u{1F600}", "a\\u{1F600}"),
("a\\u{110000}", "a\\u{110000}"),
("a\\u{FFFFFFFFFFFFFFFFFFFF}b", "a\\u{FFFFFFFFFFFFFFFFFFFF}b"),
("a\\u{}b", "a\\u{}b"),
("a\\u{D800}", "a\\u{D800}"),
("a\\u{61", "a\\u{61"),
("a\\u{6 1}", "a\\u{6 1}"),
] {
assert_eq!(unescaped(src), want, "{src:?}");
}
assert_eq!(tokens("\\u{61}pp.x"), [(T::Ident, "\\u{61}pp"), (T::Punct, "."), (T::Ident, "x")]);
assert_eq!(tokens("a\\u{0000000062}"), [(T::Ident, "a\\u{0000000062}")]);
assert_eq!(tokens("a\\u{1F600}").get(..2), Some(&[(T::Ident, "a"), (T::Other, "\\")][..]));
}
#[test]
fn braced_name_escape_reports_later_syntax_and_member() {
let r = report("\\u{61}pp.activeDocument.acmeName;\nvar a\\u{62}c = 1, \\u{0000000061}pp2 = 2;\n\\u{61}pp.x + \\u{61}pp.y;\n");
let members: Vec<&str> = r.members.iter().map(|m| m.name.as_str()).collect();
assert_eq!(members, ["app.activeDocument.acmeName", "app.x", "app.y"]);
assert_eq!(
problems(&r),
[
("newerSyntax", Some(1), Some("\\u{61}")),
("newerSyntax", Some(2), Some("\\u{62}")),
("newerSyntax", Some(2), Some("\\u{0000000061}")),
("newerSyntax", Some(3), Some("\\u{61}")),
]
);
// An escape that isn't a name's character, or isn't closed, makes no name and no report of one.
for src in [
"\\u{1F600}pp.activeDocument;",
"\\u{110000}pp.activeDocument;",
"\\u{}pp.activeDocument;",
"\\u{D800}pp.activeDocument;",
"\\u{61pp.activeDocument;",
] {
let r = report(src);
assert!(r.members.iter().all(|m| !m.name.starts_with("app")), "{src:?}: {:?}", r.members);
assert!(r.problems.iter().all(|p| p.detail.as_deref().is_none_or(|d| !d.starts_with("\\u{"))), "{src:?}: {:?}", r.problems);
}
// In an included file, the report names the file.
let mut m = mem(&[("lib.jsxinc", "\\u{61}pp.name;\n")]);
let r = check("main.jsx", b"#include 'lib.jsxinc'\n", Some(&mut m), &Limits::default());
let found: Vec<_> = r.problems.iter().map(|p| (p.code, p.line, p.file.as_deref(), p.detail.as_deref())).collect();
assert_eq!(found, [("newerSyntax", Some(1), Some("lib.jsxinc"), Some("\\u{61}"))]);
}
#[test]
fn private_names_with_escaped_starts_are_single_tokens() {
assert_eq!(tokens("a #\\u0061b"), [(T::Ident, "a"), (T::Other, "#\\u0061b")]);
assert_eq!(tokens("this.#\\u0061pp"), [(T::Ident, "this"), (T::Punct, "."), (T::Other, "#\\u0061pp")]);
assert_eq!(tokens("o.#\\u{61}b"), [(T::Ident, "o"), (T::Punct, "."), (T::Other, "#\\u{61}b")]);
// An escape for a character no name starts with leaves `#` on its own.
assert_eq!(tokens("a #\\u0030b").get(..3), Some(&[(T::Ident, "a"), (T::Other, "#"), (T::Other, "\\")][..]));
let r = report("var q = this.#\\u0061pp;\nvar r = o.#\\u{61}b;\n");
assert!(r.members.is_empty(), "{:?}", r.members);
assert_eq!(
problems(&r),
[("newerSyntax", Some(1), Some("#app")), ("newerSyntax", Some(2), Some("\\u{61}")), ("newerSyntax", Some(2), Some("#ab"))]
);
}
#[test]
fn private_name_reports_decoded_construct() {
let r = report("var q = this.#hid\\u0064en;\nvar r = this.#\\u0061\\u0062;\n");
assert_eq!(problems(&r), [("newerSyntax", Some(1), Some("#hidden")), ("newerSyntax", Some(2), Some("#ab"))]);
}
#[test]
fn private_name_at_line_start_is_not_a_directive() {
let r = report(concat!(
"class Box {\n",
" #size = 1;\n",
" #\\u0061rea = 2;\n",
" #include 'a.jsxinc'\n",
" #\\u0069nclude = 4;\n",
" #\\u{69}nclude = 5;\n",
"}\n",
"#\\u0062ox = 3;\n",
"#pragma once\n",
"#target acme\n",
));
let directives: Vec<_> = r.directives.iter().map(|d| (d.name, d.line)).collect();
assert_eq!(directives, [("include", 4), ("target", 10)]);
// Where the name a private name's escapes spell would start a preprocessor line, the report
// gives the name as written.
assert_eq!(
problems(&r),
[
("newerSyntax", Some(1), Some("class")),
("newerSyntax", Some(2), Some("#size")),
("newerSyntax", Some(3), Some("#area")),
("newerSyntax", Some(5), Some("#\\u0069nclude")),
("newerSyntax", Some(6), Some("\\u{69}")),
("newerSyntax", Some(6), Some("#\\u{69}nclude")),
("newerSyntax", Some(8), Some("#\\u0062ox")),
("unknownDirective", Some(9), Some("#pragma")),
]
);
}
#[test]
fn private_name_that_starts_with_a_directive_name_is_not_a_directive() {
let r = report(concat!(
"class Box {\n",
" #include$size = 1;\n",
" #include\u{e9} = 2;\n",
" #include\\u0041 = 3;\n",
" #include\\u{41} = 4;\n",
" #INCLUDE 'a.jsxinc'\n",
"}\n",
));
let directives: Vec<_> = r.directives.iter().map(|d| (d.name, d.line)).collect();
assert_eq!(directives, [("include", 6)]);
}
#[test]
fn xml_code_uses_escaped_names() {
let r = report("var x = <a>{\\u{61}pp.activeDocument}</a>;\nvar y = <b>\\u0061pp.z \\u{61}</b>;\n");
let members: Vec<&str> = r.members.iter().map(|m| m.name.as_str()).collect();
assert_eq!(members, ["app.activeDocument"]);
assert_eq!(problems(&r), [("newerSyntax", Some(1), Some("\\u{61}"))]);
assert!(tokens("var x = <a>{this.#\\u0061b}</a>;").contains(&(T::Other, "#\\u0061b")));
}
#[test]
fn the_scripts_own_platform_names_are_not_the_platforms() {
let r = report(concat!(
"var Socket = 0;\n",
"function alert(m) { return m; }\n",
"alert('x');\n",
"function f(File) { return new File(p); }\n",
"var g = new File(q); g.remove();\n",
));
assert!(feature(&r, "socket").is_none());
assert!(feature(&r, "notify").is_none());
assert_eq!(feature(&r, "file.objects").map(|f| f.lines.as_slice()), Some(&[5][..]));
assert_eq!(r.outside.iter().map(|o| o.id).collect::<Vec<_>>(), ["filesWritten"]);
}
/// A document's `saveAs` and `exportFile` write the file they are given; `app.open` only reads one.
#[test]
fn save_as_and_export_file_write_a_file() {
let r = report(concat!(
"var f = new File(p);\n",
"var d = app.open(f);\n",
"d.saveAs(f);\n",
"app.activeDocument.exportFile(f, ExportType.PNG24);\n",
));
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[3, 4][..]));
assert_eq!(r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect::<Vec<_>>(), [("filesWritten", &[3, 4][..])]);
}
/// A document's `exportVariables` writes the file it is given; `importVariables` only reads one.
#[test]
fn export_variables_writes_a_file() {
let r = report("var f = new File(p);\napp.activeDocument.importVariables(f);\napp.activeDocument.exportVariables(f);\n");
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[3][..]));
assert_eq!(r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect::<Vec<_>>(), [("filesWritten", &[3][..])]);
}
/// `exportVariables` called with its name as a string literal in brackets writes the file too;
/// reading the method without calling it does not.
#[test]
fn export_variables_in_brackets_writes_a_file() {
let r = report(concat!(
"var f = new File(p);\n",
"app.activeDocument['importVariables'](f);\n",
"app.activeDocument[\"exportVariables\"](f);\n",
"var d = app.activeDocument; d['exportVariables'] (f);\n",
"var n = d['exportVariables'];\n",
));
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[3, 4][..]));
assert_eq!(r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect::<Vec<_>>(), [("filesWritten", &[3, 4][..])]);
}
#[test]
fn evaluated_code_keeps_warnings_for_rebound_writer_receivers() {
for call in ["local.exportVariables(f);", "local['exportVariables'](f);", "local.saveAs(f);", "local['exportFile'](f);"] {
let r = report(&format!(
"var local = {{exportVariables: function () {{}}, saveAs: function () {{}}, exportFile: function () {{}}}};\n\
eval('local = app.activeDocument');\n{call}\n"
));
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[3][..]), "{call}");
let written = r.outside.iter().find(|o| o.id == "filesWritten");
assert_eq!(written.map(|o| o.lines.as_slice()), Some(&[3][..]), "{call}");
}
}
/// `SaveOptions.SAVECHANGES` makes a document's `close` save it to its own file, read directly, in
/// brackets, as a string or through a variable; closing without it, and the other save options,
/// write nothing.
#[test]
fn closing_with_save_changes_writes_a_file() {
let r = report(concat!(
"app.activeDocument.close();\n",
"app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);\n",
"app.activeDocument.close(SaveOptions.PROMPTTOSAVECHANGES);\n",
"app.activeDocument.close(SaveOptions.SAVECHANGES);\n",
"var how = SaveOptions['SAVECHANGES'];\n",
"documents[0].close(how);\n",
"documents[1].close('SaveOptions.SAVECHANGES');\n",
"var w = new Window('dialog'); w.close(1);\n",
));
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[4, 5, 7][..]));
assert_eq!(r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect::<Vec<_>>(), [("filesWritten", &[4, 5, 7][..])]);
}
#[test]
fn unrelated_save_changes_properties_and_strings_do_not_report_writes() {
let r = report(concat!(
"var a = other.SAVECHANGES;\n",
"var b = other['SAVECHANGES'];\n",
"app.activeDocument.close('SAVECHANGES');\n",
"var c = holder.SaveOptions.SAVECHANGES;\n",
"var d = holder.SaveOptions['SAVECHANGES'];\n",
"var e = 'SAVECHANGES';\n",
"var key = SaveOptions['SAVECHANGES' + suffix];\n",
"var f = SaveOptions.SAVECHANGES;\n",
"var g = SaveOptions['SAVECHANGES'];\n",
"var h = 'SaveOptions.SAVECHANGES';\n",
));
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[8, 9, 10][..]));
assert_eq!(r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect::<Vec<_>>(), [("filesWritten", &[8, 9, 10][..])]);
}
/// `saveAs` and `exportFile` called with their names as string literals in brackets write the file
/// too; reading the method without calling it does not.
#[test]
fn save_as_and_export_file_in_brackets_write_a_file() {
let r = report(concat!(
"var f = new File(p);\n",
"app.activeDocument['open'](f);\n",
"app.activeDocument[\"saveAs\"](f);\n",
"var d = app.activeDocument; d['exportFile'] (f, ExportType.SVG);\n",
"var n = d['saveAs'];\n",
));
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[3, 4][..]));
assert_eq!(r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect::<Vec<_>>(), [("filesWritten", &[3, 4][..])]);
}
/// `app.open` reads the file it is given, called by name or with its name as a string literal in
/// 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!(
"var f = new File(p);\n",
"var d = app.open(f);\n",
"app['open'](f);\n",
"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, 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`, `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() {
let r = report(concat!(
"var o = new PrintOptions(); o.jobOptions.file = new File(p);\n",
"app.activeDocument.print(o);\n",
"app.activeDocument['print'](o);\n",
"var n = app.activeDocument['print'];\n",
"var d = app.documents[0]; d.print(o);\n",
"logger.print('hello'); logger['print']('hello');\n",
"app.activeDocument.name.concat(logger.print('x'));\n",
"app.print(o); app.activeDocument.layers[0]['print'](o); app.documents['print'](o); app.preferences.activeDocument.print(o);\n",
"var e = app.open(new File(p)); e.print(o);\n",
"activeDocument.print(o);\n",
"documents.addDocument('Print').print(o);\n",
"app.documents.addDocument('Print', s, false).print(o);\n",
"documents.addDocumentNoUI('Print').print(o);\n",
"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, 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::<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)]);
}
/// `saveAs` and `exportFile` called on an object literal of the script's own that defines them write
/// nothing, with `.` or in brackets. On a document, on a variable the check does not follow, on one
/// assigned something else since, and for a method the literal does not define, they write the file.
#[test]
fn the_scripts_own_save_as_writes_no_file() {
let r = report(concat!(
"var f = new File(p);\n",
"var local = {saveAs: function () {}, 'n': 1};\n",
"local.saveAs(f);\n",
"local['saveAs'](f);\n",
"app.activeDocument.saveAs(f);\n",
"local.exportFile(f);\n",
"other.saveAs(f);\n",
"local = app.activeDocument;\n",
"local.saveAs(f);\n",
"var t = {x: 1, a: ok ? saveAs : 0, 'exportFile': function () {}};\n",
"t['exportFile'](f);\n",
"t.saveAs(f);\n",
"var activeDocument = {saveAs: function () {}};\n",
"app.activeDocument['saveAs'](f);\n",
"app.activeDocument.saveAs(f);\n",
));
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[5, 6, 7, 9, 12, 14, 15][..]));
assert_eq!(r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect::<Vec<_>>(), [("filesWritten", &[5, 6, 7, 9, 12, 14, 15][..])]);
}
/// Once the script replaces or deletes such a method, indexes the object with a computed key, or
/// lets another name hold the object, its `saveAs` may write a file again, and so may a `saveAs` key
/// whose last value in the literal is not a function expression.
#[test]
fn a_changed_or_shared_own_save_as_writes_a_file() {
let r = report(concat!(
"var f = new File(p);\n",
"var a = {saveAs: function () {}}; var b = {saveAs: function () {}};\n",
"var d = {saveAs: function () {}}; var e = {saveAs: function () {}}; var g = {saveAs: function () {}};\n",
"a.saveAs(f); b['saveAs'](f); a.n = 1; a.n;\n",
"a.saveAs = app.activeDocument.saveAs;\n",
"b['saveAs'] = w;\n",
"delete d.saveAs;\n",
"var h = e;\n",
"use(g);\n",
"a.saveAs(f);\n",
"b.saveAs(f);\n",
"d.saveAs(f);\n",
"e.saveAs(f);\n",
"g['saveAs'](f);\n",
"var m = {saveAs: d.saveAs}; var n = {saveAs: function () {}, saveAs: d.saveAs};\n",
"m.saveAs(f);\n",
"n.saveAs(f);\n",
"var q = {saveAs: function () {}, saveAs: (d.saveAs)}; var s = {saveAs: function () {}, saveAs: [w][0]};\n",
"q['saveAs'](f);\n",
"s.saveAs(f);\n",
));
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[10, 11, 12, 13, 14, 16, 17, 19, 20][..]));
// A computed key may name any variable, so every `saveAs` in the script may write, the ones
// before the computed key included.
let r = report("var f = new File(p);\nvar c = {saveAs: function () {}};\nc.saveAs(f);\nc[k] = w;\nc.saveAs(f);\n");
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[3, 5][..]));
}
/// A variable assigned in a loop may hold a document when a later iteration calls its `saveAs`,
/// before the assignment in the text: the call writes a file. One assigned only after a loop does
/// not.
#[test]
fn an_own_save_as_reassigned_later_in_a_loop_writes_a_file() {
let r = report(concat!(
"var f = new File(p);\n",
"var a = {saveAs: function () {}}; var b = {saveAs: function () {}}; var c = {saveAs: function () {}};\n",
"var d = {saveAs: function () {}};\n",
"for (var i = 0; i < 2; i++) { a.saveAs(f); a = app.activeDocument; }\n",
"while (n--) b.saveAs(f), b = app.activeDocument;\n",
"do { c.saveAs(f); } while ((c = next()));\n",
"for (;;) { d.saveAs(f); break; }\n",
"d = app.activeDocument;\n",
));
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[4, 5, 6][..]));
assert_eq!(r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect::<Vec<_>>(), [("filesWritten", &[4, 5, 6][..])]);
// The `else` of a body without braces is in the loop; a statement after a line break that ends
// such a body is not, and neither is a variable of the same name that a function declares.
let r = report(concat!(
"var f = new File(p);\n",
"var e = {saveAs: function () {}}; var g = {saveAs: function () {}}; var h = {saveAs: function () {}};\n",
"while (n--) if (n) e.saveAs(f); else e = app.activeDocument;\n",
"while (n--) g.saveAs(f), n\n",
"g = app.activeDocument\n",
"function k() { var h; for (;;) h = app.activeDocument; }\n",
"h.saveAs(f);\n",
));
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[3][..]));
// An object literal's `}` does not end a body without braces, a call's `)` before a line break
// does, and a literal given after the last loop that assigns its variable is the script's own.
let r = report(concat!(
"var f = new File(p);\n",
"var a = {saveAs: function () {}}; var b = {saveAs: function () {}};\n",
"while (n--) a.saveAs(f), t = {}, a = app.activeDocument;\n",
"b.saveAs(f);\n",
"while (n--) tick()\n",
"b = app.activeDocument\n",
"for (;;) { c = app.activeDocument; }\n",
"var c = {saveAs: function () {}};\n",
"c.saveAs(f);\n",
));
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[3][..]));
let r = report(concat!(
"var f = new File(p);\n",
"var a = {saveAs: function () {}};\n",
"while (n--) if (n)\n",
" a.saveAs(f), a = app.activeDocument;\n",
));
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[4][..]));
let r = report(concat!(
"var f = new File(p);\n",
"var a = {saveAs: function () {}}; var b = {saveAs: function () {}};\n",
"a.saveAs(f); b['saveAs'](f);\n",
"while (n--) { a.saveAs(f); a = app.activeDocument; }\n",
"while (n--) { b['saveAs'](f); b = app.activeDocument; }\n",
"var a = {saveAs: function () {}};\n",
"a.saveAs(f);\n",
"while (n--) { a.saveAs(f); a = app.activeDocument; }\n",
));
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[4, 5, 8][..]));
}
/// The `catch` and `finally` of a `try` body, and an `else` after a `do` loop inside a body without
/// braces, are in the loop; a statement after the whole loop is not.
#[test]
fn try_clauses_and_else_after_a_nested_do_loop_are_in_the_loop() {
let r = report(concat!(
"var f = new File(p);\n",
"var d = {saveAs: function () {}}; var e = {saveAs: function () {}}; var g = {saveAs: function () {}};\n",
"var h = {saveAs: function () {}};\n",
"while (i++ < 2) try { d.saveAs(f); } finally { d = app.activeDocument; }\n",
"while (i++ < 2)\n",
" if (i === 2) do e.saveAs(f); while (false);\n",
" else e = app.activeDocument;\n",
"while (n--) do g.saveAs(f); while (false);\n",
"g = app.activeDocument;\n",
"while (n--) h.saveAs(f), holder.finally\n",
"h = app.activeDocument;\n",
));
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[4, 6][..]));
}
/// A literal after a label's line break may not run, a `this` assignment in a function whose own
/// variable hides the global one changes the global, and a property `d` of another object is not
/// the variable `d`.
#[test]
fn labels_hidden_globals_and_other_objects_keep_writes_known() {
let r = report(concat!(
"var f = new File(p);\n",
"var a = app.activeDocument;\n",
"if (no) l:\n",
"var a = {saveAs: function () {}};\n",
"a.saveAs(f);\n",
"var b = {saveAs: function () {}};\n",
"function change() { var b; this.b = app.activeDocument; }\n",
"change(); b.saveAs(f);\n",
"var c = {saveAs: function () {}};\n",
"function keep() { var c; c = 1; }\n",
"keep(); c.saveAs(f);\n",
"var d = {saveAs: function () {}};\n",
"holder.d['saveAs'] = 1;\n",
"d.saveAs(f);\n",
"var e = {saveAs: function () {}};\n",
"e['saveAs'] = app.activeDocument.saveAs;\n",
"e.saveAs(f);\n",
"var g = {saveAs: function () {}};\n",
"function other() { var g; holder.this.g = app.activeDocument; holder.this['g'] = 1; }\n",
"other(); g.saveAs(f);\n",
));
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[5, 8, 17][..]));
}
/// Only a `var` statement directly in a function body or the global code that gives its first name
/// the literal as its whole value makes the literal's `saveAs` the script's own, from the end of the
/// statement. A literal given in a branch, one the value goes on from, one given by an assignment
/// or to a later name of the statement, a call inside the literal, and a variable a function gave
/// another value keep reporting the write.
#[test]
fn only_a_whole_var_value_makes_save_as_the_scripts_own() {
let r = report(concat!(
"var f = new File(p);\n",
"var d = {saveAs: function () {}};\n",
"function choose() { d = null; d = app.activeDocument; }\n",
"d.saveAs(f);\n",
"var e = app.activeDocument;\n",
"if (flag) var e = {saveAs: function () {}};\n",
"e.saveAs(f);\n",
"if (flag) { var e2 = {saveAs: function () {}}; }\n",
"e2.saveAs(f);\n",
"var g = {saveAs: function () {}} && app.activeDocument;\n",
"g.saveAs(f);\n",
"var h = app.activeDocument;\n",
"h = {saveAs: function () {}, value: h.saveAs(f)};\n",
"var k = {saveAs: function () {}, value: k.saveAs(f)};\n",
"k.saveAs(f);\n",
"var x = 1, y = {saveAs: function () {}};\n",
"y.saveAs(f);\n",
));
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[4, 7, 9, 11, 13, 14, 17][..]));
}
#[test]
fn writer_calls_after_many_assignment_loops_have_bounded_work() {
for reinitialize in [false, true] {
let mut src = "var d = {saveAs: function () {}};\n".to_string();
for _ in 0..512 {
src.push_str("while (n--) { d = next(); }\n");
}
if reinitialize {
src.push_str("var d = {saveAs: function () {}};\n");
}
src.push_str("var local = {saveAs: function () {}};\n");
for _ in 0..2048 {
src.push_str("d.saveAs(f); local.saveAs(f);\n");
}
let limits = Limits::default();
let mut acc = report::Acc::new(&limits);
let declared = analyze::declarations(&src, &mut acc);
let before = declared.loop_span_reads();
analyze::scan(&src, &declared, &[], None, std::rc::Rc::from([]), &analyze::Known::default(), &mut acc);
let reads = declared.loop_span_reads() - before;
if reinitialize {
assert!(reads < 16 * 2048, "{reads} span accesses for 2048 calls on d");
} else {
assert_eq!(reads, 0);
}
}
}
/// A function body read before the literal that assigns its variable without declaring it may run
/// after the literal, so the literal's `saveAs` keeps reporting the write; a body that declares the
/// name assigns its own variable.
#[test]
fn a_function_read_earlier_that_assigns_the_variable_keeps_save_as_writing() {
let r = report(concat!(
"var f = new File(p);\n",
"function choose() { d = app.activeDocument; }\n",
"var d = {saveAs: function () {}};\n",
"choose(); d.saveAs(f);\n",
"function bump() { e++; }\n",
"var e = {saveAs: function () {}};\n",
"e.saveAs(f);\n",
"function own() { var g; g = app.activeDocument; }\n",
"var g = {saveAs: function () {}};\n",
"g.saveAs(f);\n",
));
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[4, 7][..]));
}
#[test]
fn captured_writer_receivers_can_change_before_each_call() {
for method in ["saveAs", "exportFile"] {
for call in [format!("d.{method}(f)"), format!("d['{method}'](f)")] {
for (prefix, suffix) in [("", ""), ("function outer() {\n", "}\nouter();\n")] {
let src = format!(
"{prefix}var d = {{{method}: function () {{}}}};\n\
function save(f) {{ {call}; }}\n\
d = app.activeDocument; save(f);\n{suffix}"
);
let r = report(&src);
let line = if prefix.is_empty() { 2 } else { 3 };
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[line][..]), "{src}");
assert_eq!(r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect::<Vec<_>>(), [("filesWritten", &[line][..])], "{src}");
}
}
}
}
#[test]
fn captured_reassignments_keep_save_as_writing_after_a_later_initializer() {
let r = report(concat!(
"var f = new File(p);\n",
"function outer() {\n",
" function choose() { d = app.activeDocument; }\n",
" var d = {saveAs: function () {}};\n",
" choose(); d.saveAs(f);\n",
" function bump() { e++; }\n",
" var e = {saveAs: function () {}};\n",
" bump(); e.saveAs(f);\n",
" function own() { var g; g = app.activeDocument; }\n",
" var g = {saveAs: function () {}};\n",
" own(); g.saveAs(f);\n",
"}\n",
"outer();\n",
));
assert_eq!(r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect::<Vec<_>>(), [("filesWritten", &[5, 8][..])]);
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[5, 8][..]));
}
/// Assigning a property of `this`, the global object in the global code and in a function called
/// without a receiver, may give the global variable of that name another value, before or after
/// its literal.
#[test]
fn assigning_a_property_of_this_keeps_save_as_writing() {
let r = report(concat!(
"var f = new File(p);\n",
"var d = {saveAs: function () {}};\n",
"this.d = app.activeDocument;\n",
"d.saveAs(f);\n",
"function swap() { this['e'] = app.activeDocument; }\n",
"var e = {saveAs: function () {}};\n",
"swap(); e.saveAs(f);\n",
"var g = {saveAs: function () {}};\n",
"this.other = 1;\n",
"g.saveAs(f);\n",
));
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[4, 7][..]));
}
/// A `saveAs` key's value is a method of the script's own only when the whole value is one function
/// expression: a value that goes on after the function's body, and a literal inside another, leave
/// `saveAs` writing the file. A literal given to a function body's own variable at the end of the body
/// leaves the outer variable as it was.
#[test]
fn a_value_that_goes_on_after_the_function_writes_a_file() {
let r = report(concat!(
"var f = new File(p);\n",
"var a = {saveAs: function () {} && app.activeDocument.saveAs};\n",
"var b = {saveAs: function () {}(f), n: 1};\n",
"var c = {saveAs: function () {} ? x : y};\n",
"var e = {saveAs: function () {}.call};\n",
"var k = {exportFile: function w(x) { return x; }, saveAs: function () {}};\n",
"var m = {o: {saveAs: function () {}}};\n",
"var q = app.activeDocument; function r() { var q = {saveAs: function () {}}\n",
"}\n",
"a.saveAs(f);\n",
"b.saveAs(f);\n",
"c.saveAs(f);\n",
"e.saveAs(f);\n",
"k.saveAs(f); k.exportFile(f);\n",
"m.saveAs(f);\n",
"q.saveAs(f);\n",
));
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[10, 11, 12, 13, 15, 16][..]));
}
/// 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][..]));
}
/// `new Folder(…)` and the `Folder` places always give a Folder object, which has no `length`,
/// `hidden` or `readonly` setter. `Folder(…)` gives a File object when the path names a file,
/// `Folder.selectDialog()` gives a File or Folder object, and a variable assigned another variable
/// may hold either, so setting those writes. Removing a folder still writes.
#[test]
fn setting_a_folders_length_or_flags_does_not_write() {
let r = report(concat!(
"var d = new Folder(p);\n",
"d.length = 0;\n",
"d.hidden = true;\n",
"Folder.temp.length = 0;\n",
"(new Folder(q)).length = 0;\n",
"(Folder.desktop)['readonly'] = true;\n",
"Folder(p).readonly = true;\n",
"var e = Folder.selectDialog();\n",
"e.length = 0;\n",
"var g = (d);\n",
"g['length'] = 0;\n",
"var h = Folder(p);\n",
"h.hidden = true;\n",
"(Folder(p)).length = 0;\n",
"d.remove();\n",
"Folder.current.hidden = true;\n",
));
let outside: Vec<(&str, &[usize])> = r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect();
assert_eq!(outside, [("filesWritten", &[7, 9, 11, 13, 14, 15, 16][..])]);
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[7, 9, 11, 13, 14, 15, 16][..]));
}
/// A Folder object has none of the File object's `open`, `read`, `readch`, `readln`, `write`,
/// `writeln`, `copy`, `createAlias`, `openDlg` and `saveDlg`, so calling them on `new Folder(…)`, a
/// `Folder` place or a variable holding one reads, writes and asks nothing. On `Folder(…)`, which
/// gives a File object when the path names a file, and on `Folder.current` they still count, and the
/// Folder object's own `create`, `remove`, `getFiles` and `selectDlg` count on any of them.
#[test]
fn file_only_calls_on_a_folder_do_nothing() {
let r = report(concat!(
"var d = new Folder(p);\n",
"d.write(s); d.writeln(s); d.copy(q); d.createAlias(q);\n",
"d.open('w'); d.read(); d.readch(); d.readln();\n",
"Folder.temp.write(s); (new Folder(q)).copy(q);\n",
"d.openDlg(); Folder.desktop.saveDlg();\n",
"Folder(p).write(s);\n",
"Folder.current.copy(q);\n",
"d.create(); d.getFiles(); d.selectDlg();\n",
"var g = Folder(p); g.open('r');\n",
));
let lines = |id: &str| feature(&r, id).map(|f| f.lines.clone()).unwrap_or_default();
let outside: Vec<(&str, &[usize])> = r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect();
assert_eq!(outside, [("filesWritten", &[6, 7, 8][..])]);
assert_eq!(lines("file.write"), [6, 7, 8]);
assert_eq!(lines("file.read"), [8, 9]);
assert_eq!(lines("file.dialog"), [8]);
}
/// A variable holding a Folder object is the same object in grouping parentheses, so `(d).write(s)`
/// and `(d).length = 0` do nothing, as `d.write(s)` and `d.length = 0` do. A variable that may hold
/// a File object still counts in them.
#[test]
fn a_folder_in_parentheses_is_still_a_folder() {
let r = report(concat!(
"var d = new Folder(p);\n",
"(d).write(s); ((d)).copy(q); (d).open('r'); (d).length = 0;\n",
"var f = new File(p);\n",
"(f).write(s);\n",
"var g = Folder(p); (g).open('r');\n",
));
let lines = |id: &str| feature(&r, id).map(|f| f.lines.clone()).unwrap_or_default();
let outside: Vec<(&str, &[usize])> = r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect();
assert_eq!(outside, [("filesWritten", &[4][..])]);
assert_eq!(lines("file.write"), [4]);
assert_eq!(lines("file.read"), [5]);
}
/// A File object assigned to a function's own variable, its parameter or a `catch` block's
/// parameter ends with them; one assigned to an outer variable stays. Such a variable or parameter
/// starts without what the outer variable of the same name was assigned.
#[test]
fn a_file_in_a_local_variable_leaves_an_outer_folder_alone() {
let r = report(concat!(
"var d = new Folder(p);\n",
"function f(e) { var d = new File(q); e = new File(q); d.length = 0; }\n",
"var e = new Folder(p);\n",
"try { x(); } catch (d) { d = new File(q); }\n",
"d.length = 0; e.hidden = true;\n",
"var g = new Folder(p);\n",
"function h() { g = y(); g = new File(q); }\n",
"g.length = 0;\n",
"var k = new File(q);\n",
"function m(n) { var k = new Folder(p); k.length = 0; n = new Folder(p); n.hidden = true; }\n",
"try { x(); } catch (k) { k = new Folder(p); k.readonly = true; }\n",
));
let outside: Vec<(&str, &[usize])> = r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect();
assert_eq!(outside, [("filesWritten", &[2, 8][..])]);
}
/// The check reads branches and function bodies in order, so a variable that any assignment gives an
/// object that may be a File object keeps counting as one, in the same file, in a function the code
/// after it assigns, and across included files.
#[test]
fn a_variable_that_may_hold_a_file_counts_as_one() {
let r = report(concat!(
"var d;\n",
"if (c) { d = new File(p); } else { d = new Folder(p); }\n",
"d.length = 0;\n",
"function w() { h.hidden = true; }\n",
"var h = new Folder(q);\n",
"if (c) h = File(p);\n",
"var k = new Folder(q);\n",
"k.length = 0;\n",
));
let outside: Vec<(&str, &[usize])> = r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect();
assert_eq!(outside, [("filesWritten", &[3, 4][..])]);
for lib in ["d = new File(q);\n", "d = Folder.selectDialog();\n", "var x = new File(q); d = x;\n"] {
let mut m = mem(&[("lib.jsxinc", lib)]);
let r = check("main.jsx", b"#include 'lib.jsxinc'\nvar d = new Folder(p);\nd.length = 0;\n", Some(&mut m), &Limits::default());
let outside: Vec<(&str, &[usize])> = r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect();
assert_eq!(outside, [("filesWritten", &[3][..])], "{lib}");
}
// The function that may give `d` a File object only does so once `e` holds one, which the
// second scan learns.
let r = report(concat!(
"var d = new Folder(p);\n",
"function write() { d.length = 0; }\n",
"function assign() { d = e; }\n",
"var e = new File(q);\n",
));
let outside: Vec<(&str, &[usize])> = r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect();
assert_eq!(outside, [("filesWritten", &[2][..])]);
let mut m = mem(&[("lib.jsxinc", "d = Folder.temp;\n")]);
let r = check("main.jsx", b"#include 'lib.jsxinc'\nvar d = new Folder(p);\nd.length = 0;\n", Some(&mut m), &Limits::default());
assert!(r.outside.is_empty(), "{:?}", r.outside);
}
/// A `with` statement, `eval`, `Function`, `$.evalFile`, a member assignment that may write the
/// global object's property of a variable's name, and a script's own `Folder` may give a name a
/// File object the check doesn't see, before or after the setter in the text, so the setter counts.
#[test]
fn a_folder_that_other_code_may_rebind_counts_as_a_file() {
let scripts: [(&str, usize); 11] = [
("var d = new Folder(p);\nwith (o) { d.length = 0; }\n", 2),
("var d = new Folder(p);\nfunction w() { d.hidden = true; }\n$.global.d = new File(q);\n", 2),
("var d = new Folder(p);\nfunction w() { d.hidden = true; }\nthis.d = new File(q);\n", 2),
("var d = new Folder(p);\nfunction w() { d.hidden = true; }\nthis['d'] = new File(q);\n", 2),
("var d = new Folder(p);\nfunction w() { d.hidden = true; }\n$.global[k] = new File(q);\n", 2),
("eval(s);\nnew Folder(p).length = 0;\n", 2),
("var d = new Folder(p);\nd.readonly = true;\n$.evalFile(f);\n", 2),
("var d = new Folder(p);\nd.readonly = true;\nvar g = new Function(s);\n", 2),
("$.global.Folder = File;\nnew Folder(p).length = 0;\n", 2),
("function w() { new Folder(p).hidden = true; }\nFolder = File;\n", 1),
("function w() { Folder.temp.hidden = true; }\nvar Folder = File;\n", 1),
];
let written = |r: &Report| r.outside.iter().find(|o| o.id == "filesWritten").map(|o| o.lines.clone());
for (script, line) in scripts {
assert_eq!(written(&report(script)), Some(vec![line]), "{script}");
}
// Another variable's name, or a number as the index, leaves the Folder alone.
let r = report("var d = new Folder(p);\no.e = new File(q);\na[0] = new File(q);\nd.length = 0;\n");
assert_eq!(written(&r), None);
// A computed index may name the variable on any object that may be the global object.
let r = report("var d = new Folder(p);\na[k] = new File(q);\nd.length = 0;\n");
assert_eq!(written(&r), Some(vec![3]));
// A `with` statement in an included file counts in the script too.
let mut m = mem(&[("lib.jsxinc", "with (o) { x = 1; }\n")]);
let r = check("main.jsx", b"#include 'lib.jsxinc'\nvar d = new Folder(p);\nd.length = 0;\n", Some(&mut m), &Limits::default());
let outside: Vec<(&str, &[usize])> = r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect();
assert_eq!(outside, [("filesWritten", &[3][..])]);
}
/// An assignment to a computed property of any object, a string literal in brackets that names
/// `eval`, `Function` or `$.evalFile`, and any `constructor` property may give a name a File object
/// the check doesn't see, so no object counts as a Folder object.
#[test]
fn a_folder_that_unnamed_code_may_rebind_counts_as_a_file() {
let scripts: [(&str, usize); 13] = [
("var d = new Folder(p);\nvar g = this;\ng[k] = new File(q);\nd.length = 0;\n", 4),
("var d = new Folder(p);\nvar g = $.global;\ng[k] = File;\nd.length = 0;\n", 4),
("var d = new Folder(p);\n$['global'][k] = new File(q);\nd.length = 0;\n", 3),
("var d = new Folder(p);\n$[\"global\"][k] = new File(q);\nd.length = 0;\n", 3),
("var d = new Folder(p);\no[f(k)] = new File(q);\nd.length = 0;\n", 3),
("var d = new Folder(p);\nd.length = 0;\n$['evalFile'](f);\n", 2),
("var d = new Folder(p);\nd.length = 0;\nthis['eval'](s);\n", 2),
("var d = new Folder(p);\nd.length = 0;\nthis['\\u0065val'](s);\n", 2),
("var d = new Folder(p);\nd.length = 0;\nvar g = $.global['Function'];\n", 2),
("var d = new Folder(p);\nd.length = 0;\n(function () {}).constructor(s)();\n", 2),
("var d = new Folder(p);\nd.length = 0;\nvar c = o['constructor'];\n", 2),
("new Folder(p).hidden = true;\nvar c = o.constructor;\n", 1),
("var d = new Folder(p);\nd.length = 0;\nvar c = constructor;\n", 2),
];
let written = |r: &Report| r.outside.iter().find(|o| o.id == "filesWritten").map(|o| o.lines.clone());
for (script, line) in scripts {
assert_eq!(written(&report(script)), Some(vec![line]), "{script}");
}
}
/// A string literal in brackets names the property its value spells, escapes undone, as a name
/// written with escapes is read as the name it spells. A literal longer than the check reads may
/// name any variable, so no object counts as a Folder object.
#[test]
fn a_bracketed_name_is_read_whole_or_counts_as_any_name() {
let long = "a".repeat(300);
let scripts = [
format!("var {long} = new Folder(p);\nfunction w() {{ {long}.length = 0; }}\nthis['{long}'] = new File(q);\n"),
format!("var d = new Folder(p);\nfunction w() {{ d.length = 0; }}\nthis['{long}'] = new File(q);\n"),
"var d = new Folder(p);\nfunction w() { d.hidden = true; }\nthis['\\u0064'] = new File(q);\n".to_string(),
"var \\u0064 = new Folder(p);\nfunction w() { d.hidden = true; }\nthis['d'] = new File(q);\n".to_string(),
"var d = new Folder(p);\nfunction w() { \\u0064.hidden = true; }\nthis['\\x64'] = new File(q);\n".to_string(),
];
let written = |r: &Report| r.outside.iter().find(|o| o.id == "filesWritten").map(|o| o.lines.clone());
for script in &scripts {
assert_eq!(written(&report(script)), Some(vec![2]), "{script}");
}
// A short literal that names another variable leaves the Folder alone.
let r = report(&format!("var d = new Folder(p);\nfunction w() {{ d.length = 0; }}\nthis['{}'] = new File(q);\n", "a".repeat(200)));
assert_eq!(written(&r), None);
}
/// The lines of `filesWritten` in the report on `script`.
fn files_written(script: &str) -> Option<Vec<usize>> {
report(script).outside.iter().find(|o| o.id == "filesWritten").map(|o| o.lines.clone())
}
/// A name in brackets held in a variable may be `eval`, so no object counts as a Folder object.
#[test]
fn a_bracketed_variable_read_counts_as_any_name() {
let script = "var d = new Folder(p);\nd.length = 0;\nvar k = 'eval';\nthis[k]('d = new File(q)');\n";
assert_eq!(files_written(script), Some(vec![2]));
}
/// A name in brackets joined from strings may be `eval`, so no object counts as a Folder object.
#[test]
fn a_bracketed_joined_name_counts_as_any_name() {
let script = "var d = new Folder(p);\nd.length = 0;\nthis['ev' + 'al']('d = new File(q)');\n";
assert_eq!(files_written(script), Some(vec![2]));
}
/// A string literal in grouping parentheses inside brackets is read as a computed name.
#[test]
fn a_bracketed_name_in_parentheses_counts_as_any_name() {
let script = "var d = new Folder(p);\nd.length = 0;\nthis[('eval')]('d = new File(q)');\n";
assert_eq!(files_written(script), Some(vec![2]));
}
/// A comma expression in brackets names its last operand's value, which may be `eval`.
#[test]
fn a_bracketed_comma_expression_counts_as_any_name() {
let script = "var d = new Folder(p);\nd.length = 0;\nthis[0, 'eval']('d = new File(q)');\n";
assert_eq!(files_written(script), Some(vec![2]));
}
/// A name in brackets held in a variable may be `evalFile`, called on `$`.
#[test]
fn a_bracketed_variable_call_on_dollar_counts_as_any_name() {
let script = "var d = new Folder(p);\nd.length = 0;\nvar k = 'evalFile';\n$[k](f);\n";
assert_eq!(files_written(script), Some(vec![2]));
}
/// A name in brackets held in a variable may be `constructor`, which reaches `Function`.
#[test]
fn a_bracketed_variable_constructor_counts_as_any_name() {
let script = "var d = new Folder(p);\nd.length = 0;\nvar k = 'constructor';\n(function () {})[k](s)();\n";
assert_eq!(files_written(script), Some(vec![2]));
}
/// One string or number literal in brackets names one property, and an array literal names none,
/// also with brackets inside brackets, so the Folder is left alone. A computed name inside another
/// name in brackets still counts as any name.
#[test]
fn a_literal_name_in_brackets_leaves_the_folder_alone() {
let script = "var d = new Folder(p);\nvar x = o['plain'];\nvar y = a[0][1];\na[0] = 1;\nvar z = [k, o['x'], a[0]];\nd.length = 0;\n";
assert_eq!(files_written(script), None);
assert_eq!(files_written("var d = new Folder(p);\nd.length = 0;\nvar x = o[a[0]];\n"), Some(vec![2]));
}
/// A preprocessor line inside a name in brackets hides the `[` before the name, so the name may be
/// `eval` and no object counts as a Folder object.
#[test]
fn a_directive_in_a_bracketed_name_counts_as_any_name() {
let script = "var d = new Folder(p);\nd.length = 0;\nthis[\n#target illustrator\n'eval']('d = new File(q)');\n";
assert_eq!(files_written(script), Some(vec![2]));
}
#[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");
assert!(feature(&r, "file.objects").is_none(), "{:?}", r.features);
assert!(feature(&r, "socket").is_none(), "{:?}", r.features);
}
#[test]
fn object_keys_are_not_platform_names() {
let r = report("var options = {Socket: false, ExternalObject: false, Window: 1};\nf(a, Socket);\n");
assert_eq!(feature(&r, "socket").map(|f| f.lines.as_slice()), Some(&[2][..]));
assert!(feature(&r, "externalObject").is_none());
assert!(feature(&r, "scriptui.window").is_none());
let outside: Vec<(&str, &[usize])> = r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect();
assert_eq!(outside, [("network", &[2][..])]);
}
#[test]
fn the_scripts_own_variables_hide_the_roots() {
let r = report(concat!(
"var selection = [1, 2];\n",
"selection.push(3);\n",
"function documents() { return []; }\n",
"documents().length;\n",
"var o = { activeDocument: 1 };\n",
"foo.app.x = 1;\n",
"var doc = app.activeDocument;\n",
"doc = 5;\n",
"doc.acmeItems;\n",
));
let members: Vec<&str> = r.members.iter().map(|m| m.name.as_str()).collect();
assert_eq!(members, ["app.activeDocument"], "only the line that reads the document");
}
#[test]
fn only_the_commas_of_a_var_statement_declare_names() {
let r = report(concat!(
"var x = f(0, app.activeDocument), y = [1, selection];\n",
"app.activeDocument.acmeLayers;\n",
"selection.length;\n",
"var a = 1, b, documents = [];\n",
"documents.length;\n",
));
let members: Vec<&str> = r.members.iter().map(|m| m.name.as_str()).collect();
assert_eq!(members, ["app.activeDocument", "app.activeDocument.acmeLayers", "selection", "selection.length"]);
}
/// In `for (var name in …)` the names after `in` are the loop's object, also after a comma (ECMA-262
/// 3rd edition, section 12.6.4): the statement declares none of them, in a function's body or outside.
#[test]
fn the_names_after_the_in_of_a_for_var_are_not_declared() {
let r = report(concat!(
"function run() {\n",
" new File(p).remove();\n",
" for (var key in obj, File) {}\n",
" for each (var item in list, alert) {}\n",
" alert('a');\n",
"}\n",
"for (var k in obj, Folder) {}\n",
));
assert_eq!(feature(&r, "file.objects").map(|f| f.lines.as_slice()), Some(&[2, 3, 7][..]));
assert_eq!(feature(&r, "notify").map(|f| f.lines.as_slice()), Some(&[5][..]));
let outside: Vec<(&str, &[usize])> = r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect();
assert_eq!(outside, [("filesWritten", &[2][..])]);
}
#[test]
fn a_var_without_a_value_keeps_what_the_name_holds() {
let r = report(concat!(
"var f = new File(p); var f; f.remove();\n",
"var doc = app.activeDocument;\n",
"var doc;\n",
"doc.acmeName;\n",
"function g() { var f; f.remove(); var app; app.acmeLocal; }\n",
));
let outside: Vec<(&str, &[usize])> = r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect();
assert_eq!(outside, [("filesWritten", &[1][..])]);
let members: Vec<&str> = r.members.iter().map(|m| m.name.as_str()).collect();
assert_eq!(members, ["app.activeDocument", "app.activeDocument.acmeName"]);
}
#[test]
fn a_block_comment_with_a_line_break_ends_a_line() {
let r = report("var d = app.activeDocument/*\n*/d.acmeLayers;\n");
let members: Vec<&str> = r.members.iter().map(|m| m.name.as_str()).collect();
assert_eq!(members, ["app.activeDocument", "app.activeDocument.acmeLayers"]);
}
#[test]
fn a_var_statement_goes_on_after_a_function_with_a_var_statement_of_its_own() {
let r = report("var helper = function () { var x = 0; }, f = new File(p);\nf.remove();\n");
let outside: Vec<(&str, &[usize])> = r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect();
assert_eq!(outside, [("filesWritten", &[2][..])]);
}
#[test]
fn a_var_statement_goes_on_across_the_line_breaks_inside_it() {
let r = report(concat!(
"var first =\n 0, doc = app.activeDocument;\n",
"doc.acmeLayers;\n",
"var opts = {\n a: 1\n}, sel = selection;\n",
"sel.length;\n",
"var count = 1\n , layer = app.activeDocument;\n",
"layer.acmeName;\n",
"var n = 1\nvar documents = 2;\n",
"documents.length;\n",
));
let members: Vec<&str> = r.members.iter().map(|m| m.name.as_str()).collect();
assert_eq!(members, ["app.activeDocument", "app.activeDocument.acmeLayers", "app.activeDocument.acmeName", "selection", "selection.length"]);
}
#[test]
fn a_catch_blocks_parameter_is_its_own() {
let r = report(concat!(
"var f = new File('out');\n",
"try {} catch (f) { f = null; }\n",
"f.remove();\n",
"var doc = app.activeDocument;\n",
"try { x(); } catch (doc) { doc.acmeName; }\n",
"doc.acmeLayers;\n",
"try { x(); } catch (e) { g = new File(p); }\n",
"g.remove();\n",
"try { x(); } catch (e) { var d2 = app.activeDocument; }\n",
"d2.acmeMode;\n",
));
let outside: Vec<(&str, &[usize])> = r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect();
assert_eq!(outside, [("filesWritten", &[3, 8][..])]);
let members: Vec<&str> = r.members.iter().map(|m| m.name.as_str()).collect();
assert_eq!(members, ["app.activeDocument", "app.activeDocument.acmeLayers", "app.activeDocument.acmeMode"]);
// In a function, the parameter is not the function's own name, assigned in the block or not.
let r = report(concat!(
"var f = new File(p);\n",
"function g() { try {} catch (f) {} var f; f.remove(); }\n",
"function h() { try {} catch (f) { f = null; } }\n",
"f.remove();\n",
"function k() { try { throw 0; } catch (e) { var e = new File(p); var e; e.remove(); } }\n",
"function m() { try {} catch (f) { var f; } f.remove(); }\n",
"f.remove();\n",
"function n() { try {} catch (f) { try {} catch (f) { var f; } f.remove(); } f.remove(); }\n",
"f.remove();\n",
));
let outside: Vec<(&str, &[usize])> = r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect();
assert_eq!(outside, [("filesWritten", &[4, 5, 7, 9][..])]);
}
#[test]
fn assignments_inside_expressions_are_followed() {
let r = report(concat!(
"var f;\n",
"(f = new File('out.txt'));\n",
"f.open('w');\n",
"if ((g = File.openDialog('Pick')) != null) { g.remove(); }\n",
"switch (k) { case 1: doc = app.activeDocument; break; }\n",
"doc.acmeLayers;\n",
"if (!ok) x = 1; else h = new File(p);\n",
"h.remove();\n",
"o.e = new File(p);\n",
"e.remove();\n",
"var c; ok ? c = new File(p) : 0;\n",
"c.remove();\n",
"ok ? d2 = app.activeDocument : 0;\n",
"d2.acmeName;\n",
));
let outside: Vec<(&str, &[usize])> = r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect();
assert_eq!(outside, [("filesWritten", &[3, 4, 8, 12][..])]);
let members: Vec<&str> = r.members.iter().map(|m| m.name.as_str()).collect();
assert_eq!(members, ["app.activeDocument", "app.activeDocument.acmeLayers", "app.activeDocument.acmeName"]);
}
/// `name += …` and the other compound assignments, `name++`, `name--`, `++name` and `--name` give the
/// name a number or a string (ECMA-262 3rd edition, sections 11.3, 11.4 and 11.13.2), so it no longer
/// holds a member chain or a File object. A compound assignment's name holds its old value while the
/// value on the right is read, until that value ends: the `:` of a conditional inside the value
/// doesn't end it, and the `:` of one around it does. A `++` after a line break is the next name's,
/// and a property changed so leaves the variable as it is.
#[test]
fn compound_assignments_and_increments_empty_a_name() {
let r = report(concat!(
"var d = app.activeDocument; d += ''; d.acmeA;\n",
"var f = new File(p); f += '.txt'; f.remove();\n",
"var e = app.activeDocument; e >>>= 1; e.acmeB;\n",
"var g = app.activeDocument; g++; g.acmeC;\n",
"var h = app.activeDocument; --h; h.acmeD;\n",
"var k = app.activeDocument; x = y + ++k; k.acmeE;\n",
"var m = app.activeDocument; m\n",
"++n; m.acmeF;\n",
"var o = app.activeDocument; o.count += 1; ++o.total; o.acmeG;\n",
"var q = new File(p); w.q += 1; q.remove();\n",
"var r = new File(p); r += r.remove();\n",
"r.remove();\n",
"var s = app.activeDocument; s += s.acmeH; s.acmeI;\n",
"var u = app.activeDocument; u += 'x'\n",
"u.acmeJ;\n",
"var v = new File(p); v += ok ? '' : v.remove();\n",
"v.remove();\n",
"var z; ok ? z += 1 : z = new File(p); z.remove();\n",
"var y = new File(p); y += a ? b ? 1 : 2 : y.remove();\n",
));
let outside: Vec<(&str, &[usize])> = r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect();
assert_eq!(outside, [("filesWritten", &[10, 11, 16, 18, 19][..])]);
let members: Vec<&str> = r.members.iter().map(|m| m.name.as_str()).collect();
assert_eq!(
members,
[
"app.activeDocument",
"app.activeDocument.acmeF",
"app.activeDocument.acmeG",
"app.activeDocument.acmeH",
"app.activeDocument.count",
"app.activeDocument.total"
]
);
}
/// The lines of the `filesWritten` use in `r`, empty when it has none, and the member names, for a
/// script whose other uses are not under test.
fn written_lines_and_members(r: &Report) -> (Vec<usize>, Vec<&str>) {
let lines = r.outside.iter().flat_map(|o| {
assert_eq!(o.id, "filesWritten");
o.lines.iter().copied()
});
(lines.collect(), r.members.iter().map(|m| m.name.as_str()).collect())
}
/// `(name, script, lines of the files written, members)`: what a name holds after a value that goes
/// on past its parentheses or past a call, or that is a comma expression or a grouped value.
const HELD_VALUE_CASES: &[(&str, &str, &[usize], &[&str])] = &[
(
"a_value_that_goes_on_on_the_next_line_is_not_what_the_name_holds",
concat!(
"var a = new File(p);\n",
"var b = a\n .name;\n",
"b.remove();\n",
"var d = app.activeDocument\n + 'x';\n",
"d.acmeName;\n",
"var c = a\nc.remove();\n",
"var e = app.activeDocument\ne.acmeLayers;\n",
),
&[9],
&["app.activeDocument", "app.activeDocument.acmeLayers"],
),
// A comma expression in parentheses is the value of its last operand (ECMA-262 3rd edition,
// section 11.14).
(
"a_comma_expression_in_parentheses_is_its_last_operand",
concat!(
"var d = (0, app.activeDocument);\n",
"d.acmeName;\n",
"var f = (x, (new File(p)));\n",
"f.remove();\n",
"var g = (new File(p), 0);\n",
"g.remove();\n",
"var e = ((app.activeDocument), 0);\n",
"e.acmeLayers;\n",
"var k = (a = 1, f);\n",
"k.remove();\n",
),
&[4, 10],
&["app.activeDocument", "app.activeDocument.acmeName"],
),
// Parentheses that hold only the start of the value, or a value that goes on after them, leave the
// name holding nothing the check follows, as a call's argument list does.
(
"a_value_that_only_begins_in_parentheses_is_not_what_the_name_holds",
concat!(
"var f = new File(p);\n",
"var x = (app.activeDocument).name;\n",
"x.acmeLayers;\n",
"var y = (f).name;\n",
"y.remove();\n",
"var doc = ((app.activeDocument) + 1);\n",
"doc.acmeName;\n",
"var g = (new File(p).name);\n",
"g.remove();\n",
"var h = (new File(p))\n .name;\n",
"h.remove();\n",
"var k = (File.name);\nnew File(p);\n",
"k.remove();\n",
"var flag = (File === File(p));\n",
"flag.remove();\n",
"var c = (f)(1), z = foo(new File(p));\n",
"c.remove(); z.remove();\n",
"var u = (x;\ny, new File(p));\n",
"u.remove();\n",
),
&[],
&["app.activeDocument"],
),
// The name holds a grouped value from the token after its last `)`: the next statement reads it, a
// function's own variable holds it only inside the function, and grouped values inside a function in
// parentheses don't change what the outer name holds.
(
"a_grouped_value_is_held_from_the_token_after_its_parentheses",
concat!(
"var doc = (app.activeDocument)\n",
"doc.acmeName;\n",
"function test() { var d2 = (app.activeDocument) }\n",
"d2.acmeLayers;\n",
"var f = (new File(File(p).fsName));\n",
"f.remove();\n",
"var lib = (function () { var sel = (selection); return sel.length; })();\n",
"var w = new File(p);\n",
"w = (function () { var inner = (q); return inner; });\n",
"w.remove();\n",
),
&[6],
&["app.activeDocument", "app.activeDocument.acmeName", "selection", "selection.length"],
),
// A value without parentheses that starts with `File` or `Folder` is what the name holds only when
// nothing follows the call, the dialog or the place, as in parentheses.
(
"a_file_value_that_goes_on_is_not_what_the_name_holds",
concat!(
"var a = File(p) && {remove: function () {}};\n",
"a.remove();\n",
"var b = new File(p).name;\n",
"b.remove();\n",
"var c = new File(p)\n .parent;\n",
"c.remove();\n",
"var d = Folder.temp + '/x', e = File.openDialog('Pick') || other;\n",
"d.remove(); e.remove();\n",
"var g = File(p) == null, h = File, k = File.saveDialog;\n",
"g.remove(); h.remove(); k.remove();\n",
),
&[],
&[],
),
];
const HELD_VALUE_CASE_COUNT: usize = 5;
#[test]
fn a_name_holds_what_the_value_it_was_given_ends_as() {
assert_eq!(HELD_VALUE_CASES.len(), HELD_VALUE_CASE_COUNT);
for &(name, src, lines, members) in HELD_VALUE_CASES {
let r = report(src);
let (got_lines, got_members) = written_lines_and_members(&r);
assert_eq!((got_lines.as_slice(), got_members.as_slice()), (lines, members), "{name}");
}
}
/// A value in grouping parentheses, nested to any depth, is what the name holds (ECMA-262 3rd
/// edition, section 11.1.6).
#[test]
fn a_value_in_grouping_parentheses_is_what_the_name_holds() {
let deep = format!("var deep = {}new File(p){};\ndeep.remove();\n", "(".repeat(1000), ")".repeat(1000));
let src = concat!(
"var f = (new File(p));\n",
"f.open('w');\n",
"g = ((new File(p)));\n",
"g.remove();\n",
"var d = (app.activeDocument);\n",
"d.acmeLayers;\n",
"var b = (f), n = (new Folder);\n",
"b.remove();\n",
"n.create();\n",
"var s = (File.saveDialog()), t = (Folder.temp);\n",
"s.write(x);\n",
"t.remove();\n",
"if ((h = (new File(p))) != null) { h.rename(q); }\n",
"var sel = ((selection));\n",
"sel.length;\n",
)
.to_string();
let r = report(&(src + &deep));
let (lines, members) = written_lines_and_members(&r);
assert_eq!(lines, [2, 4, 8, 9, 11, 12, 13, 17]);
assert_eq!(members, ["app.activeDocument", "app.activeDocument.acmeLayers", "selection", "selection.length"]);
}
/// Grouping parentheses, nested or after a comma, leave a name holding what the value without them
/// gives it.
#[test]
fn a_value_holds_the_same_with_or_without_grouping_parentheses() {
let start = "var f = new File(p), doc = app.activeDocument;\n";
let uses = "x.remove();\nx.acmeName;\n";
for value in [
"new File(p)",
"File(p)",
"new Folder(p)",
"new Folder",
"Folder.temp",
"File.openDialog('Pick')",
"f",
"app.activeDocument",
"app.documents[0]",
"doc.acmeLayers.add()",
"selection",
"1",
"pick(f)",
] {
let bare = report(&format!("{start}var x = {value};\n{uses}"));
for grouped in [format!("({value})"), format!("(({value}))"), format!("(0, {value})")] {
let r = report(&format!("{start}var x = {grouped};\n{uses}"));
assert_eq!((&r.outside, &r.members), (&bare.outside, &bare.members), "{grouped}");
}
}
}
/// A call on a File or Folder object in grouping parentheses is a call on the object (ECMA-262 3rd
/// edition, section 11.1.6). A call on parentheses that hold more than the object, on the result of a
/// call, or on an E4X filter is not a call on the object.
#[test]
fn a_call_on_a_file_in_grouping_parentheses_is_followed() {
let r = report(concat!(
"var f = new File(p);\n",
"(f).remove();\n",
"(new File(p)).remove();\n",
"((f)).open('w');\n",
"(0, f).rename(q);\n",
"(File.saveDialog('Save')).write(s);\n",
"(Folder.temp).remove();\n",
"(new File).create();\n",
"if (ok) (f).remove();\n",
"{} (new Folder(p)).create();\n",
"var s = (f).read();\n",
"(f.parent).remove();\n",
"(x + f).remove();\n",
"(f).name.remove();\n",
"foo(f).remove();\n",
"var g = function () {}\n(f).remove();\n",
"a\n(f).remove();\n",
"o.return(f).remove();\n",
"list.(f).remove();\n",
));
let outside: Vec<(&str, &[usize], &[&str])> = r.outside.iter().map(|o| (o.id, o.lines.as_slice(), o.modes.as_slice())).collect();
assert_eq!(outside, [("filesWritten", &[2, 3, 4, 5, 6, 7, 8, 9, 10][..], &["w"][..])]);
assert_eq!(feature(&r, "file.read").map(|f| f.lines.as_slice()), Some(&[11][..]));
}
/// The name holds a value without parentheses from the token that ends it: the next statement reads
/// it, the value itself reads what the name held before, a function's own variable holds it only
/// inside the function, a preprocessor line or the end of a file ends it, and a line break after a
/// `.` does not.
#[test]
fn a_file_value_is_held_from_the_token_that_ends_it() {
let mut m = mem(&[("lib.jsxinc", "var out = new File(p)")]);
let src = concat!(
"var a = new File(p)\n",
"a.remove();\n",
"var doc = app.activeDocument;\n",
"doc = new File(doc.fullName);\n",
"doc.remove();\n",
"function test() { var w = new File(p) }\n",
"function test2() { var v = a }\n",
"w.remove(); v.remove();\n",
"var t = Folder.temp\n",
"#target acme\n",
"t.remove();\n",
"#include 'lib.jsxinc'\n",
"out.remove();\n",
"var s = File.\n saveDialog('Save');\n",
"s.remove();\n",
);
let r = check("main.jsx", src.as_bytes(), Some(&mut m), &Limits::default());
let (lines, members) = written_lines_and_members(&r);
assert_eq!(lines, [2, 5, 11, 13, 16]);
assert_eq!(members, ["app.activeDocument", "app.activeDocument.fullName"]);
}
#[test]
fn a_functions_own_variables_hide_the_roots_only_inside_it() {
let r = report(concat!(
"function helper() { var app = {}; app.acmeLocal = 1; }\n",
"app.activeDocument.acmeLayers;\n",
"function pick() { var selection = []; return selection.length; }\n",
"selection.length;\n",
"var doc = app.activeDocument;\n",
"var other = function () { var doc = 5; doc.acmeName; };\n",
"doc.acmeName;\n",
"function outer() { function documents() {} documents().length; }\n",
"documents.length;\n",
));
let members: Vec<&str> = r.members.iter().map(|m| m.name.as_str()).collect();
assert_eq!(
members,
["app.activeDocument", "app.activeDocument.acmeLayers", "app.activeDocument.acmeName", "documents.length", "selection.length"]
);
}
#[test]
fn a_name_a_function_body_empties_is_empty_only_inside_it() {
let r = report(concat!(
"var f = new File(p);\n",
"function unused() { f = null; app = 1; }\n",
"f.remove();\n",
"var doc;\n",
"function main() { doc = app.activeDocument; }\n",
"doc.acmeLayers;\n",
));
let outside: Vec<(&str, &[usize])> = r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect();
assert_eq!(outside, [("filesWritten", &[3][..])]);
let members: Vec<&str> = r.members.iter().map(|m| m.name.as_str()).collect();
assert_eq!(members, ["app.activeDocument", "app.activeDocument.acmeLayers"]);
}
#[test]
fn a_function_expressions_name_hides_a_root_only_inside_it() {
let r = report(concat!(
"var f = function app() { return app.length; };\n",
"app.activeDocument.acmeLayers;\n",
"function selection() {}\n",
"selection.length;\n",
));
let members: Vec<&str> = r.members.iter().map(|m| m.name.as_str()).collect();
assert_eq!(members, ["app.activeDocument.acmeLayers"]);
}
#[test]
fn a_functions_parameters_hide_the_roots_and_variables_only_inside_it() {
assert!(report("function count(selection) { return selection.length; }\n").needs.is_empty());
let r = report(concat!(
"function count(selection) { return selection.length; }\n",
"var doc = app.activeDocument;\n",
"function f(doc, app) { doc.acmeLocal; app.acmeLocal; }\n",
"var g = function (documents) { return documents.length; };\n",
"doc.acmeName;\n",
"selection.length;\n",
));
let members: Vec<&str> = r.members.iter().map(|m| m.name.as_str()).collect();
assert_eq!(members, ["app.activeDocument", "app.activeDocument.acmeName", "selection.length"]);
}
/// A function declaration later in a function's body makes the name the function's own in all of the
/// body (ECMA-262 3rd edition, section 10.1.3), and nowhere else. A function expression's name is the
/// expression's own (section 13), and a function declared inside a block is the function's own from
/// where it stands.
#[test]
fn a_function_declared_later_in_a_body_is_its_own_in_all_of_it() {
let r = report(concat!(
"function run() {\n",
" alert('a');\n",
" var f = new File(p);\n",
" f.remove();\n",
" app.activeDocument.acmeName;\n",
" confirm('b');\n",
" if (ok) { prompt('c'); function prompt() {} }\n",
" function alert(m) {}\n",
" function File(p) {}\n",
" function app() {}\n",
" var g = function confirm() {};\n",
"}\n",
"alert('d');\n",
"var h = new File(q);\n",
"h.remove();\n",
));
assert_eq!(feature(&r, "notify").map(|f| f.lines.as_slice()), Some(&[6, 7, 13][..]));
assert_eq!(feature(&r, "file.objects").map(|f| f.lines.as_slice()), Some(&[14][..]));
let outside: Vec<(&str, &[usize])> = r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect();
assert_eq!(outside, [("filesWritten", &[15][..])]);
assert!(r.members.is_empty(), "{:?}", r.members);
}
/// A `var` statement later in a function's body, in a block or a `for` included, makes the name the
/// function's own in all of the body: neither the outer variable nor the platform's object or root of
/// that name is what the name stands for there. A `var` statement goes on after a function in it, and
/// a line break ends it as in the analysis.
#[test]
fn a_var_later_in_a_body_is_its_own_in_all_of_it() {
let r = report(concat!(
"var doc = app.activeDocument, out = new File(p);\n",
"function tidy() {\n",
" doc.acmeName;\n",
" out.remove();\n",
" confirm('a');\n",
" selection.length;\n",
" prompt('b');\n",
" for (var i = 0, doc; i < 1; i++) {}\n",
" if (ok) { var out = 1, confirm = ask; }\n",
" var helper = function () { var x; }, prompt = ask\n",
" x, alert('c');\n",
" var selection;\n",
"}\n",
"doc.acmeLayers;\n",
"out.remove();\n",
"confirm('d');\n",
));
assert_eq!(feature(&r, "notify").map(|f| f.lines.as_slice()), Some(&[11, 16][..]));
let outside: Vec<(&str, &[usize])> = r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect();
assert_eq!(outside, [("filesWritten", &[15][..])]);
let members: Vec<&str> = r.members.iter().map(|m| m.name.as_str()).collect();
assert_eq!(members, ["app.activeDocument", "app.activeDocument.acmeLayers"]);
}
/// When a function's body declares `Folder`, `$` or `Socket` later on, each is the body's own in all
/// of the body, as `alert` and `File` are, and the platform's object again after the body.
#[test]
fn each_kind_of_platform_name_a_body_declares_later_is_its_own() {
let r = report(concat!(
"function run() {\n",
" var t = Folder.temp; t.remove();\n",
" $.writeln('a');\n",
" new Socket();\n",
" var Folder = {}, $ = {}, Socket = Object;\n",
"}\n",
"Folder.temp.remove();\n",
"$.writeln('b');\n",
"new Socket();\n",
));
for (id, line) in [("file.objects", 7), ("file.places", 7), ("dollar", 8), ("socket", 9)] {
assert_eq!(feature(&r, id).map(|f| f.lines.as_slice()), Some(&[line][..]), "{id}");
}
let outside: Vec<(&str, &[usize])> = r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect();
assert_eq!(outside, [("filesWritten", &[7][..]), ("network", &[9][..])]);
}
/// A function's parameters are its own, and not the code's around it. A `catch` block's parameter
/// is the block's own (ECMA-262 3rd edition, section 12.14), while a `var` statement in the block
/// declares a name of the function's own.
#[test]
fn parameters_and_catch_parameters_are_own_only_where_they_are_declared() {
let r = report(concat!(
"alert('a');\n",
"function show(alert, File) { alert('b'); new File(p).remove(); }\n",
"function run() {\n",
" confirm('c');\n",
" try { x(); } catch (confirm) { confirm('d'); }\n",
" confirm('e');\n",
"}\n",
"function load() {\n",
" prompt('f');\n",
" try { x(); } catch (prompt) { var prompt = 1; prompt('g'); }\n",
" prompt('h');\n",
"}\n",
));
assert_eq!(feature(&r, "notify").map(|f| f.lines.as_slice()), Some(&[1, 4, 6][..]));
assert!(r.outside.is_empty(), "{:?}", r.outside);
}
/// A nested function reads the names the functions around it declare later in their bodies, and a
/// name a function declares holds neither in the code around it nor in another function.
#[test]
fn a_nested_function_reads_what_the_functions_around_it_declare_later() {
let r = report(concat!(
"var doc = app.activeDocument;\n",
"function outer() {\n",
" function inner() { alert('a'); doc.acmeName; }\n",
" var alert = log, doc = 1;\n",
"}\n",
"function first() { var confirm; function nested() { var prompt; } prompt('b'); }\n",
"function second() { confirm('c'); doc.acmeLayers; }\n",
"alert('d');\n",
));
assert_eq!(feature(&r, "notify").map(|f| f.lines.as_slice()), Some(&[6, 7, 8][..]));
let members: Vec<&str> = r.members.iter().map(|m| m.name.as_str()).collect();
assert_eq!(members, ["app.activeDocument", "app.activeDocument.acmeLayers"]);
}
/// The global code's function declarations make their names the script's own from the start of the
/// file, and a member chain or a File object that another file's variable of that name holds stays.
/// A `var` statement in the global code leaves the host's object of that name as it is until a value
/// is assigned (ECMA-262 3rd edition, sections 10.1.3, 10.1.5 and 10.2.1), and a function declared
/// inside a block is the script's own from where it stands.
#[test]
fn a_function_declared_later_in_the_global_code_is_the_scripts_own_from_the_start() {
let r = report(concat!(
"alert('a');\n",
"app.activeDocument.acmeName;\n",
"var f = new File(p); f.remove();\n",
"confirm('b');\n",
"var confirm = ask;\n",
"confirm('c');\n",
"if (ok) { prompt('d'); function prompt() {} }\n",
"prompt('e');\n",
"function alert(m) {}\n",
"function app() {}\n",
"function File(p) {}\n",
));
assert_eq!(feature(&r, "notify").map(|f| f.lines.as_slice()), Some(&[4, 7][..]));
assert!(feature(&r, "file.objects").is_none(), "{:?}", r.features);
assert!(r.outside.is_empty(), "{:?}", r.outside);
assert!(r.members.is_empty(), "{:?}", r.members);
let mut m = mem(&[("lib.jsxinc", "var out = new File(p);\n")]);
let r = check("main.jsx", b"#include 'lib.jsxinc'\nout.remove();\nfunction out() {}\n", Some(&mut m), &Limits::default());
let outside: Vec<(&str, &[usize])> = r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect();
assert_eq!(outside, [("filesWritten", &[2][..])]);
}
/// A function declaration is not evaluated where it stands (ECMA-262 3rd edition, section 14): a
/// member chain assigned to the function's name before the declaration stays.
#[test]
fn a_chain_assigned_before_a_function_declaration_stays() {
let r = report(concat!(
"doc = app.activeDocument;\n",
"function doc() {}\n",
"doc.acmeName;\n",
"function run() { sel = selection; function sel() {} sel.length; }\n",
));
let members: Vec<&str> = r.members.iter().map(|m| m.name.as_str()).collect();
assert_eq!(members, ["app.activeDocument", "app.activeDocument.acmeName", "selection", "selection.length"]);
}
/// A name a function body or the global code declares past [`Limits::entries`], or in a function
/// nested past [`Limits::nesting`], is the script's own only from where it is declared, and a name
/// left out marks the report truncated.
#[test]
fn declarations_past_a_limit_hold_from_where_they_stand() {
let notify = |src: &str, limits: &Limits| {
let r = check("test.jsx", src.as_bytes(), None, limits);
(feature(&r, "notify").map(|f| f.lines.clone()).unwrap_or_default(), r.truncated)
};
let entries = Limits { entries: 3, ..Limits::default() };
assert_eq!(notify("function g() {\n alert('a');\n var b, c, alert;\n}\n", &entries), (vec![], false));
assert_eq!(notify("function g() {\n alert('a');\n var b, c, d, alert;\n}\n", &entries), (vec![2], true));
assert_eq!(notify("function g(b, c) {\n alert('a');\n var alert;\n}\n", &entries), (vec![], false));
assert_eq!(notify("function g(b, c, d) {\n alert('a');\n var alert;\n}\n", &entries), (vec![2], true));
assert_eq!(notify("alert('a');\nfunction b() {}\nfunction c() {}\nfunction alert() {}\n", &entries), (vec![], false));
assert_eq!(notify("alert('a');\nfunction b() {}\nfunction c() {}\nfunction d() {}\nfunction alert() {}\n", &entries), (vec![1], true));
let nesting = Limits { nesting: 4, ..Limits::default() };
let four = "function a() { function b() { function c() { function d() {\n alert('a');\n var alert;\n} } } }\n";
let five = "function a() { function b() { function c() { function d() { function e() {\n alert('a');\n var alert;\n} } } } }\n";
assert_eq!(notify(four, &nesting).0, Vec::<usize>::new());
assert_eq!(notify(five, &nesting).0, [2]);
assert_eq!(notify(five, &Limits::default()).0, Vec::<usize>::new());
}
#[test]
fn application_methods_are_features_not_members() {
let r = report("app.executeMenuCommand('acmecopyall');\napp.selectTool('acme');\n");
assert!(r.members.is_empty(), "{:?}", r.members);
assert_eq!(r.needs, [Need::Dom, Need::MenuCommands]);
}
#[test]
fn e4x_and_the_xml_object_need_only_the_language() {
let r = report("var x = <a><b/></a>;\nvar y = new XML(s);\nfor each (var v in x.b) {}\n");
assert!(feature(&r, "e4x").is_some() && feature(&r, "xml").is_some());
assert_eq!(r.needs, [Need::Language]);
}
#[test]
fn type_names_compared_or_created_are_listed() {
let r = report(concat!(
"if (item.typename == 'AcmeShape') {}\n",
"var c = new AcmeColor();\n",
"function Helper() {}\n",
"var h = new Helper();\n",
"var d = new Date();\n",
"var Maker = function () {};\n",
"var m = new Maker();\n",
));
let types: Vec<&str> = r.types.iter().map(|t| t.name.as_str()).collect();
assert_eq!(types, ["AcmeColor", "AcmeShape"]);
assert_eq!(r.needs, [Need::Dom]);
}
#[test]
fn collections_and_typed_arrays_are_not_type_names() {
let r = report(concat!(
"var m = new Map(), s = new Set(), wm = new WeakMap(), ws = new WeakSet();\n",
"var b = new ArrayBuffer(8), v = new DataView(b), u = new Uint8Array(b), c = new Uint8ClampedArray(2);\n",
"var i = [new Int8Array(1), new Int16Array(1), new Uint16Array(1), new Int32Array(1), new Uint32Array(1)];\n",
"var f = [new Float32Array(1), new Float64Array(1)];\n",
));
assert!(r.types.is_empty(), "{:?}", r.types.iter().map(|t| t.name.as_str()).collect::<Vec<_>>());
}
#[test]
fn menu_commands_are_counted_with_their_lines() {
let r = report(concat!(
"app.executeMenuCommand('acmecopyall');\n",
"app.executeMenuCommand(\"acmecopyall\");\n",
"app.executeMenuCommand('acme\\x41');\n",
"app.executeMenuCommand(name);\n",
"app.executeMenuCommand('acme' + n);\n",
"app.executeMenuCommand('\\141cmecopyall');\n",
));
let strings: Vec<(&str, usize, &[usize])> = r.menu_strings.iter().map(|s| (s.value.as_str(), s.count, s.lines.as_slice())).collect();
assert_eq!(strings, [("acmecopyall", 3, &[1, 2, 6][..]), ("acmeA", 1, &[3][..])]);
assert_eq!(r.menu_dynamic, 2);
assert_eq!(feature(&r, "menuCommand").map(|f| f.count), Some(6));
}
#[test]
fn a_menu_command_string_with_a_trailing_comma_is_listed() {
let r = report(concat!(
"app.executeMenuCommand('acmecopyall',);\n",
"app.executeMenuCommand('acmecopyall', n);\n",
"app.executeMenuCommand('acmecopyall',\n",
));
let strings: Vec<(&str, usize, &[usize])> = r.menu_strings.iter().map(|s| (s.value.as_str(), s.count, s.lines.as_slice())).collect();
assert_eq!(strings, [("acmecopyall", 1, &[1][..])]);
assert_eq!(r.menu_dynamic, 2);
}
#[test]
fn effects_outside_the_document_are_listed_in_order() {
let r = report(concat!(
"var log = new File(p);\n",
"log.open('a');\n",
"log.open('w');\n",
"log.remove();\n",
"var s = new Socket();\n",
"var lib = new ExternalObject('lib:x');\n",
"var bt = new BridgeTalk();\n",
"$.setenv('K', 'V');\n",
"eval(code);\n",
"out.execute();\n",
"app.preferences.acmeFlag = true;\n",
"var v = app.preferences.acmeFlag;\n",
"app.preferences.setAcmeFlag('k', 1);\n",
"app.preferences.removeAcmeFlag('k');\n",
"var w = app.preferences.getAcmeFlag('k');\n",
));
let outside: Vec<(&str, usize, &[usize], &[&str])> = r.outside.iter().map(|o| (o.id, o.count, o.lines.as_slice(), o.modes.as_slice())).collect();
assert_eq!(
outside,
[
("filesWritten", 3, &[2, 3, 4][..], &["a", "w"][..]),
("filesOpened", 1, &[10][..], &[][..]),
("network", 1, &[5][..], &[][..]),
("nativeCode", 1, &[6][..], &[][..]),
("otherApps", 1, &[7][..], &[][..]),
("environment", 1, &[8][..], &[][..]),
("unseenCode", 1, &[9][..], &[][..]),
("preferences", 3, &[11, 13, 14][..], &[][..]),
]
);
assert_eq!(feature(&r, "preferences").map(|f| f.count), Some(5), "reading a preference is no change");
}
#[test]
fn problems_are_listed_with_their_lines() {
let r = report(concat!(
"var a = 'open;\n",
"var b = /open;\n",
"var c = (1;\n",
"var d = 2];\n",
"#pragma once\n",
"var e = `tpl`;\n",
"var x = <a><b></a>;\n",
"/* open\n",
));
assert_eq!(
problems(&r),
[
("unterminatedString", Some(1), None),
("unterminatedRegExp", Some(2), None),
("unexpectedBracket", Some(4), Some("]")),
("unknownDirective", Some(5), Some("#pragma")),
("newerSyntax", Some(6), Some("`")),
("unterminatedXml", Some(7), None),
("unclosedBracket", Some(3), Some("(")),
]
);
assert_eq!(problems(&report("/* open")), [("unterminatedComment", Some(1), None)]);
assert_eq!(problems(&report("var t = `open")), [("unterminatedTemplate", Some(1), None), ("newerSyntax", Some(1), Some("`"))]);
}
#[test]
fn a_regular_expression_may_start_a_statement_after_a_condition() {
for src in [
"if (ok) /[)]/.test(s);\n",
"while (more(s)) /[(]/.exec(s);\n",
"for (var i = 0; i < n; i++) /[)]/.test(list[i]);\n",
"for each (var v in list) /[)]/.test(v);\n",
"with (o) /a[)]b/.test(s);\n",
] {
assert_eq!(problems(&report(src)), [], "{src}");
}
assert_eq!(tokens("if (f(a)) /[)]/.test(s)").get(7), Some(&(T::RegExp, "/[)]/")));
// After the `)` of a call or a grouping, `/` divides.
for src in ["f(a) / 2 / b", "(a) / 2 / b", "if (f(a) / 2 / b) x();", "if (a) (b) / 2 / c;"] {
assert!(tokens(src).iter().all(|(kind, _)| *kind != T::RegExp), "{src}: {:?}", tokens(src));
}
}
#[test]
fn a_slash_divides_after_an_object_literal_and_starts_a_regular_expression_after_a_block() {
for src in [
"var n = {} / 2; alert(n);\n",
"var o = {a: {b: 1}} / 2; alert(o);\n",
"f({} / 2); alert(1);\n",
"var x = <x>{ {a: 1} / 2 }</x>; alert(x);\n",
"var n = function () {} / 2; alert(n);\n",
"x = [function () { return {}; } / 2]; alert(x);\n",
"var n = ok ? 1 : function () {} / 2; alert(n);\n",
"var m = ok ? {a: 1} : function () {} / 2; alert(m);\n",
] {
let r = report(src);
assert_eq!(problems(&r), [], "{src}");
assert!(feature(&r, "notify").is_some(), "{src}");
}
for src in [
"if (a) { b(); } /[)]/.test(s);\n",
"function f() {} /[)]/.test(s);\n",
"{ function g() {} /[)]/.test(s); }\n",
"{ } /[)]/.test(s);\n",
"switch (a) { case 1: {} /[)]/.test(s); }\n",
"switch (a) { case b ? 1 : 2: {} /[)]/.test(s); }\n",
"function f() { return\n{}\n/[)]/.test(s); }\n",
"while (a) { break\n{}\n/[)]/.test(s); }\n",
] {
assert_eq!(problems(&report(src)), [], "{src}");
}
}
#[test]
fn a_keyword_after_a_dot_is_a_property_name() {
for src in [
"var n = obj.return / 2; alert(n);\n",
"var n = obj.typeof / 2; alert(n);\n",
"var n = obj.if (a) / 2; alert(n);\n",
"var n = obj.else / 2; alert(n);\n",
"var n = obj.throw\n/ 2; alert(n);\n",
"var n = obj.function / 2; {} /[)]/.test(s); alert(n);\n",
] {
let r = report(src);
assert_eq!(problems(&r), [], "{src}");
assert!(feature(&r, "notify").is_some(), "{src}");
}
}
#[test]
fn braces_in_the_strings_comments_and_regular_expressions_of_xml_code_are_text() {
for src in [
"var x = <x>{\"{\"}</x>;\n",
"var x = <x a={'}'}>{f(/[{]/)}</x>;\n",
"var x = <x>{a /* { */ + b}</x>;\n",
"var x = <x>{a // {\n}</x>;\n",
"var x = <x>{n / 2 + m}</x>;\n",
"var x = <x>{<y>{'}'}</y>}</x>;\n",
] {
let r = report(&format!("{src}app.executeMenuCommand('acmecopyall');\n"));
assert_eq!(problems(&r), [], "{src}");
let strings: Vec<&str> = r.menu_strings.iter().map(|s| s.value.as_str()).collect();
assert_eq!(strings, ["acmecopyall"], "{src}");
}
}
#[test]
fn the_code_in_the_braces_of_an_xml_literal_is_checked() {
let r = report(concat!(
"var x = <x a={app.acmeMode}>{$.evalFile('other.jsx')}</x>;\n",
"var y = <list>{items.map(function (i) { return <item>{app.executeMenuCommand('acmecopyall')}</item>; })}</list>;\n",
));
assert_eq!(problems(&r), []);
// Three XML literals, one of them in another's code.
assert_eq!(feature(&r, "e4x").map(|f| (f.count, f.lines.as_slice())), Some((3, &[1, 2][..])));
assert_eq!(feature(&r, "dollar.evalFile").map(|f| f.lines.as_slice()), Some(&[1][..]));
assert_eq!(r.outside.iter().map(|o| o.id).collect::<Vec<_>>(), ["unseenCode"]);
let members: Vec<&str> = r.members.iter().map(|m| m.name.as_str()).collect();
assert_eq!(members, ["app.acmeMode"]);
let strings: Vec<&str> = r.menu_strings.iter().map(|s| s.value.as_str()).collect();
assert_eq!(strings, ["acmecopyall"]);
// An XML literal the text ends in is one problem, at the line it starts on.
assert_eq!(problems(&report("var x = <x>{\n a + b;\n")), [("unterminatedXml", Some(1), None)]);
}
#[test]
fn later_syntax_is_reported_once_per_construct_and_line() {
let r = report(concat!(
"var f = (a) => a;\n",
"var g = (a) => (b) => b;\n",
"class Shape {}\n",
"var all = [...list];\n",
"var v = a?.b ?? c;\n",
"var p = 2 ** 3;\n",
"var n = [0b101, 0o17, 1_000, 10n];\n",
"var q = this.#hidden;\n",
"async function go() {}\n",
"var ok = cond ?.5 : 1;\n",
"let count = 1;\n",
"for (let i = 0; i < 2; i++) {}\n",
"var let = 1; let = 2; let.x = 3; if (let in o) {}\n",
));
let found: Vec<(usize, &str)> = r.problems.iter().filter_map(|p| Some((p.line?, p.detail.as_deref()?))).collect();
assert_eq!(
found,
[
(1, "=>"),
(2, "=>"),
(3, "class"),
(4, "..."),
(5, "?."),
(5, "??"),
(6, "**"),
(7, "0b101"),
(7, "0o17"),
(7, "1_000"),
(7, "10n"),
(8, "#hidden"),
(9, "async"),
(11, "let"),
(12, "let"),
]
);
}
#[test]
fn deep_nesting_is_a_problem_not_a_stack_overflow() {
let inputs: Vec<String> = vec![
"(".repeat(100_000),
"[".repeat(100_000),
"{".repeat(100_000),
"x = ".repeat(100_000),
"if (x) ".repeat(100_000) + ";",
"!".repeat(100_000) + "x",
"a ? ".repeat(100_000),
"var x = ".to_string() + &"<a>".repeat(100_000),
"app.x(".repeat(50_000),
"app[".repeat(50_000),
"x = {a: ".repeat(50_000),
"var x = ".to_string() + &"<a>{".repeat(50_000),
"x = (".repeat(50_000),
"x = new File(".repeat(50_000),
"function f() { var alert; ".repeat(50_000),
"x += a ? ".repeat(50_000),
];
// A small stack: anything that recursed once per level would overflow it and end the process.
let checked = std::thread::Builder::new()
.stack_size(256 << 10)
.spawn(move || {
inputs.iter().map(|s| report(s)).map(|r| (r.problems.iter().any(|p| p.code == "tooDeep"), r.problems.len())).collect::<Vec<_>>()
})
.unwrap()
.join()
.unwrap();
let deep: Vec<bool> = checked.iter().map(|c| c.0).collect();
assert_eq!(deep, [true, true, true, false, false, false, false, false, true, true, true, true, true, true, true, false]);
assert!(checked.iter().all(|c| c.1 <= Limits::default().entries));
// Deeper than the cap: one problem, and the check goes on.
let small = Limits { nesting: 8, ..Limits::default() };
let r = check("t.jsx", format!("{}alert(1);{}", "(".repeat(20), ")".repeat(20)).as_bytes(), None, &small);
assert_eq!(problems(&r), [("tooDeep", Some(1), None)]);
assert!(feature(&r, "notify").is_some());
// Past the cap, a value in grouping parentheses is not followed, nor is one without them.
let grouped = b"var a = (b = (c = (new File(p))));\nc.remove();\n";
assert_eq!(check("t.jsx", grouped, None, &Limits::default()).outside.len(), 1);
assert!(check("t.jsx", grouped, None, &Limits { nesting: 2, ..Limits::default() }).outside.is_empty());
let bare = b"var a = new File(b = new File(c = new File(p)));\nc.remove();\n";
assert_eq!(check("t.jsx", bare, None, &Limits::default()).outside.len(), 1);
assert!(check("t.jsx", bare, None, &Limits { nesting: 2, ..Limits::default() }).outside.is_empty());
// Past the cap, a call on a File in grouping parentheses is not followed.
let receiver = b"var f = new File(p);\n(((f))).remove();\n";
assert_eq!(check("t.jsx", receiver, None, &Limits::default()).outside.len(), 1);
assert!(check("t.jsx", receiver, None, &Limits { nesting: 2, ..Limits::default() }).outside.is_empty());
// An XML literal in the code of an XML literal takes two levels: its markup and the code. The
// literal ends where a level past the cap would start.
let nested = b"var x = <a>{<b>{<c>{<d>{1}</d>}</c>}</b>}</a>;\nalert(1);\n";
assert_eq!(problems(&check("t.jsx", nested, None, &small)), []);
let r = check("t.jsx", nested, None, &Limits { nesting: 6, ..Limits::default() });
assert_eq!(problems(&r).first(), Some(&("tooDeep", Some(1), None)));
assert_eq!(r.problems.iter().filter(|p| p.code == "tooDeep").count(), 1, "{:?}", r.problems);
assert!(feature(&r, "notify").is_some());
}
/// The inputs that take the most steps for their size: brackets never closed, parameters and the
/// names a function declares, and grouped values, each after a name written with an escape. The
/// text is read three times (for the escapes, the names declared and the analysis), and the work
/// stays within the bound the properties below check.
#[test]
fn the_costliest_inputs_stay_within_five_steps_a_byte() {
let names: Vec<String> = (0..600).map(|i| format!("n{i}")).collect();
for src in [
format!("\\u0061;{}", "{".repeat(2_000)),
format!("\\u0061;function f({}) {{ var {}; }}", names.join(","), names.join(",")),
format!("\\u0061;function f({}) {{}}", vec!["a"; 900].join(",")),
format!("\\u0061;{}", "x=(".repeat(600)),
format!("\\u0061;o[k];{}", "{".repeat(2_000)),
] {
let r = report(&src);
assert!(r.steps <= 5 * src.len() + 16, "{} steps for {} bytes", r.steps, src.len());
}
}
/// The most steps a check of `bytes` without a loader may take. The text is read once for the
/// escapes and once for the names declared, and a scan takes at most three steps a byte. A script
/// whose variables hold nothing the check follows is scanned once: five steps a byte. Any other
/// script is scanned up to five times (the first scan, three more while what its variables hold
/// grows, and the report's): seventeen steps a byte.
fn step_bound(bytes: &[u8]) -> usize {
let limits = Limits::default();
let mut acc = report::Acc::new(&limits);
let (text, _) = decode(bytes);
let (text, braced) = analyze::unescaped(text, &mut acc);
let declared = analyze::declarations(&text, &mut acc);
let known = analyze::scan(&text, &declared, &braced, None, std::rc::Rc::from([]), &analyze::Known::default(), &mut acc).known;
let scans = if known.no_names() { 1 } else { 5 };
(2 + 3 * scans) * bytes.len() + 16
}
/// A name in brackets that is not one literal (`0[ \| … ]`) gives no variable a value, so the
/// script is scanned once. The bytes are the ones a run of the property below found.
#[test]
fn a_script_that_binds_no_name_is_scanned_once() {
let bytes: &[u8] = b"\x01\x01 \x010 !0!\x01!\x01#\x01#{#{ 0( #0\x01( \x01( {#{ # \x01#0:((0[\t \\| \x1b!!0:|!| 0) ) )] ! \x1b \x1b \x1b \\u\t\x01";
assert_eq!(bytes.len(), 104);
let r = check("a.jsx", bytes, None, &Limits::default());
assert_eq!(r.kind, Kind::Script);
assert_eq!(step_bound(bytes), 5 * bytes.len() + 16);
assert!(r.steps <= 5 * bytes.len() + 16, "{} steps for {} bytes", r.steps, bytes.len());
}
#[test]
fn lists_stop_at_their_limits_and_echoes_are_cut() {
let mut s = String::new();
for i in 0..20_000 {
s += &format!("app.executeMenuCommand('acme{i}');\nfoo = app.m{i};\n");
}
let r = report(&s);
let limits = Limits::default();
assert_eq!((r.menu_strings.len(), r.members.len()), (limits.entries, limits.entries));
assert!(r.truncated);
assert_eq!(feature(&r, "menuCommand").map(|f| (f.count, f.lines.len())), Some((20_000, limits.lines)));
let long = "x".repeat(500);
let r = report(&format!("app.executeMenuCommand('{long}');\n#target {long}\napp.{long}.y;\nnew {long}();\n"));
let echoed = r
.menu_strings
.iter()
.map(|s| &s.value)
.chain(r.members.iter().map(|m| &m.name))
.chain(r.directives.iter().map(|d| &d.value))
.chain([&r.targets[0].app]);
for e in echoed {
assert_eq!(e.chars().count(), limits.echo, "{e}");
assert!(e.ends_with('…'));
}
assert!(!r.truncated);
}
#[test]
fn variables_past_the_limit_mark_the_report_truncated() {
let limits = Limits { entries: 3, ..Limits::default() };
for (src, truncated) in [
("var f1 = new File(p), f2 = new File(p), f3 = new File(p);\nf3.remove();\n", false),
("var f1 = new File(p), f2 = new File(p), f3 = new File(p), f4 = new File(p);\nf4.remove();\n", true),
("var f1 = new File(p), f2 = new File(p), f3 = new File(p);\nvar f4 = f1;\n", true),
("var f1 = new File(p), f2 = new File(p), f3 = new File(p);\nvar f4 = f1", true),
("var f1 = (new File(p)), f2 = (new File(p)), f3 = (new File(p)), f4 = (new File(p));\nf4.remove();\n", true),
("var d1 = app.activeDocument, d2 = app.activeDocument, d3 = app.activeDocument;\n", false),
("var d1 = app.activeDocument, d2 = app.activeDocument, d3 = app.activeDocument, d4 = app.activeDocument;\n", true),
("function g(a, b, c) {}\n", false),
("function g(a, b, c, d) {}\n", true),
("function g() { var a = 1, b = 2, c = 3; }\n", false),
("function g() { var a = 1, b = 2, c = 3, e = 4; }\n", true),
] {
let r = check("test.jsx", src.as_bytes(), None, &limits);
assert_eq!(r.truncated, truncated, "{src}");
if src.contains("remove") {
assert_eq!(r.outside.is_empty(), truncated, "{src}");
}
}
// The script and the file it includes hold four File variables between them.
let mut m = mem(&[("lib.jsxinc", "var f3 = new File(p), f4 = new File(p);\n")]);
let r = check("main.jsx", b"#include 'lib.jsxinc'\nvar f1 = new File(p), f2 = new File(p);\n", Some(&mut m), &limits);
assert!(r.truncated);
}
/// Files in memory, with every path asked for and the bytes handed out.
struct Mem {
files: Vec<(String, Vec<u8>)>,
refused: Vec<&'static str>,
asked: Vec<String>,
served: usize,
}
fn mem(files: &[(&str, &str)]) -> Mem {
Mem { files: files.iter().map(|(p, s)| (p.to_string(), s.as_bytes().to_vec())).collect(), refused: vec![], asked: vec![], served: 0 }
}
impl Loader for Mem {
fn load(&mut self, path: &str, max: usize) -> Loaded {
self.asked.push(path.to_string());
if self.refused.contains(&path) {
return Loaded::Refused;
}
match self.files.iter().find(|(p, _)| p == path) {
Some((_, b)) => {
let head = b[..b.len().min(max)].to_vec();
self.served += head.len();
Loaded::Bytes(head)
}
None => Loaded::Missing,
}
}
}
/// An include as `(value, line, file, status, path)`.
type IncludeRow<'r> = (&'r str, usize, Option<&'r str>, IncludeStatus, Option<&'r str>);
fn includes(r: &Report) -> Vec<IncludeRow<'_>> {
r.includes.iter().map(|i| (i.value.as_str(), i.line, i.file.as_deref(), i.status, i.path.as_deref())).collect()
}
#[test]
fn includes_are_followed_inside_the_scripts_folder() {
let mut m = mem(&[
("lib/a.jsxinc", "#include 'b.jsxinc'\n$.writeln(1);\nvar s = 'open;\n"),
("lib/b.jsxinc", "alert('b');\n#include '../main.jsx'\n#include 'b.jsxinc'\n"),
("shared/c.jsxinc", "var u = new UnitValue(1, 'in');\n"),
("bin.jsxinc", "@JSXBIN@[email protected]@AcmeBody"),
("secret.jsxinc", "alert(1);"),
]);
m.refused = vec!["secret.jsxinc"];
let main = concat!(
"#include 'lib/a.jsxinc'\n",
"#include 'lib/a.jsxinc'\n",
"#include 'nope.jsxinc'\n",
"#include '/etc/x.jsx'\n",
"#include '../up.jsx'\n",
"#includepath 'shared'\n",
"#include 'c.jsxinc'\n",
"#include 'bin.jsxinc'\n",
"#include 'secret.jsxinc'\n",
);
let r = check("folder/main.jsx", main.as_bytes(), Some(&mut m), &Limits::default());
use IncludeStatus::*;
// main.jsx includes lib/a, lib/a includes lib/b and lib/b includes main.jsx: each of those lines
// is part of a cycle, as is lib/b's line that includes lib/b.
assert_eq!(
includes(&r),
[
("lib/a.jsxinc", 1, None, Cycle, Some("lib/a.jsxinc")),
("lib/a.jsxinc", 2, None, Cycle, Some("lib/a.jsxinc")),
("nope.jsxinc", 3, None, Missing, None),
("/etc/x.jsx", 4, None, NotFollowed, None),
("../up.jsx", 5, None, NotFollowed, None),
("c.jsxinc", 7, None, Checked, Some("shared/c.jsxinc")),
("bin.jsxinc", 8, None, Unreadable, Some("bin.jsxinc")),
("secret.jsxinc", 9, None, NotFollowed, None),
("b.jsxinc", 1, Some("lib/a.jsxinc"), Cycle, Some("lib/b.jsxinc")),
("../main.jsx", 2, Some("lib/b.jsxinc"), Cycle, Some("main.jsx")),
("b.jsxinc", 3, Some("lib/b.jsxinc"), Cycle, Some("lib/b.jsxinc")),
]
);
// A refused file is looked for in the include path's folder next.
assert_eq!(
m.asked,
["lib/a.jsxinc", "nope.jsxinc", "c.jsxinc", "shared/c.jsxinc", "bin.jsxinc", "secret.jsxinc", "shared/secret.jsxinc", "lib/b.jsxinc"],
"each file is read once"
);
// What the included files use counts, without their lines.
for id in ["dollar", "notify", "unitValue"] {
assert_eq!(feature(&r, id).map(|f| (f.count, f.lines.len())), Some((1, 0)), "{id}");
}
let p = &r.problems[0];
assert_eq!((p.code, p.line, p.file.as_deref()), ("unterminatedString", Some(3), Some("lib/a.jsxinc")));
}
#[test]
fn files_that_include_each_other_are_cycles_wherever_they_were_found() {
// The script includes a and b, and a and b include each other.
let mut m = mem(&[("a.jsxinc", "#include 'b.jsxinc'\n"), ("b.jsxinc", "#include 'a.jsxinc'\n")]);
let r = check("main.jsx", b"#include 'a.jsxinc'\n#include 'b.jsxinc'\n", Some(&mut m), &Limits::default());
use IncludeStatus::*;
assert_eq!(
includes(&r),
[
("a.jsxinc", 1, None, Checked, Some("a.jsxinc")),
("b.jsxinc", 2, None, Checked, Some("b.jsxinc")),
("b.jsxinc", 1, Some("a.jsxinc"), Cycle, Some("b.jsxinc")),
("a.jsxinc", 1, Some("b.jsxinc"), Cycle, Some("a.jsxinc")),
]
);
assert_eq!(m.asked, ["a.jsxinc", "b.jsxinc"], "each file is read once");
}
#[test]
fn include_lines_share_the_folders_of_the_include_path_before_them() {
let folders: Vec<String> = (0..64).map(|i| format!("{}{i}", "d".repeat(4000))).collect();
let mut src = format!("#includepath '{}'\n", folders.join(";"));
for i in 0..1000 {
src += &format!("#include 'f{i}.jsxinc'\n");
}
let limits = Limits::default();
let mut acc = report::Acc::new(&limits);
let declared = analyze::declarations(&src, &mut acc);
let scan = analyze::scan(&src, &declared, &[], None, std::rc::Rc::from([]), &analyze::Known::default(), &mut acc);
let (first, rest) = scan.includes.split_first().unwrap();
assert_eq!((first.paths.len(), rest.len()), (64, 999));
assert!(rest.iter().all(|i| std::rc::Rc::ptr_eq(&i.paths, &first.paths)), "one list of folders, not a copy for each line");
}
#[test]
fn names_and_folders_longer_than_a_path_are_not_followed() {
let long = "d".repeat(4097);
let mut m = mem(&[]);
let src = format!("#includepath '{long};lib'\n#include 'a.jsxinc'\n#include '{long}'\n");
let r = check("main.jsx", src.as_bytes(), Some(&mut m), &Limits::default());
assert_eq!(m.asked, ["a.jsxinc", "lib/a.jsxinc"]);
let statuses: Vec<IncludeStatus> = r.includes.iter().map(|i| i.status).collect();
assert_eq!(statuses, [IncludeStatus::Missing, IncludeStatus::NotFollowed]);
}
#[test]
fn what_a_variable_holds_is_known_in_every_file_of_the_check() {
let mut m =
mem(&[("write.jsxinc", "f.open('w');\nf.write('data');\n"), ("lib.jsxinc", "var doc = app.activeDocument;\nvar out = new File(p);\n")]);
let main = "var f = new File('out.txt');\n#include 'write.jsxinc'\n#include 'lib.jsxinc'\ndoc.acmeLayers;\nout.remove();\n";
let r = check("main.jsx", main.as_bytes(), Some(&mut m), &Limits::default());
// The script's `f` in an included file, and an included file's `doc` and `out` in the script.
let outside: Vec<(&str, usize, &[usize], &[&str])> = r.outside.iter().map(|o| (o.id, o.count, o.lines.as_slice(), o.modes.as_slice())).collect();
assert_eq!(outside, [("filesWritten", 3, &[5][..], &["w"][..])]);
let members: Vec<&str> = r.members.iter().map(|m| m.name.as_str()).collect();
assert_eq!(members, ["app.activeDocument", "app.activeDocument.acmeLayers"]);
assert_eq!(m.asked, ["write.jsxinc", "lib.jsxinc"], "each file is read once");
}
#[test]
fn what_a_variable_holds_is_known_also_when_another_files_variable_gives_it() {
for order in [["a.jsxinc", "b.jsxinc"], ["b.jsxinc", "a.jsxinc"]] {
let mut m = mem(&[("a.jsxinc", "var doc = app.activeDocument;\n"), ("b.jsxinc", "var layer = doc.acmeLayer;\n")]);
let main = format!("#include '{}'\n#include '{}'\nlayer.remove();\n", order[0], order[1]);
let r = check("main.jsx", main.as_bytes(), Some(&mut m), &Limits::default());
let members: Vec<&str> = r.members.iter().map(|m| m.name.as_str()).collect();
assert_eq!(members, ["app.activeDocument", "app.activeDocument.acmeLayer", "app.activeDocument.acmeLayer.remove()"], "{order:?}");
}
}
#[test]
fn a_file_two_branches_include_is_followed_from_the_shallower_one() {
let mut m = mem(&[
("a.jsxinc", "#include 'shared.jsxinc'\n"),
("b.jsxinc", "#include 'c.jsxinc'\n"),
("c.jsxinc", "#include 'shared.jsxinc'\n"),
("shared.jsxinc", "#include 'dep.jsxinc'\n"),
("dep.jsxinc", "var s = new Socket();\n"),
]);
let limits = Limits { include_depth: 3, ..Limits::default() };
let r = check("main.jsx", b"#include 'a.jsxinc'\n#include 'b.jsxinc'\n", Some(&mut m), &limits);
// One row per #include line: main's two, a's, b's, c's and shared's.
assert_eq!(r.includes.len(), 6, "{:?}", includes(&r));
assert!(r.includes.iter().all(|i| i.status == IncludeStatus::Checked), "{:?}", includes(&r));
assert_eq!(r.outside.iter().map(|o| o.id).collect::<Vec<_>>(), ["network"]);
}
#[test]
fn include_limits_stop_the_following() {
// A chain of files, each including the next.
let files: Vec<(String, String)> = (0..20).map(|i| (format!("f{i}.jsxinc"), format!("#include 'f{}.jsxinc'\n", i + 1))).collect();
let pairs: Vec<(&str, &str)> = files.iter().map(|(p, s)| (p.as_str(), s.as_str())).collect();
// The script is 20 bytes, and each file 21.
let statuses = |limits: &Limits| {
let mut m = mem(&pairs);
let r = check("main.jsx", b"#include 'f0.jsxinc'", Some(&mut m), limits);
r.includes.iter().map(|i| i.status).collect::<Vec<_>>()
};
let mut want = vec![IncludeStatus::Checked; 16];
want.push(IncludeStatus::Limit);
assert_eq!(statuses(&Limits::default()), want, "followed 16 deep");
let three = Limits { includes: 3, ..Limits::default() };
assert_eq!(statuses(&three), [IncludeStatus::Checked, IncludeStatus::Checked, IncludeStatus::Checked, IncludeStatus::Limit]);
// The second file would take the total past 50 bytes.
let total = Limits { total_bytes: 50, ..Limits::default() };
assert_eq!(statuses(&total), [IncludeStatus::Checked, IncludeStatus::Limit]);
let one_file = Limits { file_bytes: 20, ..Limits::default() };
assert_eq!(statuses(&one_file), [IncludeStatus::Limit]);
// Each file is found at the first path looked up.
let two_lookups = Limits { lookups: 2, ..Limits::default() };
assert_eq!(statuses(&two_lookups), [IncludeStatus::Checked, IncludeStatus::Checked, IncludeStatus::Limit]);
}
#[test]
fn included_files_not_checked_count_toward_the_limits() {
use IncludeStatus::*;
let big = "a".repeat(2000);
let mut pairs: Vec<(String, String)> = (0..20).map(|i| (format!("big{i}.jsxinc"), big.clone())).collect();
pairs.extend((0..10).map(|i| (format!("bin{i}.jsxinc"), "@JSXBIN@[email protected]@AcmeBody".to_string())));
let files: Vec<(&str, &str)> = pairs.iter().map(|(p, s)| (p.as_str(), s.as_str())).collect();
// Files too large to check (over 1,000 bytes): the reads stop at the total, 1,500 bytes past the
// script's own, with one byte more that shows a file is larger.
let script: String = (0..20).map(|i| format!("#include 'big{i}.jsxinc'\n")).collect();
let mut m = mem(&files);
let r = check("main.jsx", script.as_bytes(), Some(&mut m), &Limits { file_bytes: 1000, total_bytes: script.len() + 1500, ..Limits::default() });
assert_eq!(m.served, 1501);
assert!(r.includes.iter().all(|i| i.status == Limit), "{:?}", includes(&r));
// Compiled files count toward the files read.
let script: String = (0..10).map(|i| format!("#include 'bin{i}.jsxinc'\n")).collect();
let mut m = mem(&files);
let r = check("main.jsx", script.as_bytes(), Some(&mut m), &Limits { includes: 3, ..Limits::default() });
let statuses: Vec<IncludeStatus> = r.includes.iter().map(|i| i.status).collect();
assert_eq!(statuses[..4], [Unreadable, Unreadable, Unreadable, Limit]);
assert_eq!(m.asked.len(), 3);
}
#[test]
fn missing_includes_stop_at_the_lookup_limit() {
// Each line names a missing file, looked up as given and in each of 64 include path folders:
// 65 paths a line, so the first 15 lines take 975 of the 1,024 lookups.
let folders: Vec<String> = (0..64).map(|i| format!("d{i}")).collect();
let mut script = format!("#includepath '{}'\n", folders.join(";"));
for i in 0..1000 {
script += &format!("#include 'f{i}.jsxinc'\n");
}
let mut m = mem(&[]);
let r = check("main.jsx", script.as_bytes(), Some(&mut m), &Limits::default());
assert_eq!(m.asked.len(), Limits::default().lookups);
let statuses: Vec<IncludeStatus> = r.includes.iter().map(|i| i.status).collect();
assert_eq!(statuses.len(), 1000);
assert!(statuses.iter().take(15).all(|s| *s == IncludeStatus::Missing), "{statuses:?}");
assert!(statuses.iter().skip(15).all(|s| *s == IncludeStatus::Limit), "{statuses:?}");
}
#[test]
fn without_a_loader_includes_are_listed_unread() {
let r = report("//@include 'a.jsxinc'\n");
assert_eq!(includes(&r), [("a.jsxinc", 1, None, IncludeStatus::NotRead, None)]);
}
/// Text built from pieces of scripts: directives, strings, regular expressions, XML, later syntax,
/// every line break, a byte order mark and replaced bytes.
fn script_text() -> impl Strategy<Value = String> {
let pieces = prop::sample::select(vec![
"#target acme",
"//@include \"a.jsxinc\"",
"#includepath 'lib;..'",
"#pragma",
"#",
"var",
" x",
"=",
"app",
"\\u0061pp",
".activeDocument",
".",
"(",
")",
"[",
"]",
"{",
"}",
";",
",",
"'str'",
"\"str",
"'",
"\\",
"/re[/]x/g",
"/",
"/=",
"<a b=\"1\">",
"</a>",
"<>",
"</>",
"<!--",
"-->",
"<![CDATA[",
"{v}",
".@",
"..",
"::",
"`tpl",
"`",
"=>",
"?.",
"??",
"\n",
"\r\n",
"\r",
"\u{2028}",
"\u{feff}",
"\u{fffd}",
"é",
"0x1F",
"1e5",
"1_000",
"10n",
"#priv",
"/*",
"*/",
"//",
"@JSXBIN@",
"executeMenuCommand",
"File",
"Window",
"'dialog'",
"new",
"function",
"import",
" ",
]);
prop::collection::vec(pieces, 0..80).prop_map(|v| v.concat())
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(512))]
/// The tokens cover the text exactly once, in order, each at least one character long.
#[test]
fn tokens_cover_the_text_exactly_once(text in script_text()) {
let mut joined = String::new();
for t in Lexer::new(&text, Limits::default().nesting) {
prop_assert!(!t.text.is_empty());
joined.push_str(t.text);
}
prop_assert_eq!(joined, text);
}
/// Any bytes are checked without a panic, every list within its limit, with work linear in the
/// size.
#[test]
fn any_bytes_are_checked_in_linear_work(bytes in prop::collection::vec(any::<u8>(), 0..2_000), jsxbin in any::<bool>()) {
let r = check(if jsxbin { "a.jsxbin" } else { "a.jsx" }, &bytes, None, &Limits::default());
prop_assert!(r.steps <= step_bound(&bytes), "{} steps for {} bytes", r.steps, bytes.len());
}
#[test]
fn script_like_text_is_checked_in_linear_work(text in script_text()) {
let r = report(&text);
prop_assert!(r.steps <= step_bound(text.as_bytes()), "{} steps for {} bytes", r.steps, text.len());
let limits = Limits::default();
for list in [r.targets.len(), r.directives.len(), r.includes.len(), r.members.len(), r.types.len(), r.menu_strings.len(), r.problems.len()] {
prop_assert!(list <= limits.entries);
}
}
/// Compiled input is never read, whatever follows its prefix.
#[test]
fn compiled_scripts_are_never_lexed(rest in prop::collection::vec(any::<u8>(), 0..500), blanks in "[ \t\r\n]{0,4}", bom in any::<bool>()) {
let mut b = if bom { b"\xEF\xBB\xBF".to_vec() } else { vec![] };
b.extend(blanks.as_bytes());
b.extend(b"@JSXBIN@");
b.extend(rest);
let r = check("x.jsx", &b, None, &Limits::default());
prop_assert_eq!(r.kind, Kind::Compiled);
prop_assert_eq!(r.steps, 0);
}
/// Include graphs over files in memory, cycles and missing files among them: each file is read
/// at most once, and no more files are read or paths looked up than the limits allow.
#[test]
fn include_graphs_respect_every_cap(
edges in prop::collection::vec((0usize..10, 0usize..12), 0..40),
most in 1usize..6,
depth in 1usize..5,
lookups in 1usize..12,
) {
// Files f0 to f9; f10 and f11 are missing.
let files: Vec<(String, String)> = (0..10)
.map(|i| (format!("f{i}.jsxinc"), edges.iter().filter(|e| e.0 == i).map(|e| format!("#include 'f{}.jsxinc'\n", e.1)).collect()))
.collect();
let pairs: Vec<(&str, &str)> = files.iter().map(|(p, s)| (p.as_str(), s.as_str())).collect();
let mut m = mem(&pairs);
let limits = Limits { includes: most, include_depth: depth, lookups, ..Limits::default() };
let r = check("main.jsx", b"#include 'f0.jsxinc'\n#include 'f1.jsxinc'\n", Some(&mut m), &limits);
let read: Vec<&String> = m.asked.iter().filter(|p| files.iter().any(|(f, _)| f == *p)).collect();
let unique: std::collections::BTreeSet<&&String> = read.iter().collect();
prop_assert_eq!(unique.len(), read.len(), "read twice: {:?}", m.asked);
prop_assert!(read.len() <= most);
prop_assert!(m.asked.len() <= lookups, "{} paths looked up: {:?}", m.asked.len(), m.asked);
prop_assert!(r.includes.iter().all(|i| i.status != IncludeStatus::NotRead));
}
}
/// A function that writes to a variable the global code assigns a File object after it: the
/// variable declaration is hoisted, so the function writes a file, with a loader and without one.
#[test]
fn a_variable_assigned_after_the_function_using_it_holds_its_value() {
let src = "function g(){ f.write('x'); } var f = new File('a');";
let without = report(src);
let with = check("test.jsx", src.as_bytes(), Some(&mut mem(&[])), &Limits::default());
assert!(feature(&without, "file.write").is_some(), "{:?}", without.features);
assert!(feature(&with, "file.write").is_some(), "{:?}", with.features);
}
/// Script-like text with functions that use variables the code after them assigns, a chain of them
/// taking more than one scan to settle.
fn hoisting_text() -> impl Strategy<Value = String> {
let pieces = prop::sample::select(vec![
"function g(){ ",
"function h(){ ",
"}",
"f.write('x'); ",
"b.remove(); ",
"e.acmeItems.add(); ",
"b = a; ",
"var a = f; ",
"var f = new File('a'); ",
"var d = app.activeDocument; ",
"var e = d; ",
"e = d.acmeLayers; ",
"f = d; ",
]);
(prop::collection::vec(pieces, 0..30), script_text()).prop_map(|(v, tail)| v.concat() + &tail)
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(512))]
/// A script that includes nothing gets the same report with a loader and without one.
#[test]
fn a_script_without_includes_reports_the_same_with_and_without_a_loader(text in hoisting_text()) {
let mut without = report(&text);
prop_assume!(without.includes.is_empty());
let mut with = check("test.jsx", text.as_bytes(), Some(&mut mem(&[])), &Limits::default());
without.steps = 0;
with.steps = 0;
prop_assert_eq!(without, with);
}
}
/// Chains of variables a function assigns before the code after it assigns their source take a
/// scan each to settle: the script is read at most seven times.
#[test]
fn variables_that_settle_over_several_scans_stay_linear() {
let mut src = String::new();
for i in 0..150 {
src += &format!("function a{i}(){{ e{i} = d{i}; }} function b{i}(){{ d{i} = c{i}; }} function c{i}(){{ c{i} = b{i}; }} ");
src += &format!("function d{i}(){{ b{i} = a{i}; }} var a{i} = new File('x');\n");
}
let r = report(&src);
assert!(r.steps <= 9 * src.len() + 16, "{} steps for {} bytes", r.steps, src.len());
}
/// A script whose declarations give names values the analysis doesn't read, and whose variables
/// hold nothing the check knows, is scanned once: a second scan would find the same. The bytes are
/// the input `any_bytes_are_checked_in_linear_work` shrank to when it found the second scan.
#[test]
fn a_script_that_is_only_unsure_is_scanned_once() {
let bytes: &[u8] = &[
1, 1, 32, 1, 48, 32, 33, 48, 33, 1, 33, 1, 35, 1, 35, 123, 35, 123, 32, 48, 40, 32, 35, 48, 1, 40, 32, 1, 40, 32, 32, 123, 35, 123, 32, 35,
32, 1, 35, 48, 58, 40, 40, 48, 91, 9, 32, 92, 124, 32, 27, 33, 33, 48, 58, 124, 33, 124, 32, 48, 41, 32, 32, 41, 32, 41, 93, 32, 32, 32, 32,
33, 32, 32, 32, 32, 27, 32, 32, 32, 32, 27, 32, 32, 32, 27, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 92, 117, 9, 1,
];
let limits = Limits::default();
let mut acc = crate::report::Acc::new(&limits);
let (text, _) = decode(bytes);
let (text, braced) = analyze::unescaped(text, &mut acc);
let declared = analyze::declarations(&text, &mut acc);
let scan = analyze::scan(&text, &declared, &braced, None, std::rc::Rc::from([]), &analyze::Known::default(), &mut acc);
assert!(scan.known.unsure && scan.known.bound.is_empty() && scan.known.files.is_empty());
let r = check("a.jsx", bytes, None, &limits);
assert_eq!(r.steps, acc.steps);
assert!(r.steps <= 5 * bytes.len() + 16, "{} steps for {} bytes", r.steps, bytes.len());
}