diff --git a/lib/comment.js b/lib/comment.js index cffa16a..155a2c9 100644 --- a/lib/comment.js +++ b/lib/comment.js @@ -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; } toString () { diff --git a/lib/index.js b/lib/index.js index 757f173..f8f7fed 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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) { @@ -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) { diff --git a/lib/node.js b/lib/node.js index fbd355b..55b2055 100644 --- a/lib/node.js +++ b/lib/node.js @@ -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); } diff --git a/lib/number.js b/lib/number.js index d8d52a1..f045265 100644 --- a/lib/number.js +++ b/lib/number.js @@ -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 () { diff --git a/test/clone.js b/test/clone.js new file mode 100644 index 0000000..cb71c2a --- /dev/null +++ b/test/clone.js @@ -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); + } + }); + }); + }); + +});