forked from csscomb/csscomb.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
space-before-closing-brace.js
112 lines (88 loc) · 2.54 KB
/
space-before-closing-brace.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'use strict';
var gonzales = require('gonzales-pe');
module.exports = (function () {
var valueFromSettings;
var blockIndent;
function getLastWhitespaceNode(_x) {
var _again = true;
_function: while (_again) {
var node = _x;
_again = false;
var lastNode = node.last();
if (!lastNode || !lastNode.content) return null;
if (lastNode.is('block')) return null;
if (lastNode.is('space')) return lastNode;
_x = lastNode;
_again = true;
lastNode = undefined;
continue _function;
}
}
function processBlock(x, level) {
level = level || 0;
x.forEach(function (node) {
if (!node.is('block') && !node.is('atrulers')) return processBlock(node, level);
level++;
var value = valueFromSettings;
if (value.indexOf('\n') > -1) {
// TODO: Check that it works for '' block indent value <tg>
if (blockIndent) {
value += new Array(level).join(blockIndent);
}
}
// If found block node stop at the next one for space check
// For the pre-block node, find its last (the deepest) child
var whitespaceNode = getLastWhitespaceNode(node);
// If it's spaces, modify this node
// If it's something different from spaces, add a space node
// to the end
if (whitespaceNode) {
whitespaceNode.content = value;
} else if (value !== '') {
var space = gonzales.createNode({
type: 'space',
content: value
});
node.content.push(space);
}
processBlock(node, level);
});
}
return {
name: 'space-before-closing-brace',
runBefore: 'tab-size',
syntax: ['css', 'less', 'scss'],
accepts: {
number: true,
string: /^[ \t\n]*$/
},
/**
* Processes tree node.
* @param {node} ast
* @param {Object} config
*/
process: function process(ast, config) {
valueFromSettings = this.value;
blockIndent = config['block-indent'];
processBlock(ast);
},
/**
* Detects the value of an option at the tree node.
*
* @param {node} ast
*/
detect: function detect(ast) {
var detected = [];
ast.traverseByTypes(['block', 'atrulers'], function (node) {
// For the block node, find its last (the deepest) child
var whitespaceNode = getLastWhitespaceNode(node);
if (whitespaceNode) {
detected.push(whitespaceNode.content);
} else {
detected.push('');
}
});
return detected;
}
};
})();