Skip to content

Support expressions starting with var in the evaluator - #4930

Open
HawkForge wants to merge 1 commit into
alpinejs:mainfrom
HawkForge:fix/evaluator-var-statements
Open

HawkForge wants to merge 1 commit into
alpinejs:mainfrom
HawkForge:fix/evaluator-var-statements

Conversation

@HawkForge

@HawkForge HawkForge commented Oct 15, 2026 •

Copy link
Copy Markdown

Fixes the bug reported in discussion #4741 (originally #4740). Thanks @kwilson21 for the clear reproduction.

An inline expression that starts with a var declaration, like @input="var v = $event.target.value; if (v.length > 0) { result = 'PASS' }", never runs. The evaluator only wraps an expression in a function body when it starts with if, let or const. Anything else is placed on the right-hand side of an assignment (__self.result = ...), so var throws SyntaxError: Unexpected token 'var'. The error is only logged as a console warning and the handler does nothing.

This adds var to the same check that already handles let and const (#2224). The check appears in three places: the normal evaluator and both branches of the raw evaluator, so all three are updated.

The two new tests in tests/vitest/evaluator.spec.js (evaluate and evaluateRaw with a leading var) fail with the SyntaxError without the change and pass with it. npx vitest run tests/vitest/ passes.

@ekwoka

ekwoka commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

....feels like you just shouldn't use var.

@HawkForge

Copy link
Copy Markdown
Author

Agreed, let or const is the better choice, and that already works. The problem is that var is valid JavaScript but fails silently here: the handler does nothing and the only sign is a console warning, which is what confused the reporter in #4741. The change only adds var to the existing let/const check. If you would rather not support it, I am fine closing this.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants