diff --git a/lib/LessParser.js b/lib/LessParser.js index 95dc110..23aec8b 100644 --- a/lib/LessParser.js +++ b/lib/LessParser.js @@ -82,6 +82,9 @@ module.exports = class LessParser extends Parser { } unknownWord(tokens) { + // NOTE: keep commented for examining unknown structures + // console.log('unknown', tokens); + const [first] = tokens; // TODO: move this into a util function/file @@ -110,6 +113,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]).reduce((a, c) => a + c); + 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) { @@ -125,28 +151,14 @@ module.exports = class LessParser extends Parser { this.lastNode.mixin = true; this.lastNode.raws.identifier = identifier; - // const importantIndex = tokens.findIndex((t) => importantPattern.test(t[1])); - if (important) { this.lastNode.important = true; this.lastNode.raws.important = important; } - // if (importantIndex > 0) { - // nodes.splice(importantIndex, 1); - // [this.lastNode.raws.important] = this.lastNode.params.match(importantPattern); - - // this.lastNode.params = this.lastNode.params.replace(importantPattern, ''); - - // const [spaces] = this.lastNode.params.match(/\s+$/) || ['']; - // this.lastNode.raws.between = spaces; - // this.lastNode.params = this.lastNode.params.trim(); - // } - return; } - // NOTE: keep commented for examining unknown structures - // console.log('unknown', tokens); + super.unknownWord(tokens); } }; diff --git a/test/parser/mixins.test.js b/test/parser/mixins.test.js index 87efdd0..d107503 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}`;