Skip to content
Merged

#45 #46

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
18 changes: 15 additions & 3 deletions lib/less-parser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Comment from 'postcss/lib/comment';
import Parser from 'postcss/lib/parser';
import Rule from './rule';
import findExtendRule from './find-extend-rule';
import isMixinToken from './is-mixin-token';
import lessTokenizer from './less-tokenize';
Expand All @@ -11,6 +12,17 @@ export default class LessParser extends Parser {
this.tokens = lessTokenizer(this.input);
}

rule (tokens) {
tokens.pop();

const node = new Rule();

this.init(node, tokens[0][2], tokens[0][3]);
node.raws.between = this.spacesFromEnd(tokens);
this.raw(node, 'selector', tokens);
this.current = node;
}

comment (token) {
const node = new Comment();
const content = token[1];
Expand All @@ -33,7 +45,7 @@ export default class LessParser extends Parser {
node.raws.right = '';
} else {
const match = text.match(/^(\s*)([^]*[^\s])(\s*)$/);

node.text = match[2];

// Add extra spaces to generate a comment in a common style /*[space][text][space]*/
Expand Down Expand Up @@ -79,15 +91,15 @@ export default class LessParser extends Parser {
* @type {boolean}
*/
this.current.ruleWithoutBody = true;

// remove `nodes` property from rules without body
// eslint-disable-next-line
delete this.current.nodes;
this.current.extendRule = this.current.selector.indexOf('&:extend') >= 0;
this.current.important = this.current.selector.indexOf('!important') >= 0;

this.pos--;

this.end(this.tokens[this.pos]);
}

Expand Down
14 changes: 14 additions & 0 deletions lib/rule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import PostCssRule from 'postcss/lib/rule';
import stringify from './less-stringify';

export default class Rule extends PostCssRule {
toString (stringifier) {
if (!stringifier) {
stringifier = {
stringify
};
}

return super.toString(stringifier);
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postcss-less",
"version": "0.12.0",
"version": "0.13.0",
"description": "LESS parser for PostCSS",
"keywords": [
"css",
Expand Down
1 change: 1 addition & 0 deletions test/parser/extend.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe('Parser', () => {
expect(root.first.first.selector).to.eql('&:extend(.bucket tr)');
expect(root.first.first.params).to.eql('(.bucket tr)');
expect(root.first.first.extendRule).to.eql(true);
expect(root.first.first.toString()).to.be.eql('&:extend(.bucket tr);');
});

it('parses :extend() after selector', () => {
Expand Down
4 changes: 3 additions & 1 deletion test/parser/mixins.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ describe('Parser', () => {

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

expect(root.first.type).to.eql('rule');
expect(root.first.selector).to.eql('.mixin-name (#FFF)');
expect(root.first.params).to.eql('(#FFF)');
expect(root.first.ruleWithoutBody).to.eql(true);
expect(root.first.nodes).to.be.an('undefined');
expect(root.first.toString()).to.be.eql('.mixin-name (#FFF)');
});

it('mixin without body #2', () => {
Expand Down
6 changes: 3 additions & 3 deletions test/tokenize.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import Input from 'postcss/lib/input';
import {expect} from 'chai';
import tokenize from '../lib/less-tokenize';

function testTokens (css, tokens) {
expect(tokenize(new Input(css))).to.eql(tokens);
function testTokens (less, tokens) {
expect(tokenize(new Input(less))).to.eql(tokens);
}

describe('#tokenize()', () => {
Expand Down Expand Up @@ -162,7 +162,7 @@ describe('#tokenize()', () => {
]);
});

it('tokenizes @ in a string in parens', () => {
it('tokenizes @ in a string in parentheses', () => {
testTokens('("a@b")', [
['(', '(', 1, 1],
['string', '"a@b"', 1, 2, 1, 6],
Expand Down