[core-http] Fix utils isValidUuid() regression#7387
Conversation
It caused the next isValidUuid() call to return false. According to MDN: > When a regex has the global flag set, test() will advance the lastIndex of the regex. (RegExp.prototype.exec() also advances the lastIndex property.) Further calls to test(str) will resume searching str starting from lastIndex. The lastIndex property will continue to increase each time test() returns true. Note: As long as test() returns true, lastIndex will not reset—even when testing a different string! When test() returns false, the calling regex's lastIndex property will reset to 0.
|
Or we could use |
xirzec
left a comment
There was a problem hiding this comment.
Great find!!
Do you think this is something we can/should make a lint rule about? If so, can you log an issue for that.
chradek
left a comment
There was a problem hiding this comment.
Good catch! And I learned something new: I didn't realize using a regex with global set with test() used lastIndex to keep track of state across invocations:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex#Description
yes it would be useful to have a rule for warning against |
|
Logged #7399 for potential lint rule |
The regression was introduced in #4916, when the local variable
validUuidRegexwas moved out of the function into a const. Regextest()with global flag set will change the state of the regex (advancing thelastIndex) and cause nexttest()to fail in our case. We don't need the global flag here for validating a uuid string.This change removes the global flag from
validUuidRegexconstant.I also remove usage of regex global flags in serializer.ts since they are all used to match single lines.