Skip to content

Commit

Permalink
Add tests for dependency kind
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeLane committed Aug 5, 2024
1 parent 7c76b0c commit bfd803b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ pub(crate) fn convert_specifier_type(

#[cfg(test)]
mod test {
use crate::transformer::test_helpers::run_swc_core_transform;
use parcel_js_swc_core::DependencyKind;
use crate::transformer::test_helpers::{
make_test_swc_config, run_swc_core_transform, run_swc_core_transform_with_config,
};
use parcel_js_swc_core::{Config, DependencyKind};

use super::*;

Expand All @@ -59,6 +61,36 @@ mod test {
assert_eq!(convert_specifier_type(&dependency), SpecifierType::Esm);
}

#[test]
fn test_deferred_for_display_dependency_kind() {
let dependency = get_last_dependency_with_config(Config {
tier_imports: true,
..make_test_swc_config(
r#"
const x = importDeferredForDisplay('other');
"#,
)
});
assert_eq!(dependency.kind, DependencyKind::DeferredForDisplayImport);
assert_eq!(convert_priority(&dependency), Priority::Tier);
assert_eq!(convert_specifier_type(&dependency), SpecifierType::Esm);
}

#[test]
fn test_deferred_dependency_kind() {
let dependency = get_last_dependency_with_config(Config {
tier_imports: true,
..make_test_swc_config(
r#"
const x = importDeferred('other');
"#,
)
});
assert_eq!(dependency.kind, DependencyKind::DeferredImport);
assert_eq!(convert_priority(&dependency), Priority::Tier);
assert_eq!(convert_specifier_type(&dependency), SpecifierType::Esm);
}

#[test]
fn test_dynamic_import_dependency_kind() {
let dependency = get_last_dependency(
Expand Down Expand Up @@ -171,4 +203,10 @@ mod test {
let swc_output = run_swc_core_transform(source);
swc_output.dependencies.last().unwrap().clone()
}

/// Run the SWC transformer with a config and return the last dependency descriptor listed
fn get_last_dependency_with_config(config: Config) -> parcel_js_swc_core::DependencyDescriptor {
let swc_output = run_swc_core_transform_with_config(config);
swc_output.dependencies.last().unwrap().clone()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ pub(crate) fn run_swc_core_transform(source: &str) -> TransformResult {
swc_output
}

/// Parse a file with the `parcel_js_swc_core` parser for testing and specify a config
pub(crate) fn run_swc_core_transform_with_config(config: Config) -> TransformResult {
let swc_output = parcel_js_swc_core::transform(config, None).unwrap();
swc_output
}

/// SWC configuration for testing
pub(crate) fn make_test_swc_config(source: &str) -> Config {
Config {
Expand Down
11 changes: 8 additions & 3 deletions packages/transformers/js/core/src/dependency_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,10 +443,15 @@ impl<'a> Fold for DependencyCollector<'a> {
Callee::Import(_) => DependencyKind::DynamicImport,
Callee::Expr(expr) => {
match &**expr {
Ident(ident) if ident.sym.to_string().as_str() == "importDeferredForDisplay" => {
Ident(ident)
if self.config.tier_imports
&& ident.sym.to_string().as_str() == "importDeferredForDisplay" =>
{
DependencyKind::DeferredForDisplayImport
}
Ident(ident) if ident.sym.to_string().as_str() == "importDeferred" => {
Ident(ident)
if self.config.tier_imports && ident.sym.to_string().as_str() == "importDeferred" =>
{
DependencyKind::DeferredImport
}
Ident(ident) => {
Expand Down Expand Up @@ -795,7 +800,7 @@ impl<'a> Fold for DependencyCollector<'a> {
message: format!("{} requires 1 argument", kind),
code_highlights: Some(vec![CodeHighlight {
message: None,
loc: SourceLocation::from(self.source_map, call.span),
loc: SourceLocation::from(&self.source_map, call.span),
}]),
hints: None,
show_environment: false,
Expand Down

0 comments on commit bfd803b

Please sign in to comment.