Skip to content

Commit a59fac9

Browse files
authored
fix: Quoted – clone throw an error (#141) (#142)
- add missing `quoted.test.js` file (fixtures were already present)
1 parent 5aea4d7 commit a59fac9

File tree

4 files changed

+1509
-2
lines changed

4 files changed

+1509
-2
lines changed

lib/nodes/Quoted.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@ class Quoted extends Node {
1818
constructor(options) {
1919
super(options);
2020
this.type = 'quoted';
21-
this.contents = unquote(options.value);
22-
[this.quote] = options.value;
21+
/**
22+
* When cloning the node via {@link Node.clone()} there are no constructor params
23+
*/
24+
if (options && options.value) {
25+
this.contents = unquote(options.value);
26+
[this.quote] = options.value;
27+
}
2328
}
2429

2530
static fromTokens(tokens, parser) {

test/quoted.test.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Copyright © 2018 Andrew Powell
3+
4+
This Source Code Form is subject to the terms of the Mozilla Public
5+
License, v. 2.0. If a copy of the MPL was not distributed with this
6+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
8+
The above copyright notice and this permission notice shall be
9+
included in all copies or substantial portions of this Source Code Form.
10+
*/
11+
const test = require('ava');
12+
13+
const { nodeToString, parse } = require('../lib');
14+
15+
const { snapshot, throws } = require('./fixtures/quoted');
16+
17+
for (const fixture of snapshot) {
18+
test(fixture, (t) => {
19+
const root = parse(fixture);
20+
const nodes = root.nodes.map((node) => {
21+
delete node.parent; // eslint-disable-line no-param-reassign
22+
return node;
23+
});
24+
const string = nodeToString(root);
25+
26+
t.is(string, fixture);
27+
t.is(fixture, root.toString());
28+
t.snapshot(root.first.toString());
29+
t.snapshot(string);
30+
t.snapshot(nodes);
31+
32+
root.clone();
33+
});
34+
35+
test(`${fixture} be should cloned`, (t) => {
36+
const root = parse(fixture);
37+
const nodes = root.nodes.map((node) => {
38+
delete node.parent; // eslint-disable-line no-param-reassign
39+
return node;
40+
});
41+
const string = nodeToString(root);
42+
43+
const cloned = root.clone();
44+
45+
t.is(string, fixture);
46+
t.is(fixture, root.toString());
47+
t.snapshot(cloned.first.toString());
48+
t.snapshot(string);
49+
t.snapshot(nodes);
50+
});
51+
}
52+
53+
for (const fixture of throws) {
54+
test(fixture, (t) => {
55+
t.throws(() => parse(fixture));
56+
});
57+
}

0 commit comments

Comments
 (0)