From e8f118381cc5b6b0bdc084452c2acd524a5c8cfa Mon Sep 17 00:00:00 2001 From: Andrew Powell Date: Thu, 15 Nov 2018 08:18:41 -0500 Subject: [PATCH] fix: mixin !important (#126) * fix: proposed token important fix * chore: use join instead of reduce * chore: update signature to prevent conflict --- lib/LessParser.js | 23 +++++++++++++++++++++++ test/parser/mixins.test.js | 24 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/lib/LessParser.js b/lib/LessParser.js index 7398fe7..f887f9e 100644 --- a/lib/LessParser.js +++ b/lib/LessParser.js @@ -98,6 +98,29 @@ module.exports = class LessParser extends Parser { tokens = tokens.concat(tokensAfter); } + const importantTokens = []; + + for (const token of tokens) { + if (token[1] === '!' || importantTokens.length) { + importantTokens.push(token); + } + + if (token[1] === 'important') { + break; + } + } + + if (importantTokens.length) { + const [bangToken] = importantTokens; + const bangIndex = tokens.indexOf(bangToken); + const last = importantTokens[importantTokens.length - 1]; + const start = [bangToken[2], bangToken[3]]; + const end = [last[4], last[5]]; + const combined = importantTokens.map((t) => t[1]).join(''); + const newToken = ['word', combined].concat(start, end); + tokens.splice(bangIndex, importantTokens.length, newToken); + } + const importantIndex = tokens.findIndex((t) => importantPattern.test(t[1])); if (importantIndex > 0) { diff --git a/test/parser/mixins.test.js b/test/parser/mixins.test.js index 5a1f625..dd4b123 100644 --- a/test/parser/mixins.test.js +++ b/test/parser/mixins.test.js @@ -175,6 +175,30 @@ test('!important, no whitespace', (t) => { t.is(nodeToString(root), less); }); +test('!important, whitespace between', (t) => { + const less = ` + .foo()! important; + `; + const root = parse(less); + const { first } = root; + t.is(first.name, 'foo'); + t.is(first.params, '()'); + t.is(first.important, true); + t.is(nodeToString(root), less); +}); + +test('!important, whitespace before and between', (t) => { + const less = ` + .foo() ! important; + `; + const root = parse(less); + const { first } = root; + t.is(first.name, 'foo'); + t.is(first.params, '()'); + t.is(first.important, true); + t.is(nodeToString(root), less); +}); + test('parses nested mixins with the rule set', (t) => { const params = '({background-color: red;})'; const ruleSet = `.desktop-and-old-ie ${params}`;