Skip to content

Commit 451bb51

Browse files
committed
set the default stringifier for Rule. It return original LESS code, f.e. mixins without body.
1 parent b25d01f commit 451bb51

File tree

6 files changed

+37
-8
lines changed

6 files changed

+37
-8
lines changed

lib/less-parser.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Comment from 'postcss/lib/comment';
22
import Parser from 'postcss/lib/parser';
3+
import Rule from './rule';
34
import findExtendRule from './find-extend-rule';
45
import isMixinToken from './is-mixin-token';
56
import lessTokenizer from './less-tokenize';
@@ -11,6 +12,17 @@ export default class LessParser extends Parser {
1112
this.tokens = lessTokenizer(this.input);
1213
}
1314

15+
rule (tokens) {
16+
tokens.pop();
17+
18+
const node = new Rule();
19+
20+
this.init(node, tokens[0][2], tokens[0][3]);
21+
node.raws.between = this.spacesFromEnd(tokens);
22+
this.raw(node, 'selector', tokens);
23+
this.current = node;
24+
}
25+
1426
comment (token) {
1527
const node = new Comment();
1628
const content = token[1];
@@ -33,7 +45,7 @@ export default class LessParser extends Parser {
3345
node.raws.right = '';
3446
} else {
3547
const match = text.match(/^(\s*)([^]*[^\s])(\s*)$/);
36-
48+
3749
node.text = match[2];
3850

3951
// Add extra spaces to generate a comment in a common style /*[space][text][space]*/
@@ -79,15 +91,15 @@ export default class LessParser extends Parser {
7991
* @type {boolean}
8092
*/
8193
this.current.ruleWithoutBody = true;
82-
94+
8395
// remove `nodes` property from rules without body
8496
// eslint-disable-next-line
8597
delete this.current.nodes;
8698
this.current.extendRule = this.current.selector.indexOf('&:extend') >= 0;
8799
this.current.important = this.current.selector.indexOf('!important') >= 0;
88100

89101
this.pos--;
90-
102+
91103
this.end(this.tokens[this.pos]);
92104
}
93105

lib/rule.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import PostCssRule from 'postcss/lib/rule';
2+
import stringify from './less-stringify';
3+
4+
export default class Rule extends PostCssRule {
5+
toString (stringifier) {
6+
if (!stringifier) {
7+
stringifier = {
8+
stringify
9+
};
10+
}
11+
12+
return super.toString(stringifier);
13+
}
14+
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postcss-less",
3-
"version": "0.12.0",
3+
"version": "0.13.0",
44
"description": "LESS parser for PostCSS",
55
"keywords": [
66
"css",

test/parser/extend.spec.js

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ describe('Parser', () => {
3535
expect(root.first.first.selector).to.eql('&:extend(.bucket tr)');
3636
expect(root.first.first.params).to.eql('(.bucket tr)');
3737
expect(root.first.first.extendRule).to.eql(true);
38+
expect(root.first.first.toString()).to.be.eql('&:extend(.bucket tr);');
3839
});
3940

4041
it('parses :extend() after selector', () => {

test/parser/mixins.spec.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ describe('Parser', () => {
2020

2121
describe('Mixins without body', () => {
2222
it('mixin without body #1', () => {
23-
const root = parse('.mixin-name (#FFF);');
23+
const less = '.mixin-name (#FFF);';
24+
const root = parse(less);
2425

2526
expect(root.first.type).to.eql('rule');
2627
expect(root.first.selector).to.eql('.mixin-name (#FFF)');
2728
expect(root.first.params).to.eql('(#FFF)');
2829
expect(root.first.ruleWithoutBody).to.eql(true);
2930
expect(root.first.nodes).to.be.an('undefined');
31+
expect(root.first.toString()).to.be.eql('.mixin-name (#FFF)');
3032
});
3133

3234
it('mixin without body #2', () => {

test/tokenize.spec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import Input from 'postcss/lib/input';
55
import {expect} from 'chai';
66
import tokenize from '../lib/less-tokenize';
77

8-
function testTokens (css, tokens) {
9-
expect(tokenize(new Input(css))).to.eql(tokens);
8+
function testTokens (less, tokens) {
9+
expect(tokenize(new Input(less))).to.eql(tokens);
1010
}
1111

1212
describe('#tokenize()', () => {
@@ -162,7 +162,7 @@ describe('#tokenize()', () => {
162162
]);
163163
});
164164

165-
it('tokenizes @ in a string in parens', () => {
165+
it('tokenizes @ in a string in parentheses', () => {
166166
testTokens('("a@b")', [
167167
['(', '(', 1, 1],
168168
['string', '"a@b"', 1, 2, 1, 6],

0 commit comments

Comments
 (0)