Skip to content
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

fix: manipulation of node structures, proper stringifying #73

Merged
merged 3 commits into from
Mar 7, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 23 additions & 16 deletions lib/ValuesStringifier.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const Stringifier = require('postcss/lib/stringifier');

module.exports = class LessStringifier extends Stringifier {
basic(node) {
const after = this.raw(node, 'after');
basic(node, value) {
const print = value || node.value;
const after = node.raws.after ? this.raw(node, 'after') || '' : '';
// NOTE: before is handled by postcss in stringifier.body

this.builder(node.value, node, 'start');
this.builder(after || '', node, 'end');
this.builder(print, node, 'start');
this.builder(after, node, 'end');
}

atword(...args) {
Expand All @@ -23,25 +25,30 @@ module.exports = class LessStringifier extends Stringifier {
}

func(node) {
const after = this.raw(node, 'after');
const after = this.raw(node, 'after') || '';

this.builder(node.name + node.params, node, 'start');
this.builder(after || '', node, 'end');
this.builder(`${node.name}(`, node, 'start');

for (const child of node.nodes) {
// since we're duplicating this.body here, we have to handle `before`
// but we don't want the postcss default \n value, so check it's non-empty first
const before = child.raws.before ? this.raw(child, 'before') : '';
if (before) {
this.builder(before);
}
this.stringify(child);
}

this.builder(`)${after}`, node, 'end');
}

interpolation(node) {
const after = this.raw(node, 'after');

this.builder(node.prefix + node.params, node, 'start');
this.builder(after || '', node, 'end');
this.basic(node, node.prefix + node.params);
}

numeric(node) {
const start = node.value + node.unit;
const after = this.raw(node, 'after');

this.builder(start, node, 'start');
this.builder(after || '', node, 'end');
const print = node.value + node.unit;
this.basic(node, print);
}

operator(node) {
Expand Down
7 changes: 6 additions & 1 deletion lib/nodes/Func.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,17 @@ class Func extends Container {
// use a new parser to parse the params of the function. recursion here makes for easier maint
// we must require this here due to circular dependency resolution
const { parse } = require('../'); // eslint-disable-line global-require
const { nodes: children } = parse(params, opts);
const root = parse(params, opts);
const { nodes: children } = root;

// TODO: correct line and character position (should we just pad the input? probably easiest)
for (const child of children) {
node.push(child);
}

if (root.raws.after) {
node.last.raws.after = root.raws.after;
}
}

parser.end(lastToken);
Expand Down
19 changes: 18 additions & 1 deletion test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
*/
const test = require('ava');

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

test('integration', (t) => {
let root = parse(`normal normal 1em/1 'Source Sans Pro', serif`);
Expand All @@ -26,3 +27,19 @@ test('integration', (t) => {
root = parse('1 / -1');
t.is(root.nodes.length, 3);
});

test('manipulation', (t) => {
const source = 'rgb(100% 100% 100%)';
const root = parse(source);
const { first } = root;

let string = nodeToString(root);
t.is(source, string);

first.nodes.splice(1, 0, new Punctuation({ value: ',', parent: first }));
first.nodes.splice(3, 0, new Punctuation({ value: ',', parent: first }));

string = nodeToString(root);
t.not(source, string);
t.snapshot(string);
});
20 changes: 10 additions & 10 deletions test/snapshots/func.test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ Generated by [AVA](https://ava.li).
Numeric {
parent: [Circular],
raws: {
after: '',
after: ' ',
before: ' ',
},
source: {
Expand Down Expand Up @@ -344,7 +344,7 @@ Generated by [AVA](https://ava.li).
Numeric {
parent: [Circular],
raws: {
after: '',
after: ' ',
before: ' ',
},
source: {
Expand Down Expand Up @@ -1314,7 +1314,7 @@ Generated by [AVA](https://ava.li).
Numeric {
parent: [Circular],
raws: {
after: '',
after: ' ',
before: ' ',
},
source: {
Expand Down Expand Up @@ -1379,7 +1379,7 @@ Generated by [AVA](https://ava.li).
parent: [Circular],
quote: '"',
raws: {
after: '',
after: ' ',
before: ' ',
},
source: {
Expand Down Expand Up @@ -1443,7 +1443,7 @@ Generated by [AVA](https://ava.li).
parent: [Circular],
quote: '"',
raws: {
after: '',
after: ' ',
before: ' ',
},
source: {
Expand Down Expand Up @@ -1507,7 +1507,7 @@ Generated by [AVA](https://ava.li).
parent: [Circular],
quote: '\'',
raws: {
after: '',
after: ' ',
before: ' ',
},
source: {
Expand Down Expand Up @@ -1571,7 +1571,7 @@ Generated by [AVA](https://ava.li).
parent: [Circular],
quote: '\'',
raws: {
after: '',
after: ' ',
before: ' ',
},
source: {
Expand Down Expand Up @@ -1766,7 +1766,7 @@ Generated by [AVA](https://ava.li).
isVariable: false,
parent: [Circular],
raws: {
after: '',
after: ' ',
before: '',
},
source: {
Expand Down Expand Up @@ -1858,7 +1858,7 @@ Generated by [AVA](https://ava.li).
isVariable: false,
parent: [Circular],
raws: {
after: '',
after: ' ',
before: ' ',
},
source: {
Expand Down Expand Up @@ -1950,7 +1950,7 @@ Generated by [AVA](https://ava.li).
isVariable: false,
parent: [Circular],
raws: {
after: '',
after: ' ',
before: ' ',
},
source: {
Expand Down
Binary file modified test/snapshots/func.test.js.snap
Binary file not shown.
11 changes: 11 additions & 0 deletions test/snapshots/integration.test.js.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Snapshot report for `test/integration.test.js`

The actual snapshot is saved in `integration.test.js.snap`.

Generated by [AVA](https://ava.li).

## manipulation

> Snapshot 1

'rgb(100%, 100%, 100%)'
Binary file added test/snapshots/integration.test.js.snap
Binary file not shown.