Skip to content

Commit f321723

Browse files
jonathantnealshellscape
authored andcommitted
Protect constructors from empty opts (shellscape#55)
* Test cloning * Protect constructors from empty opts
1 parent b45eebd commit f321723

File tree

5 files changed

+49
-12
lines changed

5 files changed

+49
-12
lines changed

lib/comment.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Comment extends Node {
77
constructor (opts) {
88
super(opts);
99
this.type = 'comment';
10-
this.inline = opts.inline || false;
10+
this.inline = Object(opts).inline || false;
1111
}
1212

1313
toString () {

lib/index.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,11 @@ parser.atword = function (opts) {
2323
};
2424

2525
parser.colon = function (opts) {
26-
opts.value = opts.value || ':';
27-
return new Colon(opts);
26+
return new Colon(Object.assign({ value: ':' }, opts));
2827
};
2928

3029
parser.comma = function (opts) {
31-
opts.value = opts.value || ',';
32-
return new Comma(opts);
30+
return new Comma(Object.assign({ value: ',' }, opts));
3331
};
3432

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

5149
parser.paren = function (opts) {
52-
opts.value = opts.value || '(';
53-
return new Paren(opts);
50+
return new Paren(Object.assign({ value: '(' }, opts));
5451
};
5552

5653
parser.string = function (opts) {
57-
opts.quote = opts.quote || '\'';
58-
return new Str(opts);
54+
return new Str(Object.assign({ quote: '\'' }, opts));
5955
};
6056

6157
parser.value = function (opts) {

lib/node.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,10 @@ module.exports = class Node {
201201
positionBy (opts) {
202202
let pos = this.source.start;
203203

204-
if (opts.index) {
204+
if (Object(opts).index) {
205205
pos = this.positionInside(opts.index);
206206
}
207-
else if (opts.word) {
207+
else if (Object(opts).word) {
208208
let index = this.toString().indexOf(opts.word);
209209
if (index !== -1) pos = this.positionInside(index);
210210
}

lib/number.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class NumberNode extends Node {
77
constructor (opts) {
88
super(opts);
99
this.type = 'number';
10-
this.unit = opts.unit || '';
10+
this.unit = Object(opts).unit || '';
1111
}
1212

1313
toString () {

test/clone.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
3+
const expect = require('chai').expect;
4+
const Parser = require('../lib/parser');
5+
const ParserError = require('../lib/errors/ParserError');
6+
7+
describe('Parser → Number', () => {
8+
let fixtures = [
9+
{
10+
it: 'should clone an rgb function',
11+
test: 'rgb(255, 0, 0)',
12+
expected: [
13+
{ type: 'func', value: 'rgb' },
14+
{ type: 'paren', value: '(' },
15+
{ type: 'number', value: '255' },
16+
{ type: 'comma', value: ',' },
17+
{ type: 'number', value: '0' },
18+
{ type: 'comma', value: ',' },
19+
{ type: 'number', value: '0' },
20+
{ type: 'paren', value: ')' }
21+
]
22+
}
23+
];
24+
25+
fixtures.forEach((fixture) => {
26+
it(fixture.it, () => {
27+
let ast = new Parser(fixture.test, { loose: fixture.loose }).parse().clone(),
28+
index = 0;
29+
30+
ast.first.walk((node) => {
31+
let expected = fixture.expected[index];
32+
index ++;
33+
34+
if (expected) {
35+
expect(node).to.shallowDeepEqual(expected);
36+
}
37+
});
38+
});
39+
});
40+
41+
});

0 commit comments

Comments
 (0)