Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow await in invalid expression wrapper #3146

Merged
merged 4 commits into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'))
}
)