Skip to content

Commit a246c6b

Browse files
committed
Refactored the detection method
1 parent 1ac2aa1 commit a246c6b

17 files changed

Lines changed: 57 additions & 59 deletions

lib/cli.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,24 @@ if (!program.args.length) {
2424
}
2525

2626
var configPath = program.config || (process.cwd() + '/.csscomb.json');
27+
var comb = new Comb();
2728

2829
if (program.detect) {
29-
var comb = new Comb();
30-
comb.detect();
31-
console.log(JSON.stringify(comb.processFile(program.args[0]), false, 4));
30+
console.log(JSON.stringify(comb.detectInFile(program.args[0]), false, 4));
3231
process.exit(0);
3332
}
3433

3534
if (fs.existsSync(configPath)) {
36-
var comb;
3735
var config;
3836
if (configPath.match(/\.css$/)) {
39-
comb = new Comb();
40-
comb.detect();
41-
config = comb.processFile(configPath);
37+
config = comb.detectInFile(configPath);
4238
} else {
4339
config = require(configPath);
4440
}
4541

4642
if (config.template) {
4743
if (fs.existsSync(config.template)) {
48-
comb = new Comb();
49-
comb.detect();
50-
var templateConfig = comb.processFile(config.template);
44+
var templateConfig = comb.detectInFile(config.template);
5145
for (var attrname in templateConfig) {
5246
if (!config[attrname]) {
5347
config[attrname] = templateConfig[attrname];
@@ -64,7 +58,6 @@ if (fs.existsSync(configPath)) {
6458
config.verbose = program.verbose === true || config.verbose;
6559
config.lint = program.lint;
6660

67-
comb = new Comb();
6861
comb.configure(config);
6962

7063
vow.all(program.args.map(comb.processPath.bind(comb)))

lib/csscomb.js

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,14 @@ Comb.prototype = {
6868
},
6969

7070
/**
71-
* Sets up the detection environment.
71+
* Detects the options in the given string
72+
*
73+
* @param {String} text Stylesheet
74+
* @param {Array} options List of options to detect
75+
* @returns {Object}
7276
*/
73-
detect: function(options) {
77+
detectInString: function(text, options) {
78+
var result;
7479
this._detect = true;
7580
this._detected = {};
7681
this._handlers = [];
@@ -87,8 +92,21 @@ Comb.prototype = {
8792
}
8893
}, this);
8994

90-
this.processed = 0;
91-
this.changed = 0;
95+
result = this.processString(text);
96+
this._detect = false;
97+
return result;
98+
},
99+
100+
/**
101+
* Detects the options in the given file
102+
*
103+
* @param {String} path Path to the stylesheet
104+
* @param {Array} options List of options to detect
105+
* @returns {Object}
106+
*/
107+
detectInFile: function(path, options) {
108+
var stylesheet = fs.readFileSync(path, 'utf8');
109+
return this.detectInString(stylesheet, options);
92110
},
93111

94112
/**
@@ -98,7 +116,6 @@ Comb.prototype = {
98116
* @returns {Array}
99117
*/
100118
processTree: function(tree) {
101-
102119
// We walk across complete tree for each handler,
103120
// because we need strictly maintain order in which handlers work,
104121
// despite fact that handlers work on different level of the tree.
@@ -170,10 +187,6 @@ Comb.prototype = {
170187
*/
171188
processFile: function(path) {
172189
var _this = this;
173-
if (_this._detect) {
174-
var syntax = path.split('.').pop();
175-
return _this.processString(fs.readFileSync(path, 'utf8'), syntax, path);
176-
}
177190
if (this._shouldProcessFile(path)) {
178191
return vfs.read(path, 'utf8').then(function(data) {
179192
var syntax = path.split('.').pop();
@@ -292,31 +305,38 @@ Comb.prototype = {
292305
_getDetectedOptions: function(detected) {
293306
var options = {};
294307
Object.keys(detected).forEach(function(option) {
308+
// List of all the detected variants from the stylesheet for the given option:
295309
var values = detected[option];
296310
var i;
297311
if (values.length) {
298312
if (values.length === 1) {
299313
options[option] = values[0];
300314
} else {
315+
// If there are more than one value for the option, find the most popular one;
316+
// `variants` would be populated with the popularity for different values.
301317
var variants = {};
302-
var best_guess = null;
318+
var bestGuess = null;
303319
var maximum = 0;
304320
for (i = values.length; i--;) {
305-
if (variants[values[i]]) {
306-
variants[values[i]]++;
321+
var currentValue = values[i];
322+
// Count the current value:
323+
if (variants[currentValue]) {
324+
variants[currentValue]++;
307325
} else {
308-
variants[values[i]] = 1;
326+
variants[currentValue] = 1;
309327
}
310-
if (variants[values[i]] >= maximum) {
311-
maximum = variants[values[i]];
312-
best_guess = values[i];
328+
// If the current variant is the most popular one, treat it as the best guess:
329+
if (variants[currentValue] >= maximum) {
330+
maximum = variants[currentValue];
331+
bestGuess = currentValue;
313332
}
314333
}
315-
if (best_guess !== null) {
316-
options[option] = best_guess;
334+
if (bestGuess !== null) {
335+
options[option] = bestGuess;
317336
}
318337
}
319338
} else {
339+
// If there are no values for the option, check if there is a default one:
320340
for (i = this._handlers.length; i--;) {
321341
if (this._handlers[i]._name === option && this._handlers[i]._detectDefault !== undefined) {
322342
options[option] = this._handlers[i]._detectDefault;

test/always-semicolon.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,8 @@ describe('options/always-semicolon', function() {
6060

6161
// Helper to check the detection
6262
function should_detect(options, a, b) {
63-
comb.detect(options);
6463
assert.equal(
65-
JSON.stringify(comb.processString(a)),
64+
JSON.stringify(comb.detectInString(a, options)),
6665
JSON.stringify(b)
6766
);
6867
}

test/block-indent.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@ describe('options/block-indent', function() {
4444

4545
// Helper to check the detection
4646
function should_detect(options, a, b) {
47-
comb.detect(options);
4847
assert.equal(
49-
JSON.stringify(comb.processString(a)),
48+
JSON.stringify(comb.detectInString(a, options)),
5049
JSON.stringify(b)
5150
);
5251
}

test/colon-space.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,8 @@ describe('options/colon-space', function() {
149149

150150
// Helper to check the detection
151151
function should_detect(options, a, b) {
152-
comb.detect(options);
153152
assert.equal(
154-
JSON.stringify(comb.processString(a)),
153+
JSON.stringify(comb.detectInString(a, options)),
155154
JSON.stringify(b)
156155
);
157156
}

test/color-case.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,8 @@ describe('options/color-case', function() {
5252

5353
// Helper to check the detection
5454
function should_detect(options, a, b) {
55-
comb.detect(options);
5655
assert.equal(
57-
JSON.stringify(comb.processString(a)),
56+
JSON.stringify(comb.detectInString(a, options)),
5857
JSON.stringify(b)
5958
);
6059
}

test/color-shorthand.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ describe('options/color-shorthand', function() {
4141

4242
// Helper to check the detection
4343
function should_detect(options, a, b) {
44-
comb.detect(options);
4544
assert.equal(
46-
JSON.stringify(comb.processString(a)),
45+
JSON.stringify(comb.detectInString(a, options)),
4746
JSON.stringify(b)
4847
);
4948
}

test/combinator-space.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,8 @@ describe('options/combinator-space', function() {
157157

158158
// Helper to check the detection
159159
function should_detect(options, a, b) {
160-
comb.detect(options);
161160
assert.equal(
162-
JSON.stringify(comb.processString(a)),
161+
JSON.stringify(comb.detectInString(a, options)),
163162
JSON.stringify(b)
164163
);
165164
}

test/element-case.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,8 @@ describe('options/element-case', function() {
5050

5151
// Helper to check the detection
5252
function should_detect(options, a, b) {
53-
comb.detect(options);
5453
assert.equal(
55-
JSON.stringify(comb.processString(a)),
54+
JSON.stringify(comb.detectInString(a, options)),
5655
JSON.stringify(b)
5756
);
5857
}

test/eof-newline.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ describe('options/eof-newline', function() {
3434

3535
// Helper to check the detection
3636
function should_detect(options, a, b) {
37-
comb.detect(options);
3837
assert.equal(
39-
JSON.stringify(comb.processString(a)),
38+
JSON.stringify(comb.detectInString(a, options)),
4039
JSON.stringify(b)
4140
);
4241
}

0 commit comments

Comments
 (0)