-
Notifications
You must be signed in to change notification settings - Fork 12
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
fix(zap_dev): Wrap interpolated identifiers in braces #23
Conversation
When component variables are interpolated in strings, the replacement identifier can cause invalidate Dart code to be generated if the identifier is not wrapped in braces already. For example: ```html <script> String? value; String? interpolated; $: interpolated = '$value'; </script> ``` Here, the interpolated string currently becomes something like `$_$v2 /* value */` which is invalid Dart code, when we really want `${_$v2 /* value */}`.
void _patchIdentifier( | ||
SyntacticEntity entity, | ||
Element? target, { | ||
bool wrapVariablesInBraces = false, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This solution feels pretty hacky. Open to better ways to handle it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can handle this in visitInterpolationElement
without changing _patchIdentifier
by writing the braces into an empty span:
if (missingBraces) {
_replaceRange(node.leftBracket.end, 0, '{');
}
_patchIdentifier(expression, expression.staticElement);
if (missingBraces) {
_replaceRange(expression.end, 0, '}');
}
return;
I didn't try this so there might be an off-by-one error here, but as long as the patches are made with increasing offsets that don't overlap, this should work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worked like a charm! Very cool system here. I was confused how that could work since after _patchIdentifier
, the contents have changed. Turns out it's as simple as keeping tracking of the skew 😅 brilliant!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
void _patchIdentifier( | ||
SyntacticEntity entity, | ||
Element? target, { | ||
bool wrapVariablesInBraces = false, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can handle this in visitInterpolationElement
without changing _patchIdentifier
by writing the braces into an empty span:
if (missingBraces) {
_replaceRange(node.leftBracket.end, 0, '{');
}
_patchIdentifier(expression, expression.staticElement);
if (missingBraces) {
_replaceRange(expression.end, 0, '}');
}
return;
I didn't try this so there might be an off-by-one error here, but as long as the patches are made with increasing offsets that don't overlap, this should work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, thanks for the PRs!
When component variables are interpolated in strings, the replacement identifier can cause invalidate Dart code to be generated if the identifier is not wrapped in braces already.
For example:
Here, the interpolated string currently becomes something like
$_$v2 /* value */
which is invalid Dart code, when we really want${_$v2 /* value */}
.