Skip to content

Commit 6af71be

Browse files
committed
Update options
1 parent 6960b03 commit 6af71be

19 files changed

Lines changed: 228 additions & 294 deletions

lib/options/always-semicolon.js

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,57 +12,46 @@ module.exports = {
1212
* @param {node} node
1313
*/
1414
process: function(node) {
15-
if (node.type !== 'block') return;
15+
var nodeWithoutSemicolon;
1616

17-
node = node.content;
17+
if (!node.is('block')) return;
18+
19+
mainLoop:
1820
for (var i = node.length; i--;) {
19-
var currentNode = node[i];
20-
var currentNodeType = currentNode.type;
21-
var nodeWithoutSemicolon;
21+
var currentNode = node.get(i);
2222

2323
// Skip nodes that already have `;` at the end:
24-
if (currentNodeType === 'declarationDelimiter') break;
24+
if (currentNode.is('declarationDelimiter')) break;
2525

2626
// Add semicolon only after declarations and includes.
2727
// If current node is include, insert semicolon right into it.
2828
// If it's declaration, look for value node:
29-
if (currentNodeType === 'include') {
29+
if (currentNode.is('include')) {
3030
nodeWithoutSemicolon = currentNode;
31-
} else if (currentNodeType === 'declaration') {
32-
for (var k = currentNode.content.length; k--;) {
33-
if (currentNode.content[k].type === 'value') {
34-
nodeWithoutSemicolon = currentNode.content[k];
35-
break;
36-
}
37-
}
31+
} else if (currentNode.is('declaration')) {
32+
nodeWithoutSemicolon = currentNode.last('value');
3833
} else {
3934
continue;
4035
}
4136

42-
var space = [];
43-
var isBlock = false;
44-
4537
// Check if there are spaces and comments at the end of the node:
46-
for (var j = nodeWithoutSemicolon.content.length; j--;) {
47-
var lastNode = nodeWithoutSemicolon.content[j].type;
38+
for (var j = nodeWithoutSemicolon.length; j--; ) {
39+
var lastNode = nodeWithoutSemicolon.get(j);
40+
4841
// If the node's last child is block, do not add semicolon:
4942
// TODO: Add syntax check and run the code only for scss
50-
if (lastNode === 'block') {
51-
isBlock = true;
43+
if (lastNode.is('block')) {
44+
break mainLoop;
45+
} else if (!lastNode.is('space') &&
46+
!lastNode.is('multilineComment') &&
47+
!lastNode.is('singlelineComment')) {
48+
j++;
5249
break;
53-
} else if (['space', 'multilineComment', 'singlelineComment'].indexOf(lastNode) === -1) break;
54-
55-
space.unshift(nodeWithoutSemicolon.content[j]);
50+
}
5651
}
5752

58-
if (isBlock) break;
59-
60-
// Temporarily remove last spaces and comments and insert `;`
61-
// before them:
62-
nodeWithoutSemicolon.content.splice(nodeWithoutSemicolon.content.length - space.length);
6353
var declDelim = gonzales.createNode({ type: 'declarationDelimiter', content: ';' });
64-
var args = [i + 1, 0, declDelim].concat(space);
65-
node.splice.apply(node, args);
54+
nodeWithoutSemicolon.insert(j, declDelim);
6655
break;
6756
}
6857
},

lib/options/block-indent.js

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,26 @@ module.exports = (function() {
22
var syntax;
33
var value;
44

5-
function processStylesheet(node) {
6-
var spaces;
7-
var whitespaceNode;
8-
var i;
5+
function processNode(node, level) {
6+
level = level || 0;
97

10-
for (i = node.length; i--;) {
11-
whitespaceNode = node.get(i);
12-
13-
if (!whitespaceNode.is('space')) continue;
8+
for (var i = 0; i < node.length; i++) {
9+
var n = node.get(i);
10+
if (!n) continue;
1411

15-
spaces = whitespaceNode.content.replace(/\n[ \t]+/gm, '\n');
16-
17-
if (spaces === '') {
18-
node.content.splice(i, 1);
19-
} else {
20-
whitespaceNode.content = spaces;
12+
if (syntax === 'sass' && n.is('block')) {
13+
processSassBlock(n, level, value);
2114
}
22-
}
23-
24-
function processBlock(x, level) {
25-
level = level || 0;
2615

27-
for (var i = 0; i < x.content.length; i++) {
28-
var n = x.get(i);
29-
if (!n) continue;
30-
31-
32-
if (syntax === 'sass' && n.is('block')) {
33-
processSassBlock(n, level, value);
34-
}
35-
36-
// Continue only with space nodes inside {...}:
37-
if (syntax !== 'sass' && level !== 0 && n.is('space')) {
38-
processSpaceNode(n, level, value);
39-
}
16+
// Continue only with space nodes inside {...}:
17+
if (syntax !== 'sass' && level !== 0 && n.is('space')) {
18+
processSpaceNode(n, level, value);
19+
}
4020

41-
if (n.is('block') || n.is('atrulers')) level++;
21+
if (n.is('block') || n.is('atrulers')) level++;
4222

43-
processBlock(n, level);
44-
}
23+
processNode(n, level);
4524
}
46-
47-
processBlock(node);
4825
}
4926

5027
function processSassBlock(node, level, value) {
@@ -95,13 +72,31 @@ module.exports = (function() {
9572
* @param {node} node
9673
*/
9774
process: function process(node) {
75+
var spaces;
76+
var whitespaceNode;
77+
var i;
78+
9879
if (!node.is('stylesheet')) return;
9980

10081
syntax = this.getSyntax();
10182
value = this.getValue('block-indent');
10283

103-
processStylesheet(node);
10484

85+
for (i = node.length; i--;) {
86+
whitespaceNode = node.get(i);
87+
88+
if (!whitespaceNode.is('space')) continue;
89+
90+
spaces = whitespaceNode.content.replace(/\n[ \t]+/gm, '\n');
91+
92+
if (spaces === '') {
93+
node.remove(i);
94+
} else {
95+
whitespaceNode.content = spaces;
96+
}
97+
}
98+
99+
processNode(node);
105100
},
106101

107102
/**

lib/options/color-case.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,11 @@ module.exports = {
1010
* @param {node} node
1111
*/
1212
process: function(node) {
13-
var value = this.getValue('color-case');
14-
if (node.type === 'color') {
15-
if (value === 'lower') {
16-
node.content = node.content.toLowerCase();
17-
} else if (value === 'upper') {
18-
node.content = node.content.toUpperCase();
19-
}
20-
}
13+
if (!node.is('color')) return;
14+
15+
node.content = this.getValue('color-case') === 'lower' ?
16+
node.content.toLowerCase() :
17+
node.content.toUpperCase();
2118
},
2219

2320
/**

lib/options/color-shorthand.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ module.exports = {
1010
* @param {node} node
1111
*/
1212
process: function(node) {
13-
if (node.type === 'color') {
14-
if (this.getValue('color-shorthand')) {
15-
node.content = node.content.replace(/(\w)\1(\w)\2(\w)\3/i, '$1$2$3');
16-
} else {
17-
node.content = node.content.replace(/^(\w)(\w)(\w)$/, '$1$1$2$2$3$3');
18-
}
19-
}
13+
if (!node.is('color')) return;
14+
15+
node.content = this.getValue('color-shorthand') ?
16+
node.content.replace(/(\w)\1(\w)\2(\w)\3/i, '$1$2$3') :
17+
node.content.replace(/^(\w)(\w)(\w)$/, '$1$1$2$2$3$3');
2018
},
2119

2220
/**

lib/options/element-case.js

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,18 @@ module.exports = {
1010
* @param {node} node
1111
*/
1212
process: function(node) {
13-
if (node.type !== 'selector' &&
14-
node.type !== 'arguments') return;
15-
16-
for (var x = node.content.length; x--;) {
17-
var selector = node.content[x];
18-
if (selector.type !== 'simpleSelector') continue;
19-
20-
for (var i = selector.content.length; i--;) {
21-
var simpleselector = selector.content[i];
22-
if (!simpleselector ||
23-
simpleselector.type !== 'ident') continue;
24-
25-
simpleselector.content = this.getValue('element-case') === 'lower' ?
26-
simpleselector.content.toLowerCase() :
27-
simpleselector.content.toUpperCase();
28-
}
29-
}
13+
if (!node.is('selector') &&
14+
!node.is('arguments')) return;
15+
16+
var value = this.getValue('element-case');
17+
18+
node.forEach('simpleSelector', function(selector) {
19+
selector.forEach('ident', function(ident) {
20+
ident.content = value === 'lower' ?
21+
ident.content.toLowerCase() :
22+
ident.content.toUpperCase();
23+
});
24+
});
3025
},
3126

3227
/**

lib/options/eof-newline.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var gonzales = require('gonzales-pe');
2+
13
module.exports = {
24
name: 'eof-newline',
35

@@ -10,15 +12,15 @@ module.exports = {
1012
* @param {node} node
1113
*/
1214
process: function(node) {
13-
if (node.type === 'stylesheet') {
14-
var lastChild = node.content[node.content.length - 1];
15-
if (lastChild.type !== 'space') {
16-
lastChild = { type: 'space', content: '' };
17-
node.content.push(lastChild);
18-
}
19-
lastChild.content = lastChild.content.replace(/\n$/, '');
20-
if (this.getValue('eof-newline')) lastChild.content += '\n';
15+
if (!node.is('stylesheet')) return;
16+
17+
var lastChild = node.last();
18+
if (!lastChild.is('space')) {
19+
lastChild = gonzales.CreateNode({ type: 'space', content: '' });
20+
node.content.push(lastChild);
2121
}
22+
lastChild.content = lastChild.content.replace(/\n$/, '');
23+
if (this.getValue('eof-newline')) lastChild.content += '\n';
2224
},
2325

2426
/**

lib/options/leading-zero.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ module.exports = {
1010
* @param {node} node
1111
*/
1212
process: function(node) {
13-
if (node.type === 'number') {
14-
if (this.getValue('leading-zero')) {
15-
if (node.content[0] === '.')
16-
node.content = '0' + node.content;
17-
} else {
18-
node.content = node.content.replace(/^0+(?=\.)/, '');
19-
}
13+
if (!node.is('number')) return;
14+
15+
if (this.getValue('leading-zero')) {
16+
if (node.content[0] === '.')
17+
node.content = '0' + node.content;
18+
} else {
19+
node.content = node.content.replace(/^0+(?=\.)/, '');
2020
}
2121
},
2222

lib/options/quotes.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,21 @@ module.exports = {
1010
* @param {node} node
1111
*/
1212
process: function(node) {
13+
if (!node.is('string')) return;
14+
1315
var value = this.getValue('quotes');
14-
if (node.type === 'string') {
15-
if (node.content[0] === '"' && value === 'single') {
16-
node.content = node.content
17-
.replace(/\\"/g, '"') // unescape all escaped double quotes
18-
.replace(/([^\\])'/g, '$1\\\'') // escape all the single quotes
19-
.replace(/^"|"$/g, '\''); // replace the first and the last quote
2016

21-
} else if (node.content[0] === '\'' && value === 'double') {
22-
node.content = node.content
23-
.replace(/\\'/g, '\'') // unescape all escaped single quotes
24-
.replace(/([^\\])"/g, '$1\\\"') // escape all the double quotes
25-
.replace(/^'|'$/g, '"'); // replace the first and the last quote
26-
}
17+
if (node.content[0] === '"' && value === 'single') {
18+
node.content = node.content
19+
.replace(/\\"/g, '"') // unescape all escaped double quotes
20+
.replace(/([^\\])'/g, '$1\\\'') // escape all the single quotes
21+
.replace(/^"|"$/g, '\''); // replace the first and the last quote
22+
23+
} else if (node.content[0] === '\'' && value === 'double') {
24+
node.content = node.content
25+
.replace(/\\'/g, '\'') // unescape all escaped single quotes
26+
.replace(/([^\\])"/g, '$1\\\"') // escape all the double quotes
27+
.replace(/^'|'$/g, '"'); // replace the first and the last quote
2728
}
2829
},
2930

0 commit comments

Comments
 (0)