-
Notifications
You must be signed in to change notification settings - Fork 458
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New option: space-before-closing-brace
- Loading branch information
Showing
10 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
module.exports = (function() { | ||
function getLastWhitespaceNode(node) { | ||
var lastNode = node[node.length - 1]; | ||
|
||
if (typeof lastNode !== 'object' || lastNode[0] === 'block') return null; | ||
if (lastNode[0] === 's') return lastNode; | ||
|
||
return getLastWhitespaceNode(lastNode); | ||
} | ||
|
||
return { | ||
name: 'space-before-closing-brace', | ||
|
||
accepts: { | ||
number: true, | ||
string: /^[ \t\n]*$/ | ||
}, | ||
|
||
/** | ||
* Processes tree node. | ||
* @param {String} nodeType | ||
* @param {node} node | ||
* @param {Number} level | ||
*/ | ||
process: function(nodeType, node, level) { | ||
if (nodeType !== 'block' && nodeType !== 'atrulers') return; | ||
|
||
var value = this.getValue('space-before-closing-brace'); | ||
|
||
// If found block node stop at the next one for space check | ||
// For the pre-block node, find its last (the deepest) child | ||
var whitespaceNode = getLastWhitespaceNode(node); | ||
|
||
if (value.indexOf('\n') > -1) { | ||
var blockIndent = this.getValue('block-indent'); | ||
// TODO: Check that it works for '' block indent value <tg> | ||
if (blockIndent) value += new Array(level + 1).join(blockIndent); | ||
} | ||
|
||
// If it's spaces, modify this node | ||
// If it's something different from spaces, add a space node to the end | ||
|
||
if (whitespaceNode) { | ||
whitespaceNode[1] = value; | ||
} else if (value !== '') { | ||
node.push(['s', value]); | ||
} | ||
}, | ||
|
||
/** | ||
* Detects the value of an option at the tree node. | ||
* | ||
* @param {String} nodeType | ||
* @param {node} node | ||
*/ | ||
detect: function(nodeType, node) { | ||
var variants = []; | ||
|
||
// Loop through node from the end to the beginning: | ||
for (var i = node.length; i--;) { | ||
// If found block node stop for space check: | ||
if (node[i][0] !== 'block' && node[i][0] !== 'atrulers') continue; | ||
|
||
// For the block node, find its last (the deepest) child | ||
var whitespaceNode = getLastWhitespaceNode(node[i]); | ||
|
||
if (whitespaceNode) { | ||
variants.push(whitespaceNode[1]); | ||
} else { | ||
variants.push(''); | ||
} | ||
} | ||
|
||
return variants; | ||
} | ||
}; | ||
})(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
describe('options/space-before-closing-brace:', function() { | ||
beforeEach(function() { | ||
this.filename = __filename; | ||
}); | ||
|
||
it('Array value => should not change anything', function() { | ||
this.comb.configure({ 'space-before-closing-brace': ['', ' '] }); | ||
this.shouldBeEqual('test.css'); | ||
}); | ||
|
||
it('Invalid string value => should not change anything', function() { | ||
this.comb.configure({ 'space-before-closing-brace': ' nani ' }); | ||
this.shouldBeEqual('test.css'); | ||
}); | ||
|
||
it('Float number value => should not change anything', function() { | ||
this.comb.configure({ 'space-before-closing-brace': 3.5 }); | ||
this.shouldBeEqual('test.css'); | ||
}); | ||
|
||
it('Integer value => should set proper space before }', function() { | ||
this.comb.configure({ 'space-before-closing-brace': 0 }); | ||
this.shouldBeEqual('test.css', 'test.expected.css'); | ||
}); | ||
|
||
it('Valid string value (spaces only) => should set proper space before }', function() { | ||
this.comb.configure({ 'space-before-closing-brace': ' ' }); | ||
this.shouldBeEqual('test.css', 'test-2.expected.css'); | ||
}); | ||
|
||
it('Valid string value (spaces and newlines) => should set proper space before }', function() { | ||
this.comb.configure({ 'space-before-closing-brace': '\n ' }); | ||
this.shouldBeEqual('test.css', 'test-3.expected.css'); | ||
}); | ||
|
||
it('Should detect no whitespace', function() { | ||
this.shouldDetect( | ||
['space-before-closing-brace'], | ||
'a{top:0}', | ||
{ 'space-before-closing-brace': '' } | ||
); | ||
}); | ||
|
||
it('Should detect no whitespace (2 blocks)', function() { | ||
this.shouldDetect( | ||
['space-before-closing-brace'], | ||
'a{top:0} b { color: tomato; }', | ||
{ 'space-before-closing-brace': '' } | ||
); | ||
}); | ||
|
||
it('Should detect whitespace', function() { | ||
this.shouldDetect( | ||
['space-before-closing-brace'], | ||
'a { top:0 }', | ||
{ 'space-before-closing-brace': ' ' } | ||
); | ||
}); | ||
|
||
it('Should detect whitespace (2 blocks)', function() { | ||
this.shouldDetect( | ||
['space-before-closing-brace'], | ||
'a { top:0 } b{color:tomato;}', | ||
{ 'space-before-closing-brace': ' ' } | ||
); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
test/options/space-before-closing-brace/test-3.expected.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} |