-
Notifications
You must be signed in to change notification settings - Fork 941
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
handle regex special characters #250
Conversation
Sorry for the additional commits, right after I pushed up this PR I saw I was being inefficient. The current code should be a winner. |
why not squash the commits then? |
@@ -141,7 +141,7 @@ function enable(namespaces) { | |||
|
|||
for (var i = 0; i < len; i++) { | |||
if (!split[i]) continue; // ignore empty strings | |||
namespaces = split[i].replace(/\*/g, '.*?'); | |||
namespaces = split[i].replace(/[\\^$+?.()|[\]{}]/g, '\\$&').replace(/\*/g, '.*?'); |
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.
imo, we should offload this work
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.
I looked at that, but *
would be escaped by that (or any such) library -- you'll note I'm not escaping it here so the existing wildcard replace will work as-is. It's possible to work around this issue, but for a 1-liner it seemed like overkill.
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.
I should add: it especially complicates the issue since this needs to work in both node and the browser. If this were a node-only library it wouldn't be such a big deal.
If the functionality needed were a big deal and possibly subject to further development, bug fixes, etc, it might be worth it -- but again, in comparison to a 1-liner, it seems like unnecessary complication.
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.
ah gotcha
@stephenmathieson: Very well, squashed. |
Thanks! |
Hey guys. This PR introduced a breaking change for us, in version 2.3.0. In 2.2.0, this was perfectly valid: DEBUG='(foo|bar)*' node index.js
// This would log both "foo:example:something-else" and "bar:blabla:example". After applying the previous replace, the process.env.DEBUG variable is now In 2.3.0, DEBUG='(foo|bar)*' node index.js
// Does not log anything starting with "foo" or "bar" at all. After applying the new I understand that parenthesis are a regexp special character, but their usage is reserved for matching groups. Given the functionality that EDIT: Also, it goes without saying that the regexp introduced in 2.3.0 also blocks the use of the @TooTallNate: given that this is a BC (we suddenly stopped seeing logs!) would you consider merging a fix (I can submit a PR) and bumping the package asap? |
The question @TooTallNate and the visionmedia folks need to figure out is whether they WANT to allow regex-ish enables for You are considering it a breaking change, but the other perspective is that you wrote code which relies on buggy behavior. It all depends on whether you consider the regex-ish behavior to be a bug or an undocumented feature. If the decision is that the regex-ish behavior is desired, then it should be properly documented to prevent users from having problems like #312. |
@nunofgs: FYI, the documented way to do something like |
Well, breaking logs is never a good thing, so 👍 for a revert on the 2.x level. Then, if we are going to keep this patch, it should be saved for the |
Plus having namespaces with these special characters seems... strange to me as well. So if they had previous utility in specifying the names in DEBUG then that's probably more useful IMO. |
FWIW, a revert will wind up breaking logs for at least 1 person (@wenkanglin, the originator of #312). |
I always assumed that regex-ish behavior was desired and is part of the reason I consider this package to be very useful and powerful. I still recommend reverting and then immediately bumping to 3.0.0 since, even though it may be considered undesirable behavior but this decision is, of course, your prerogative. |
+1 for keeping compatibility with native RegExps and allowing some syntactic sugar on top of it. |
This reverts commit 8dd8345. We shouldn't have changed the original behavior, which too many people are relying on at this point. It's also technically a breaking change, which we shouldn't have landed on a minor/patch version change. So in the interest of not breaking people's logs in production, reverting this. We can discuss in a new issue if we want to restore this patch for a `v3` release, but at the moment I'm personally leaningo towards *no*, for historical reasons (i.e. this is reminding me of Node.js trying to remove `sys` if anybody reading this remembers those days). See the discussion in #250 for more backlog.
I strongly recommend documenting the regex-ish behavior, since the current documentation indicates how Alternatively, you could consider a v3.0 release that uses 100% regex syntax (with appropriate documentation), since otherwise the non-regex (and currently documented) behavior of |
For another example of the types of issues that can result from folks not expecting the regex behavior, see #288 |
Currently, enabling namespaces which contain regex special characters leads to unexpected results. Examples:
This PR fixes that by escaping the regex special characters within the namespace strings (while still properly allowing
*
to be a wildcard). The escape code is based on the proposed ES7RegEx.escape
polyfill found at https://github.com/benjamingr/RegExp.escape/blob/master/polyfill.jsSince this escaping only happens when
.enable()
is called and not when.enabled()
is called or the log function is actually used, it should not negatively impact the performance of apps using this module.