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

Update PolyUtil.js #8840

Merged
merged 8 commits into from Feb 11, 2023
Merged

Update PolyUtil.js #8840

merged 8 commits into from Feb 11, 2023

Conversation

davidmgvaz
Copy link
Contributor

Replace for..in to avoid TypeError: Cannot read properties of null (reading 'lat') with extra attributes of array

Replace for..in to avoid TypeError: Cannot read properties of null (reading 'lat') with extra attributes of array
@jonkoops
Copy link
Collaborator

jonkoops commented Feb 9, 2023

Can you provide an example of how to reproduce this bug?

@davidmgvaz
Copy link
Contributor Author

davidmgvaz commented Feb 9, 2023 via email

Copy link
Collaborator

@jonkoops jonkoops left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidmgvaz could you add a regression test for this?

@davidmgvaz
Copy link
Contributor Author

I am not too comfortable to create that. I just found a common error, and decided to warn about it.

I literally changed 2 lines of code, easy to check by any reviewer. Can you please help?

@Falke-Design
Copy link
Member

I added the same fix to LineUtil and also tests.

Another solutions woule be hasOwnProperty

	const points = [];
	for (const k in latlngs) {
		if(latlngs.hasOwnProperty(k)) {
			points.push(crs.project(toLatLng(latlngs[k])));
		}
	}

@davidmgvaz
Copy link
Contributor Author

Another solutions would be hasOwnProperty

True, and safer for partially filled arrays e.g

const x = [];
x[5] = 0;
for (let i=0; i < x.length; i++) {console.log(x[i]);}

Copy link
Member

@mourner mourner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering, why is it a for ... in loop in the first place, iterating over an array? Looks like a mistake that slipped in earlier. This probably should be a for ... of, right?

@davidmgvaz
Copy link
Contributor Author

even better. Perfect.

@Falke-Design Falke-Design merged commit 73cdf9d into Leaflet:main Feb 11, 2023
Falke-Design added a commit that referenced this pull request Feb 11, 2023
Co-authored-by: Florian Bischof <design.falke@gmail.com>
@Falke-Design Falke-Design added this to the 1.9.x milestone Feb 11, 2023
Falke-Design added a commit to Falke-Design/Leaflet that referenced this pull request Feb 11, 2023
@jonkoops
Copy link
Collaborator

@Falke-Design @mourner a quick enable of the guard-for-in rule brings up a lot of red flags:

Screenshot 2023-02-11 at 20 02 42

Although these are of course not all bugs like this one, they would have the potential to be. Perhaps we should enable this rule and replace all existing loops with for...of, WDYT?

@Falke-Design
Copy link
Member

I have in mind that not all usages of for ... in can be replaced with for ... of, but else yeah makes maybe sense

@jonkoops
Copy link
Collaborator

I have in mind that not all usages of for ... in can be replaced with for ... of, but else yeah makes maybe sense

Yeah I wonder how many we can replace and which will remain. I've created an issue for it (#8843) with a 'help wanted' label, since it's not really high prio, but should be looked into by someone.

@davidmgvaz davidmgvaz deleted the patch-1 branch February 13, 2023 08:01
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.

None yet

4 participants