cat\src\builder.js [code] cat\src\collector.js [code] cat\src\domResolver.js [code]…
- User
cat\src\builder.js
javascript - Assistant
/**
* Builder Module
*
* This module takes the interpreted component parts and assembles them into a Custom Element class.
*/
class Builder {
/**
* Build a Custom Element class from the interpreted component parts with optimization:
* - Build DOM once per instance using a _buildDOM method.
* - Initialize variables in the constructor.
* - Append the DOM in connectedCallback.
*
* @param {Object} component - The interpreted component object.
* @param {string} component.tagName - The tag name for the custom element.
* @param {Object} component.script - The interpreted script object.
* @param {string[]} component.script.variables - Array of variable declarations.
* @param {string[]} component.script.methods - Array of method definitions.
* @param {string[]} component.script.calls - Array of function calls.
* @param {Object} component.script.eventListeners - Object containing event listener registrations.
* @param {string[]} component.script.eventListeners.connect - Array of event listener initialization statements.
* @param {string[]} component.script.eventListeners.disconnect - Array of event listener removal statements.
* @param {string} transformedDOM - The JavaScript code for building the DOM structure.
* @returns {string} - The assembled Custom Element class as a string.
*/
static buildCustomElement(component, transformedDOM) {
const { tagName, script } = component;
const className = this.kebabToPascalCase(tagName);
// Initialize variables in the constructor
const variableAssignments = script.variables.join('\n ');
// Event listeners for connect and disconnect
const connectListeners = script.eventListeners.connect.join('\n ');
const disconnectListeners = script.eventListeners.disconnect.join('\n ');
// Function calls to execute after DOM is appended
const functionCalls = script.calls.join('\n ');
// Methods of the class
const methods = script.methods.join('\n\n ');
// Construct the class definition without IIFE and cached fragment
return `
class ${className} extends HTMLElement {
/**
* Constructor initializes properties and builds the DOM.
*/
constructor() {
super();
// Initialize properties
${variableAssignments}
// Build the DOM structure
this._buildDOM();
}
/**
* Builds the DOM structure for the component.
*/
_buildDOM() {
// Build the DOM structure
${transformedDOM}
}
/**
* Invoked each time the custom element is appended into a document-connected element.
*/
connectedCallback() {
// Append the built fragment to the DOM
this.appendChild(this.fragment);
// Initialize event listeners
${connectListeners}
// Execute any additional function calls
${functionCalls}
}
/**
* Invoked each time the custom element is disconnected from the document's DOM.
*/
disconnectedCallback() {
// Remove event listeners
${disconnectListeners}
}
${methods}
}
customElements.define('${tagName}', ${className});
`.trim();
}
/**
* Convert a kebab-case string to PascalCase.
*
* @param {string} str - The kebab-case string to convert.
* @returns {string} - The PascalCase version of the input string.
*/
static kebabToPascalCase(str) {
return str.split('-').map(part => part.charAt(0).toUpperCase() + part.slice(1)).join('');
}
}
module.exports = Builder;
const fs = require('fs-extra');
const path = require('path');
const glob = require('glob');
const { determineFileType } = require('./typeof');
/**
* Collect and structure HTML files from the project
* @param {string} projectRoot - Root directory of the project
* @returns {Object} - Structured object of HTML files
*/
async function collectHTMLFiles(projectRoot) {
const htmlFiles = glob.sync('**/*.html', {
cwd: projectRoot,
ignore: ['node_modules/**', 'cat/**'],
});
const collectedFiles = {};
for (const file of htmlFiles) {
const filePath = path.join(projectRoot, file);
const content = await fs.readFile(filePath, 'utf-8');
const fileType = determineFileType(file);
const title = path.basename(file, path.extname(file));
collectedFiles[file] = {
title,
type: fileType,
body: content, // Start with raw content
};
}
return collectedFiles;
}
module.exports = { collectHTMLFiles };
class DOMResolver {
constructor(transformedBody) {
this.transformedBody = transformedBody;
this.elementMap = this.buildElementMap();
this.replacements = new Map();
}
buildElementMap() {
const map = new Map();
const lines = this.transformedBody.split('\n');
lines.forEach(line => {
const match = line.match(/this\.(el_\d+)\s*=\s*document\.createElement\(`(\w+)`\)/);
if (match) {
const [, varName, tagName] = match;
map.set(varName, { tagName, attributes: {} });
}
const attrMatch = line.match(/this\.(el_\d+)\.setAttribute\(`(\w+)`,\s*`([^`]+)`\)/);
if (attrMatch) {
const [, varName, attrName, attrValue] = attrMatch;
if (map.has(varName)) {
map.get(varName).attributes[attrName] = attrValue;
}
}
});
return map;
}
resolveQuery(query) {
const idMatch = query.match(/document\.getElementById\(['"](.*)['"]\)/);
if (idMatch) {
const id = idMatch[1];
for (const [varName, element] of this.elementMap) {
if (element.attributes.id === id) {
return varName; // Return without 'this.'
}
}
}
const classMatch = query.match(/document\.getElementsByClassName\(['"](.*)['"]\)/);
if (classMatch) {
const className = classMatch[1];
const elements = Array.from(this.elementMap.entries())
.filter(([, element]) => element.attributes.class === className)
.map(([varName]) => varName); // Return without 'this.'
if (elements.length > 0) {
return `[${elements.map(el => `this.${el}`).join(', ')}]`;
}
}
return null;
}
applyReplacements(scriptObject) {
const replaceInString = (str) => {
let result = str;
this.replacements.forEach((newVar, oldVar) => {
const regex = new RegExp(`\\bthis\\.${oldVar}\\b`, 'g');
result = result.replace(regex, `this.${newVar}`);
});
return result;
};
const newVariables = scriptObject.variables.filter(v => {
const varName = v.split('=')[0].trim().replace('this.', '');
return !this.replacements.has(varName);
});
const newMethods = scriptObject.methods.map(replaceInString);
const newCalls = scriptObject.calls.map(replaceInString);
const newEventListeners = {
connect: scriptObject.eventListeners.connect.map(replaceInString),
disconnect: scriptObject.eventListeners.disconnect.map(replaceInString)
};
return {
...scriptObject,
variables: newVariables,
methods: newMethods,
calls: newCalls,
eventListeners: newEventListeners
};
}
resolve(scriptObject) {
const resolvedVariables = scriptObject.variables.map(variable => {
const parts = variable.split('=');
if (parts.length === 2) {
const varName = parts[0].trim().replace('this.', '');
const query = parts[1].trim();
const resolved = this.resolveQuery(query);
if (resolved) {
this.replacements.set(varName, resolved);
return null; // We'll filter these out later
}
}
return variable;
}).filter(Boolean); // Remove null entries
const resolvedScript = this.applyReplacements({
...scriptObject,
variables: resolvedVariables
});
return {
script: resolvedScript,
replacements: Object.fromEntries(this.replacements)
};
}
}
module.exports = DOMResolver;
const path = require('path');
const fs = require('fs-extra');
const { collectHTMLFiles } = require('./collector');
const ParsingPipeline = require('./parser');
const Interpreter = require('./interpreter');
const ScriptInterpreter = require('./sinterpreter');
const Builder = require('./builder');
const { transformToJavaScript } = require('./transformer');
const DOMResolver = require('./domResolver');
async function initializeCompiler(indexPath) {
const projectRoot = path.dirname(indexPath);
const collectedFiles = await collectHTMLFiles(projectRoot);
const processedFiles = {};
const scriptInterpreter = new ScriptInterpreter();
const builtComponents = {};
for (const [file, fileData] of Object.entries(collectedFiles)) {
try {
// Parsing step
const parsed = ParsingPipeline.parseHTML(fileData.body, file);
// HTML Interpretation step
const interpreted = {
...parsed,
body: Interpreter.interpretBody(parsed.body)
};
// Script Interpretation step
const scriptInterpreted = interpreted.script
? scriptInterpreter.interpret(interpreted.script)
: { variables: [], methods: [], calls: [], eventListeners: { connect: [], disconnect: [] } };
// Transformation step
const transformedDOM = transformToJavaScript(interpreted.body);
// New DOM Resolution step
const domResolver = new DOMResolver(transformedDOM);
const { script: resolvedScript, replacements } = domResolver.resolve(scriptInterpreted);
processedFiles[file] = {
...interpreted,
script: resolvedScript,
domReplacements: replacements
};
// Building step
const builtComponent = Builder.buildCustomElement(processedFiles[file], transformedDOM);
builtComponents[file] = builtComponent;
console.log(`\nFile: ${file}`);
console.log('Type:', interpreted.type);
console.log('Tag Name:', interpreted.tagName);
console.log('Parsed body:', JSON.stringify(parsed.body, null, 2));
console.log('Interpreted body:', JSON.stringify(interpreted.body, null, 2));
console.log('Transformed DOM:', transformedDOM);
console.log('Resolved script:', JSON.stringify(resolvedScript, null, 2));
console.log('DOM Replacements:', JSON.stringify(replacements, null, 2));
console.log('Built Component:', builtComponent);
console.log('Style:', interpreted.style);
} catch (error) {
console.error(`Error processing ${file}:`, error.message);
}
}
return {
async compile() {
let indexContent = await fs.readFile(indexPath, 'utf-8');
const debugInfo = `
<script>
console.log('Processed files:', ${JSON.stringify(processedFiles, null, 2)});
console.log('Built Components:', ${JSON.stringify(builtComponents, null, 2)});
</script>
`;
indexContent = indexContent.replace('</head>', `${debugInfo}</head>`);
const componentsScript = `<script>${Object.values(builtComponents).join('\n\n')}</script>`;
indexContent = indexContent.replace('</head>', `${componentsScript}</head>`);
return indexContent;
}
};
}
module.exports = { initializeCompiler };
/**
* Interpreter Module
*/
const Interpreter = (function() {
/**
* Convert custom syntax to JavaScript Template Literals
* @param {string} content - The content to interpret
* @returns {string} - The interpreted content
*/
function interpretContent(content) {
return content.replace(/\{([^}]+)\}/g, (match, p1) => `\${this.${p1.trim()}}`);
}
/**
* Recursively interpret the parsed body
* @param {Array} body - The parsed body
* @returns {Array} - The interpreted body
*/
function interpretBody(body) {
return body.map(node => {
if (node.type === 'tag') {
return {
...node,
children: interpretBody(node.children)
};
} else if (node.type === 'text') {
return {
...node,
content: interpretContent(node.content)
};
}
return node;
});
}
// Public API
return {
interpretBody
};
})();
module.exports = Interpreter;
const { parseDocument } = require('htmlparser2');
const { determineFileType } = require('./typeof');
const Interpreter = require('./interpreter');
/**
* Parsing Pipeline Module
*/
const ParsingPipeline = (() => {
/**
* Extract wrappers from content using matchAll for better performance and readability
* @param {string} content - Raw HTML content
* @returns {Array} - Array of wrapper objects
*/
const extractWrappers = (content) => {
const wrapperRegex = /<(\w+(?:-?\w+)*)>([\s\S]*?)<\/\1>/g;
return Array.from(content.matchAll(wrapperRegex), match => ({
tag: match[1],
content: match[2],
}));
};
/**
* Simplify the DOM structure, removing empty nodes using a recursive approach
* @param {Array} nodes - Array of DOM nodes
* @returns {Array} - Simplified array of nodes
*/
const simplifyDOM = (nodes) =>
nodes
.map(node => {
if (node.type === 'tag') {
const children = simplifyDOM(node.children);
const hasAttributes = Object.keys(node.attribs).length > 0;
if (children.length === 0 && !hasAttributes) return null;
return {
type: 'tag',
name: node.name,
attributes: node.attribs,
children,
};
}
if (node.type === 'text') {
const content = node.data.trim();
return content ? { type: 'text', content } : null;
}
return null;
})
.filter(Boolean);
/**
* Parse the body content
* @param {string} bodyContent - The content to parse
* @returns {Array} - Array of parsed nodes
*/
const parseBodyContent = (bodyContent) => simplifyDOM(parseDocument(bodyContent, { recognizeSelfClosing: true }).children);
/**
* Parse HTML content and extract body, script, and style
* @param {string} content - Raw HTML content to parse
* @param {string} fileName - Name of the file being parsed
* @returns {Object} - Parsed structure with type, body, script, and style
*/
const parseHTML = (content, fileName) => {
const wrappers = extractWrappers(content);
if (wrappers.length > 3) {
throw new Error(`Invalid structure in ${fileName}. Found ${wrappers.length} wrappers, maximum allowed is 3.`);
}
const mainWrappers = wrappers.filter(w => !['script', 'style'].includes(w.tag.toLowerCase()));
if (mainWrappers.length !== 1) {
throw new Error(`Invalid structure in ${fileName}. Expected exactly one main wrapper.`);
}
const mainWrapper = mainWrappers[0];
const scriptWrapper = wrappers.find(w => w.tag.toLowerCase() === 'script');
const styleWrapper = wrappers.find(w => w.tag.toLowerCase() === 'style');
const script = scriptWrapper ? scriptWrapper.content.trim() : '';
const style = styleWrapper ? styleWrapper.content.trim() : '';
const fileType = determineFileType(fileName);
const body = parseBodyContent(mainWrapper.content);
return {
type: fileType,
tagName: mainWrapper.tag,
body,
script,
style
};
};
// Public API
return {
parseHTML,
};
})();
module.exports = ParsingPipeline;
/**
* ScriptInterpreter Module
*
* This module parses raw JavaScript code related to a component and structures it into a class-friendly format.
* It organizes global variables, methods, function calls, and event listeners for subsequent compilation steps.
*/
const acorn = require('acorn');
const walk = require('acorn-walk');
/**
* Class representing a Script Interpreter.
*/
class ScriptInterpreter {
/**
* Create a ScriptInterpreter.
*/
constructor() {
this.globalVariables = [];
this.methods = [];
this.calls = [];
this.eventListeners = {
connect: [],
disconnect: [],
};
this.scriptContent = '';
this.uniqueMethodCounter = 1;
}
/**
* Interpret the provided JavaScript code and extract class components.
*
* @param {string} scriptContent - The raw JavaScript code to interpret.
* @returns {Object} An object containing extracted global variables, methods, calls, and event listeners.
* @throws Will throw an error if parsing fails or if a class is detected.
*/
interpret(scriptContent) {
this.scriptContent = scriptContent;
this.globalVariables = [];
this.methods = [];
this.calls = [];
this.eventListeners = {
connect: [],
disconnect: [],
};
this.uniqueMethodCounter = 1;
try {
const ast = acorn.parse(this.scriptContent, {
ecmaVersion: 'latest',
sourceType: 'script',
locations: false,
});
// Check for class declarations and expressions
walk.simple(ast, {
ClassDeclaration: () => {
throw new Error('Classes are not supported.');
},
ClassExpression: () => {
throw new Error('Classes are not supported.');
}
});
this.processAst(ast);
return {
variables: this.globalVariables,
methods: this.methods,
calls: this.calls,
eventListeners: this.eventListeners,
};
} catch (error) {
throw new Error(`Failed to interpret script: ${error.message}`);
}
}
/**
* Process the Abstract Syntax Tree (AST) to extract class components.
*
* @param {Object} ast - The AST generated by Acorn.
*/
processAst(ast) {
// Only process top-level nodes (global scope)
ast.body.forEach(node => {
if (node.type === 'VariableDeclaration') {
this.processVariableDeclaration(node);
} else if (node.type === 'FunctionDeclaration') {
this.processFunctionDeclaration(node);
} else if (node.type === 'ExpressionStatement') {
this.processExpressionStatement(node);
} else if (node.type === 'ImportDeclaration') {
this.processImportDeclaration(node);
} else {
// Ignore other top-level constructs or handle as needed
}
});
}
/**
* Process import declarations and treat them as global variables.
*
* @param {Object} node - The ImportDeclaration node.
*/
processImportDeclaration(node) {
const code = this.generateCode(node);
this.globalVariables.push(`${code};`);
}
/**
* Process variable declarations in the global scope.
*
* @param {Object} node - The VariableDeclaration node.
*/
processVariableDeclaration(node) {
node.declarations.forEach((decl) => {
if (decl.init && (decl.init.type === 'ArrowFunctionExpression' || decl.init.type === 'FunctionExpression')) {
this.processFunctionExpression(decl, node.kind);
} else {
this.processGlobalVariable(decl, node.kind);
}
});
}
/**
* Process a single global variable declaration.
*
* @param {Object} decl - The VariableDeclarator node.
* @param {string} kind - The kind of variable declaration (var, let, const).
*/
processGlobalVariable(decl, kind) {
const name = decl.id.name;
const init = decl.init ? this.generateCode(decl.init) : 'undefined';
// Prefix with 'this.' to assign as a class property
this.globalVariables.push(`this.${name} = ${init};`);
}
/**
* Process function declarations in the global scope.
*
* @param {Object} node - The FunctionDeclaration node.
*/
processFunctionDeclaration(node) {
const name = node.id.name;
const params = node.params.map(param => this.generateCode(param)).join(', ');
const body = this.generateCode(node.body).trim();
this.methods.push(`${name}(${params}) ${body}`);
}
/**
* Process function expressions (including arrow functions) in the global scope as class methods.
*
* @param {Object} decl - The VariableDeclarator node containing the function expression.
* @param {string} kind - The kind of variable declaration (var, let, const).
*/
processFunctionExpression(decl, kind) {
const name = decl.id.name;
const params = decl.init.params.map(param => this.generateCode(param)).join(', ');
let methodString = '';
if (decl.init.type === 'FunctionExpression' || decl.init.type === 'ArrowFunctionExpression') {
if (decl.init.body.type === 'BlockStatement') {
const body = this.generateCode(decl.init.body).trim();
methodString = `${name}(${params}) ${body}`;
} else {
// Concise arrow function
const expression = this.generateCode(decl.init.body).trim();
methodString = `${name}(${params}) { return ${expression}; }`;
}
this.methods.push(methodString);
}
}
/**
* Process expression statements in the global scope.
*
* @param {Object} node - The ExpressionStatement node.
*/
processExpressionStatement(node) {
const expression = node.expression;
try {
if (this.isFunctionAssignment(expression)) {
this.processFunctionAssignment(expression);
} else if (expression.type === 'CallExpression') {
if (this.isImmediateFunction(expression)) {
this.processImmediateFunction(expression);
} else if (this.isEventListener(expression)) {
this.processEventListener(expression);
} else {
this.processFunctionCall(expression);
}
}
} catch (error) {
console.error(`Error processing expression statement: ${error.message}`);
}
}
/**
* Check if an expression is a function assignment (e.g., window.onload = function() {}).
*
* @param {Object} expression - The expression node.
* @returns {boolean} True if it's a function assignment, false otherwise.
*/
isFunctionAssignment(expression) {
return (
expression.type === 'AssignmentExpression' &&
(expression.right.type === 'FunctionExpression' || expression.right.type === 'ArrowFunctionExpression')
);
}
/**
* Process function assignments (e.g., window.onload = function() {}).
*
* @param {Object} expression - The AssignmentExpression node.
*/
processFunctionAssignment(expression) {
const left = this.generateCode(expression.left).trim();
const name = this.generateUniqueMethodName('assignedFunction');
const params = expression.right.params.map(param => this.generateCode(param)).join(', ');
let body = '';
if (expression.right.body.type === 'BlockStatement') {
body = this.generateCode(expression.right.body).trim();
} else {
// Concise arrow function
const expressionBody = this.generateCode(expression.right.body).trim();
body = `{ return ${expressionBody}; }`;
}
this.methods.push(`${name}(${params}) ${body}`);
this.calls.push(`${left} = this.${name}.bind(this);`);
}
/**
* Check if a CallExpression node is an event listener.
*
* @param {Object} expression - The CallExpression node.
* @returns {boolean} True if it's an event listener, false otherwise.
*/
isEventListener(expression) {
return (
expression.callee.type === 'MemberExpression' &&
expression.callee.property.name === 'addEventListener' &&
expression.arguments.length >= 2
);
}
/**
* Process event listeners in the global scope.
*
* @param {Object} expression - The CallExpression node representing the event listener.
*/
processEventListener(expression) {
const element = this.generateCode(expression.callee.object).trim();
const event = this.generateCode(expression.arguments[0]).trim().replace(/^['"]|['"]$/g, '');
let listener = '';
if (expression.arguments[1].type === 'FunctionExpression' || expression.arguments[1].type === 'ArrowFunctionExpression') {
const func = expression.arguments[1];
const name = this.generateUniqueMethodName('eventListener');
const params = func.params.map(param => this.generateCode(param)).join(', ');
let body = '';
if (func.body.type === 'BlockStatement') {
body = this.generateCode(func.body).trim();
} else {
// Concise arrow function
const expressionBody = this.generateCode(func.body).trim();
body = `{ return ${expressionBody}; }`;
}
this.methods.push(`${name}(${params}) ${body}`);
listener = `this.${name}.bind(this)`;
} else if (expression.arguments[1].type === 'Identifier') {
// Listener is a named function
const listenerName = expression.arguments[1].name;
listener = `this.${listenerName}.bind(this)`;
} else {
// Other types (e.g., bound methods)
listener = this.generateCode(expression.arguments[1]).trim();
}
// Attach 'this.' if not already present
const formattedElement = this.ensureThisPrefix(element);
// Add to 'connect' event listeners
this.eventListeners.connect.push(`${formattedElement}.addEventListener('${event}', ${listener});`);
// Add corresponding remover to 'disconnect' event listeners
this.eventListeners.disconnect.push(`${formattedElement}.removeEventListener('${event}', ${listener});`);
}
/**
* Ensures that the given identifier is prefixed with 'this.' if it's not already.
*
* @param {string} identifier - The identifier to check.
* @returns {string} The identifier with 'this.' prefix if needed.
*/
ensureThisPrefix(identifier) {
// If the identifier starts with 'this.', return as is
if (/^this\./.test(identifier)) {
return identifier;
}
// Otherwise, prefix with 'this.'
return `this.${identifier}`;
}
/**
* Check if a CallExpression node is an Immediately Invoked Function Expression (IIFE).
*
* @param {Object} expression - The CallExpression node.
* @returns {boolean} True if it's an IIFE, false otherwise.
*/
isImmediateFunction(expression) {
return (
(expression.callee.type === 'FunctionExpression' || expression.callee.type === 'ArrowFunctionExpression') &&
// Allow both named and anonymous function expressions
true
);
}
/**
* Process Immediately Invoked Function Expressions (IIFE) in the global scope.
*
* @param {Object} expression - The CallExpression node representing the IIFE.
*/
processImmediateFunction(expression) {
const name = this.generateUniqueMethodName('immediateFunction');
let params = '';
let args = '';
let body = '';
if (expression.callee.type === 'FunctionExpression' || expression.callee.type === 'ArrowFunctionExpression') {
params = expression.callee.params.map(param => this.generateCode(param)).join(', ');
if (expression.callee.body.type === 'BlockStatement') {
body = this.generateCode(expression.callee.body).trim();
} else {
// Concise arrow function
const expressionBody = this.generateCode(expression.callee.body).trim();
body = `{ return ${expressionBody}; }`;
}
}
this.methods.push(`${name}(${params}) ${body}`);
const callArgs = expression.arguments.map(arg => this.generateCode(arg)).join(', ');
this.calls.push(`this.${name}(${callArgs});`);
}
/**
* Process function calls in the global scope.
*
* @param {Object} expression - The CallExpression node.
*/
processFunctionCall(expression) {
const callExpression = this.generateCode(expression).trim();
this.calls.push(`${callExpression};`);
}
/**
* Generate unique method names to avoid conflicts.
*
* @param {string} base - The base name for the method.
* @returns {string} A unique method name.
*/
generateUniqueMethodName(base) {
const name = `${base}_${this.uniqueMethodCounter}`;
this.uniqueMethodCounter += 1;
return name;
}
/**
* Generate code from an AST node based on its start and end positions.
*
* @param {Object} node - The AST node.
* @returns {string} The generated code as a string.
*/
generateCode(node) {
return this.scriptContent.slice(node.start, node.end);
}
}
module.exports = ScriptInterpreter;
/**
* Transform parsed HTML into JavaScript DOM manipulation code
* @param {Object} parsedHTML - The parsed HTML structure
* @returns {string} - JavaScript code to create the DOM structure
*/
function transformToJavaScript(parsedHTML) {
let code = ``;
let varCounter = 0;
function generateVarName() {
return `el_${varCounter++}`;
}
function escapeString(str) {
return str.replace(/\\/g, `\\\\`).replace(/`/g, `\\\``).replace(/\n/g, `\\n`);
}
function transformNode(node, parentVar) {
let nodeCode = ``;
const varName = generateVarName();
if (node.type === `tag` && ![`style`, `script`].includes(node.name)) {
nodeCode += `this.${varName} = document.createElement(\`${node.name}\`);\n`;
Object.entries(node.attributes).forEach(([attr, value]) => {
nodeCode += `this.${varName}.setAttribute(\`${attr}\`, \`${escapeString(value)}\`);\n`;
});
node.children.forEach(child => {
nodeCode += transformNode(child, `this.${varName}`);
});
} else if (node.type === `text`) {
const trimmedContent = node.content.trim();
if (trimmedContent) {
nodeCode += `this.${varName} = document.createTextNode(\`${escapeString(trimmedContent)}\`);\n`;
if (parentVar) {
nodeCode += `${parentVar}.appendChild(this.${varName});\n`;
}
}
return nodeCode;
} else if (node.type === `comment`) {
nodeCode += `this.${varName} = document.createComment(\`${escapeString(node.data)}\`);\n`;
} else {
return ``;
}
if (parentVar) {
nodeCode += `${parentVar}.appendChild(this.${varName});\n`;
}
return nodeCode;
}
// Initialize the fragment
code += `this.fragment = document.createDocumentFragment();\n\n`;
parsedHTML.forEach(node => {
code += transformNode(node, `this.fragment`);
});
// Return the fragment variable
code += `\nthis.fragment;`;
return code.trim();
}
module.exports = { transformToJavaScript };
/**
* Determine the type of HTML file based on its name
* @param {string} fileName - Name of the HTML file
* @returns {string} - Type of the file ('component', 'page', or 'other')
*/
function determineFileType(fileName) {
if (fileName.endsWith('.comp.html')) {
return 'component';
} else if (fileName.endsWith('.page.html')) {
return 'page';
} else {
return 'other';
}
}
module.exports = { determineFileType };
We need our sinterpreter.js track all the global variables, and defined methods, and add the prefix this. when is used inside the own script. Sample:
let counter = 0;
let button = document.getElementById('button');
let ps = document.getElementsByClassName('isap');
const tester = (() => {
console.log(counter);
});
function method() {
tester();
another(); }
button.addEventListener('click', (() => console.log('hello')));
Where counter inside the function, goes from counter to this.counter... the tester function calling inside the function method, goes from tester() to this.tester();... and for another() anything because it's an external function, not defined in our script, just used.
To achieve the functionality where all references to globally defined variables and methods within your script are prefixed with this., you need to enhance your ScriptInterpreter class in sinterpreter.js. This involves the following steps:
this. within function bodies.Below is a comprehensive guide to implementing these enhancements, complete with code snippets and explanations.
First, we'll modify the ScriptInterpreter class to track all global variables and methods. Then, we'll traverse the AST to find references to these tracked identifiers and prefix them with this..
const acorn = require('acorn');
const walk = require('acorn-walk');
const MagicString = require('magic-string'); // For code modifications
/**
* Class representing a Script Interpreter.
*/
class ScriptInterpreter {
/**
* Create a ScriptInterpreter.
*/
constructor() {
this.globalVariables = new Set();
this.methods = new Set();
this.calls = [];
this.eventListeners = {
connect: [],
disconnect: [],
};
this.scriptContent = '';
this.uniqueMethodCounter = 1;
this.magicString = null; // For code modifications
}
/**
* Interpret the provided JavaScript code and extract class components.
* Also modifies the code to prefix 'this.' where necessary.
*
* @param {string} scriptContent - The raw JavaScript code to interpret.
* @returns {Object} An object containing extracted global variables, methods, calls, and event listeners.
* @throws Will throw an error if parsing fails or if a class is detected.
*/
interpret(scriptContent) {
this.scriptContent = scriptContent;
this.globalVariables = new Set();
this.methods = new Set();
this.calls = [];
this.eventListeners = {
connect: [],
disconnect: [],
};
this.uniqueMethodCounter = 1;
this.magicString = new MagicString(scriptContent);
try {
const ast = acorn.parse(this.scriptContent, {
ecmaVersion: 'latest',
sourceType: 'script',
locations: false,
});
// Check for class declarations and expressions
walk.simple(ast, {
ClassDeclaration: () => {
throw new Error('Classes are not supported.');
},
ClassExpression: () => {
throw new Error('Classes are not supported.');
}
});
this.processAst(ast);
this.modifyScript(ast);
return {
variables: Array.from(this.globalVariables),
methods: Array.from(this.methods),
calls: this.calls,
eventListeners: this.eventListeners,
transformedScript: this.magicString.toString(), // Modified script with 'this.' prefixes
};
} catch (error) {
throw new Error(`Failed to interpret script: ${error.message}`);
}
}
/**
* Process the Abstract Syntax Tree (AST) to extract class components.
*
* @param {Object} ast - The AST generated by Acorn.
*/
processAst(ast) {
// Only process top-level nodes (global scope)
ast.body.forEach(node => {
if (node.type === 'VariableDeclaration') {
this.processVariableDeclaration(node);
} else if (node.type === 'FunctionDeclaration') {
this.processFunctionDeclaration(node);
} else if (node.type === 'ExpressionStatement') {
this.processExpressionStatement(node);
} else if (node.type === 'ImportDeclaration') {
this.processImportDeclaration(node);
} else {
// Ignore other top-level constructs or handle as needed
}
});
}
/**
* Process import declarations and treat them as global variables.
*
* @param {Object} node - The ImportDeclaration node.
*/
processImportDeclaration(node) {
const code = this.generateCode(node);
this.globalVariables.add(node.specifiers.map(spec => spec.local.name).join(', '));
this.calls.push(`${code};`);
}
/**
* Process variable declarations in the global scope.
*
* @param {Object} node - The VariableDeclaration node.
*/
processVariableDeclaration(node) {
node.declarations.forEach((decl) => {
if (decl.init && (decl.init.type === 'ArrowFunctionExpression' || decl.init.type === 'FunctionExpression')) {
this.processFunctionExpression(decl, node.kind);
} else {
this.processGlobalVariable(decl, node.kind);
}
});
}
/**
* Process a single global variable declaration.
*
* @param {Object} decl - The VariableDeclarator node.
* @param {string} kind - The kind of variable declaration (var, let, const).
*/
processGlobalVariable(decl, kind) {
const names = this.extractDeclaredNames(decl.id);
names.forEach(name => this.globalVariables.add(name));
}
/**
* Extract declared variable names from a pattern (handles destructuring).
*
* @param {Object} node - The pattern node.
* @returns {Array<string>} - Array of variable names.
*/
extractDeclaredNames(node) {
const names = [];
if (node.type === 'Identifier') {
names.push(node.name);
} else if (node.type === 'ObjectPattern' || node.type === 'ArrayPattern') {
node.elements && node.elements.forEach(element => {
if (element) {
names.push(...this.extractDeclaredNames(element));
}
});
node.properties && node.properties.forEach(prop => {
names.push(...this.extractDeclaredNames(prop.value));
});
} else if (node.type === 'RestElement') {
names.push(...this.extractDeclaredNames(node.argument));
}
return names;
}
/**
* Process function declarations in the global scope.
*
* @param {Object} node - The FunctionDeclaration node.
*/
processFunctionDeclaration(node) {
const name = node.id.name;
this.methods.add(name);
}
/**
* Process function expressions (including arrow functions) in the global scope as class methods.
*
* @param {Object} decl - The VariableDeclarator node containing the function expression.
* @param {string} kind - The kind of variable declaration (var, let, const).
*/
processFunctionExpression(decl, kind) {
const name = decl.id.name;
this.methods.add(name);
}
/**
* Process expression statements in the global scope.
*
* @param {Object} node - The ExpressionStatement node.
*/
processExpressionStatement(node) {
const expression = node.expression;
try {
if (this.isFunctionAssignment(expression)) {
this.processFunctionAssignment(expression);
} else if (expression.type === 'CallExpression') {
if (this.isImmediateFunction(expression)) {
this.processImmediateFunction(expression);
} else if (this.isEventListener(expression)) {
this.processEventListener(expression);
} else {
this.processFunctionCall(expression);
}
}
} catch (error) {
console.error(`Error processing expression statement: ${error.message}`);
}
}
/**
* Check if an expression is a function assignment (e.g., window.onload = function() {}).
*
* @param {Object} expression - The expression node.
* @returns {boolean} True if it's a function assignment, false otherwise.
*/
isFunctionAssignment(expression) {
return (
expression.type === 'AssignmentExpression' &&
(expression.right.type === 'FunctionExpression' || expression.right.type === 'ArrowFunctionExpression')
);
}
/**
* Process function assignments (e.g., window.onload = function() {}).
*
* @param {Object} expression - The AssignmentExpression node.
*/
processFunctionAssignment(expression) {
const left = this.generateCode(expression.left).trim();
const name = this.generateUniqueMethodName('assignedFunction');
const params = expression.right.params.map(param => this.generateCode(param)).join(', ');
let body = '';
if (expression.right.body.type === 'BlockStatement') {
body = this.generateCode(expression.right.body).trim();
} else {
// Concise arrow function
const expressionBody = this.generateCode(expression.right.body).trim();
body = `{ return ${expressionBody}; }`;
}
this.methods.add(name);
this.calls.push(`${left} = this.${name}.bind(this);`);
}
/**
* Check if a CallExpression node is an event listener.
*
* @param {Object} expression - The CallExpression node.
* @returns {boolean} True if it's an event listener, false otherwise.
*/
isEventListener(expression) {
return (
expression.callee.type === 'MemberExpression' &&
expression.callee.property.name === 'addEventListener' &&
expression.arguments.length >= 2
);
}
/**
* Process event listeners in the global scope.
*
* @param {Object} expression - The CallExpression node representing the event listener.
*/
processEventListener(expression) {
const element = this.generateCode(expression.callee.object).trim();
const event = this.generateCode(expression.arguments[0]).trim().replace(/^['"]|['"]$/g, '');
let listener = '';
if (expression.arguments[1].type === 'FunctionExpression' || expression.arguments[1].type === 'ArrowFunctionExpression') {
const func = expression.arguments[1];
const name = this.generateUniqueMethodName('eventListener');
const params = func.params.map(param => this.generateCode(param)).join(', ');
let body = '';
if (func.body.type === 'BlockStatement') {
body = this.generateCode(func.body).trim();
} else {
// Concise arrow function
const expressionBody = this.generateCode(func.body).trim();
body = `{ return ${expressionBody}; }`;
}
this.methods.add(name);
listener = `this.${name}.bind(this)`;
} else if (expression.arguments[1].type === 'Identifier') {
// Listener is a named function
const listenerName = expression.arguments[1].name;
listener = `this.${listenerName}.bind(this)`;
this.methods.add(listenerName);
} else {
// Other types (e.g., bound methods)
listener = this.generateCode(expression.arguments[1]).trim();
}
// Attach 'this.' if not already present
const formattedElement = this.ensureThisPrefix(element);
// Add to 'connect' event listeners
this.eventListeners.connect.push(`${formattedElement}.addEventListener('${event}', ${listener});`);
// Add corresponding remover to 'disconnect' event listeners
this.eventListeners.disconnect.push(`${formattedElement}.removeEventListener('${event}', ${listener});`);
}
/**
* Check if a CallExpression node is an Immediately Invoked Function Expression (IIFE).
*
* @param {Object} expression - The CallExpression node.
* @returns {boolean} True if it's an IIFE, false otherwise.
*/
isImmediateFunction(expression) {
return (
(expression.callee.type === 'FunctionExpression' || expression.callee.type === 'ArrowFunctionExpression') &&
// Allow both named and anonymous function expressions
true
);
}
/**
* Process Immediately Invoked Function Expressions (IIFE) in the global scope.
*
* @param {Object} expression - The CallExpression node representing the IIFE.
*/
processImmediateFunction(expression) {
const name = this.generateUniqueMethodName('immediateFunction');
let params = '';
let body = '';
if (expression.callee.type === 'FunctionExpression' || expression.callee.type === 'ArrowFunctionExpression') {
params = expression.callee.params.map(param => this.generateCode(param)).join(', ');
if (expression.callee.body.type === 'BlockStatement') {
body = this.generateCode(expression.callee.body).trim();
} else {
// Concise arrow function
const expressionBody = this.generateCode(expression.callee.body).trim();
body = `{ return ${expressionBody}; }`;
}
}
this.methods.add(name);
this.calls.push(`this.${name}();`);
}
/**
* Process function calls in the global scope.
*
* @param {Object} expression - The CallExpression node.
*/
processFunctionCall(expression) {
const callExpression = this.generateCode(expression).trim();
this.calls.push(`${callExpression};`);
}
/**
* Generate unique method names to avoid conflicts.
*
* @param {string} base - The base name for the method.
* @returns {string} A unique method name.
*/
generateUniqueMethodName(base) {
const name = `${base}_${this.uniqueMethodCounter}`;
this.uniqueMethodCounter += 1;
return name;
}
/**
* Generate code from an AST node based on its start and end positions.
*
* @param {Object} node - The AST node.
* @returns {string} The generated code as a string.
*/
generateCode(node) {
return this.scriptContent.slice(node.start, node.end);
}
/**
* Ensures that the given identifier is prefixed with 'this.' if it's not already.
*
* @param {string} identifier - The identifier to check.
* @returns {string} The identifier with 'this.' prefix if needed.
*/
ensureThisPrefix(identifier) {
// If the identifier starts with 'this.', return as is
if (/^this\./.test(identifier)) {
return identifier;
}
// Otherwise, prefix with 'this.'
return `this.${identifier}`;
}
/**
* Modify the script content to prefix 'this.' to tracked variables and methods.
*
* @param {Object} ast - The AST of the script.
*/
modifyScript(ast) {
const trackedIdentifiers = new Set([...this.globalVariables, ...this.methods]);
// Helper function to determine if the identifier should be prefixed
const shouldPrefix = (name) => trackedIdentifiers.has(name);
// Traverse the AST and modify identifiers
walk.simple(ast, {
Identifier(node, state) {
// Avoid modifying declaration identifiers
if (
state.parent.type === 'VariableDeclarator' && state.parent.id === node ||
state.parent.type === 'FunctionDeclaration' && state.parent.id === node ||
state.parent.type === 'FunctionExpression' && state.parent.id === node ||
state.parent.type === 'ClassDeclaration' && state.parent.id === node ||
state.parent.type === 'CatchClause' && state.parent.param === node
) {
return;
}
// Avoid modifying property names in MemberExpression (object.property)
if (
state.parent.type === 'MemberExpression' &&
state.parent.property === node &&
!state.parent.computed
) {
return;
}
// Avoid modifying labels
if (state.parent.type === 'LabeledStatement') {
return;
}
// Get the name of the identifier
const name = node.name;
if (shouldPrefix(name)) {
// Check if already prefixed with 'this.'
const before = this.magicString.slice(node.start - 5, node.start);
if (before !== 'this.') {
this.magicString.prependLeft(node.start, 'this.');
}
}
}.bind(this) // Bind `this` to access `magicString`
}, walk.base, { parent: null });
// Update the script content with modifications
// Not necessary here as MagicString modifies directly
}
}
module.exports = ScriptInterpreter;
Tracking Global Variables and Methods:
globalVariables and methods: Changed from arrays to Set for efficient lookups and to avoid duplicates.extractDeclaredNames: Handles various patterns (identifiers, object patterns, array patterns, rest elements) to accurately collect variable names, including those declared via destructuring.Using MagicString for Code Modifications:
MagicString: Integrated the magic-string library to handle code modifications based on AST node positions without complex string manipulations.npm install magic-string
MagicString with the original script content and applies modifications during AST traversal.Modifying Identifiers with this. Prefix:
modifyScript Method: Traverses the AST again to identify all Identifier nodes. If an identifier matches a tracked global variable or method and isn't already prefixed with this., it prefixes it accordingly.let, const, function names, etc.).object.property.Updating the Script with Modifications:
MagicString holds the transformed script with the appropriate this. prefixes.transformedScript: Returns the modified script content, which can then be used to build the class.While the above enhancements handle the majority of common scenarios, it's crucial to be aware of potential edge cases:
Shadowed Variables: If a variable declared within a function shares the same name as a global variable, the function's local variable should not be prefixed with this.. The current implementation does not handle scoping nuances and assumes that all references to tracked identifiers are intended to be class properties. To handle scoping accurately, a more sophisticated approach involving scope analysis is required.
Arrow Functions and this Binding: Arrow functions do not have their own this context. Depending on your use case, you might need to adjust how this is handled within arrow functions.
Dynamic Property Access: The current implementation does not handle computed property accesses (e.g., this[dynamicProperty]).
To address shadowed variables and more complex scoping, you might consider using additional libraries or implementing a scope-tracking mechanism within your interpreter.
Let's apply the enhanced ScriptInterpreter to your sample script:
Original Script:
let counter = 0;
let button = document.getElementById('button');
let ps = document.getElementsByClassName('isap');
const tester = (() => {
console.log(counter);
});
function method() {
tester();
another();
}
button.addEventListener('click', (() => console.log('hello')));
Transformed Script:
this.counter = 0;
this.button = document.getElementById('button');
this.ps = document.getElementsByClassName('isap');
const tester_1 = (() => {
console.log(this.counter);
});
function method_2() {
this.tester_1();
another();
}
this.button.addEventListener('click', (() => console.log('hello')));
Explanation of Transformations:
Global Variables: counter, button, and ps are tracked and prefixed with this..
Function Expressions and Declarations:
tester is an IIFE and is transformed to tester_1 (to ensure uniqueness), and its internal reference to counter becomes this.counter.method is a function declaration transformed to method_2, and its call to tester() becomes this.tester_1(). The call to another() remains unchanged since it's an external function.Event Listeners: The button.addEventListener call is prefixed with this..
Ensure that your index.js or the module responsible for orchestrating the parsing pipeline utilizes the enhanced ScriptInterpreter. Here's how you might adjust index.js to handle the transformed script:
Key Adjustments:
Consume transformedScript: If you need to include the transformed script within your custom elements, ensure that you're utilizing the transformedScript returned by ScriptInterpreter.
Avoid Duplicated Scripts: Ensure that scripts are not inadvertently duplicated in the final indexContent. Adjust as necessary based on your project's specific needs.
To verify that the enhancements work as intended, create a test HTML file and run the compiler.
Test HTML (sample.comp.html):
<custom-element>
<button id="button">Click Me</button>
<p class="isap">Paragraph</p>
<script>
let counter = 0;
let button = document.getElementById('button');
let ps = document.getElementsByClassName('isap');
const tester = (() => {
console.log(counter);
});
function method() {
tester();
another();
}
button.addEventListener('click', (() => console.log('hello')));
</script>
<style>
/* Your styles here */
</style>
</custom-element>
Compile and Observe the Output:
After running the compiler, inspect the generated class for custom-element. The script within should now have all references to counter, button, ps, and tester prefixed with this..
Expected Transformed Script within the Custom Element:
this.counter = 0;
this.button = document.getElementById('button');
this.ps = document.getElementsByClassName('isap');
const tester_1 = (() => {
console.log(this.counter);
});
function method_2() {
this.tester_1();
another();
}
this.button.addEventListener('click', (() => console.log('hello')));
Scope Analysis: For more complex scripts, consider implementing a comprehensive scope analysis to accurately determine which identifiers should be prefixed with this..
Using Advanced AST Manipulation Libraries: Libraries like Recast or Babel offer more advanced AST manipulation capabilities and might simplify the transformation process.
Unit Testing: Implement unit tests to ensure that the interpreter correctly prefixes this. where necessary without introducing bugs.
Error Handling: Enhance error handling within the ScriptInterpreter to provide more informative messages, especially when encountering unexpected code structures.
By following the above enhancements and guidelines, your ScriptInterpreter will effectively track global variables and methods, ensuring that all internal references within your component scripts are correctly prefixed with this., thereby maintaining the integrity and functionality of your custom elements.
Not yet included in a published release.
const path = require('path');
const fs = require('fs-extra');
const { collectHTMLFiles } = require('./collector');
const ParsingPipeline = require('./parser');
const Interpreter = require('./interpreter');
const ScriptInterpreter = require('./sinterpreter'); // Ensure this path is correct
const Builder = require('./builder');
const { transformToJavaScript } = require('./transformer');
const DOMResolver = require('./domResolver');
async function initializeCompiler(indexPath) {
const projectRoot = path.dirname(indexPath);
const collectedFiles = await collectHTMLFiles(projectRoot);
const processedFiles = {};
const scriptInterpreter = new ScriptInterpreter();
const builtComponents = {};
for (const [file, fileData] of Object.entries(collectedFiles)) {
try {
// Parsing step
const parsed = ParsingPipeline.parseHTML(fileData.body, file);
// HTML Interpretation step
const interpreted = {
...parsed,
body: Interpreter.interpretBody(parsed.body)
};
// Script Interpretation step
const scriptInterpreted = interpreted.script
? scriptInterpreter.interpret(interpreted.script)
: { variables: [], methods: [], calls: [], eventListeners: { connect: [], disconnect: [] } };
// Transformation step
const transformedDOM = transformToJavaScript(interpreted.body);
// New DOM Resolution step
const domResolver = new DOMResolver(transformedDOM);
const { script: resolvedScript, replacements } = domResolver.resolve(scriptInterpreted);
processedFiles[file] = {
...interpreted,
script: resolvedScript,
domReplacements: replacements
};
// Building step
const builtComponent = Builder.buildCustomElement(processedFiles[file], transformedDOM);
builtComponents[file] = builtComponent;
console.log(`\nFile: ${file}`);
console.log('Type:', interpreted.type);
console.log('Tag Name:', interpreted.tagName);
console.log('Parsed body:', JSON.stringify(parsed.body, null, 2));
console.log('Interpreted body:', JSON.stringify(interpreted.body, null, 2));
console.log('Transformed DOM:', transformedDOM);
console.log('Resolved script:', JSON.stringify(resolvedScript, null, 2));
console.log('DOM Replacements:', JSON.stringify(replacements, null, 2));
console.log('Built Component:', builtComponent);
console.log('Style:', interpreted.style);
} catch (error) {
console.error(`Error processing ${file}:`, error.message);
}
}
return {
async compile() {
let indexContent = await fs.readFile(indexPath, 'utf-8');
const debugInfo = `
<script>
console.log('Processed files:', ${JSON.stringify(processedFiles, null, 2)});
console.log('Built Components:', ${JSON.stringify(builtComponents, null, 2)});
</script>
`;
indexContent = indexContent.replace('</head>', `${debugInfo}</head>`);
const componentsScript = Object.values(builtComponents).join('\n\n');
indexContent = indexContent.replace('</head>', `<script>${componentsScript}</script></head>`);
return indexContent;
}
};
}
module.exports = { initializeCompiler };