All files / lib/compiler ComponentParser.js

89.04% Statements 382/429
85.26% Branches 81/95
100% Functions 10/10
89.04% Lines 382/429

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 4306x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 5x 5x 5x 5x 5x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x               1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x     2x 2x 2x 2x 2x 2x 2x 82x 82x 2x 2x 82x   80x 2x 2x 2x 2x 2x 80x 82x 2x 2x 6x 6x 6x 6x 6x 6x 6x 6x 2x 2x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 2x 2x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 3x 3x 3x     3x 3x 3x 3x 3x 3x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 3x 3x 3x 6x 6x 6x 6x 6x 8x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 8x 6x 6x 3x 3x 3x 3x 3x 5x 7x 7x 4x 7x 3x 3x 3x 3x 3x 3x 7x 3x 5x       3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 5x 2x 2x 3x 3x 3x 3x 3x 3x 3x 6x 6x 6x 6x 6x 6x 6x 6x 18x 18x 18x 31x 31x 31x 18x 18x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 31x 115x 115x 47x 6x 6x 115x 6x 68x 13x 13x 4x 4x 13x 13x 13x 102x 102x 13x 31x     13x 13x 13x 31x 4x 4x 13x 13x 13x 13x 13x 31x 6x 6x 6x 6x 6x 1x 6x 5x 5x 3x 4x 2x 2x 5x 6x 6x 31x 31x 31x 4x 4x 31x 9x 9x 9x 9x 9x 9x 9x 528x 528x 528x 528x 9x 9x 9x 9x 9x 9x   528x                                                 519x 519x 519x 528x 9x 9x       9x 9x 9x 9x 9x 9x 9x 9x 31x 18x 18x 6x 6x 6x  
const fs = require('fs');
const path = require('path');
const ExpressionParser = require('./expressionParser');
 
/**
 * ComponentParser handles the parsing of Avenx component files (.js and .css).
 * It extracts component state, computed properties, methods, and templates,
 * and coordinates with the StyleProcessor to handle styles.
 */
class ComponentParser {
    /**
     * @param {StyleProcessor} styleProcessor - An instance of StyleProcessor to handle styles.
     */
    constructor(styleProcessor) {
        /** @type {StyleProcessor} */
        this.styleProcessor = styleProcessor;
        /** @type {ExpressionParser} */
        this.expressionParser = new ExpressionParser();
    }
 
    /**
     * Parses a .component.js or .page.js file and its corresponding CSS file.
     * @param {string} filePath - The absolute path to the file.
     * @param {'component'|'page'} [type='component'] - The type of file being parsed.
     * @returns {string} The generated JavaScript class.
     */
    parse(filePath, type = 'component') {
        const isPage = type === 'page';
        const suffix = isPage ? '.page.js' : '.component.js';
        const content = fs.readFileSync(filePath, 'utf-8');
        const fileName = path.basename(filePath, suffix);
        
        // Convert user-profile or user_profile to UserProfile
        const name = fileName
            .split(/[-_]/)
            .map(part => part.charAt(0).toUpperCase() + part.slice(1))
            .join('');
 
        const desPath = filePath.replace(suffix, isPage ? '.page.css' : '.component.css');
        let desBlocks = {};
 
        if (fs.existsSync(desPath)) {
            this.extractStylesAndVars(fs.readFileSync(desPath, 'utf-8'), desBlocks);
        }
 
        const state = this.extractState(content);
        const computed = this.extractComputed(content);
        const methods = this.extractMethods(content);
        let template = this.extractTemplate(content, desBlocks, name);
 
        // Handle declarative tags: <MyComponent /> or <MyComponent>...</MyComponent> -> <div data-avenx-comp="MyComponent">...</div>
        // Only if it looks like a component (starts with uppercase)
        template = this.processComponentTags(template);
 
        const methodStrings = Object.entries(methods)
            .map(([k, v]) => `${k}: \`${v}\``).join(',\n        ');
 
        if (isPage) {
            return `
class ${name} extends AvenxPage {
    constructor(bridges, componentRegistry, props) {
        super(${JSON.stringify(state)}, ${JSON.stringify(computed)}, bridges, \`${template}\`, { ${methodStrings} }, componentRegistry, props);
    }
}`;
        }
 
        return `
class ${name} extends AvenxComponent {
    constructor(bridges, props) {
        super(${JSON.stringify(state)}, ${JSON.stringify(computed)}, bridges, \`${template}\`, { ${methodStrings} }, props);
    }
}`;
    }
 
    /**
     * Extracts global CSS variables and component-specific style blocks from CSS content.
     * @param {string} desContent - The content of the .component.css file.
     * @param {Object} desBlocks - An object to store the extracted style blocks.
     * @private
     */
    extractStylesAndVars(desContent, desBlocks) {
        const globalMatch = desContent.match(/<@global>([\s\S]*?)<\/ ?@global>/i);
        if (globalMatch) {
            let inner = globalMatch[1];
            const defRegex = /@def\s+([\w-]+)\s+([^;]+);/g;
            let defMatch;
            while ((defMatch = defRegex.exec(inner)) !== null) {
                this.styleProcessor.addVariable(defMatch[1], defMatch[2].trim());
            }
            
            // Remove @def lines and add the rest as global CSS
            const rawCss = inner.replace(/@def\s+[\w-]+\s+[^;]+;/g, '').trim();
            if (rawCss) {
                this.styleProcessor.addGlobalCSS(rawCss);
            }
        }
 
        const cssBlockMatch = desContent.match(/<@css>([\s\S]*?)<\/ ?@css>/i);
        if (cssBlockMatch) {
            const inner = cssBlockMatch[1];
            let depth = 0, currentName = "", currentBody = "", inBlock = false;
            for (let i = 0; i < inner.length; i++) {
                const char = inner[i];
                if (char === '{' && depth === 0) {
                    currentName = inner.substring(0, i).trim().split('}').pop().trim();
                    inBlock = true; depth++;
                } else if (char === '{') {
                    depth++; currentBody += char;
                } else if (char === '}') {
                    depth--;
                    if (depth === 0) {
                        if (currentName) desBlocks[currentName] = currentBody.trim();
                        currentBody = ""; currentName = ""; inBlock = false;
                    } else { currentBody += char; }
                } else if (inBlock) { currentBody += char; }
            }
        }
    }
 
    /**
     * Extracts the initial state from the component's <state /> tags.
     * @param {string} content - The content of the .component.js file.
     * @returns {Object} The extracted state object.
     * @private
     */
    extractState(content) {
        return this.expressionParser.parseState(content);
    }
 
    /**
     * Extracts computed properties from the component's <computed /> tags.
     * @param {string} content - The content of the .component.js file.
     * @returns {Object} A map of property names to their expression strings.
     * @private
     */
    extractComputed(content) {
        return this.expressionParser.parseComputed(content);
    }
 
    /**
     * Extracts actions (methods) from the component's <action /> tags.
     * @param {string} content - The content of the .component.js file.
     * @returns {Object<string, string>} A map of method names to their stringified bodies.
     * @private
     */
    extractMethods(content) {
        return this.expressionParser.parseMethods(content);
    }
 
    /**
     * Extracts the HTML template and processes internal styles.
     * @param {string} content - The content of the .component.js file.
     * @param {Object} desBlocks - The previously extracted design blocks.
     * @param {string} name - The name of the component for style hashing.
     * @returns {string} The cleaned and processed HTML template.
     * @private
     */
    extractTemplate(content, desBlocks, name) {
        let template = content
            .replace(/<state.*? \/>/g, '')
            .replace(/<computed.*? \/>/g, '')
            .replace(/<action.*?>[\s\S]*?<\/action>/g, '')
            .trim();
        template = this.styleProcessor.process(template, desBlocks, name);
        template = this.processBindDirectives(template);
        template = this.processForLoops(template);
        return template.split('\n').filter(line => line.trim() !== '').join('\n');
    }
 
    /**
     * Processes data-ax-bind attributes on input, textarea, and select elements.
     * Converts data-ax-bind="expr" to value="{{ expr }}" and event listener.
     * @param {string} template - The template string.
     * @returns {string} The processed template.
     */
    processBindDirectives(template) {
        const tagRegex = /<(input|textarea|select)\b([^>]*?)>/gi;
        return template.replace(tagRegex, (match, tagName, attrs) => {
            const bindRegex = /\bdata-ax-bind\s*=\s*(?:"([^"]*)"|'([^']*)')/i;
            const bindMatch = attrs.match(bindRegex);
            if (!bindMatch) {
                return match;
            }
            
            const bindExpr = (bindMatch[1] !== undefined ? bindMatch[1] : bindMatch[2]).trim();
            let cleanAttrs = attrs.replace(bindRegex, '').trim();
            
            let isSelfClosing = false;
            if (cleanAttrs.endsWith('/')) {
                isSelfClosing = true;
                cleanAttrs = cleanAttrs.slice(0, -1).trim();
            }
            
            const eventName = tagName.toLowerCase() === 'select' ? 'change' : 'input';
            const valueAttr = `value="{{ ${bindExpr} }}"`;
            const eventAttr = `@${eventName}="${bindExpr} = event.target.value"`;
            
            const suffix = isSelfClosing ? ' />' : '>';
            return `<${tagName} ${cleanAttrs} ${valueAttr} ${eventAttr}`.trim().replace(/\s+/g, ' ') + suffix;
        });
    }
 
    /**
     * Processes <@for> loops in the template, converting them to <template> tags
     * that can be handled by the runtime for efficient list rendering.
     * @param {string} template - The HTML template string.
     * @returns {string} The processed template.
     * @private
     */
    processForLoops(template) {
        let currentTemplate = template;
 
        while (true) {
            // Matches <@for item in list> or <@for item in list key="item.id">, or closing tag </@for> / </ @for>
            const tagRegex = /(<@for\s+(\w+)\s+in\s+([^>]+?)(?:\s+key="([^"]*)")?>)|(<\/ ?@for>)/gi;
            let match;
            const tags = [];
            while ((match = tagRegex.exec(currentTemplate)) !== null) {
                if (match[1]) {
                    tags.push({
                        type: 'start',
                        index: match.index,
                        length: match[0].length,
                        item: match[2],
                        list: match[3],
                        key: match[4]
                    });
                } else {
                    tags.push({
                        type: 'end',
                        index: match.index,
                        length: match[0].length
                    });
                }
            }
 
            if (tags.length === 0) {
                break;
            }
 
            let innerPair = null;
            const stack = [];
            for (let i = 0; i < tags.length; i++) {
                const tag = tags[i];
                if (tag.type === 'start') {
                    stack.push(tag);
                } else {
                    const startTag = stack.pop();
                    if (startTag) {
                        innerPair = { start: startTag, end: tag };
                        break; // Found innermost loop!
                    }
                }
            }
 
            if (!innerPair) {
                console.warn("[ComponentParser] Unmatched <@for> tags in template.");
                break;
            }
 
            const startIdx = innerPair.start.index;
            const endIdx = innerPair.end.index + innerPair.end.length;
 
            const bodyStart = startIdx + innerPair.start.length;
            const bodyEnd = innerPair.end.index;
            const body = currentTemplate.substring(bodyStart, bodyEnd);
 
            // Escape inner interpolation tags to prevent them from being processed
            // by the initial template render. They will be processed per-item at runtime.
            const escapedBody = body.replace(/\{\{/g, '{%').replace(/\}\}/g, '%}');
            let attrs = `data-ax-for="${innerPair.start.list.trim()}" data-ax-as="${innerPair.start.item.trim()}"`;
            if (innerPair.start.key) {
                attrs += ` data-ax-key="${innerPair.start.key.trim()}"`;
            }
 
            const replacement = `<template ${attrs}>${escapedBody}</template>`;
            currentTemplate = currentTemplate.substring(0, startIdx) + replacement + currentTemplate.substring(endIdx);
        }
 
        return currentTemplate;
    }
 
    /**
     * Processes component tags recursively to handle transclusion slots.
     * Maps `<CompName ...>...</CompName>` to `<div data-avenx-comp="CompName">...</div>`.
     * @param {string} template - The template string.
     * @returns {string} The processed template.
     */
    processComponentTags(template) {
        let currentTemplate = template;
        
        while (true) {
            // Find the first occurrence of < followed by an uppercase letter
            const match = currentTemplate.match(/<([A-Z][a-zA-Z0-9]*)\b/);
            if (!match) {
                break;
            }
            
            const compName = match[1];
            const startIndex = match.index;
            
            // Find the end of this opening/self-closing tag
            let i = startIndex + 1 + compName.length;
            let inQuote = null;
            let isSelfClosing = false;
            let tagEndIndex = -1;
            
            while (i < currentTemplate.length) {
                const char = currentTemplate[i];
                if (inQuote) {
                    if (char === inQuote) {
                        inQuote = null;
                    }
                } else if (char === '"' || char === "'") {
                    inQuote = char;
                } else if (char === '>') {
                    const trimmedBefore = currentTemplate.substring(startIndex + 1 + compName.length, i).trim();
                    if (trimmedBefore.endsWith('/')) {
                        isSelfClosing = true;
                    }
                    tagEndIndex = i + 1;
                    break;
                }
                i++;
            }
            
            if (tagEndIndex === -1) {
                break; // Malformed tag, stop parsing to prevent infinite loops
            }
            
            // Extract the attributes string
            let attrsStr = currentTemplate.substring(startIndex + 1 + compName.length, tagEndIndex - 1).trim();
            if (isSelfClosing && attrsStr.endsWith('/')) {
                attrsStr = attrsStr.slice(0, -1).trim();
            }
            
            // Parse attributes
            const props = [];
            const attrRegex = /(\w+)\s*=\s*(?:"([^"]*)"|'([^']*)')/g;
            let attrMatch;
            while ((attrMatch = attrRegex.exec(attrsStr)) !== null) {
                const attrName = attrMatch[1];
                const attrVal = attrMatch[2] !== undefined ? attrMatch[2] : attrMatch[3];
                
                let propExpr;
                if (attrVal.startsWith('{{') && attrVal.endsWith('}}')) {
                    propExpr = attrVal.slice(2, -2).trim();
                } else {
                    const trimmed = attrVal.trim();
                    if (trimmed === 'true' || trimmed === 'false' || trimmed === 'null' || (trimmed !== '' && !isNaN(trimmed))) {
                        propExpr = trimmed;
                    } else {
                        propExpr = `'${trimmed.replace(/'/g, "\\'")}'`;
                    }
                }
                props.push(`data-props-${attrName}="${propExpr}"`);
            }
            const propsAttr = props.length > 0 ? ` ${props.join(' ')}` : '';
            
            if (isSelfClosing) {
                const replacement = `<div data-avenx-comp="${compName}"${propsAttr}></div>`;
                currentTemplate = currentTemplate.substring(0, startIndex) + replacement + currentTemplate.substring(tagEndIndex);
            } else {
                // Find matching closing tag </CompName>
                let searchIndex = tagEndIndex;
                let depth = 1;
                let closingTagIndex = -1;
                let closingTagLength = 0;
                
                while (searchIndex < currentTemplate.length) {
                    const nextOpen = currentTemplate.substring(searchIndex).match(new RegExp(`^<${compName}\\b`));
                    const nextClose = currentTemplate.substring(searchIndex).match(new RegExp(`^</\\s*${compName}\\s*>`));
                    
                    if (nextClose) {
                        depth--;
                        if (depth === 0) {
                            closingTagIndex = searchIndex;
                            closingTagLength = nextClose[0].length;
                            break;
                        }
                        searchIndex += nextClose[0].length;
                    } else if (nextOpen) {
                        // Scan to end of this open tag to see if it is self-closing
                        let tempIdx = searchIndex + nextOpen[0].length;
                        let tempInQuote = null;
                        let tempIsSelfClosing = false;
                        while (tempIdx < currentTemplate.length) {
                            const tc = currentTemplate[tempIdx];
                            if (tempInQuote) {
                                if (tc === tempInQuote) tempInQuote = null;
                            } else if (tc === '"' || tc === "'") {
                                tempInQuote = tc;
                            } else if (tc === '>') {
                                const trimmedBefore = currentTemplate.substring(searchIndex + nextOpen[0].length, tempIdx).trim();
                                if (trimmedBefore.endsWith('/')) {
                                    tempIsSelfClosing = true;
                                }
                                tempIdx++;
                                break;
                            }
                            tempIdx++;
                        }
                        if (!tempIsSelfClosing) {
                            depth++;
                        }
                        searchIndex = tempIdx;
                    } else {
                        searchIndex++;
                    }
                }
                
                if (closingTagIndex === -1) {
                    // No matching closing tag, treat as self-closing
                    const replacement = `<div data-avenx-comp="${compName}"${propsAttr}></div>`;
                    currentTemplate = currentTemplate.substring(0, startIndex) + replacement + currentTemplate.substring(tagEndIndex);
                } else {
                    const innerContent = currentTemplate.substring(tagEndIndex, closingTagIndex);
                    // Recursively process tags inside innerContent
                    const processedInner = this.processComponentTags(innerContent);
                    const replacement = `<div data-avenx-comp="${compName}"${propsAttr}>${processedInner}</div>`;
                    currentTemplate = currentTemplate.substring(0, startIndex) + replacement + currentTemplate.substring(closingTagIndex + closingTagLength);
                }
            }
        }
        return currentTemplate;
    }
}
 
module.exports = ComponentParser;