Skip to content

Fix variable shadowing bug in utilCombinedTags sort#11932

Open
JaiswalShivang wants to merge 1 commit intoopenstreetmap:developfrom
JaiswalShivang:fix/utilCombinedTags-variable-shadowing
Open

Fix variable shadowing bug in utilCombinedTags sort#11932
JaiswalShivang wants to merge 1 commit intoopenstreetmap:developfrom
JaiswalShivang:fix/utilCombinedTags-variable-shadowing

Conversation

@JaiswalShivang
Copy link
Contributor

Fixes #11931
In [utilCombinedTags()], the sort callback on line 401 does var key = key
intending to capture the outer loop variable. Due to JavaScript's var hoisting,
this actually creates a new [key] that is undefined, shadowing the outer one.

This means tagCounts[key + '=' + val] always looks up "undefined=..." which
returns undefined, so the frequency comparison is skipped entirely. Tag values
are sorted alphabetically instead of by frequency when multiple entities are selected.

Changes

  • Changed var to [let] in the for...in loop for proper block scoping
  • Removed the broken var key = key; // capture line

Why this works

Array.sort() runs synchronously, so the callback executes immediately while
the loop variable still holds the correct value. With [let], each iteration
gets its own binding — no capture needed.

@matkoniecz
Copy link
Contributor

On which use case you have tested this?

@JaiswalShivang
Copy link
Contributor Author

Tested on the preview deployment by selecting multiple road segments
at an intersection in Berlin (mix of Primary and Secondary roads).

The multi-entity tag editor renders correctly — no errors or regressions.
Combined tag values display as expected when clicking "Multiple Values" dropdowns.

The bug itself is a classic JavaScript var hoisting issue. Inside the sort
callback, var key = key hoists the declaration, so both sides of the
assignment refer to the inner (undefined) variable — the outer loop
variable is never captured. You can confirm this in any JS console:

var x = 10;
(function() { var x = x; console.log(x); })(); // undefined

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.

Bug: utilCombinedTags tag value sort broken due to variable shadowing

3 participants