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
Replace array indexOf
checks with includes
#954
Replace array indexOf
checks with includes
#954
Conversation
indexOf
checks with includes
indexOf
checks with includes
I'll need to take a closer look at the tests, since that makes it sound like something else is broken, but otherwise this looks great! Thanks! |
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.
@bendtherules Thanks for taking this up. I agree the const index = [].indexOf(); if (index !== -1) [].splice(index, 1)
should remain as is. I wonder if there are any more instances that would make sense to change. See the files I found below and let me know if there is a reason we should keep those as is.
- ComentPlugin.ts:66
- DynamicModulePlugin.ts:57
- converter/converter.ts:315
I also want to be careful about handling indexOf(undefined)
since it isn't the same with includes
. Hopefully we aren't dealing with arrays with holes but it's worth mentioning.
Thanks for noticing these. I tried to looked through |
In it('Minimatch can match absolute paths expressions', () => {
const paths = ['/unix/absolute/**/path', '\\windows\\alternative\\absolute\\path', 'C:\\Windows\\absolute\\*\\path', '**/arbitrary/path/**'];
const mms = createMinimatch(paths);
const patterns = mms.map(({ pattern }) => pattern);
const comparePaths = [
absolutePath('/unix/absolute/**/path'),
absolutePath('/windows/alternative/absolute/path'),
absolutePath('/Windows/absolute/*/path'),
'**/arbitrary/path/**'
];
Assert(isEqual(patterns, comparePaths), `Patterns have been altered:\nMMS: ${patterns}\nPaths: ${comparePaths}`); Here the third path This is failing in my local Windows setup. We might want to change it somehow (or make this optional?). |
Hi @aciccarello , do this changes look good? |
Sorry for the delay. These changes look great! If you are having trouble running the tests on windows, could you please open another issue so we can get that fixed separately. Thanks! |
Closes #898
Changes:
Replaced
indexOf
calls to check presence or absence withincludes
(with negation operator when checking absence).Decisions:
There are some indexOf calls which use the index value to both check presence and use it to remove some elements at that position. They can be potentially refactored with separate calls to
includes
andindexOf
, but I decided not to do that asindexOf
alone can serve both the purposes here.In testcases, replaced
assert.equal
+indexOf
check with -1 byassert.ok
+includes
. This will probably show less information during error, as the actual index value won't be printed. That seems like a small price to pay.Observations:
includes
or!includes
) with the opposite one. I often did this mistake when working on this initially, but none of the test cases failed as they should have.Maybe we should add some more tests around that?