Skip to content

Commit 538fefa

Browse files
committed
Merge pull request #79 from csscomb/eof-newline
Option: eof-newline (close #50)
2 parents d9d55cf + 062adfe commit 538fefa

7 files changed

Lines changed: 116 additions & 12 deletions

File tree

.csscomb.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"color-shorthand": true,
1111
"combinator-space": true,
1212
"element-case": "lower",
13+
"eof-newline": true,
1314
"leading-zero": false,
1415
"rule-indent": true,
1516
"stick-brace": "\n",

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Example configuration:
4848
"color-case": "lower",
4949
"color-shorthand": true,
5050
"element-case": "lower",
51+
"eof-newline": true,
5152
"leading-zero": false,
5253
"rule-indent": true,
5354
"stick-brace": true,
@@ -238,6 +239,18 @@ li > a { color: red }
238239
LI > A { color: red }
239240
```
240241
242+
### eof-newline
243+
244+
Available values: `{Boolean}` `true` or `false`
245+
246+
Example: `{ "eof-newline": true }`
247+
248+
`a { color: red }` → `a { color: red }\n`
249+
250+
Example: `{ "eof-newline": false }`
251+
252+
`a { color: red }\n` → `a { color: red }`
253+
241254
### leading-zero
242255
243256
Available values: `{Boolean}` `true` or `false`
@@ -341,9 +354,9 @@ Available value: `{Boolean}` true
341354
342355
Example: `{ "strip-spaces": true }`
343356
344-
Before: `a { color: red } \nb { font-weight: normal }\n\n\n`
357+
`a { color: red } \n \n \n` → `a { color: red }\n`
345358
346-
After: `a { color: red }\nb { font-weight: normal }\n`
359+
`a { color: red }\t` → `a { color: red }`
347360
348361
### unitless-zero
349362

lib/csscomb.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var Comb = function() {
1717
'element-case',
1818
'leading-zero',
1919
'strip-spaces',
20+
'eof-newline',
2021
'stick-brace',
2122
'colon-space',
2223
'combinator-space',

lib/options/eof-newline.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module.exports = {
2+
3+
/**
4+
* Sets handler value.
5+
*
6+
* @param {Boolean} value Option value
7+
* @returns {Object|undefined}
8+
*/
9+
setValue: function(value) {
10+
if (value === true || value === false) {
11+
this._value = value;
12+
return this;
13+
}
14+
},
15+
16+
/**
17+
* Processes tree node.
18+
* @param {String} nodeType
19+
* @param {node} node
20+
*/
21+
process: function(nodeType, node) {
22+
if (nodeType === 'stylesheet') {
23+
var lastChild = node[node.length - 1];
24+
if (lastChild[0] !== 's') {
25+
lastChild = ['s', ''];
26+
node.push(lastChild);
27+
}
28+
lastChild[1] = lastChild[1].replace(/\n$/, '');
29+
if (this._value) lastChild[1] += '\n';
30+
}
31+
}
32+
33+
};

lib/options/strip-spaces.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,26 @@ module.exports = {
1919
*/
2020
process: function(nodeType, node) {
2121
if (nodeType === 's') {
22-
node[0] = node[0].replace(/[ \t]+\n/g, '\n');
22+
node[0] = this._trim(node[0]);
2323
}
24-
2524
if (nodeType === 'stylesheet') {
26-
if (node[node.length - 1][0] === 's') node.pop();
27-
node.push(['s', '\n']);
25+
var lastChild = node[node.length - 1];
26+
if (lastChild[0] === 's') {
27+
lastChild[1] = this._trim(lastChild[1])
28+
.replace(/[ \t]+$/, '')
29+
.replace(/[\n]+/g, '\n');
30+
}
2831
}
32+
},
33+
34+
/**
35+
* Trim trailing spaces on each line.
36+
* @private
37+
* @param {String} s Spaceful string
38+
* @returns {String}
39+
*/
40+
_trim: function(s) {
41+
return s.replace(/[ \t]+\n/g, '\n');
2942
}
3043

3144
};

test/eof-newline.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var Comb = require('../lib/csscomb');
2+
var assert = require('assert');
3+
4+
describe('options/eof-newline', function() {
5+
var comb;
6+
beforeEach(function() {
7+
comb = new Comb();
8+
});
9+
it('Invalid value should not change trim trailing brac', function() {
10+
comb.configure({ 'eof-newline': 'foobar' });
11+
assert.equal(
12+
comb.processString('a { color: red } \n'),
13+
'a { color: red } \n'
14+
);
15+
});
16+
it('Boolean true value should insert line-break at eof', function() {
17+
comb.configure({ 'eof-newline': true });
18+
assert.equal(
19+
comb.processString(
20+
'a {color:red} '
21+
),
22+
'a {color:red} \n'
23+
);
24+
});
25+
it('Boolean false value should remove line-break from eof', function() {
26+
comb.configure({ 'eof-newline': false });
27+
assert.equal(
28+
comb.processString(
29+
'a {color:red} \n'
30+
),
31+
'a {color:red} '
32+
);
33+
});
34+
});

test/strip-spaces.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,33 @@ describe('options/strip-space', function() {
66
beforeEach(function() {
77
comb = new Comb();
88
});
9-
it('Invalid value should not change trim trailing brace', function() {
9+
it('Invalid value should not trim trailing spaces', function() {
1010
comb.configure({ 'strip-spaces': 'foobar' });
1111
assert.equal(
12-
comb.processString('a { color: red }'),
13-
'a { color: red }'
12+
comb.processString('a { color: red } \n'),
13+
'a { color: red } \n'
1414
);
1515
});
16-
it('True Boolean value should trim all trailing spaces', function() {
16+
it('Boolean true value should trim all trailing spaces', function() {
1717
comb.configure({ 'strip-spaces': true });
1818
assert.equal(
1919
comb.processString(
2020
'a { color: red } \n' +
21-
'a{color:red}\t /* foobar */ \n' +
22-
'a {color:red}'
21+
'a{color:red}\t /* foobar */\t \n' +
22+
'a {color:red} \n \n'
2323
),
2424
'a { color: red }\n' +
2525
'a{color:red}\t /* foobar */\n' +
2626
'a {color:red}\n'
2727
);
2828
});
29+
it('Boolean true value should trim trailing spaces at eof', function() {
30+
comb.configure({ 'strip-spaces': true });
31+
assert.equal(
32+
comb.processString(
33+
'a {color:red} '
34+
),
35+
'a {color:red}'
36+
);
37+
});
2938
});

0 commit comments

Comments
 (0)