Skip to content

Commit dc1cffd

Browse files
committed
Gonzales 3.0: Fix detection tests
1 parent 7c4f5cb commit dc1cffd

File tree

43 files changed

+312
-328
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+312
-328
lines changed

lib/csscomb.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function cssToAST(text, syntax, filename) {
1717
var tree;
1818

1919
try {
20-
tree = gonzales.srcToAST({ syntax: syntax, src: text });
20+
tree = gonzales.parse(text, { syntax: syntax });
2121
} catch (e) {
2222
throw new Error('Parsing error' + fileInfo + ': ' + e.message);
2323
}
@@ -56,22 +56,16 @@ function getHandler(optionName) {
5656
* @param {Object} detectedOptions
5757
*/
5858
function detectInNode(node, level, handler, detectedOptions) {
59-
node.forEach(function(node) {
60-
if (!Array.isArray(node)) return;
61-
62-
var nodeType = node.shift();
63-
var detected = handler.detect(nodeType, node, level);
59+
node.map(function(tree) {
60+
var detected = handler.detect(tree);
6461
var variants = detectedOptions[handler.name];
6562
if (typeof detected === 'object') {
6663
variants.push.apply(variants, detected);
6764
} else if (typeof detected !== 'undefined') {
6865
variants.push(detected);
6966
}
70-
node.unshift(nodeType);
71-
72-
if (nodeType === 'atrulers' || nodeType === 'block') level++;
7367

74-
detectInNode(node, level, handler, detectedOptions);
68+
//if (nodeType === 'atrulers' || nodeType === 'block') level++;
7569
});
7670
}
7771

@@ -91,7 +85,7 @@ function detectInTree(tree, handlers) {
9185
handlers.forEach(function(handler) {
9286
detectedOptions[handler.name] = [];
9387
// TODO: Pass all parameters as one object? <tg>
94-
detectInNode(['tree', tree], 0, handler, detectedOptions);
88+
detectInNode(tree, 0, handler, detectedOptions);
9589
});
9690
return detectedOptions;
9791
}

lib/options/always-semicolon.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,12 @@ module.exports = {
6262
* @param {node} node
6363
*/
6464
detect: function(node) {
65-
if (node.type === 'block') {
66-
for (var i = node.content.length; i--;) {
67-
var nodeItem = node.content[i];
68-
var type = nodeItem.type;
69-
if (type === 'declDelim') return true;
65+
if (!node.is('block')) return;
7066

71-
if (type === 'declaration') return false;
72-
}
67+
for (var i = node.length; i--;) {
68+
var nodeItem = node.get(i);
69+
if (nodeItem.is('declarationDelimiter')) return true;
70+
if (nodeItem.is('declaration')) return false;
7371
}
7472
}
7573
};

lib/options/block-indent.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ module.exports = (function() {
55
function processNode(node, level) {
66
level = level || 0;
77

8+
// XXX: Hack for braces
9+
if (node.is('braces')) return;
10+
811
for (var i = 0; i < node.length; i++) {
912
var n = node.get(i);
1013
if (!n) continue;
@@ -102,30 +105,28 @@ module.exports = (function() {
102105
/**
103106
* Detects the value of an option at the tree node.
104107
*
105-
* @param {String} nodeType
106108
* @param {node} node
107-
* @param {Number} level
108109
*/
109-
detect: function(nodeType, node, level) {
110+
detect: function(node) {
110111
var result = [];
111112

112113
// Continue only with non-empty {...} blocks:
113-
if (nodeType !== 'atrulers' && nodeType !== 'block' || !node.length) return;
114+
if (!node.is('atrulers') && !node.is('block') || !node.length)
115+
return;
114116

115117
for (var i = node.length; i--;) {
116-
var whitespaceNode = node[i];
117-
if (whitespaceNode[0] !== 's') continue;
118+
var whitespaceNode = node.get(i);
119+
if (!whitespaceNode.is('space')) continue;
118120

119-
var spaces = whitespaceNode[1];
121+
var spaces = whitespaceNode.content;
120122
var lastIndex = spaces.lastIndexOf('\n');
121123

122124
// Do not continue if there is no line break:
123125
if (lastIndex < 0) continue;
124126

125127
// Number of spaces from beginning of line:
126-
var spacesLength = spaces.slice(lastIndex + 1).length;
127-
var arrayLength = Math.floor(spacesLength / (level + 1)) + 1;
128-
result.push(new Array(arrayLength).join(' '));
128+
var spacesLength = spaces.slice(lastIndex + 1).length + 1;
129+
result.push(new Array(spacesLength).join(' '));
129130
}
130131

131132
return result;

lib/options/color-case.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ module.exports = {
2323
* @param {node} node
2424
*/
2525
detect: function(node) {
26-
if (node.type === 'vhash') {
27-
if (node.content[0].match(/^[^A-F]*[a-f][^A-F]*$/)) {
28-
return 'lower';
29-
} else if (node.content[0].match(/^[^a-f]*[A-F][^a-f]*$/)) {
30-
return 'upper';
31-
}
26+
if (!node.is('color')) return;
27+
28+
if (node.content.match(/^[^A-F]*[a-f][^A-F]*$/)) {
29+
return 'lower';
30+
} else if (node.content.match(/^[^a-f]*[A-F][^a-f]*$/)) {
31+
return 'upper';
3232
}
3333
}
3434
};

lib/options/color-shorthand.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ module.exports = {
2323
* @param {node} node
2424
*/
2525
detect: function(node) {
26-
if (node.type === 'vhash') {
27-
if (node.content[0].match(/^\w{3}$/)) {
28-
return true;
29-
} else if (node.content[0].match(/^(\w)\1(\w)\2(\w)\3$/)) {
30-
return false;
31-
}
26+
if (!node.is('color')) return;
27+
28+
if (node.content.match(/^\w{3}$/)) {
29+
return true;
30+
} else if (node.content.match(/^(\w)\1(\w)\2(\w)\3$/)) {
31+
return false;
3232
}
3333
}
3434
};

lib/options/element-case.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,23 @@ module.exports = {
2727
/**
2828
* Detects the value of an option at the tree node.
2929
*
30-
* @param {String} nodeType
3130
* @param {node} node
3231
*/
33-
detect: function(nodeType, node) {
34-
if (nodeType !== 'simpleselector') return;
32+
detect: function(node) {
33+
if (!node.is('selector') &&
34+
!node.is('arguments')) return;
3535

3636
var variants = [];
37-
for (var i = node.length; i--;) {
38-
var nodeItem = node[i];
39-
if (nodeItem[0] !== 'ident') continue;
40-
if (nodeItem[1].match(/^[a-z]+$/)) {
41-
variants.push('lower');
42-
} else if (nodeItem[1].match(/^[A-Z]+$/)) {
43-
variants.push('upper');
44-
}
45-
}
37+
38+
node.forEach('simpleSelector', function(selector) {
39+
selector.forEach('ident', function(ident) {
40+
if (ident.content.match(/^[a-z]+$/)) {
41+
variants.push('lower');
42+
} else if (ident.content.match(/^[A-Z]+$/)) {
43+
variants.push('upper');
44+
}
45+
});
46+
});
4647
return variants;
4748
}
4849
};

lib/options/eof-newline.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = {
1616

1717
var lastChild = node.last();
1818
if (!lastChild.is('space')) {
19-
lastChild = gonzales.CreateNode({ type: 'space', content: '' });
19+
lastChild = gonzales.createNode({ type: 'space', content: '' });
2020
node.content.push(lastChild);
2121
}
2222
lastChild.content = lastChild.content.replace(/\n$/, '');
@@ -26,16 +26,16 @@ module.exports = {
2626
/**
2727
* Detects the value of an option at the tree node.
2828
*
29-
* @param {String} nodeType
3029
* @param {node} node
3130
*/
32-
detect: function(nodeType, node) {
33-
if (nodeType === 'stylesheet') {
34-
if (node[node.length - 1][0] === 's' && node[node.length - 1][1].indexOf('\n') !== -1) {
35-
return true;
36-
} else {
37-
return false;
38-
}
31+
detect: function(node) {
32+
if (!node.is('stylesheet')) return;
33+
34+
var lastChild = node.last();
35+
if (lastChild.is('space') && lastChild.content.indexOf('\n') !== -1) {
36+
return true;
37+
} else {
38+
return false;
3939
}
4040
}
4141
};

lib/options/leading-zero.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,15 @@ module.exports = {
2323
/**
2424
* Detects the value of an option at the tree node.
2525
*
26-
* @param {String} nodeType
2726
* @param {node} node
2827
*/
29-
detect: function(nodeType, node) {
30-
if (nodeType === 'number') {
31-
if (node.toString().match(/^\.[0-9]+/)) {
32-
return false;
33-
} else if (node.toString().match(/^0\.[0-9]+/)) {
34-
return true;
35-
}
28+
detect: function(node) {
29+
if (!node.is('number')) return;
30+
31+
if (node.content.match(/^\.[0-9]+/)) {
32+
return false;
33+
} else if (node.content.match(/^0\.[0-9]+/)) {
34+
return true;
3635
}
3736
}
3837
};

lib/options/quotes.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,15 @@ module.exports = {
3131
/**
3232
* Detects the value of an option at the tree node.
3333
*
34-
* @param {String} nodeType
3534
* @param {node} node
3635
*/
37-
detect: function(nodeType, node) {
38-
if (nodeType === 'string') {
39-
if (node[0][0] === '"') {
40-
return 'double';
41-
} else if (node[0][0] === '\'') {
42-
return 'single';
43-
}
36+
detect: function(node) {
37+
if (!node.is('string')) return;
38+
39+
if (node.content[0] === '"') {
40+
return 'double';
41+
} else if (node.content[0] === '\'') {
42+
return 'single';
4443
}
4544
}
4645
};

lib/options/remove-empty-rulesets.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,20 @@ module.exports = (function() {
6161
processNode(node);
6262
},
6363

64+
detectDefault: true,
65+
6466
/**
6567
* Detects the value of an option at the tree node.
6668
* This option is treated as `true` by default, but any trailing space would invalidate it.
6769
*
68-
* @param {String} nodeType
6970
* @param {node} node
7071
*/
71-
detectDefault: true,
72+
detect: function(node) {
73+
if (!node.is('atrulers') && !node.is('block')) return;
7274

73-
detect: function(nodeType, node) {
74-
if (nodeType === 'atrulers' || nodeType === 'block') {
75-
if (node.length === 0 || (node.length === 1 && node[0][0] === 's')) {
76-
return false;
77-
}
75+
if (node.length === 0 ||
76+
(node.length === 1 && node.first().is('space'))) {
77+
return false;
7878
}
7979
}
8080
};

0 commit comments

Comments
 (0)