use std::path::Path;
use oxc_ast::ast::*;
use super::{
MAX_VITE_PLUGIN_WRAPPER_DEPTH, PluginImportBindings, collect_plugin_import_bindings,
expression_to_path_string, expression_to_string, extract_from_source, find_property,
get_nested_expression,
};
#[must_use]
pub fn extract_vite_plugin_call_option_path(
source: &str,
path: &Path,
package: &str,
option_path: &[&str],
) -> Option<String> {
read_vite_plugin_call_options(source, path, package, |options| {
expression_to_path_string(get_nested_expression(options?, option_path)?)
})
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StringListOption {
Missing,
Values(Vec<String>),
Unreadable,
}
#[must_use]
pub fn extract_vite_plugin_call_string_lists(
source: &str,
path: &Path,
package: &str,
keys: &[&str],
object_key: &str,
) -> Option<Vec<StringListOption>> {
read_vite_plugin_call_options(source, path, package, |options| {
Some(
keys.iter()
.map(|key| {
options
.and_then(|options| find_property(options, key))
.map_or(StringListOption::Missing, |property| {
string_list(&property.value, object_key)
})
})
.collect(),
)
})
}
fn string_list(expression: &Expression<'_>, object_key: &str) -> StringListOption {
if let Some(value) = expression_to_string(expression) {
return StringListOption::Values(vec![value]);
}
let Expression::ArrayExpression(array) = expression else {
return StringListOption::Unreadable;
};
let values: Option<Vec<String>> = array
.elements
.iter()
.map(|element| match element.as_expression()? {
Expression::ObjectExpression(object) => {
expression_to_string(&find_property(object, object_key)?.value)
}
expression => expression_to_string(expression),
})
.collect();
values.map_or(StringListOption::Unreadable, StringListOption::Values)
}
fn read_vite_plugin_call_options<T: Send>(
source: &str,
path: &Path,
package: &str,
read: impl Fn(Option<&ObjectExpression<'_>>) -> Option<T> + Send + Sync,
) -> Option<T> {
extract_from_source(source, path, |program| {
let imports = collect_plugin_import_bindings(program, package);
if imports.is_empty() {
return None;
}
super::read_last_match(program, |obj| {
let Expression::ArrayExpression(plugin_array) =
get_nested_expression(obj, &["plugins"])?
else {
return None;
};
plugin_array.elements.iter().find_map(|element| {
let Some(Expression::CallExpression(call)) = element.as_expression() else {
return None;
};
let call = find_plugin_binding_call(call, &imports, 0)?;
match call.arguments.first().map(Argument::as_expression) {
None => read(None),
Some(Some(Expression::ObjectExpression(options))) => read(Some(options)),
Some(_) => None,
}
})
})
})
}
fn find_plugin_binding_call<'a>(
call: &'a CallExpression<'a>,
imports: &PluginImportBindings,
depth: usize,
) -> Option<&'a CallExpression<'a>> {
if is_plugin_binding_call(call, imports) {
return Some(call);
}
if depth >= MAX_VITE_PLUGIN_WRAPPER_DEPTH {
return None;
}
call.arguments
.iter()
.find_map(|argument| match argument.as_expression() {
Some(Expression::CallExpression(inner)) => {
find_plugin_binding_call(inner, imports, depth + 1)
}
_ => None,
})
}
pub(super) fn is_plugin_binding_call(
call: &CallExpression<'_>,
imports: &PluginImportBindings,
) -> bool {
match &call.callee {
Expression::Identifier(identifier) => imports
.callables
.iter()
.any(|name| name == identifier.name.as_str()),
Expression::StaticMemberExpression(member) if matches!(&member.object, Expression::Identifier(object) if imports.namespaces.iter().any(|name| name == object.name.as_str())) => {
member.property.name == "default"
}
_ => false,
}
}