Skip to content

Commit e8f1183

Browse files
authored
fix: mixin !important (#126)
* fix: proposed token important fix * chore: use join instead of reduce * chore: update signature to prevent conflict
1 parent e3731c3 commit e8f1183

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

lib/LessParser.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,29 @@ module.exports = class LessParser extends Parser {
9898
tokens = tokens.concat(tokensAfter);
9999
}
100100

101+
const importantTokens = [];
102+
103+
for (const token of tokens) {
104+
if (token[1] === '!' || importantTokens.length) {
105+
importantTokens.push(token);
106+
}
107+
108+
if (token[1] === 'important') {
109+
break;
110+
}
111+
}
112+
113+
if (importantTokens.length) {
114+
const [bangToken] = importantTokens;
115+
const bangIndex = tokens.indexOf(bangToken);
116+
const last = importantTokens[importantTokens.length - 1];
117+
const start = [bangToken[2], bangToken[3]];
118+
const end = [last[4], last[5]];
119+
const combined = importantTokens.map((t) => t[1]).join('');
120+
const newToken = ['word', combined].concat(start, end);
121+
tokens.splice(bangIndex, importantTokens.length, newToken);
122+
}
123+
101124
const importantIndex = tokens.findIndex((t) => importantPattern.test(t[1]));
102125

103126
if (importantIndex > 0) {

test/parser/mixins.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,30 @@ test('!important, no whitespace', (t) => {
175175
t.is(nodeToString(root), less);
176176
});
177177

178+
test('!important, whitespace between', (t) => {
179+
const less = `
180+
.foo()! important;
181+
`;
182+
const root = parse(less);
183+
const { first } = root;
184+
t.is(first.name, 'foo');
185+
t.is(first.params, '()');
186+
t.is(first.important, true);
187+
t.is(nodeToString(root), less);
188+
});
189+
190+
test('!important, whitespace before and between', (t) => {
191+
const less = `
192+
.foo() ! important;
193+
`;
194+
const root = parse(less);
195+
const { first } = root;
196+
t.is(first.name, 'foo');
197+
t.is(first.params, '()');
198+
t.is(first.important, true);
199+
t.is(nodeToString(root), less);
200+
});
201+
178202
test('parses nested mixins with the rule set', (t) => {
179203
const params = '({background-color: red;})';
180204
const ruleSet = `.desktop-and-old-ie ${params}`;

0 commit comments

Comments
 (0)