Skip to content

Commit d14dd1e

Browse files
author
Sergii.Kliuchnyk
committed
added more checks
1 parent 58fef76 commit d14dd1e

File tree

4 files changed

+49
-11
lines changed

4 files changed

+49
-11
lines changed

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 Sergii Kliuchnyk
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

conv.js

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ var tplAnnotations = /\/\*+\s*@jsx\-tpl\s+([\w\-]+)\s*\*\//g,
1616
function convert(js, html, callback) {
1717
parseHtml(html, function (err, body) {
1818
if (err) {
19-
callback(err);
20-
return;
19+
return callback(err);
2120
}
2221

2322
var templates = {};
@@ -35,7 +34,7 @@ function convert(js, html, callback) {
3534
domUtils.removeElement(node);
3635
});
3736

38-
var _return = search(renderReturnSelector, js)[0],
37+
var _return = search(renderReturnSelector, js),
3938
_props = search(renderReturnSelector + ' > obj > prop', js),
4039
props = {};
4140

@@ -51,14 +50,33 @@ function convert(js, html, callback) {
5150
props[name] = value;
5251
});
5352

54-
var template = clearAttrQuotes(domUtils.getOuterHTML(body))
55-
.replace(renderAnnotations, function (x, name) {
56-
return props[name];
57-
});
53+
var template = clearAttrQuotes(domUtils.getOuterHTML(body));
54+
55+
try {
56+
template = template
57+
.replace(renderAnnotations, function (x, name) {
58+
if (!props.hasOwnProperty(name)) {
59+
throw new Error('Template "' + name + '" not exists');
60+
}
61+
return props[name];
62+
})
63+
;
64+
}
65+
catch (e) {
66+
return callback(e);
67+
}
5868

59-
js = insert(js, _return.start, _return.end, format('return (%s);', template));
69+
if (_return.length === 0) {
70+
return callback(new Error('React.createClass return: option not found'));
71+
}
72+
else if (_return.length > 2) {
73+
return callback(new Error('More then one React.createClass return: option'));
74+
}
75+
else {
76+
js = insert(js, _return.start, _return.end, format('return (%s);', template));
77+
}
6078

61-
callback(js);
79+
return callback(err, js);
6280
});
6381
}
6482

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-st",
3-
"version": "0.6.1",
3+
"version": "0.6.2",
44
"description": "React separate template",
55
"main": "conv.js",
66
"dependencies": {

test/conv.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function file(path) {
99
describe('convert function', function () {
1010

1111
it ('should convert js + html to jsx', function (done) {
12-
conv(file('menu.js'), file('menu.html'), function (jsx) {
12+
conv(file('menu.js'), file('menu.html'), function (err, jsx) {
1313
expect(jsx).to.equal(file('menu.jsx'));
1414
done();
1515
});

0 commit comments

Comments
 (0)