Skip to content

Commit 51425b6

Browse files
committed
Merge pull request #15 from puzankov/always-semicolon
Option: always-semicolon
2 parents 89e46c1 + f4f4234 commit 51425b6

File tree

7 files changed

+115
-4
lines changed

7 files changed

+115
-4
lines changed

.csscomb.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"rule-indent": true,
88
"stick-brace": "\n",
99
"strip-spaces": true,
10+
"always-semicolon": true,
1011
"sort-order": [
1112
[
1213
"position",

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ Example configuration:
3535
"exclude": ["node_modules/**"],
3636
"colon-space": true,
3737
"stick-brace": true,
38-
"strip-spaces": true
38+
"strip-spaces": true,
39+
"always-semicolon": true
3940
}
4041
```
4142

lib/csscomb.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var Comb = function() {
1515
'stick-brace',
1616
'colon-space',
1717
'rule-indent',
18+
'always-semicolon',
1819
'sort-order'
1920
];
2021
this._config = {};

lib/options/always-semicolon.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
module.exports = {
2+
3+
/**
4+
* Sets handler value.
5+
*
6+
* @param {String|Boolean} value Option value
7+
* @returns {Object}
8+
*/
9+
setValue: function(value) {
10+
this._value = value === true;
11+
if (!this._value) return;
12+
return this;
13+
},
14+
15+
/**
16+
* Processes tree node.
17+
* @param {String} nodeType
18+
* @param {node} node
19+
*/
20+
process: function(nodeType, node) {
21+
if (this._value === true) {
22+
if (nodeType === 'block') {
23+
for (var i = node.length; i--;) {
24+
var nodeItem = node[i];
25+
var type = nodeItem[0];
26+
var value = nodeItem[2];
27+
28+
if (type === 'decldelim') break;
29+
30+
if (type === 'declaration') {
31+
var space = [];
32+
33+
for (var j = value.length; j--;) {
34+
if (value[j][0] !== 's' && value[j][0] !== 'comment') break;
35+
36+
space.unshift(value.splice(j)[0]);
37+
}
38+
39+
node.splice.apply(node, [i + 1, 0, ['decldelim']].concat(space));
40+
break;
41+
}
42+
}
43+
}
44+
}
45+
46+
}
47+
48+
};

test/always-semicolon.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
var Comb = require('../lib/csscomb');
2+
var assert = require('assert');
3+
4+
describe('options/always-semicolon', function() {
5+
var comb;
6+
7+
beforeEach(function() {
8+
comb = new Comb();
9+
});
10+
11+
it('Should add semicolon for last property if missing. Test 1', function() {
12+
comb.configure({ 'always-semicolon': true });
13+
assert.equal(
14+
comb.processString(
15+
'div { height: 0 }'
16+
),
17+
'div { height: 0; }'
18+
);
19+
});
20+
21+
it('Should add semicolon for last property if missing. Test 2', function() {
22+
comb.configure({ 'always-semicolon': true });
23+
assert.equal(
24+
comb.processString(
25+
'div {\nheight: 0\n}'
26+
),
27+
'div {\nheight: 0;\n}'
28+
);
29+
});
30+
31+
it('Should add semicolon for last property if missing. Test 3', function() {
32+
comb.configure({ 'always-semicolon': true });
33+
assert.equal(
34+
comb.processString(
35+
'div {height: 0}'
36+
),
37+
'div {height: 0;}'
38+
);
39+
});
40+
41+
it('Should add semicolon for last property if missing. Test 4', function() {
42+
comb.configure({ 'always-semicolon': true });
43+
assert.equal(
44+
comb.processString(
45+
'div {\nheight: 0 /* Comment */\n}'
46+
),
47+
'div {\nheight: 0; /* Comment */\n}'
48+
);
49+
});
50+
51+
it('Should add semicolon for last property if missing. Test 5', function() {
52+
comb.configure({ 'always-semicolon': true });
53+
assert.equal(
54+
comb.processString(
55+
'div {\ntop: 1px;\nheight: 0 /* 1comment */ /* 2comment */\n}'
56+
),
57+
'div {\ntop: 1px;\nheight: 0; /* 1comment */ /* 2comment */\n}'
58+
);
59+
});
60+
});

test/integral.expect.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ div
4747
{
4848
/* upline comment*/
4949
font-style: italic;
50-
border-bottom: 1px solid red
50+
border-bottom: 1px solid red; /* trololo */ /* trololo */
5151
}
5252

5353
a
5454
{
5555
top: 0;/* ololo */margin: 0;}
5656
b
5757
{
58-
top: 0/* trololo */;margin: 0}
58+
top: 0/* trololo */;margin: 0;}

test/integral.origin.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ div {
4141
div p em {
4242
/* upline comment*/
4343
font-style:italic;
44-
border-bottom:1px solid red
44+
border-bottom:1px solid red /* trololo */ /* trololo */
4545
}
4646

4747
a{

0 commit comments

Comments
 (0)