Skip to content

Commit

Permalink
New option: space-after-opening-brace
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyganch committed Jun 9, 2014
1 parent ea49b87 commit 3453030
Show file tree
Hide file tree
Showing 9 changed files with 185 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 @@ -14,6 +14,7 @@
"remove-empty-rulesets": true,
"space-after-colon": " ",
"space-after-combinator": " ",
"space-after-opening-brace": "\n",
"space-before-colon": "",
"space-before-combinator": " ",
"space-before-opening-brace": "\n",
Expand Down
30 changes: 30 additions & 0 deletions doc/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,36 @@ p >
a { color: panda; }
```

## space-after-opening-brace

Set space after `{`.

Acceptable values:

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

Example: `{ 'space-after-opening-brace': 1 }`

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

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

Example: `{ 'space-after-opening-brace': '\n' }`

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

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

## space-before-colon

Set space before `:` in declarations.
Expand Down
1 change: 1 addition & 0 deletions lib/csscomb.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var OPTIONS = [
'space-before-colon',
'space-after-colon',
'space-before-opening-brace',
'space-after-opening-brace',
'sort-order',
'block-indent',
'unitless-zero',
Expand Down
43 changes: 43 additions & 0 deletions lib/options/space-after-opening-brace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module.exports = {
name: 'space-after-opening-brace',

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

/**
* Processes tree node.
*
* @param {String} nodeType
* @param {node} node
*/
process: function(nodeType, node) {
// If found block node stop at the next one for space check
if (nodeType !== 'block' && nodeType !== 'atrulers') return;

var value = this.getValue('space-after-opening-brace');

if (node[0][0] === 's') {
node[0][1] = value;
} else if (value !== '') {
node.unshift(['s', value]);
}
},

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

if (node[0][0] === 's') {
return node[0][1];
} else {
return '';
}
}
};
84 changes: 84 additions & 0 deletions test/options/space-after-opening-brace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
describe('options/space-after-opening-brace:', function() {
beforeEach(function() {
this.filename = __filename;
});

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

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

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

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

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

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

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

it('Should detect whitespace', function() {
this.shouldDetect(
['space-after-opening-brace'],
'a{\n\ttop:0}',
{ 'space-after-opening-brace': '\n\t' }
);
});

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

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

it('Should detect no whitespace (3 blocks)', function() {
this.shouldDetect(
['space-after-opening-brace'],
'a{top:0} b { left: 0 } c{\n\tright:0}',
{ 'space-after-opening-brace': '' }
);
});

it('Should detect whitespace (3 blocks)', function() {
this.shouldDetect(
['space-after-opening-brace'],
'a{\ntop:0} b{\nleft:0} c{\n right:0}',
{ 'space-after-opening-brace': '\n' }
);
});
});

5 changes: 5 additions & 0 deletions test/options/space-after-opening-brace/test-2.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
a{ top: 0}
a { top: 0 }

@media print{ a{ top: 0}}
@media print { a { top: 0 } }
11 changes: 11 additions & 0 deletions test/options/space-after-opening-brace/test-3.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
a{
top: 0}
a {
top: 0 }

@media print{
a{
top: 0}}
@media print {
a {
top: 0 } }
5 changes: 5 additions & 0 deletions test/options/space-after-opening-brace/test.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
a{top: 0}
a { top: 0 }

@media print{a{top: 0}}
@media print { a { top: 0 } }
5 changes: 5 additions & 0 deletions test/options/space-after-opening-brace/test.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
a{top: 0}
a {top: 0 }

@media print{a{top: 0}}
@media print {a {top: 0 } }

0 comments on commit 3453030

Please sign in to comment.