validate method
Validates the JavaScript AST with context-aware rules Checks for syntax errors, undefined variables/objects, invalid property or method access, and provides typo suggestions. Throws JSException if validation fails; returns true if valid.
Implementation
bool validate({Node? node}) {
try {
if (node != null) {
return visit(node) ?? true;
}
// First pass: collect all function declarations and variable declarations
for (var stmt in program.body) {
if (stmt is FunctionDeclaration) {
if (stmt.function.name != null) {
_interpreter.addToContext(stmt.function.name!, true);
}
// Create a new context for the function
Context functionContext = SimpleContext({});
// Add function parameters to the context
if (stmt.function.params != null) {
for (var param in stmt.function.params) {
if (param is Name) {
functionContext.addDataContextById(param.value, null);
}
}
}
// Store the function context
_interpreter.contexts[stmt.function] = functionContext;
} else if (stmt is VariableDeclaration) {
for (var declarator in stmt.declarations) {
_interpreter.addToContext(declarator.name, true);
}
}
}
// Second pass: validate everything
for (var stmt in program.body) {
visit(stmt);
}
return true;
} catch (e) {
if (e is JSException) throw e;
throw JSException(1, e.toString(),
detailedError: 'Code: $code', originalError: e);
}
}