mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-29 01:34:29 +01:00
Move var declaration names between parser scopes
This commit is contained in:
5 files changed
+43
-7
No files matched your search
Generated
+1
-1
@@ -8480,7 +8480,7 @@ dependencies = [
|
||||
"proptest",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sha2",
|
||||
"sha2 0.10.9",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
|
||||
@@ -21,6 +21,25 @@ fn truth(src: &str) {
|
||||
assert_eq!(run(src), JsValue::Bool(true), "{src}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nested_blocks_retain_one_var_name_entry_per_declaration() {
|
||||
let declarations = (0..1_000).map(|i| format!("v{i}={i}")).collect::<Vec<_>>().join(",");
|
||||
let source = format!("{}var {declarations};{}v999", "{".repeat(64), "}".repeat(64));
|
||||
crate::parser::SCOPE_VAR_PEAK.with(|peak| peak.set(0));
|
||||
assert_eq!(run(&source), JsValue::Number(999.0));
|
||||
let peak = crate::parser::SCOPE_VAR_PEAK.with(std::cell::Cell::get);
|
||||
assert_eq!(peak, 1_000, "retained entries for 1,000 names");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nested_var_names_keep_lexical_conflicts_and_function_boundaries() {
|
||||
for source in ["{ { var x; } let x; }", "{ let x; { var x; } }", "function f() { { var x; } let x; }"] {
|
||||
syntax(source);
|
||||
}
|
||||
truth("{ function f() { var x; } let x = 3; if (x !== 3) throw 1; } true");
|
||||
truth("{ { var x; } { let x = 3; if (x !== 3) throw 1; } } true");
|
||||
}
|
||||
|
||||
fn string(src: &str) -> String {
|
||||
match run(src) {
|
||||
JsValue::String(s) => s,
|
||||
|
||||
+19
-6
@@ -23,6 +23,11 @@ use crate::token::{Directive, Keyword, Punct, Span, Token, TokenKind, strict_res
|
||||
|
||||
type Result<T> = std::result::Result<T, SyntaxError>;
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
pub(crate) static SCOPE_VAR_PEAK: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
|
||||
}
|
||||
|
||||
/// What the code being parsed allows, saved and replaced at each function body.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
struct Context {
|
||||
@@ -312,8 +317,12 @@ impl<'s> Parser<'s> {
|
||||
}
|
||||
|
||||
fn pop_scope(&mut self) {
|
||||
if self.es2015 {
|
||||
self.scopes.pop();
|
||||
if self.es2015
|
||||
&& let Some(scope) = self.scopes.pop()
|
||||
&& !scope.function
|
||||
&& let Some(parent) = self.scopes.last_mut()
|
||||
{
|
||||
parent.vars.extend(scope.vars);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -339,20 +348,24 @@ impl<'s> Parser<'s> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Declares a `var` name: in each scope out to the function's, where no lexical declaration
|
||||
/// may have it.
|
||||
/// Declares a `var` name in the current scope after checking lexical declarations through
|
||||
/// the containing function's scope. Closing a block moves its names to the parent scope.
|
||||
fn declare_var(&mut self, name: &Name) -> Result<()> {
|
||||
let mut clash = false;
|
||||
for scope in self.scopes.iter_mut().rev() {
|
||||
for scope in self.scopes.iter().rev() {
|
||||
if scope.lexical.contains_key(&name.name) {
|
||||
clash = true;
|
||||
break;
|
||||
}
|
||||
scope.vars.insert(name.name.clone());
|
||||
if scope.function {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if !clash && let Some(scope) = self.scopes.last_mut() {
|
||||
scope.vars.insert(name.name.clone());
|
||||
}
|
||||
#[cfg(test)]
|
||||
SCOPE_VAR_PEAK.with(|peak| peak.set(peak.get().max(self.scopes.iter().map(|scope| scope.vars.len()).sum())));
|
||||
if clash { Err(self.redeclared(name)) } else { Ok(()) }
|
||||
}
|
||||
|
||||
|
||||
@@ -2695,6 +2695,8 @@ vectorcraft-cli run --cmd script.check --params '{"path": "Tidy Layers.jsx"}'
|
||||
```
|
||||
|
||||
`script.run {path? | source?, name?, args?, answers?, maxFuel?}` runs a JavaScript file against the open documents.
|
||||
|
||||
Block-scoped declaration checks retain one copy of each `var` declaration name through nested blocks.
|
||||
File › Scripts › Other Script… (Cmd+F12) picks a `.js` or `.jsx` file and runs it the same way; the help page
|
||||
https://helpx.adobe.com/illustrator/desktop/automate-visualize-data/automate-actions/install-and-run-scripts.html
|
||||
describes that menu item. Pass exactly one of `path` and `source`. A `path` must end in `.js` or `.jsx`, in any
|
||||
|
||||
@@ -32,6 +32,8 @@
|
||||
|
||||
## Feature submission evidence
|
||||
|
||||
Block-scoped declaration checks keep each `var` declaration name in the current scope and move it into its parent when the block closes. Names remain within their containing function, and lexical conflicts are still rejected. Regressions in `crates/js/src/es2015_tests.rs` check retained scope entries, declaration order, sibling blocks, and function boundaries.
|
||||
|
||||
`vectorcraft-js` ends a paused or waiting run before `Realm::run` or `Realm::start` compiles new source. Its globals remain available. The earlier run's remaining code and queued jobs do not run. Regressions in `crates/js/src/host_tests.rs` cover paused scripts, Promise reactions, waiting host calls, invalid replacement source, and deferred language settings.
|
||||
|
||||
History panel row context menus (2026-11-20, G12): Right-clicking any state offers New Document from State, Step to This State, and Clear History. Creating a document uses the clicked snapshot without stepping the source document; Step to This State uses undo/redo and is disabled on the current row; Clear History removes undo and redo steps without changing artwork and is disabled when history is empty. Headless tests in `crates/ui-egui/src/panels/history.rs` cover opening an earlier row menu, snapshot creation, backward/forward jumps, and clearing history. The snapshot chosen when the menu opens is retained while it stays open; pending tool edits are settled before resolving that snapshot to its new row; a discarded snapshot reports that it is unavailable instead of acting on a neighboring row. Jumps settle pending typing before counting undo/redo steps. Shift+F10 opens the focused row menu; Escape dismisses it and restores row focus. Switching documents dismisses the menu before it can act on another document. Headless tests cover these keyboard and document-switch paths. The list and menu use shared Craft UI controls. Scores and estimates unchanged.
|
||||
|
||||
Reference in new issue
Block a user