From f0d04d9f0d2be97a6e21d92f799305a3ba2997db Mon Sep 17 00:00:00 2001 From: metacryst Date: Wed, 20 Mar 2024 18:04:13 +0100 Subject: [PATCH] adding check for super() --- Test/parse.test.js | 17 +++++++++++++++++ index.js | 26 ++++++++++++++------------ 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/Test/parse.test.js b/Test/parse.test.js index 336e3bf..c4b582a 100644 --- a/Test/parse.test.js +++ b/Test/parse.test.js @@ -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 diff --git a/index.js b/index.js index 4a39d9f..c672dc3 100644 --- a/index.js +++ b/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 @@ -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) => {