-
Notifications
You must be signed in to change notification settings - Fork 457
/
Copy pathspace-between-declarations.js
140 lines (126 loc) · 3.94 KB
/
space-between-declarations.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
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
'use strict';
var gonzales = require('gonzales-pe');
module.exports = (function() {
function getDeclarationEnd(node, i) {
for (; i < node.length; i++) {
if (!node.get(i + 1) || typeof node.get(i + 1) === 'string') {
return 0;
} else if (node.get(i + 1).is('space')) {
if (node.get(i + 1).content.indexOf('\n') > -1) {
if (node.get(i + 2) && node.get(i + 2).is('declaration')) {
return i;
} else {
return 0;
}
} else if (node.get(i + 2) &&
node.get(i + 2).is('multilineComment')) {
if (node.get(i + 3) && node.get(i + 3).is('declaration')) {
return i + 2;
} else if (node.get(i + 3) && node.get(i + 3).is('space')) {
if (node.get(i + 4) &&
node.get(i + 4).is('declaration')) {
return i + 2;
} else {
return 0;
}
} else {
return 0;
}
} else if (node.get(i + 2) &&
node.get(i + 2).is('declaration')) {
return i;
}
} else if (node.get(i + 1).is('declaration')) {
return i;
} else if (node.get(i + 1).is('multilineComment')) {
if (node.get(i + 2) && node.get(i + 2).is('declaration')) {
return i + 1;
} else if (node.get(i + 2) && node.get(i + 2).is('space')) {
if (node.get(i + 3) && node.get(i + 3).is('declaration')) {
return i + 1;
}
} else {
return 0;
}
} else {
return 0;
}
}
}
return {
name: 'space-between-declarations',
runBefore: 'block-indent',
syntax: ['css', 'less', 'scss'],
accepts: {
number: true,
string: /^[ \t\n]*$/
},
/**
* Processes tree node.
*
* @param {node} ast
*/
process: function(ast) {
let value = this.value;
ast.traverseByType('declarationDelimiter', (delimiter, i, parent) => {
// Grom user's point of view "declaration" includes semicolons
// and comments placed on the same line.
// So group those things together:
var declarationEnd = getDeclarationEnd(parent, i);
if (!declarationEnd) {
return;
} else {
i = declarationEnd;
}
var nextNode = parent.get(i + 1);
if (nextNode && nextNode.is('space')) {
nextNode.content = value;
} else {
var space = gonzales.createNode({
type: 'space',
content: value
});
parent.insert(i + 1, space);
}
});
},
/**
* Detects the value of this option in ast.
* @param {Node} ast
* @return {Array?} List of detected values
*/
detect(ast) {
var detected = [];
ast.traverseByType('block', block => {
var prevDeclaration = false;
var nextDeclaration = false;
block.forEach(blockContent => {
if (blockContent.is('declaration')) {
if (prevDeclaration) {
prevDeclaration = false;
nextDeclaration = true;
} else {
prevDeclaration = true;
nextDeclaration = false;
}
} else if (prevDeclaration) {
if (blockContent.is('multilineComment')) {
// If there is comments between two declarations, then we need
// to clean up whitespace and new line characters before each
// comment keeping up the same characters after the comment.
detected.splice(-1);
} else if (blockContent.is('space')) {
detected.push(blockContent.content);
}
}
});
if (!nextDeclaration) {
// Remove the last detected space content after the property
// declaration which does not have a pair for it in a block.
detected.splice(-1);
}
});
return detected;
}
};
})();