fix: improve readability by simplifying conditional statements and replacing forEach with for-of loops

This commit is contained in:
Astrian Zheng 2025-05-16 16:51:52 +10:00
parent aae4a85a49
commit b1dc96783a
Signed by: Astrian
SSH Key Fingerprint: SHA256:rVnhx3DAKjujCwWE13aDl7uV6+9U1MvydLkNRXJrBiA

View File

@ -214,9 +214,9 @@ export default (options: ComponentOptions) => {
const result = renderFunction() const result = renderFunction()
// Update DOM // Update DOM
if (typeof result === 'string') { if (typeof result === 'string')
element.innerHTML = result element.innerHTML = result
} else if (result instanceof Node) { else if (result instanceof Node) {
element.innerHTML = '' element.innerHTML = ''
element.appendChild(result) element.appendChild(result)
} }
@ -370,8 +370,9 @@ export default (options: ComponentOptions) => {
const macroBindings = Array.from( const macroBindings = Array.from(
currentElementNode.attributes, currentElementNode.attributes,
).filter((attr) => attr.name.startsWith('%')) ).filter((attr) => attr.name.startsWith('%'))
// biome-ignore lint/complexity/noForEach: TODO: will cause a bug, need to be fixed
macroBindings.forEach((attr) => { // macroBindings.forEach((attr) => {
for (const attr of macroBindings) {
const macroName = attr.name.substring(1) // Remove '%' const macroName = attr.name.substring(1) // Remove '%'
const expr = attr.value.trim() const expr = attr.value.trim()
@ -382,13 +383,13 @@ export default (options: ComponentOptions) => {
if (macroName === 'connect') if (macroName === 'connect')
// Handle state connection: %connect="stateName" // Handle state connection: %connect="stateName"
this._setupTwoWayBinding(currentElementNode, expr) this._setupTwoWayBinding(currentElementNode, expr)
else if (macroName === 'if') else if (macroName === 'if') {
ifDirectivesToProcess.push({ element: currentElementNode, expr }) ifDirectivesToProcess.push({ element: currentElementNode, expr })
else if (macroName === 'for') } else if (macroName === 'for')
this._setupListRendering(currentElementNode, expr) this._setupListRendering(currentElementNode, expr)
else if (macroName === 'key') return else if (macroName === 'key') continue
else console.warn(`Unknown macro: %${macroName}`) else console.warn(`Unknown macro: %${macroName}`)
}) }
} }
} }