Skip to content

Commit

Permalink
Add inline comments inside selectors support
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Feb 23, 2018
1 parent d27edb1 commit 4ce59a2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/scss-parser.es6
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,22 @@ export default class ScssParser extends Parser {
}
}

raw(node, prop, tokens) {
super.raw(node, prop, tokens);
if ( node.raws[prop] ) {
let scss = node.raws[prop].raw;
node.raws[prop].raw = tokens.reduce( (all, i) => {
if ( i[0] === 'comment' && i[6] === 'inline' ) {
let text = i[1].slice(2).replace(/(\*\/|\/\*)/g, '*//*');
return all + '/*' + text + '*/';
} else {
return all + i[1];
}
}, '');
if ( scss !== node.raws[prop].raw ) {
node.raws[prop].scss = scss;
}
}
}

}
10 changes: 10 additions & 0 deletions lib/scss-stringifier.es6
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,14 @@ export default class ScssStringifier extends Stringifier {
}
}

rawValue(node, prop) {
let value = node[prop];
let raw = node.raws[prop];
if ( raw && raw.value === value ) {
return raw.scss ? raw.scss : raw.raw;
} else {
return value;
}
}

}
6 changes: 6 additions & 0 deletions test/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ it('parses empty inline comments', () => {
});
});

it('parses inline comments inside selector', () => {
let root = parse('a\n// c/**/\nb { }');
expect(root.first.raws.selector.scss).toEqual('a\n// c/**/\nb');
expect(root.first.raws.selector.raw).toEqual('a\n/* c*//**//**/\nb');
});

it('does not parse comments inside brakets', () => {
let root = parse('a { cursor: url(http://ya.ru) }');
expect(root.first.first.value).toEqual('url(http://ya.ru)');
Expand Down
9 changes: 9 additions & 0 deletions test/stringify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ it('stringifies inline comment with comments inside', () => {
expect(result).toEqual('// a/*b*/c\na {}');
});

it('stringifies inline comment inside selectors', () => {
let root = parse('a\n// comment\nb {}');
let result = '';
stringify(root, i => {
result += i;
});
expect(result).toEqual('a\n// comment\nb {}');
});

it('stringifies inline comment in the end of file', () => {
let root = parse('// comment');
let result = '';
Expand Down

0 comments on commit 4ce59a2

Please sign in to comment.