Skip to content

fix: Quoted – clone throw an error (#141) #142

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 1 commit into from
Jan 7, 2022
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
9 changes: 7 additions & 2 deletions lib/nodes/Quoted.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ class Quoted extends Node {
constructor(options) {
super(options);
this.type = 'quoted';
this.contents = unquote(options.value);
[this.quote] = options.value;
/**
* When cloning the node via {@link Node.clone()} there are no constructor params
*/
if (options && options.value) {
this.contents = unquote(options.value);
[this.quote] = options.value;
}
}

static fromTokens(tokens, parser) {
Expand Down
57 changes: 57 additions & 0 deletions test/quoted.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copyright © 2018 Andrew Powell

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of this Source Code Form.
*/
const test = require('ava');

const { nodeToString, parse } = require('../lib');

const { snapshot, throws } = require('./fixtures/quoted');

for (const fixture of snapshot) {
test(fixture, (t) => {
const root = parse(fixture);
const nodes = root.nodes.map((node) => {
delete node.parent; // eslint-disable-line no-param-reassign
return node;
});
const string = nodeToString(root);

t.is(string, fixture);
t.is(fixture, root.toString());
t.snapshot(root.first.toString());
t.snapshot(string);
t.snapshot(nodes);

root.clone();
});

test(`${fixture} be should cloned`, (t) => {
const root = parse(fixture);
const nodes = root.nodes.map((node) => {
delete node.parent; // eslint-disable-line no-param-reassign
return node;
});
const string = nodeToString(root);

const cloned = root.clone();

t.is(string, fixture);
t.is(fixture, root.toString());
t.snapshot(cloned.first.toString());
t.snapshot(string);
t.snapshot(nodes);
});
}

for (const fixture of throws) {
test(fixture, (t) => {
t.throws(() => parse(fixture));
});
}
Loading