use oxc_ast::AstKind;
use oxc_ast::ast::{
ArrowFunctionExpression, BindingPattern, Class, Expression, Function, FunctionBody,
ReturnStatement, Statement, VariableDeclarationKind,
};
use oxc_ast_visit::Visit;
use oxc_semantic::SymbolId;
use oxc_syntax::scope::ScopeFlags;
use super::{SchemaShape, SchemaShapes, unwrap};
const MAX_HELPER_DEPTH: usize = 4;
impl<'a> SchemaShapes<'_, 'a> {
pub fn is_schema_helper(&self, name: &str) -> bool {
let scoping = self.semantic.scoping();
scoping
.get_binding(scoping.root_scope_id(), oxc_str::Ident::from(name))
.and_then(|symbol| self.helper_call_shape(symbol))
== Some(SchemaShape::Known)
}
pub(super) fn helper_call_shape(&self, symbol: SymbolId) -> Option<SchemaShape> {
if let Some(shape) = self.helpers.borrow().get(&symbol) {
return Some(shape.clone());
}
let body = self.helper_body(symbol)?;
if self.helpers_in_progress.borrow().len() >= MAX_HELPER_DEPTH
|| !self.helpers_in_progress.borrow_mut().insert(symbol)
{
return None;
}
let shape = match body {
HelperBody::Expression(expression) => self.expression_shape(expression),
HelperBody::Block(body) => self.block_shape(body),
};
self.helpers_in_progress.borrow_mut().remove(&symbol);
if let Some(shape) = &shape {
self.helpers.borrow_mut().insert(symbol, shape.clone());
}
shape
}
fn helper_body(&self, symbol: SymbolId) -> Option<HelperBody<'_, 'a>> {
let scoping = self.semantic.scoping();
if scoping.symbol_is_mutated(symbol) {
return None;
}
let mut body = None;
for declaration in scoping.symbol_declarations(symbol) {
let found = match self.semantic.nodes().kind(declaration) {
AstKind::TSTypeAliasDeclaration(_) | AstKind::TSInterfaceDeclaration(_) => continue,
AstKind::Function(function) if function.body.is_none() => continue,
AstKind::Function(function) => function_body(function)?,
AstKind::VariableDeclarator(declarator) => {
if !scoping.symbol_flags(symbol).is_const_variable() {
return None;
}
match unwrap(declarator.init.as_ref()?) {
Expression::ArrowFunctionExpression(arrow) => arrow_body(arrow)?,
Expression::FunctionExpression(function) => function_body(function)?,
_ => return None,
}
}
_ => return None,
};
if body.replace(found).is_some() {
return None;
}
}
body
}
fn block_shape(&self, body: &FunctionBody<'a>) -> Option<SchemaShape> {
if !always_exits(&body.statements) {
return None;
}
for statement in &body.statements {
let Statement::VariableDeclaration(declaration) = statement else {
continue;
};
if declaration.kind != VariableDeclarationKind::Const {
continue;
}
for declarator in &declaration.declarations {
let (BindingPattern::BindingIdentifier(id), Some(init)) =
(&declarator.id, &declarator.init)
else {
continue;
};
if let Some(shape) = self.expression_shape(init) {
self.helper_locals
.borrow_mut()
.insert(id.symbol_id(), shape);
}
}
}
let mut returns = Returns {
shapes: self,
shape: None,
all_schemas: true,
};
returns.visit_function_body(body);
returns.shape.filter(|_| returns.all_schemas)
}
}
enum HelperBody<'s, 'a> {
Expression(&'s Expression<'a>),
Block(&'s FunctionBody<'a>),
}
fn function_body<'s, 'a>(function: &'s Function<'a>) -> Option<HelperBody<'s, 'a>> {
if function.r#async || function.generator {
return None;
}
function.body.as_deref().map(HelperBody::Block)
}
fn arrow_body<'s, 'a>(arrow: &'s ArrowFunctionExpression<'a>) -> Option<HelperBody<'s, 'a>> {
if arrow.r#async {
return None;
}
if let Some(expression) = arrow.get_expression() {
return Some(HelperBody::Expression(expression));
}
arrow.body.as_function_body().map(HelperBody::Block)
}
fn always_exits(statements: &[Statement<'_>]) -> bool {
statements.last().is_some_and(statement_always_exits)
}
fn statement_always_exits(statement: &Statement<'_>) -> bool {
match statement {
Statement::ReturnStatement(_) | Statement::ThrowStatement(_) => true,
Statement::BlockStatement(block) => always_exits(&block.body),
Statement::IfStatement(branch) => {
statement_always_exits(&branch.consequent)
&& branch
.alternate
.as_ref()
.is_some_and(statement_always_exits)
}
_ => false,
}
}
struct Returns<'h, 's, 'a> {
shapes: &'h SchemaShapes<'s, 'a>,
shape: Option<SchemaShape>,
all_schemas: bool,
}
impl Returns<'_, '_, '_> {
fn join(&mut self, next: Option<SchemaShape>) {
let Some(next) = next else {
self.all_schemas = false;
return;
};
match (&self.shape, &next) {
(None | Some(SchemaShape::Known), _) => self.shape = Some(next),
(Some(_), SchemaShape::Known) => {}
(Some(current), _) if *current == next => {}
_ => self.all_schemas = false,
}
}
}
impl<'a> Visit<'a> for Returns<'_, '_, 'a> {
fn visit_return_statement(&mut self, statement: &ReturnStatement<'a>) {
let shape = statement
.argument
.as_ref()
.and_then(|argument| self.shapes.expression_shape(argument));
self.join(shape);
}
fn visit_function(&mut self, _: &Function<'a>, _: ScopeFlags) {}
fn visit_arrow_function_expression(&mut self, _: &ArrowFunctionExpression<'a>) {}
fn visit_class(&mut self, _: &Class<'a>) {}
}