use compmd::analysis::{Prop, PropType};
use oxc_ast::ast::{
Declaration, Program, PropertyKey, Statement, TSInterfaceDeclaration, TSLiteral, TSSignature,
TSType, TSTypeAliasDeclaration, TSTypeName,
};
const SLOT_TYPES: [&str; 4] = ["ReactNode", "ReactElement", "JSX.Element", "Snippet"];
fn name_of(name: &TSTypeName<'_>) -> Option<String> {
match name {
TSTypeName::IdentifierReference(identifier) => Some(identifier.name.to_string()),
TSTypeName::QualifiedName(qualified) => Some(format!(
"{}.{}",
name_of(&qualified.left)?,
qualified.right.name
)),
TSTypeName::ThisExpression(_) => None,
}
}
fn is_nothing(member: &TSType<'_>) -> bool {
matches!(
member,
TSType::TSUndefinedKeyword(_) | TSType::TSNullKeyword(_)
)
}
fn literal_of<'t, 'a>(member: &'t TSType<'a>) -> Option<&'t TSLiteral<'a>> {
match member {
TSType::TSLiteralType(literal) => Some(&literal.literal),
_ => None,
}
}
pub(crate) fn type_of(node: &TSType<'_>) -> Option<PropType> {
match node {
TSType::TSStringKeyword(_) => Some(PropType::String),
TSType::TSNumberKeyword(_) => Some(PropType::Number),
TSType::TSBooleanKeyword(_) => Some(PropType::Boolean),
TSType::TSParenthesizedType(inner) => type_of(&inner.type_annotation),
TSType::TSTypeReference(reference) => {
let name = name_of(&reference.type_name)?;
let name = name.strip_prefix("React.").unwrap_or(&name);
SLOT_TYPES.contains(&name).then_some(PropType::Slot)
}
TSType::TSLiteralType(literal) => match &literal.literal {
TSLiteral::StringLiteral(string) => Some(PropType::Enum {
members: vec![string.value.to_string()],
}),
_ => None,
},
TSType::TSUnionType(union) => {
let members: Vec<&TSType<'_>> = union
.types
.iter()
.filter(|member| !is_nothing(member))
.collect();
if let [only] = members.as_slice() {
return type_of(only);
}
let literals: Option<Vec<&TSLiteral<'_>>> =
members.iter().map(|member| literal_of(member)).collect();
let literals = literals?;
let strings: Option<Vec<String>> = literals
.iter()
.map(|literal| match literal {
TSLiteral::StringLiteral(string) => Some(string.value.to_string()),
_ => None,
})
.collect();
if let Some(members) = strings {
return Some(PropType::Enum { members });
}
literals
.iter()
.all(|literal| matches!(literal, TSLiteral::BooleanLiteral(_)))
.then_some(PropType::Boolean)
}
_ => None,
}
}
pub(crate) fn key_name(key: &PropertyKey<'_>) -> Option<String> {
match key {
PropertyKey::StaticIdentifier(identifier) => Some(identifier.name.to_string()),
PropertyKey::StringLiteral(string) => Some(string.value.to_string()),
_ => None,
}
}
fn props_of_members(members: &[TSSignature<'_>]) -> Option<Vec<Prop>> {
members
.iter()
.map(|member| match member {
TSSignature::TSPropertySignature(property) if !property.computed => Some(Prop {
name: key_name(&property.key)?,
kind: property
.type_annotation
.as_ref()
.and_then(|annotation| type_of(&annotation.type_annotation)),
optional: property.optional,
default: None,
}),
_ => None,
})
.collect()
}
fn named_members(program: &Program<'_>, name: &str) -> Option<Vec<Prop>> {
for statement in &program.body {
let declaration = match statement {
Statement::TSInterfaceDeclaration(interface) => Some(Declared::Interface(interface)),
Statement::TSTypeAliasDeclaration(alias) => Some(Declared::Alias(alias)),
Statement::ExportNamedDeclaration(export) => match &export.declaration {
Some(Declaration::TSInterfaceDeclaration(interface)) => {
Some(Declared::Interface(interface))
}
Some(Declaration::TSTypeAliasDeclaration(alias)) => Some(Declared::Alias(alias)),
_ => None,
},
_ => None,
};
match declaration {
Some(Declared::Interface(interface)) if interface.id.name == name => {
return (interface.extends.is_empty() && interface.type_parameters.is_none())
.then(|| props_of_members(&interface.body.body))
.flatten();
}
Some(Declared::Alias(alias)) if alias.id.name == name => {
return alias
.type_parameters
.is_none()
.then(|| members_of(program, &alias.type_annotation))
.flatten();
}
_ => {}
}
}
None
}
enum Declared<'d, 'a> {
Interface(&'d TSInterfaceDeclaration<'a>),
Alias(&'d TSTypeAliasDeclaration<'a>),
}
pub(crate) fn members_of(program: &Program<'_>, node: &TSType<'_>) -> Option<Vec<Prop>> {
match node {
TSType::TSTypeLiteral(literal) => props_of_members(&literal.members),
TSType::TSParenthesizedType(inner) => members_of(program, &inner.type_annotation),
TSType::TSIntersectionType(intersection) => {
let parts: Option<Vec<Vec<Prop>>> = intersection
.types
.iter()
.map(|part| members_of(program, part))
.collect();
parts.map(|parts| parts.into_iter().flatten().collect())
}
TSType::TSTypeReference(reference) if reference.type_arguments.is_none() => {
match &reference.type_name {
TSTypeName::IdentifierReference(identifier) => {
named_members(program, &identifier.name)
}
_ => None,
}
}
_ => None,
}
}