Skip to content

Fixing issue in len with string concatenation in argument #1348

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

Merged
merged 1 commit into from
Oct 28, 2024

Conversation

grantnelson-wf
Copy link
Collaborator

@grantnelson-wf grantnelson-wf commented Oct 28, 2024

This is a fix for #841

I added a check to see if the expression used as the argument inside the len contains binary expressions. If a binary expression exists, then the expression is parenthesized. For example, len(a) will still be translated to a.length without the parenthesis. len(a +b) was getting translated to a + b.lengh (which was the problem), but now get translated into (a + b).length with parenthesis. We don't want to add parenthesis all the time to avoid making the resulting JS larger when not needed.

This will cover any combination of concatenations of variable and literal strings, e.g. len(a + b), len(a + ":" + b). The issue exists only when a basic type is used in len, i.e. string. I didn't limit the check to only being additions (+) for concatenation of strings, even though no other binary operator applies to a string, to allow more robustness for future changes (e.g. if Go suddenly decides to be more like python and add - and * as operators allowed to use on strings). This work does not need to be carried over to cap since cap is not valid for strings.

@grantnelson-wf grantnelson-wf marked this pull request as ready for review October 28, 2024 20:00
@grantnelson-wf grantnelson-wf marked this pull request as draft October 28, 2024 21:00
@grantnelson-wf grantnelson-wf marked this pull request as ready for review October 28, 2024 22:33
@grantnelson-wf grantnelson-wf requested a review from flimzy October 28, 2024 22:35
@flimzy flimzy enabled auto-merge October 28, 2024 22:52
@flimzy flimzy merged commit ebb16a4 into gopherjs:master Oct 28, 2024
10 checks passed
@grantnelson-wf grantnelson-wf deleted the parenLen branch October 29, 2024 14:50
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