Skip to content

Commit

Permalink
fix #111
Browse files Browse the repository at this point in the history
  • Loading branch information
L0stSoul authored and tonyganch committed Jun 9, 2014
1 parent 1a5aa63 commit 8103765
Showing 1 changed file with 105 additions and 31 deletions.
136 changes: 105 additions & 31 deletions lib/options/vendor-prefix-align.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,82 @@ module.exports = {
return {
id: namespace + baseName,
baseName: baseName,
prefixLength: extraSymbols + prefixLength
prefixLength: prefixLength,
extra: extraSymbols
};
},
/**
* Internal
*
* Return extra indent for item in arguments
* @param {Object} item item node
* @param {Array} nodes nodes to process
* @returns {Number|undefined}
*/
_getExtraSymbols: function(item) {
return item[3] && item[3].reduce(function(prevResult, node) {
if (!Array.isArray(node) )
return prevResult;
if (node[0] === 's')
return prevResult + node[1].length;
if (node[0] === 'commentML')
return prevResult + node[1].length + 4/* comment symbols length */;

return prevResult;
}, 0);
_extraIndent: function(nodes) {
if (!nodes || !nodes.length) return;

var i = nodes.length;
var node;
var crPos;
var result = 0;

while (i--) {
node = nodes[i];

if (!Array.isArray(node))
continue;

if (node[0] === 's') {
crPos = node[1].lastIndexOf('\n');
result += node[1].length - crPos - 1;
if (crPos !== -1)
break;
}
if (node[0] === 'commentML') {
crPos = node[1].lastIndexOf('\n');
if (crPos === -1) {
result += node[1].length + 4 /* comment symbols length */ ;
} else {
result += node[1].length + 2 /* only last comment symbols length */;
break;
}
}
}

return result;

},
/**
* Wrapper for extra indent function for decl-node
* @param {Array} nodes all nodes
* @param {Number} i position in nodes array
*/
_extraIndentDecl: function(nodes, i) {
var subset = [];
while (i--) {
if (!nodes[i] || nodes[i][0] === 'declDelim')
break;
subset.unshift(nodes[i]);
}
return this._extraIndent(subset);
},
/**
* Wrapper for extra indent function for val-node
* @param {Array} nodes all nodes
* @param {Number} i position in nodes array
*/
_extraIndentVal: function(nodes, i) {
var item = nodes[i];
var subset = [];

for (i = 0; i < item[3].length; ++ i) {
if (item[3][i][0] === 'ident')
break;
subset.push(item[3][i]);
}
return this._extraIndent(subset);
},

/**
* Internal
*
Expand All @@ -92,10 +146,11 @@ module.exports = {
*/
_walk: function(args) {
args.node.forEach(function(item, i) {
var info = this._getPrefixInfo(
args.selector(item),
var name = args.selector(item);
var info = name && this._getPrefixInfo(
name,
args.namespaceSelector && this._makeNamespace(args.namespaceSelector(item)),
args.getExtraSymbols && args.getExtraSymbols(item)
args.getExtraSymbols.apply(this, [args.node, i])
);
if (!info) return;
args.payload(info, i);
Expand Down Expand Up @@ -151,18 +206,17 @@ module.exports = {
* Update dict which contains info about items align.
* @param {Object} info,
* @param {Object} dict,
* @param {String} whitespaceNode
*/
_updateDict: function(info, dict, whitespaceNode) {
if (info.prefixLength === 0) return;
_updateDict: function(info, dict) {
if (info.prefixLength === 0 && info.extra === 0) return;

var indent = dict[info.id] || { prefixLength: 0, baseLength: 0 };
var indent = dict[info.id] || { prefixLength: 0, extra: 0 };

dict[info.id] = indent.prefixLength > info.prefixLength ?
dict[info.id] = indent.prefixLength + indent.extra > info.prefixLength + info.extra ?
indent :
{
prefixLength: info.prefixLength,
baseLength: whitespaceNode.substr(whitespaceNode.lastIndexOf('\n') + 1).length
extra: info.extra,
};
},

Expand All @@ -180,9 +234,10 @@ module.exports = {

var firstPart = whitespaceNode.substr(0, whitespaceNode.lastIndexOf('\n') + 1 );
var extraIndent = new Array(
item.prefixLength -
info.prefixLength +
item.baseLength + 1).join(' ');
(item.prefixLength - info.prefixLength) +
(item.extra - info.extra) +
whitespaceNode.length - firstPart.length +
1).join(' ');

return firstPart.concat(extraIndent);
},
Expand All @@ -208,23 +263,23 @@ module.exports = {
var dict = {};
var _this = this;


// Gathering Info
this._walk({
node: node,
selector: this._getDeclName,
getExtraSymbols: this._extraIndentDecl,
payload: function(info, i) {
_this._updateDict(info, dict, node[i - 1][1]);
}
});

this._walk({
node: node,
selector: this._getValName,
namespaceSelector: this._getDeclName,
getExtraSymbols: this._getExtraSymbols,
getExtraSymbols: this._extraIndentVal,
payload: function(info, i) {
if (node[i][3][1][0] !== 's') {
node[i][3].splice(1, 0, ['s', '']);
}
_this._updateDict(info, dict, node[i][3][1][1]);
}
});
Expand All @@ -233,17 +288,33 @@ module.exports = {
this._walk({
node: node,
selector: this._getDeclName,
getExtraSymbols: _this._extraIndentDecl,
payload: function(info, i) {
if (node[i - 1] !== 's') {
node.push(['s', '']);
}
node[i - 1][1] = _this._updateIndent(info, dict, node[i - 1][1]);
}
});
this._walk({
node: node,
selector: this._getValName,
namespaceSelector: this._getDeclName,
getExtraSymbols: this._getExtraSymbols,
getExtraSymbols: this._extraIndentVal,
payload: function(info, i) {
node[i][3][1][1] = _this._updateIndent(info, dict, node[i][3][1][1]);
for (var a = 0; a < node[i][3].length; ++a) {
if (node[i][3][a][0] !== 'function' && node[i][3][a][0] !== 'ident')
continue;

if (node[i][3][a - 1][0] !== 's') {
node[i][3].splice(a, 0, ['s', '']);
++a;
}

break;
}

node[i][3][a - 1][1] = _this._updateIndent(info, dict, node[i][3][a - 1][1]);
}
});

Expand Down Expand Up @@ -312,10 +383,12 @@ module.exports = {
prevSum = sum;
};

var _this = this;
// Gathering Info
this._walk({
node: node,
selector: this._getDeclName,
getExtraSymbols: _this._extraIndentDecl,
payload: function(info, i) {
if (node[i - 1]) {
var sum = node[i - 1][1].replace(/^[ \t]*\n+/, '').length + info.prefixLength;
Expand All @@ -327,6 +400,7 @@ module.exports = {
this._walk({
node: node,
selector: this._getValName,
getExtraSymbols: this._extraIndentVal,
payload: function(info, i) {
if (node[i][3][1]) {
var sum = node[i][3][1][1].replace(/^[ \t]*\n+/, '').length + info.prefixLength;
Expand Down

0 comments on commit 8103765

Please sign in to comment.