Closed Bug 1081520 Opened 11 years ago Closed 8 years ago

use String.includes() instead of String.indexOf() where appropriate

Categories

(Add-on SDK Graveyard :: General, defect, P2)

defect

Tracking

(Not tracked)

RESOLVED INCOMPLETE

People

(Reporter: zombie, Assigned: periahmadi, Mentored)

References

Details

Attachments

(1 file)

46 bytes, text/x-github-pull-request
zombie
: review-
Details | Review
the web is the best.. the web is the worst.. the web is why we can't have nice things (bug 1075059). but at least we have String.contains(), and it's much more readable than | ~!string.indexOf(substr) | or | string.indexOf(substr) >= 0 |.
Summary: use String.contain() instead of String.indexOf() where appropriate → use String.contains() instead of String.indexOf() where appropriate
Mentor: tomica+amo
Whiteboard: [good first bug]
I'd like to work on this bug.
ok, assigned. so the issue here is that we often use string.indexOf() method to check cf a string contains some other substring. the problem is that indexOf() was meant to do more than that, it also returns the position of the substring, or -1 in case where it's not present. the new string.contains() method just returns true/false, which makes for much more readable code where that's all that's needed. suggested steps for fixing this are: 1) use a grep, or some similar tool, to find all source files where .indexOf() method is used 2) read the code around each use to verify .indexOf() is called on a string (and not an array) 3) also filter out uses where the .indexOf() is used for position info, and not just as "contains" 4) edit to use .contains() where appropriate, and run package test for each file as you finish them let me know if any of that was not clear, or you have other questions..
Assignee: nobody → periahmadi
Status: NEW → ASSIGNED
Attached file submitted PR 1678
Attachment #8505836 - Flags: review?(tomica+amo)
I'm not finished of course, but I wanted to submit a PR just to make sure I'm doing it right before continuing. Tests passed though. Should be fine.
Comment on attachment 8505836 [details] [review] submitted PR 1678 i'm afraid this isn't correct. for this bug, the simple find and replace approach is not appropriate. the .indexOf() and .contains() methods are not equivalent. you can check out the documentation for both on mdn: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/contains but in short, the .indexOf() is a more general function that returns the position of a substring in a big string, or -1 if it's not found. testing if that return value is greater then or equal to zero (or the special trick with ~) is equivalent to calling the .contains() method. the first change in "httpd.js" is violating the point 3) from my comment above. it's using the index of the substring for something more than just deciding if it's "contained" in the bigger string or not. (and btw, that whole file "httpd.js" was replaced, and is not in the current tree. you should pull upstream changes to your own fork of addon-sdk to avoid working on already changed files) the second change in "loader.js" is using the special "~" syntax which is a bit hard to explain, but basically converts the -1 result of .indexOf() to false, and every other result to true, so basically to what we want to use the .contains() method for. to see how your current change doesn't behave like the old code, check the examples here: http://jsbin.com/zisewepagafa/2/edit?js,console (use the "run" button to see the results) feel free to play with that example code on jsbin to figure out how to actually get the same result in the changed code (hint, the "~" is not needed). ... uh, that was a bit wordier than i expected, but i hope it's at least somewhat clear..
Attachment #8505836 - Flags: review?(tomica+amo) → review-
Ah, ok. Thanks for the input. That's what I was afraid of. I'll see if I can fix it.
Priority: -- → P2
Is string.contains still necessary in light of String.prototype.includes()? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes
String.prototype.contains was renamed to String.prototype.includes (bug 1102219), and String.prototype.contains will be removed shortly (bug 1103588).
See Also: → 1102219, 1103588
Summary: use String.contains() instead of String.indexOf() where appropriate → use String.includes() instead of String.indexOf() where appropriate
Because of the difficulty finding mentors and the expected life span of the SDK, we are removing [good first bug] from remaining SDK bugs.
Whiteboard: [good first bug]
Status: ASSIGNED → RESOLVED
Closed: 8 years ago
Resolution: --- → INCOMPLETE
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: