use std::cell::RefCell;
use oxc_ast::ast::{Argument, Class, Expression, IdentifierReference};
use oxc_semantic::{Semantic, SymbolId};
use rustc_hash::{FxHashMap, FxHashSet};
use fallow_types::extract::{ImportInfo, ImportedName};
#[path = "effect_schema_names.rs"]
mod names;
use names::{HTTP_API_PIPE_TRANSFORMS, PIPE_TRANSFORMS, TYPE_ONLY_NAMES};
#[path = "effect_schema_helpers.rs"]
mod helpers;
const LOWERCASE_SCHEMA_CONSTRUCTORS: &[&str] = &[
"compose",
"declare",
"encodedSchema",
"extend",
"parseJson",
"suspend",
"toEncoded",
"toType",
"transform",
"transformOrFail",
"typeSchema",
];
const CLASS_FACTORIES: &[&str] = &[
"Class",
"Error",
"Opaque",
"TaggedClass",
"TaggedError",
"TaggedRequest",
];
const PIPE_CONSTRUCTORS: &[&str] = &[
"Array",
"NonEmptyArray",
"NullOr",
"NullishOr",
"UndefinedOr",
];
const SCHEMA_METHODS: &[&str] = &[
"annotate",
"annotateKey",
"annotations",
"brand",
"check",
"extend",
"mapFields",
"mapMembers",
"omit",
"pick",
"pipe",
"rename",
];
fn is_schema_module_import(import: &ImportInfo) -> bool {
match &import.imported_name {
ImportedName::Named(name) => {
name == "Schema" && matches!(import.source.as_str(), "effect" | "@effect/schema")
}
ImportedName::Namespace => {
matches!(
import.source.as_str(),
"effect/Schema" | "@effect/schema/Schema"
)
}
ImportedName::Default | ImportedName::SideEffect => false,
}
}
fn is_http_api_schema_module_import(import: &ImportInfo) -> bool {
match &import.imported_name {
ImportedName::Named(name) => {
name == "HttpApiSchema"
&& matches!(
import.source.as_str(),
"effect/http-api" | "@effect/platform"
)
}
ImportedName::Namespace => {
matches!(
import.source.as_str(),
"effect/http-api/HttpApiSchema" | "@effect/platform/HttpApiSchema"
)
}
ImportedName::Default | ImportedName::SideEffect => false,
}
}
fn is_schema_constructor(name: &str) -> bool {
if name.starts_with(|c: char| c.is_ascii_uppercase()) {
return !TYPE_ONLY_NAMES.contains(&name);
}
LOWERCASE_SCHEMA_CONSTRUCTORS.contains(&name)
}
fn is_class_factory(name: &str) -> bool {
CLASS_FACTORIES.contains(&name)
}
fn is_pipe_call(callee: &Expression<'_>) -> bool {
matches!(
unwrap(callee),
Expression::StaticMemberExpression(member) if member.property.name == "pipe"
)
}
fn unwrap<'e, 'a>(expression: &'e Expression<'a>) -> &'e Expression<'a> {
let mut current = expression.without_parentheses();
loop {
current = match current {
Expression::TSAsExpression(node) => node.expression.without_parentheses(),
Expression::TSSatisfiesExpression(node) => node.expression.without_parentheses(),
Expression::TSNonNullExpression(node) => node.expression.without_parentheses(),
Expression::TSInstantiationExpression(node) => node.expression.without_parentheses(),
_ => return current,
};
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SchemaShape {
Known,
Derived {
import_local: String,
through_call: bool,
},
}
pub struct SchemaShapes<'s, 'a> {
semantic: &'s Semantic<'a>,
namespaces: FxHashSet<SymbolId>,
http_api_namespaces: FxHashSet<SymbolId>,
imports: FxHashMap<SymbolId, String>,
locals: FxHashMap<SymbolId, SchemaShape>,
local_names: FxHashMap<String, SchemaShape>,
helpers: RefCell<FxHashMap<SymbolId, SchemaShape>>,
helper_locals: RefCell<FxHashMap<SymbolId, SchemaShape>>,
helpers_in_progress: RefCell<FxHashSet<SymbolId>>,
}
impl<'s, 'a> SchemaShapes<'s, 'a> {
pub fn new(semantic: &'s Semantic<'a>, imports: &[ImportInfo]) -> Self {
let namespaces = imports
.iter()
.filter(|import| is_schema_module_import(import))
.filter_map(|import| {
crate::binding_references::admitted_import_symbol(semantic, import)
})
.collect();
let http_api_namespaces = imports
.iter()
.filter(|import| is_http_api_schema_module_import(import))
.filter_map(|import| {
crate::binding_references::admitted_import_symbol(semantic, import)
})
.collect();
let imports = imports
.iter()
.filter(|import| {
!import.is_type_only
&& matches!(
import.imported_name,
ImportedName::Named(_) | ImportedName::Default
)
&& !is_schema_module_import(import)
&& !is_http_api_schema_module_import(import)
})
.filter_map(|import| {
crate::binding_references::admitted_import_symbol(semantic, import)
.map(|symbol| (symbol, import.local_name.clone()))
})
.collect();
Self {
semantic,
namespaces,
http_api_namespaces,
imports,
locals: FxHashMap::default(),
local_names: FxHashMap::default(),
helpers: RefCell::new(FxHashMap::default()),
helper_locals: RefCell::new(FxHashMap::default()),
helpers_in_progress: RefCell::new(FxHashSet::default()),
}
}
pub fn record(&mut self, symbol: SymbolId, name: &str, shape: SchemaShape) {
if self.semantic.scoping().symbol_is_mutated(symbol) {
return;
}
self.locals.insert(symbol, shape.clone());
self.local_names.insert(name.to_string(), shape);
}
pub fn name_shape(&self, name: &str) -> Option<&SchemaShape> {
self.local_names.get(name)
}
pub fn class_shape(&self, class: &Class<'a>) -> Option<SchemaShape> {
class
.heritage
.as_ref()
.and_then(|heritage| self.schema_chain(&heritage.expression, is_class_factory))
}
pub fn expression_shape(&self, expression: &Expression<'a>) -> Option<SchemaShape> {
self.schema_chain(expression, is_schema_constructor)
}
fn schema_chain(
&self,
expression: &Expression<'a>,
accepts_head: fn(&str) -> bool,
) -> Option<SchemaShape> {
if self.namespaces.is_empty() && self.imports.is_empty() {
return None;
}
let mut members: Vec<&str> = Vec::new();
let mut root_calls = 0_u32;
let mut current = expression;
let root = loop {
current = match unwrap(current) {
Expression::CallExpression(call) => {
root_calls += 1;
if is_pipe_call(&call.callee)
&& !call
.arguments
.iter()
.all(|argument| self.is_pipe_transform(argument))
{
return None;
}
&call.callee
}
Expression::StaticMemberExpression(member) => {
members.push(member.property.name.as_str());
root_calls = 0;
&member.object
}
Expression::Identifier(identifier) => break identifier,
_ => return None,
};
};
let symbol = self.symbol(root)?;
if self.namespaces.contains(&symbol) {
let (head, rest) = members.split_last()?;
return (accepts_head(head)
&& rest.iter().all(|method| SCHEMA_METHODS.contains(method)))
.then_some(SchemaShape::Known);
}
if !members.iter().all(|method| SCHEMA_METHODS.contains(method)) {
return None;
}
if let Some(shape) = self.locals.get(&symbol) {
return Some(shape.clone());
}
if let Some(shape) = self.helper_locals.borrow().get(&symbol) {
return Some(shape.clone());
}
let through_call = root_calls == 1;
if let Some(local) = self.imports.get(&symbol) {
return Some(SchemaShape::Derived {
import_local: local.clone(),
through_call,
});
}
if through_call {
return self.helper_call_shape(symbol);
}
None
}
fn is_pipe_transform(&self, argument: &Argument<'a>) -> bool {
let Some(expression) = argument.as_expression() else {
return false;
};
let (callee, called) = match unwrap(expression) {
Expression::CallExpression(call) => (unwrap(&call.callee), true),
other => (other, false),
};
let Expression::StaticMemberExpression(member) = callee else {
return false;
};
let Expression::Identifier(object) = unwrap(&member.object) else {
return false;
};
let name = member.property.name.as_str();
let Some(symbol) = self.symbol(object) else {
return false;
};
if self.http_api_namespaces.contains(&symbol) {
return called && HTTP_API_PIPE_TRANSFORMS.contains(&name);
}
let known =
PIPE_TRANSFORMS.contains(&name) || (!called && PIPE_CONSTRUCTORS.contains(&name));
known && self.namespaces.contains(&symbol)
}
fn symbol(&self, identifier: &IdentifierReference<'a>) -> Option<SymbolId> {
let reference = identifier.reference_id.get()?;
self.semantic.scoping().get_reference(reference).symbol_id()
}
}