Skip to content

Commit

Permalink
Keep configured options and handlers separate
Browse files Browse the repository at this point in the history
Add method `Comb#getValue(optionName)` to get option's value for a particular
class instance.
Use this new method to get option's value inside options' `process()` methods.
  • Loading branch information
tonyganch committed Jun 9, 2014
1 parent d9c804c commit 8e75dd5
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 44 deletions.
33 changes: 19 additions & 14 deletions lib/csscomb.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ var vfs = require('vow-fs');
* @name Comb
*/
var Comb = function(config) {

var _this = this;
// List of file paths that should be excluded from processing:
var exclude;
// List of configured options:
// List of configured options with values:
var configuredOptions;
// List of handlers:
var handlers;
// Whether lint mode is on:
var lint;
Expand All @@ -35,13 +37,9 @@ var Comb = function(config) {
*/
function addHandler(optionName, value) {
var option = require('./options/' + optionName);
handlers.push({
name: option.name,
process: option.process,
value: option.setValue ?
option.setValue(value) :
setValue(option.accepts, value)
});
value = option.setValue ? option.setValue(value) : setValue(option.accepts, value);
handlers.push(option.process);
configuredOptions[option.name] = value;
}

/**
Expand Down Expand Up @@ -148,8 +146,7 @@ var Comb = function(config) {
if (!Array.isArray(node)) return;

var nodeType = node.shift();
// TODO: Pass all parameters as one object? <tg>
handler.process(handler.value, nodeType, node, level);
handler.call(_this, nodeType, node, level);
node.unshift(nodeType);

if (nodeType === 'atrulers' || nodeType === 'block') level++;
Expand All @@ -171,6 +168,7 @@ var Comb = function(config) {
*/
this.configure = function configure(config) {
handlers = [];
configuredOptions = {};
verbose = config.verbose;
lint = config.lint;
exclude = (config.exclude || []).map(function(pattern) {
Expand All @@ -192,14 +190,23 @@ var Comb = function(config) {
return this;
};

/**
* Gets option's value.
*
* @param {String} optionName
* @returns {String|Boolean|undefined}
*/
this.getValue = function getValue(optionName) {
return configuredOptions[optionName];
};

/**
* Processes directory or file.
*
* @param {String} path
* @returns {Promise}
*/
this.processPath = function processPath(path) {
var _this = this;
path = path.replace(/\/$/, '');

return vfs.exists(path).then(function(exists) {
Expand All @@ -224,7 +231,6 @@ var Comb = function(config) {
* @returns {Promise}
*/
this.processDirectory = function processDirectory(path) {
var _this = this;
return vfs.listDir(path).then(function(filenames) {
return vow.all(filenames.map(function(filename) {
var fullname = path + '/' + filename;
Expand All @@ -248,7 +254,6 @@ var Comb = function(config) {
* @returns {Promise}
*/
this.processFile = function processFile(path) {
var _this = this;
if (!shouldProcessFile(path)) return;
return vfs.read(path, 'utf8').then(function(data) {
var syntax = path.split('.').pop();
Expand Down
3 changes: 1 addition & 2 deletions lib/options/always-semicolon.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ module.exports = {

/**
* Processes tree node.
* @param {Boolean} value
* @param {String} nodeType
* @param {node} node
*/
process: function(value, nodeType, node) {
process: function(nodeType, node) {
if (nodeType === 'block') {
for (var i = node.length; i--;) {
var currentNode = node[i];
Expand Down
4 changes: 2 additions & 2 deletions lib/options/color-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ module.exports = {

/**
* Processes tree node.
* @param {String} value
* @param {String} nodeType
* @param {node} node
*/
process: function(value, nodeType, node) {
process: function(nodeType, node) {
var value = this.getValue('color-case');
if (nodeType === 'vhash') {
if (value === 'lower') {
node[0] = node[0].toLowerCase();
Expand Down
5 changes: 2 additions & 3 deletions lib/options/color-shorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ module.exports = {

/**
* Processes tree node.
* @param {Boolean} value
* @param {String} nodeType
* @param {node} node
*/
process: function(value, nodeType, node) {
process: function(nodeType, node) {
if (nodeType === 'vhash') {
if (value) {
if (this.getValue('color-shorthand')) {
node[0] = node[0].replace(/(\w)\1(\w)\2(\w)\3/i, '$1$2$3');
} else {
node[0] = node[0].replace(/^(\w)(\w)(\w)$/, '$1$1$2$2$3$3');
Expand Down
7 changes: 3 additions & 4 deletions lib/options/element-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ module.exports = {

/**
* Processes tree node.
* @param {String} value
* @param {String} nodeType
* @param {node} node
*/
process: function(value, nodeType, node) {
process: function(nodeType, node) {
if (nodeType === 'simpleselector') {
for (var i = node.length; i--;) {
var nodeItem = node[i];
if (nodeItem[0] === 'ident') {
if (value === 'lower') {
if (this.getValue('element-case') === 'lower') {
nodeItem[1] = nodeItem[1].toLowerCase();
} else if (value === 'upper') {
} else {
nodeItem[1] = nodeItem[1].toUpperCase();
}
}
Expand Down
5 changes: 2 additions & 3 deletions lib/options/eof-newline.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@ module.exports = {

/**
* Processes tree node.
* @param {Boolean} value
* @param {String} nodeType
* @param {node} node
*/
process: function(value, nodeType, node) {
process: function(nodeType, node) {
if (nodeType === 'stylesheet') {
var lastChild = node[node.length - 1];
if (lastChild[0] !== 's') {
lastChild = ['s', ''];
node.push(lastChild);
}
lastChild[1] = lastChild[1].replace(/\n$/, '');
if (value) lastChild[1] += '\n';
if (this.getValue('eof-newline')) lastChild[1] += '\n';
}
},

Expand Down
5 changes: 2 additions & 3 deletions lib/options/leading-zero.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ module.exports = {

/**
* Processes tree node.
* @param {Boolean} value
* @param {String} nodeType
* @param {node} node
*/
process: function(value, nodeType, node) {
process: function(nodeType, node) {
if (nodeType === 'number') {
if (value) {
if (this.getValue('leading-zero')) {
if (node[0][0] === '.')
node[0] = '0' + node[0];
} else {
Expand Down
4 changes: 2 additions & 2 deletions lib/options/quotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ module.exports = {

/**
* Processes tree node.
* @param {String} value
* @param {String} nodeType
* @param {node} node
*/
process: function(value, nodeType, node) {
process: function(nodeType, node) {
var value = this.getValue('quotes');
if (nodeType === 'string') {
if (node[0][0] === '"' && value === 'single') {
node[0] = node[0]
Expand Down
3 changes: 1 addition & 2 deletions lib/options/remove-empty-rulesets.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,10 @@ module.exports = (function() {
/**
* Remove rulesets with no declarations.
*
* @param {Boolean} value
* @param {String} nodeType
* @param {Array} nodeContent
*/
process: function(value, nodeType, nodeContent) {
process: function(nodeType, nodeContent) {
if (nodeType === 'stylesheet') {
processStylesheetContent(nodeContent);
}
Expand Down
5 changes: 2 additions & 3 deletions lib/options/sort-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ module.exports = {

/**
* Processes tree node.
* @param {Array} value
* @param {String} nodeType
* @param {node} node
*/
process: function(value, nodeType, node) {
process: function(nodeType, node) {
// Types of nodes that can be sorted:
var NODES = ['atruleb', 'atruler', 'atrules', 'commentML', 'commentSL',
'declaration', 's', 'include'];
Expand All @@ -42,7 +41,7 @@ module.exports = {

var currentNode;
// Sort order of properties:
var order = value;
var order = this.getValue('sort-order');
// List of declarations that should be sorted:
var sorted = [];
// list of nodes that should be removed from parent node:
Expand Down
3 changes: 1 addition & 2 deletions lib/options/strip-spaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ module.exports = (function() {

/**
* Processes tree node.
* @param {Boolean} value
* @param {String} nodeType
* @param {node} node
*/
process: function(value, nodeType, node) {
process: function(nodeType, node) {
if (nodeType === 's') {
node[0] = trim(node[0]);
}
Expand Down
3 changes: 1 addition & 2 deletions lib/options/unitless-zero.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ module.exports = {

/**
* Processes tree node.
* @param {Boolean} value
* @param {String} nodeType
* @param {node} node
*/
process: function(value, nodeType, node) {
process: function(nodeType, node) {
if (nodeType === 'value' || nodeType === 'braces') {
node.forEach(function(child, index) {
if (
Expand Down
3 changes: 1 addition & 2 deletions lib/options/vendor-prefix-align.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,10 @@ module.exports = (function() {
/**
* Processes tree node.
*
* @param {Boolean} value
* @param {String} nodeType
* @param {node} node
*/
process: function(value, nodeType, node) {
process: function(nodeType, node) {
if (nodeType !== 'block') return;
oneline = true;

Expand Down

0 comments on commit 8e75dd5

Please sign in to comment.