Skip to content

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

Merged
merged 2 commits into from
Sep 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Owner

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?

Copy link
Collaborator Author

@jonathantneal jonathantneal Sep 17, 2018

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. 😄

Copy link
Owner

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.

}

toString () {
Expand Down
12 changes: 4 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ parser.atword = function (opts) {
};

parser.colon = function (opts) {
opts.value = opts.value || ':';
return new Colon(opts);
return new Colon(Object.assign({ value: ':' }, opts));
};

parser.comma = function (opts) {
opts.value = opts.value || ',';
return new Comma(opts);
return new Comma(Object.assign({ value: ',' }, opts));
};

parser.comment = function (opts) {
Expand All @@ -49,13 +47,11 @@ parser.operator = function (opts) {
};

parser.paren = function (opts) {
opts.value = opts.value || '(';
return new Paren(opts);
return new Paren(Object.assign({ value: '(' }, opts));
};

parser.string = function (opts) {
opts.quote = opts.quote || '\'';
return new Str(opts);
return new Str(Object.assign({ quote: '\'' }, opts));
};

parser.value = function (opts) {
Expand Down
4 changes: 2 additions & 2 deletions lib/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,10 @@ module.exports = class Node {
positionBy (opts) {
let pos = this.source.start;

if (opts.index) {
if (Object(opts).index) {
pos = this.positionInside(opts.index);
}
else if (opts.word) {
else if (Object(opts).word) {
let index = this.toString().indexOf(opts.word);
if (index !== -1) pos = this.positionInside(index);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class NumberNode extends Node {
constructor (opts) {
super(opts);
this.type = 'number';
this.unit = opts.unit || '';
this.unit = Object(opts).unit || '';
}

toString () {
Expand Down
41 changes: 41 additions & 0 deletions test/clone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

const expect = require('chai').expect;
const Parser = require('../lib/parser');
const ParserError = require('../lib/errors/ParserError');

describe('Parser → Number', () => {
let fixtures = [
{
it: 'should clone an rgb function',
test: 'rgb(255, 0, 0)',
expected: [
{ type: 'func', value: 'rgb' },
{ type: 'paren', value: '(' },
{ type: 'number', value: '255' },
{ type: 'comma', value: ',' },
{ type: 'number', value: '0' },
{ type: 'comma', value: ',' },
{ type: 'number', value: '0' },
{ type: 'paren', value: ')' }
]
}
];

fixtures.forEach((fixture) => {
it(fixture.it, () => {
let ast = new Parser(fixture.test, { loose: fixture.loose }).parse().clone(),
index = 0;

ast.first.walk((node) => {
let expected = fixture.expected[index];
index ++;

if (expected) {
expect(node).to.shallowDeepEqual(expected);
}
});
});
});

});