adding check for super()
This commit is contained in:
@@ -27,6 +27,23 @@ testParseConstructorIfNoneProvided() {
|
||||
}
|
||||
}
|
||||
|
||||
testParseConstructorFailsIfNoSuper() {
|
||||
class Space extends Shadow {
|
||||
form
|
||||
contents = []
|
||||
|
||||
constructor() {
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
let newClass = window.Registry.parseConstructor(Space)
|
||||
return "No error thrown!"
|
||||
} catch(e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
testParseClassFieldsWithNoDefault() {
|
||||
class Space extends Shadow {
|
||||
form
|
||||
|
||||
16
index.js
16
index.js
@@ -200,7 +200,6 @@ window.Registry = class Registry {
|
||||
static parseConstructor(classObject) {
|
||||
let str = classObject.toString();
|
||||
const lines = str.split('\n');
|
||||
const fields = [];
|
||||
let braceDepth = 0;
|
||||
let constructorFound = false
|
||||
|
||||
@@ -210,17 +209,23 @@ window.Registry = class Registry {
|
||||
braceDepth += (trimmedLine.match(/{/g) || []).length;
|
||||
braceDepth -= (trimmedLine.match(/}/g) || []).length;
|
||||
|
||||
if(trimmedLine.startsWith('constructor(')) {
|
||||
constructorFound = true
|
||||
}
|
||||
|
||||
if (braceDepth === 2) {
|
||||
if (trimmedLine.startsWith('super(')) {
|
||||
constructorFound = true
|
||||
var newLine = trimmedLine + "\nwindow.Registry.construct(this, window.Registry.currentStateVariables, ...window.Registry.currentParams)\n";
|
||||
str = str.replace(line, newLine)
|
||||
break;
|
||||
return eval('(' + str + ')');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(!constructorFound) {
|
||||
if(constructorFound) {
|
||||
throw new Error("Quill: Constructor must have super()! " + lines[0])
|
||||
} else {
|
||||
let constructorString = `
|
||||
constructor(...params) {
|
||||
super(...params)
|
||||
@@ -229,10 +234,9 @@ window.Registry = class Registry {
|
||||
`
|
||||
let closingBracket = str.lastIndexOf("}");
|
||||
str = str.slice(0, closingBracket - 1) + constructorString + "\n}"
|
||||
}
|
||||
|
||||
return eval('(' + str + ')');
|
||||
}
|
||||
}
|
||||
|
||||
static render = (el, parent) => {
|
||||
let renderParent = window.rendering[window.rendering.length-1]
|
||||
@@ -245,7 +249,6 @@ window.Registry = class Registry {
|
||||
}
|
||||
|
||||
static construct = (elem, stateNames, ...params) => {
|
||||
|
||||
// State -> Attributes: set each state value as getter and setter
|
||||
stateNames.forEach(name => {
|
||||
const backingFieldName = `_${name}`;
|
||||
@@ -297,7 +300,6 @@ window.Registry = class Registry {
|
||||
console.error(`Quill: state "${state}" must be initialized`)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static register = (el, tagname) => {
|
||||
|
||||
Reference in New Issue
Block a user