-
Notifications
You must be signed in to change notification settings - Fork 449
Expand file tree
/
Copy pathblock-indent.js
More file actions
144 lines (117 loc) · 3.08 KB
/
block-indent.js
File metadata and controls
144 lines (117 loc) · 3.08 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'use strict';
let option = {
/**
* Option's name as it's used in config.
* @type {String}
*/
get name() {
return 'block-indent';
},
/**
* Name of option that must run after this option.
* @type {String}
*/
get runBefore() {
return 'sort-order';
},
/**
* List of syntaxes that are supported by this option.
* @type {Array}
*/
get syntax() {
return ['css', 'less', 'sass', 'scss'];
},
/**
* Types of values this option accepts in config.
* @type {Object}
*/
get accepts() {
return {
number: true,
string: /^[ \t]*$/
};
},
/**
* Processes ast and fixes found code style errors.
* @param {Node} ast
*/
process(ast) {
ast.eachFor('space', function(whitespaceNode, i) {
var spaces = whitespaceNode.content.replace(/(\r?\n)[ \t]+/g, '$1');
if (spaces === '') {
ast.removeChild(i);
} else {
whitespaceNode.content = spaces;
}
});
this._processNode(ast, 0);
},
/**
* Detects the value of this option in ast.
* @param {Node} ast
* @return {Array} List of detected values
*/
detect(ast) {
var detected = [];
ast.traverse(function(node) {
// Continue only with non-empty {...} blocks:
if (!node.is('atrulers') && !node.is('block') || !node.length)
return;
node.eachFor('space', function(whitespaceNode) {
var spaces = whitespaceNode.content;
var lastIndex = spaces.lastIndexOf('\n');
// Do not continue if there is no line break:
if (lastIndex === -1) return;
// Number of spaces from beginning of line:
var spacesLength = spaces.slice(lastIndex + 1).length + 1;
detected.push(new Array(spacesLength).join(' '));
});
});
return detected;
},
/**
* @param {Node} node
* @param {Number} level
*/
_processNode(node, level) {
var that = this;
node.forEach(function(n) {
if (node.syntax === 'sass' && n.is('block')) {
that._processSassBlock(n, level);
}
// Continue only with space nodes inside {...}:
if (node.syntax !== 'sass' && level !== 0 && n.is('space')) {
that._processSpaceNode(n, level);
}
if (n.is('block') || n.is('atrulers')) level++;
that._processNode(n, level);
});
},
/**
* @param {Node} node
* @param {Number} level
*/
_processSassBlock(node, level) {
var value = this.value;
node.eachFor('space', function(whitespaceNode) {
var spaces = whitespaceNode.content;
if (spaces === '\n' || spaces === '\r\n') return;
spaces = spaces.replace(/[ \t]/g, '');
spaces += new Array(level + 2).join(value);
whitespaceNode.content = spaces;
});
},
/**
* @param {Node} node
* @param {Number} level
*/
_processSpaceNode(node, level) {
var value = this.value;
// Remove all whitespaces and tabs, leave only new lines:
var spaces = node.content.replace(/[ \t]/g, '');
if (!spaces) return;
spaces += new Array(level + 1).join(value);
node.content = spaces;
}
};
module.exports = option;