Skip to content

Commit 93e7d19

Browse files
update ctx.js : do some validation
1 parent c1ca67c commit 93e7d19

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/ctx.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ class Ctx {
44
this._parentTemp = `"use strict";return @temp;`;
55
this._defaultCom = null;
66
this._com = null;
7+
if (!(window.hasOwnProperty('Babel') && typeof window.Babel === 'object')) {
8+
throw new Error(`
9+
string-to-react-component package needs @babel/standalone for working correctly.
10+
you should load @babel/standalone in the browser.
11+
`);
12+
}
713
this._b = window.Babel;
814
this._babelpresets = ['react'];
915
}
@@ -15,7 +21,16 @@ class Ctx {
1521
_generateCom() {
1622
this._com = this._temp ? Function(this._parentTemp.replace('@temp', this._transpile()))() : this._defaultCom;
1723
}
24+
_validateTemplate(temp) {
25+
if (typeof temp !== 'string') {
26+
throw `passed child into string-to-react-component element should b a string`;
27+
}
28+
if (temp === '') {
29+
throw `passed string into string-to-react-component element can not be empty`;
30+
}
31+
}
1832
updateTemplate(template) {
33+
this._validateTemplate(template);
1934
template = template || '';
2035
if (template !== this._temp) {
2136
this._temp = template;

src/stringToReact.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ beforeAll(() => {
1111
document.body.appendChild(container);
1212
});
1313
beforeEach(() => {
14+
window.Babel = window.Babel || {};
1415
renderApp = (temp, deps, rerender, temp2) => {
1516
let secondRender = false;
1617
const StrintToReactCom = StrintToReact.bind(undefined, deps);
@@ -30,6 +31,7 @@ beforeEach(() => {
3031
};
3132
});
3233
afterEach(() => {
34+
delete window.Babel;
3335
unmountComponentAtNode(container);
3436
container.innerHTML = '';
3537
renderApp = null;

0 commit comments

Comments
 (0)