adding check for super()

This commit is contained in:
metacryst
2024-03-20 18:04:13 +01:00
parent c0ccb138d1
commit f0d04d9f0d
2 changed files with 31 additions and 12 deletions

View File

@@ -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

View File

@@ -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
@@ -209,29 +208,34 @@ 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)
window.Registry.construct(this, window.Registry.currentStateVariables, ...window.Registry.currentParams)
}
constructor(...params) {
super(...params)
window.Registry.construct(this, window.Registry.currentStateVariables, ...window.Registry.currentParams)
}
`
let closingBracket = str.lastIndexOf("}");
str = str.slice(0, closingBracket - 1) + constructorString + "\n}"
str = str.slice(0, closingBracket - 1) + constructorString + "\n}"
return eval('(' + str + ')');
}
return eval('(' + str + ')');
}
static render = (el, parent) => {
@@ -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) => {