Skip to content

Commit

Permalink
feat: add extend property to nodes which use :extend
Browse files Browse the repository at this point in the history
  • Loading branch information
shellscape committed Nov 15, 2018
1 parent e8f1183 commit dfb8b1b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
18 changes: 18 additions & 0 deletions lib/LessParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ module.exports = class LessParser extends Parser {
variableNode(this.lastNode);
}

decl(...args) {
super.decl(...args);

// #123: add `extend` decorator to nodes
const extendPattern = /extend\(.+\)/i;

if (extendPattern.test(this.lastNode.value)) {
this.lastNode.extend = true;
}
}

each(tokens) {
// prepend a space so the `name` will be parsed correctly
tokens[0][1] = ` ${tokens[0][1]}`;
Expand Down Expand Up @@ -168,6 +179,13 @@ module.exports = class LessParser extends Parser {
}

super.rule(tokens);

// #123: add `extend` decorator to nodes
const extendPattern = /:extend\(.+\)/i;

if (extendPattern.test(this.lastNode.selector)) {
this.lastNode.extend = true;
}
}

unknownWord(tokens) {
Expand Down
16 changes: 13 additions & 3 deletions test/parser/extend.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,30 @@ const test = require('ava');

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

test('inline &:extend()', (t) => {
test('inline :extend()', (t) => {
const less = '.a:extend(.b) {color: red;}';
const root = parse(less);
const { first } = root;

t.is(first.selector, '.a:extend(.b)');
t.truthy(first.extend);
t.is(nodeToString(root), less);
});

test('inline &:extend() with multiple parameters', (t) => {
test('inline :extend() with multiple parameters', (t) => {
const less = '.e:extend(.f, .g) {}';
const { first } = parse(less);

t.is(first.selector, '.e:extend(.f, .g)');
t.truthy(first.extend);
});

test('inline &:extend() with nested selector in parameters', (t) => {
test('inline :extend() with nested selector in parameters', (t) => {
const less = '.e:extend(.a .g, b span) {}';
const { first } = parse(less);

t.is(first.selector, '.e:extend(.a .g, b span)');
t.truthy(first.extend);
});

test('parses nested &:extend()', (t) => {
Expand All @@ -32,27 +35,31 @@ test('parses nested &:extend()', (t) => {
t.is(first.selector, '.a');
t.is(first.first.prop, '&');
t.is(first.first.value, 'extend(.bucket tr)');
t.truthy(first.first.extend);
});

test('parses :extend() after selector', (t) => {
const less = 'pre:hover:extend(div pre){}';
const { first } = parse(less);

t.is(first.selector, 'pre:hover:extend(div pre)');
t.truthy(first.extend);
});

test('parses :extend() after selector. 2', (t) => {
const less = 'pre:hover :extend(div pre){}';
const { first } = parse(less);

t.is(first.selector, 'pre:hover :extend(div pre)');
t.truthy(first.extend);
});

test('parses multiple extends', (t) => {
const less = 'pre:hover:extend(div pre):extend(.bucket tr) { }';
const { first } = parse(less);

t.is(first.selector, 'pre:hover:extend(div pre):extend(.bucket tr)');
t.truthy(first.extend);
});

test('parses nth expression in extend', (t) => {
Expand All @@ -62,13 +69,15 @@ test('parses nth expression in extend', (t) => {

t.is(first.selector, ':nth-child(1n+3)');
t.is(last.selector, '.child:extend(:nth-child(n+3))');
t.truthy(last.extend);
});

test('"all"', (t) => {
const less = '.replacement:extend(.test all) {}';
const { first } = parse(less);

t.is(first.selector, '.replacement:extend(.test all)');
t.truthy(first.extend);
});

test('with interpolation', (t) => {
Expand All @@ -77,4 +86,5 @@ test('with interpolation', (t) => {

t.is(root.nodes[0].selector, '.bucket');
t.is(root.nodes[1].selector, '.some-class:extend(@{variable})');
t.truthy(root.nodes[1].extend);
});

0 comments on commit dfb8b1b

Please sign in to comment.