-
Notifications
You must be signed in to change notification settings - Fork 450
Expand file tree
/
Copy pathspace-between-declarations.js
More file actions
97 lines (88 loc) · 2.61 KB
/
space-between-declarations.js
File metadata and controls
97 lines (88 loc) · 2.61 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
'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);
}
});
}
};
})();