Skip to content

Commit 535f042

Browse files
committed
Merge pull request #201 from jpbackman/handle-gonzales-errors
Don't bail out in case of parsing errors; emit compile error event to UI
2 parents 122d396 + 16eca50 commit 535f042

File tree

3 files changed

+70
-71
lines changed

3 files changed

+70
-71
lines changed

lib/modules/kss-splitter.js

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
'use strict'
1+
'use strict';
22

33
var gonzales = require('gonzales-pe'),
44
gonzo = require('gonzales-ast');
@@ -7,10 +7,8 @@ module.exports = {
77

88
/* Split string source into array of code and comment blocks */
99
pureSplitter: function(source, syntax) {
10-
1110
syntax = syntax || 'scss';
12-
var out = {},
13-
ast = gonzales.srcToAST({
11+
var ast = gonzales.srcToAST({
1412
src: source,
1513
syntax: syntax
1614
}),
@@ -20,7 +18,7 @@ module.exports = {
2018

2119
gonzo.traverse(ast, [{
2220
// Visitor for SASS and SCSS syntaxes
23-
test: function(name, nodes) {
21+
test: function(name) {
2422
return name !== 'stylesheet';
2523
},
2624
process: function(nodes) {
@@ -68,24 +66,23 @@ module.exports = {
6866
}]);
6967

7068
// push last block
71-
blocks.push(block);
69+
if (block) {
70+
blocks.push(block);
71+
}
7272
return blocks;
73-
7473
},
7574

7675
getBlocks: function(source, syntax) {
77-
7876
var blocks = this.pureSplitter(source, syntax),
7977
pair = {
8078
kss: '',
8179
code: []
8280
},
8381
firstBlock = true,
8482
pairs = [],
85-
isKssMarkupBlock = /Styleguide [0-9]+/
83+
isKssMarkupBlock = /Styleguide [0-9]+/;
8684

8785
blocks.forEach(function(block) {
88-
8986
if (block.type == 'comment') {
9087
// Check if KSS
9188
if (isKssMarkupBlock.test(block.content)) {
@@ -118,4 +115,4 @@ module.exports = {
118115
return pairs;
119116
}
120117

121-
}
118+
};

lib/modules/kss.js

+57-59
Original file line numberDiff line numberDiff line change
@@ -48,77 +48,75 @@ function sanitize(string) {
4848
}
4949

5050
function processBlock(block, options, json) {
51-
var blockPromise = Q.defer();
52-
53-
kss.parse(block.kss, options, function(err, styleguide) {
54-
var section,
55-
blockStyles;
56-
if (err) {
57-
new PluginError(PLUGIN_NAME, 'Error parsing', err);
58-
blockPromise.resolve();
59-
return false;
60-
} else {
61-
section = jsonSections(styleguide.section());
62-
63-
if (section.length > 0) {
64-
if (section.length > 1) {
65-
console.warn('Warning: KSS splitter returned more than 1 KSS block. Styleguide might not be properly generated.');
51+
return Q.Promise(function(resolve, reject) {
52+
kss.parse(block.kss, options, function(err, styleguide) {
53+
var section,
54+
blockStyles;
55+
if (err) {
56+
console.error(' error processing kss block', err);
57+
reject(err);
58+
return false;
59+
} else {
60+
section = jsonSections(styleguide.section());
61+
62+
if (section.length > 0) {
63+
if (section.length > 1) {
64+
console.warn('Warning: KSS splitter returned more than 1 KSS block. Styleguide might not be properly generated.');
65+
}
66+
blockStyles = trimLinebreaks(block.code);
67+
68+
// Add related CSS to section
69+
if (blockStyles && blockStyles !== '') {
70+
section[0].css = blockStyles;
71+
}
72+
json.sections = json.sections.concat(section);
6673
}
67-
blockStyles = trimLinebreaks(block.code);
68-
69-
// Add related CSS to section
70-
if (blockStyles && blockStyles !== '') {
71-
section[0].css = blockStyles;
72-
}
73-
json.sections = json.sections.concat(section);
74+
resolve();
7475
}
75-
blockPromise.resolve();
76-
}
76+
});
7777
});
78-
return blockPromise;
7978
}
8079

8180
function processFile(contents, syntax, options, json) {
82-
var filePromise = Q.defer(),
83-
blockPromises = [],
84-
splittedFile = kssSplitter.getBlocks(contents, syntax);
85-
86-
// Process every block in the current file
87-
splittedFile.forEach(function(block) {
88-
blockPromises.push(processBlock(block, options, json));
89-
});
90-
91-
Q.all(blockPromises).then(function() {
92-
// All blocks are processed, resolve file promise
93-
filePromise.resolve();
81+
return Q.Promise(function(resolve, reject) {
82+
try {
83+
var blockPromises = [],
84+
blocks = kssSplitter.getBlocks(contents, syntax);
85+
86+
// Process every block in the current file
87+
blocks.forEach(function(block) {
88+
blockPromises.push(processBlock(block, options, json));
89+
});
90+
} catch (err) {
91+
reject(err);
92+
}
93+
Q.all(blockPromises).then(resolve);
9494
});
95-
96-
return filePromise;
9795
}
9896

9997
module.exports = {
10098
// Parse node-kss object ( {'file.path': 'file.contents.toString('utf8'}' )
10199
parseKSS: function(files, options) {
102-
var parsePromise = Q.defer(),
103-
json = {
104-
sections: []
105-
},
106-
filePromises = [],
107-
fileKeys = Object.keys(files);
108-
109-
fileKeys.forEach(function(filePath) {
110-
var contents = files[filePath],
111-
syntax = path.extname(filePath).substring(1);
112-
filePromises.push(processFile(contents, syntax, options, json));
113-
});
114-
115-
Q.all(filePromises).then(function() {
116-
// All files are processed. Sort results and call main promise
117-
json.sections = _.sortBy(json.sections, function(section) {
118-
return section.reference;
100+
return Q.Promise(function(resolve, reject) {
101+
var json = {
102+
sections: []
103+
},
104+
filePromises = [],
105+
fileKeys = Object.keys(files);
106+
107+
fileKeys.forEach(function(filePath) {
108+
var contents = files[filePath],
109+
syntax = path.extname(filePath).substring(1);
110+
filePromises.push(processFile(contents, syntax, options, json));
119111
});
120-
parsePromise.resolve(json);
112+
113+
Q.all(filePromises).then(function() {
114+
// All files are processed. Sort results and call main promise
115+
json.sections = _.sortBy(json.sections, function(section) {
116+
return section.reference;
117+
});
118+
resolve(json);
119+
}).catch(reject);
121120
});
122-
return parsePromise.promise;
123121
}
124-
}
122+
};

lib/styleguide.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,14 @@ module.exports = function(options) {
212212
emitCompileSuccess();
213213
})
214214
.catch(function(error) {
215-
console.error('Style guide parsing failed:', error.message);
215+
console.error(error.toString());
216216
emitCompileError(error);
217217
})
218218
.finally(callback);
219+
}).catch(function(error) {
220+
console.error(error.toString());
221+
emitCompileError(error);
222+
callback();
219223
});
220224
}
221225
).on('error', console.error.bind(console));

0 commit comments

Comments
 (0)