-
Notifications
You must be signed in to change notification settings - Fork 450
Expand file tree
/
Copy pathstrip-spaces.js
More file actions
66 lines (55 loc) · 1.42 KB
/
strip-spaces.js
File metadata and controls
66 lines (55 loc) · 1.42 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
'use strict';
module.exports = (function() {
/**
* Trim trailing spaces on each line.
* @private
* @param {String} string Spaceful string
* @returns {String}
*/
function trim(string) {
return string.replace(/[ \t]+\n/g, '\n');
}
return {
name: 'strip-spaces',
syntax: ['css', 'less', 'sass', 'scss'],
accepts: {
boolean: [true]
},
/**
* Processes tree node.
* @param {node} ast
*/
process: function(ast) {
var lastChild = ast.last();
if (lastChild.is('space')) {
lastChild.content = trim(lastChild.content)
.replace(/[ \t]+$/, '')
.replace(/[\n]+/g, '\n');
}
ast.traverseByType('space', function(space) {
space.content = trim(space.content);
});
},
detectDefault: true,
/**
* Detects the value of an option at the tree node.
* This option is treated as `true` by default, but any trailing
* space would invalidate it.
*
* @param {node} ast
*/
detect: function(ast) {
let detected = [];
var lastChild = ast.last();
if (lastChild.is('space') &&
lastChild.content !== '\n' &&
lastChild.content.match(/^[ \n\t]+$/)) {
detected.push(false);
}
ast.traverseByType('space', function(space) {
if (space.content.match(/[ \t]\n/)) detected.push(false);
});
return detected;
}
};
})();