Skip to content

Commit

Permalink
allow await in invalid expression wrapper (#3146)
Browse files Browse the repository at this point in the history
* 🧪 Adds failing text for await in invalid expressions

* ✅ Wraps invalid expressions in an async IIFE

* Trigger CI

Co-authored-by: Josh Hanley <[email protected]>
  • Loading branch information
ekwoka and joshhanley authored Nov 24, 2022
1 parent 1038203 commit 2369462
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/alpinejs/src/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function generateFunctionFromString(expression, el) {
|| /^[\n\s]*if.*\(.*\)/.test(expression)
// Support expressions starting with "let/const" like: "let foo = 'bar'"
|| /^(let|const)\s/.test(expression)
? `(() => { ${expression} })()`
? `(async()=>{ ${expression} })()`
: expression

const safeAsyncFunction = () => {
Expand Down
14 changes: 14 additions & 0 deletions tests/cypress/integration/directives/x-on.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,3 +534,17 @@ test('.dot modifier correctly binds event listener with namespace',
get('span').should(haveText('baz'))
}
)

test('handles await in handlers with invalid right hand expressions',
html`
<div x-data="{ text: 'original' }">
<button @click="let value = 'new string'; text = await Promise.resolve(value)"></button>
<span x-text="text"></span>
</div>
`,
({ get }) => {
get('span').should(haveText('original'))
get('button').click()
get('span').should(haveText('new string'))
}
)

0 comments on commit 2369462

Please sign in to comment.