-
-
Notifications
You must be signed in to change notification settings - Fork 32
Protect constructors from empty opts #55
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
Conversation
@@ -7,7 +7,7 @@ class Comment extends Node { | |||
constructor (opts) { | |||
super(opts); | |||
this.type = 'comment'; | |||
this.inline = opts.inline || false; | |||
this.inline = Object(opts).inline || false; |
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.
with Node 4 out of LTS (for some time now) we can accomplish this with a default parameter.
constructor(opts = {})
I know it's six of one, half dozen of another, but I'd rather have the moving in a forward direction there. any objections?
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.
Thanks for reviewing, @shellscape. I will make this change if this sole argument does not convince you:
Handling opts
within the function means accidental arguments like new Comment(null)
and new Comment(false)
still work.
Handling opts
within params means potentially getting errors like Cannot read property 'unit' of null
, which may not be descriptive enough.
If you are unconvinced, let me know, and I will make the change within 5 minutes of your decision. 😄
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.
man, I'd hate to be the dev trying those wonky incantations, but you have a point.
Cheers 🎉 |
Thank you so much, @shellscape. May we have a According to the commit log @ https://github.com/shellscape/postcss-values-parser/commits/master we are still in PATCH status. |
When I get the chance to, yes. |
Which issue # if any, does this resolve?
There is a critical issue cloning nodes with children. The internal cloning function calls Node constructors without arguments, but those constructors require them. There were no tests for this. This PR:
Please check one:
Some Node constructors require an options argument that is an object (See https://github.com/shellscape/postcss-values-parser/blob/v1.5.0/lib/number.js#L7-L11)
However, the internal
cloneNode
function does pass any parameters to constructors (See https://github.com/shellscape/postcss-values-parser/blob/v1.5.0/lib/node.js#L4).Therefore, when cloning a parent node like
rgb(255, 0, 0)
, this tool throws an error:Cannot read property 'unit' of undefined
.This error has been reported in one of my projects using postcss-values-parser: postcss/postcss-custom-properties#134
This PR allows Node constructors to work without an object.
Could this be slipped into a PATCH release?