Skip to content

Commit

Permalink
New option: space-after-selector-delimiter
Browse files Browse the repository at this point in the history
  • Loading branch information
vecmezoni authored and tonyganch committed Jun 9, 2014
1 parent 8d78ac9 commit 96afced
Show file tree
Hide file tree
Showing 9 changed files with 242 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/csscomb.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"space-after-colon": " ",
"space-after-combinator": " ",
"space-after-opening-brace": "\n",
"space-after-selector-delimiter": "\n",
"space-before-closing-brace": "\n",
"space-before-colon": "",
"space-before-combinator": " ",
Expand Down
76 changes: 76 additions & 0 deletions doc/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,44 @@ a{
color: panda;}
```

## space-after-selector-delimiter

Set space after selector delimiter.

Acceptable values:

* `{Number}` — number of whitespaces;
* `{String}` — string with whitespaces, tabs or line breaks.

Example: `{ 'space-after-selector-delimiter': 1 }`

```scss
// Before:
a,b{
color: panda;
}

// After:
a, b {
color: panda;
}
```

Example: `{ 'space-before-selector-delimiter': '\n' }`

```scss
// Before:
a, b{
color: panda;
}

// After:
a,
b{
color: panda;
}
```

## space-before-closing-brace

Set space before `}`.
Expand Down Expand Up @@ -575,6 +613,44 @@ a
}
```

## space-before-selector-delimiter

Set space before selector delimiter.

Acceptable values:

* `{Number}` — number of whitespaces;
* `{String}` — string with whitespaces, tabs or line breaks.

Example: `{ 'space-before-selector-delimiter': 0 }`

```scss
// Before:
a , b{
color: panda;
}

// After:
a, b {
color: panda;
}
```

Example: `{ 'space-before-selector-delimiter': '\n' }`

```scss
// Before:
a, b{
color: panda;
}

// After:
a
,b{
color: panda;
}
```

## strip-spaces

Whether to trim trailing spaces.
Expand Down
1 change: 1 addition & 0 deletions lib/csscomb.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var OPTIONS = [
'space-before-opening-brace',
'space-after-opening-brace',
'space-before-selector-delimiter',
'space-after-selector-delimiter',
'block-indent',
'sort-order',
'space-before-closing-brace',
Expand Down
54 changes: 54 additions & 0 deletions lib/options/space-after-selector-delimiter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module.exports = {
name: 'space-after-selector-delimiter',

accepts: {
number: true,
string: /^[ \t\n]*$/
},

/**
* Processes tree node.
*
* @param {String} nodeType
* @param {node} node
*/
process: function(nodeType, node) {
if (nodeType !== 'selector') return;

var value = this.getValue('space-after-selector-delimiter');

for (var i = node.length; i--;) {
if (node[i][0] === 'delim') {
if (node[i + 1][1][0] === 's') {
node[i + 1][1][1] = value;
} else {
node[i + 1].splice(1, 0, ['s', value]);
}
}
}
},

/**
* Detects the value of an option at the tree node.
*
* @param {String} nodeType
* @param {node} node
*/
detect: function(nodeType, node) {
if (nodeType !== 'selector') return;

var variants = [];

for (var i = node.length; i--;) {
if (node[i][0] !== 'delim') continue;

if (node[i + 1][1][0] === 's') {
variants.push(node[i + 1][1][1]);
} else {
variants.push('');
}
}

return variants;
}
};
83 changes: 83 additions & 0 deletions test/options/space-after-selector-delimiter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
describe('options/space-after-selector-delimiter:', function() {
beforeEach(function() {
this.filename = __filename;
});

it('Array value => should not change anything', function() {
this.comb.configure({ 'space-after-selector-delimiter': ['', ' '] });
this.shouldBeEqual('test.css');
});

it('Invalid string value => should not change anything', function() {
this.comb.configure({ 'space-after-selector-delimiter': ' nani ' });
this.shouldBeEqual('test.css');
});

it('Float number value => should not change anything', function() {
this.comb.configure({ 'space-after-selector-delimiter': 3.5 });
this.shouldBeEqual('test.css');
});

it('Integer value => should set proper space after selector delimiter', function() {
this.comb.configure({ 'space-after-selector-delimiter': 0 });
this.shouldBeEqual('test.css', 'test.expected.css');
});

it('Valid string value (spaces only) => should set proper space after selector delimiter', function() {
this.comb.configure({ 'space-after-selector-delimiter': ' ' });
this.shouldBeEqual('test.css', 'test-2.expected.css');
});

it('Valid string value (spaces and newlines) => should set proper space after selector delimiter', function() {
this.comb.configure({ 'space-after-selector-delimiter': '\n ' });
this.shouldBeEqual('test.css', 'test-3.expected.css');
});

it('Should detect no whitespace', function() {
this.shouldDetect(
['space-after-selector-delimiter'],
'a,b{top:0}',
{ 'space-after-selector-delimiter': '' }
);
});

it('Should detect whitespace', function() {
this.shouldDetect(
['space-after-selector-delimiter'],
'a, \n b {top:0}',
{ 'space-after-selector-delimiter': ' \n ' }
);
});

it('Should detect no whitespace (2 blocks)', function() {
this.shouldDetect(
['space-after-selector-delimiter'],
'a,b{top:0} a, b{left:0}',
{ 'space-after-selector-delimiter': '' }
);
});

it('Should detect whitespace (2 blocks)', function() {
this.shouldDetect(
['space-after-selector-delimiter'],
'a, b {top:0} b,a{left:0}',
{ 'space-after-selector-delimiter': ' ' }
);
});

it('Should detect no whitespace (3 blocks)', function() {
this.shouldDetect(
['space-after-selector-delimiter'],
'a, b{top:0} b,c{left:0} c,d{right:0}',
{ 'space-after-selector-delimiter': '' }
);
});

it('Should detect whitespace (3 blocks)', function() {
this.shouldDetect(
['space-after-selector-delimiter'],
'a,b{top:0} b, c{left:0} c, sd{right:0}',
{ 'space-after-selector-delimiter': ' ' }
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
a, b { color: red }
a, b { color: red }
a , b { color: red }
a, b { color: red }
a+b, c>d, e{ color: red }
11 changes: 11 additions & 0 deletions test/options/space-after-selector-delimiter/test-3.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
a,
b { color: red }
a,
b { color: red }
a ,
b { color: red }
a,
b { color: red }
a+b,
c>d,
e{ color: red }
6 changes: 6 additions & 0 deletions test/options/space-after-selector-delimiter/test.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
a,b { color: red }
a, b { color: red }
a ,b { color: red }
a,
b { color: red }
a+b,c>d,e{ color: red }
5 changes: 5 additions & 0 deletions test/options/space-after-selector-delimiter/test.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
a,b { color: red }
a,b { color: red }
a ,b { color: red }
a,b { color: red }
a+b,c>d,e{ color: red }

0 comments on commit 96afced

Please sign in to comment.