postcss-less
Advanced tools
Comparing version 3.0.2 to 3.1.0
@@ -31,2 +31,32 @@ /* eslint no-param-reassign: off */ | ||
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]}`; | ||
const firstParenIndex = tokens.findIndex((t) => t[0] === '('); | ||
const lastParen = tokens.reverse().find((t) => t[0] === ')'); | ||
const lastParenIndex = tokens.reverse().indexOf(lastParen); | ||
const paramTokens = tokens.splice(firstParenIndex, lastParenIndex); | ||
const params = paramTokens.map((t) => t[1]).join(''); | ||
for (const token of tokens.reverse()) { | ||
this.tokenizer.back(token); | ||
} | ||
this.atrule(this.tokenizer.nextToken()); | ||
this.lastNode.function = true; | ||
this.lastNode.params = params; | ||
} | ||
init(node, line, column) { | ||
@@ -57,2 +87,72 @@ super.init(node, line, column); | ||
mixin(tokens) { | ||
const [first] = tokens; | ||
const identifier = first[1].slice(0, 1); | ||
const bracketsIndex = tokens.findIndex((t) => t[0] === 'brackets'); | ||
const firstParenIndex = tokens.findIndex((t) => t[0] === '('); | ||
let important = ''; | ||
// fix for #86. if rulesets are mixin params, they need to be converted to a brackets token | ||
if ((bracketsIndex < 0 || bracketsIndex > 3) && firstParenIndex > 0) { | ||
const lastParenIndex = tokens.findIndex((t) => t[0] === ')'); | ||
const contents = tokens.slice(firstParenIndex, lastParenIndex + firstParenIndex); | ||
const brackets = contents.map((t) => t[1]).join(''); | ||
const [paren] = tokens.slice(firstParenIndex); | ||
const start = [paren[2], paren[3]]; | ||
const [last] = tokens.slice(lastParenIndex, lastParenIndex + 1); | ||
const end = [last[2], last[3]]; | ||
const newToken = ['brackets', brackets].concat(start, end); | ||
const tokensBefore = tokens.slice(0, firstParenIndex); | ||
const tokensAfter = tokens.slice(lastParenIndex + 1); | ||
tokens = tokensBefore; | ||
tokens.push(newToken); | ||
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) { | ||
[, important] = tokens[importantIndex]; | ||
tokens.splice(importantIndex, 1); | ||
} | ||
for (const token of tokens.reverse()) { | ||
this.tokenizer.back(token); | ||
} | ||
this.atrule(this.tokenizer.nextToken()); | ||
this.lastNode.mixin = true; | ||
this.lastNode.raws.identifier = identifier; | ||
if (important) { | ||
this.lastNode.important = true; | ||
this.lastNode.raws.important = important; | ||
} | ||
} | ||
other(token) { | ||
@@ -84,72 +184,31 @@ if (!isInlineComment.bind(this)(token)) { | ||
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) { | ||
// NOTE: keep commented for examining unknown structures | ||
// console.log('unknown', tokens); | ||
const [first] = tokens; | ||
// #121 support `each` - http://lesscss.org/functions/#list-functions-each | ||
if (tokens[0][1] === 'each' && tokens[1][0] === '(') { | ||
this.each(tokens); | ||
return; | ||
} | ||
// TODO: move this into a util function/file | ||
if (isMixinToken(first)) { | ||
const identifier = first[1].slice(0, 1); | ||
const bracketsIndex = tokens.findIndex((t) => t[0] === 'brackets'); | ||
const firstParenIndex = tokens.findIndex((t) => t[0] === '('); | ||
let important = ''; | ||
// fix for #86. if rulesets are mixin params, they need to be converted to a brackets token | ||
if (bracketsIndex < 0 && firstParenIndex > 0) { | ||
const lastParenIndex = tokens.findIndex((t) => t[0] === ')'); | ||
const contents = tokens.slice(firstParenIndex, lastParenIndex + firstParenIndex); | ||
const brackets = contents.map((t) => t[1]).join(''); | ||
const [paren] = tokens.slice(firstParenIndex); | ||
const start = [paren[2], paren[3]]; | ||
const [last] = tokens.slice(lastParenIndex, lastParenIndex + 1); | ||
const end = [last[2], last[3]]; | ||
const newToken = ['brackets', brackets].concat(start, end); | ||
const tokensBefore = tokens.slice(0, firstParenIndex); | ||
const tokensAfter = tokens.slice(lastParenIndex + 1); | ||
tokens = tokensBefore; | ||
tokens.push(newToken); | ||
tokens = tokens.concat(tokensAfter); | ||
} | ||
const importantIndex = tokens.findIndex((t) => importantPattern.test(t[1])); | ||
if (importantIndex > 0) { | ||
[, important] = tokens[importantIndex]; | ||
tokens.splice(importantIndex, 1); | ||
} | ||
for (const token of tokens.reverse()) { | ||
this.tokenizer.back(token); | ||
} | ||
this.atrule(this.tokenizer.nextToken()); | ||
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(); | ||
// } | ||
this.mixin(tokens); | ||
return; | ||
} | ||
// NOTE: keep commented for examining unknown structures | ||
// console.log('unknown', tokens); | ||
super.unknownWord(tokens); | ||
} | ||
}; |
@@ -5,3 +5,3 @@ const Stringifier = require('postcss/lib/stringifier'); | ||
atrule(node, semicolon) { | ||
if (!node.mixin && !node.variable) { | ||
if (!node.mixin && !node.variable && !node.function) { | ||
super.atrule(node, semicolon); | ||
@@ -11,3 +11,4 @@ return; | ||
let name = `${node.raws.identifier || '@'}${node.name}`; | ||
const identifier = node.function ? '' : node.raws.identifier || '@'; | ||
let name = `${identifier}${node.name}`; | ||
let params = node.params ? this.rawValue(node, 'params') : ''; | ||
@@ -14,0 +15,0 @@ const important = node.raws.important || ''; |
{ | ||
"name": "postcss-less", | ||
"version": "3.0.2", | ||
"version": "3.1.0", | ||
"description": "LESS parser for PostCSS", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17869
333