1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
use std::collections::HashMap;

use swc_atoms::{js_word, JsWord};
use swc_common::{
    comments::{Comment, Comments},
    Span,
};
use swc_ecma_ast as ast;
use swc_ecma_visit::{self, Visit, VisitWith};

pub fn analyze_dependencies(
    module: &ast::Module,
    comments: &dyn Comments,
) -> Vec<DependencyDescriptor> {
    let mut v = DependencyCollector {
        comments,
        items: vec![],
        is_top_level: true,
    };
    module.visit_with(&mut v);
    v.items
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum DependencyKind {
    Import,
    ImportType,
    ImportEquals,
    Export,
    ExportType,
    ExportEquals,
    Require,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ImportAssertion {
    /// The value of this assertion could not be statically analyzed.
    Unknown,
    /// The value of this assertion is a statically analyzed string.
    Known(String),
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ImportAttributes {
    /// There was no import assertions object literal.
    None,
    /// The set of assertion keys could not be statically analyzed.
    Unknown,
    /// The set of assertion keys is statically analyzed, though each respective
    /// value may or may not not be for dynamic imports.
    Known(HashMap<String, ImportAssertion>),
}

impl Default for ImportAttributes {
    fn default() -> Self {
        ImportAttributes::None
    }
}

impl ImportAttributes {
    pub fn get(&self, key: &str) -> Option<&String> {
        match self {
            ImportAttributes::Known(map) => match map.get(key) {
                Some(ImportAssertion::Known(value)) => Some(value),
                _ => None,
            },
            _ => None,
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DependencyDescriptor {
    pub kind: DependencyKind,
    /// A flag indicating if the import is dynamic or not.
    pub is_dynamic: bool,
    /// Any leading comments associated with the dependency.  This is used for
    /// further processing of supported pragma that impact the dependency.
    pub leading_comments: Vec<Comment>,
    /// The span of the import/export statement.
    pub span: Span,
    /// The text specifier associated with the import/export statement.
    pub specifier: JsWord,
    /// The span of the specifier.
    pub specifier_span: Span,
    /// Import assertions for this dependency.
    pub import_attributes: ImportAttributes,
}

struct DependencyCollector<'a> {
    comments: &'a dyn Comments,
    pub items: Vec<DependencyDescriptor>,
    // This field is used to determine if currently visited "require"
    // is top level and "static", or inside module body and "dynamic".
    is_top_level: bool,
}

impl<'a> DependencyCollector<'a> {
    fn get_leading_comments(&self, span: Span) -> Vec<Comment> {
        self.comments.get_leading(span.lo).unwrap_or_default()
    }
}

impl<'a> Visit for DependencyCollector<'a> {
    fn visit_import_decl(&mut self, node: &ast::ImportDecl) {
        let specifier = node.src.value.clone();
        let leading_comments = self.get_leading_comments(node.span);
        let kind = if node.type_only {
            DependencyKind::ImportType
        } else {
            DependencyKind::Import
        };
        let import_attributes = parse_import_attributes(node.with.as_deref());
        self.items.push(DependencyDescriptor {
            kind,
            is_dynamic: false,
            leading_comments,
            span: node.span,
            specifier,
            specifier_span: node.src.span,
            import_attributes,
        });
    }

    fn visit_named_export(&mut self, node: &ast::NamedExport) {
        if let Some(src) = &node.src {
            let specifier = src.value.clone();
            let leading_comments = self.get_leading_comments(node.span);
            let kind = if node.type_only {
                DependencyKind::ExportType
            } else {
                DependencyKind::Export
            };
            let import_attributes = parse_import_attributes(node.with.as_deref());
            self.items.push(DependencyDescriptor {
                kind,
                is_dynamic: false,
                leading_comments,
                span: node.span,
                specifier,
                specifier_span: src.span,
                import_attributes,
            });
        }
    }

    fn visit_export_all(&mut self, node: &ast::ExportAll) {
        let specifier = node.src.value.clone();
        let leading_comments = self.get_leading_comments(node.span);
        let kind = if node.type_only {
            DependencyKind::ExportType
        } else {
            DependencyKind::Export
        };
        let import_attributes = parse_import_attributes(node.with.as_deref());
        self.items.push(DependencyDescriptor {
            kind,
            is_dynamic: false,
            leading_comments,
            span: node.span,
            specifier,
            specifier_span: node.src.span,
            import_attributes,
        });
    }

    fn visit_ts_import_type(&mut self, node: &ast::TsImportType) {
        let specifier = node.arg.value.clone();
        let span = node.span;
        let leading_comments = self.get_leading_comments(span);
        self.items.push(DependencyDescriptor {
            kind: DependencyKind::ImportType,
            is_dynamic: false,
            leading_comments,
            span: node.span,
            specifier,
            specifier_span: node.arg.span,
            import_attributes: Default::default(),
        });
    }

    fn visit_module_items(&mut self, items: &[ast::ModuleItem]) {
        swc_ecma_visit::visit_module_items(self, items);
    }

    fn visit_stmts(&mut self, items: &[ast::Stmt]) {
        self.is_top_level = false;
        swc_ecma_visit::visit_stmts(self, items);
        self.is_top_level = true;
    }

    fn visit_call_expr(&mut self, node: &ast::CallExpr) {
        use ast::{Callee, Expr, Ident, MemberProp};

        swc_ecma_visit::visit_call_expr(self, node);
        let kind = match &node.callee {
            Callee::Super(_) => return,
            Callee::Import(_) => DependencyKind::Import,
            Callee::Expr(expr) => match &**expr {
                Expr::Ident(Ident {
                    sym: js_word!("require"),
                    ..
                }) => DependencyKind::Require,
                Expr::Member(member) => match (&*member.obj, &member.prop) {
                    (
                        Expr::Ident(Ident { sym: obj_sym, .. }),
                        MemberProp::Ident(Ident { sym: prop_sym, .. }),
                    ) if obj_sym == "require" && prop_sym == "resolve" => DependencyKind::Require,
                    _ => return,
                },
                _ => return,
            },
        };

        if let Some(arg) = node.args.get(0) {
            if let Expr::Lit(ast::Lit::Str(str_)) = &*arg.expr {
                // import() are always dynamic, even if at top level
                let is_dynamic = !self.is_top_level || kind == DependencyKind::Import;
                let dynamic_import_assertions = if kind == DependencyKind::Import {
                    parse_dynamic_import_assertions(node.args.get(1))
                } else {
                    Default::default()
                };
                let specifier = str_.value.clone();
                let leading_comments = self.get_leading_comments(node.span);
                self.items.push(DependencyDescriptor {
                    kind,
                    is_dynamic,
                    leading_comments,
                    span: node.span,
                    specifier,
                    specifier_span: str_.span,
                    import_attributes: dynamic_import_assertions,
                });
            }
        }
    }

    fn visit_ts_import_equals_decl(&mut self, node: &ast::TsImportEqualsDecl) {
        use ast::TsModuleRef;

        if let TsModuleRef::TsExternalModuleRef(module) = &node.module_ref {
            let leading_comments = self.get_leading_comments(node.span);
            let expr = &module.expr;
            let specifier = expr.value.clone();

            let kind = if node.is_type_only {
                DependencyKind::ImportType
            } else if node.is_export {
                DependencyKind::ExportEquals
            } else {
                DependencyKind::ImportEquals
            };

            self.items.push(DependencyDescriptor {
                kind,
                is_dynamic: false,
                leading_comments,
                span: node.span,
                specifier,
                specifier_span: expr.span,
                import_attributes: Default::default(),
            });
        }
    }
}

/// Parses import assertions into a hashmap. According to proposal the values
/// can only be strings (https://github.com/tc39/proposal-import-assertions#should-more-than-just-strings-be-supported-as-attribute-values)
/// and thus non-string values are skipped.
fn parse_import_attributes(attrs: Option<&ast::ObjectLit>) -> ImportAttributes {
    let attrs = match attrs {
        Some(with) => with,
        None => return ImportAttributes::None,
    };
    let mut import_assertions = HashMap::new();
    for prop in attrs.props.iter() {
        if let ast::PropOrSpread::Prop(prop) = prop {
            if let ast::Prop::KeyValue(key_value) = &**prop {
                let maybe_key = match &key_value.key {
                    ast::PropName::Str(key) => Some(key.value.to_string()),
                    ast::PropName::Ident(ident) => Some(ident.sym.to_string()),
                    _ => None,
                };

                if let Some(key) = maybe_key {
                    if let ast::Expr::Lit(ast::Lit::Str(str_)) = &*key_value.value {
                        import_assertions
                            .insert(key, ImportAssertion::Known(str_.value.to_string()));
                    }
                }
            }
        }
    }
    ImportAttributes::Known(import_assertions)
}

/// Parses import assertions from the second arg of a dynamic import.
fn parse_dynamic_import_assertions(arg: Option<&ast::ExprOrSpread>) -> ImportAttributes {
    let arg = match arg {
        Some(arg) => arg,
        None => return ImportAttributes::None,
    };

    if arg.spread.is_some() {
        return ImportAttributes::Unknown;
    }

    let object_lit = match &*arg.expr {
        ast::Expr::Object(object_lit) => object_lit,
        _ => return ImportAttributes::Unknown,
    };

    let mut assertions_map = HashMap::new();
    let mut had_assert_key = false;

    for prop in object_lit.props.iter() {
        let prop = match prop {
            ast::PropOrSpread::Prop(prop) => prop,
            _ => return ImportAttributes::Unknown,
        };
        let key_value = match &**prop {
            ast::Prop::KeyValue(key_value) => key_value,
            _ => return ImportAttributes::Unknown,
        };
        let key = match &key_value.key {
            ast::PropName::Str(key) => key.value.to_string(),
            ast::PropName::Ident(ident) => ident.sym.to_string(),
            _ => return ImportAttributes::Unknown,
        };
        if key == "assert" || key == "with" {
            had_assert_key = true;
            let assertions_lit = match &*key_value.value {
                ast::Expr::Object(assertions_lit) => assertions_lit,
                _ => return ImportAttributes::Unknown,
            };

            for prop in assertions_lit.props.iter() {
                let prop = match prop {
                    ast::PropOrSpread::Prop(prop) => prop,
                    _ => return ImportAttributes::Unknown,
                };
                let key_value = match &**prop {
                    ast::Prop::KeyValue(key_value) => key_value,
                    _ => return ImportAttributes::Unknown,
                };
                let key = match &key_value.key {
                    ast::PropName::Str(key) => key.value.to_string(),
                    ast::PropName::Ident(ident) => ident.sym.to_string(),
                    _ => return ImportAttributes::Unknown,
                };
                if let ast::Expr::Lit(value_lit) = &*key_value.value {
                    assertions_map.insert(
                        key,
                        if let ast::Lit::Str(str_) = value_lit {
                            ImportAssertion::Known(str_.value.to_string())
                        } else {
                            ImportAssertion::Unknown
                        },
                    );
                } else {
                    assertions_map.insert(key, ImportAssertion::Unknown);
                }
            }
        }
    }

    if had_assert_key {
        ImportAttributes::Known(assertions_map)
    } else {
        ImportAttributes::None
    }
}

#[cfg(test)]
mod tests {
    use ast::EsVersion;
    use pretty_assertions::assert_eq;
    use swc_common::{
        comments::{Comment, CommentKind, SingleThreadedComments},
        BytePos, FileName, Span, SyntaxContext,
    };
    use swc_ecma_parser::{lexer::Lexer, Parser, Syntax, TsConfig};

    use super::*;

    fn helper(
        file_name: &str,
        source: &str,
    ) -> Result<(ast::Module, SingleThreadedComments), testing::StdErr> {
        ::testing::run_test(false, |cm, handler| {
            let fm =
                cm.new_source_file(FileName::Custom(file_name.to_string()), source.to_string());

            let comments = SingleThreadedComments::default();
            let lexer: Lexer = Lexer::new(
                Syntax::Typescript(TsConfig {
                    dts: file_name.ends_with(".d.ts"),
                    tsx: file_name.contains("tsx"),
                    decorators: true,
                    no_early_errors: true,
                    disallow_ambiguous_jsx_like: false,
                }),
                EsVersion::Es2015,
                (&*fm).into(),
                Some(&comments),
            );

            let mut p = Parser::new_from(lexer);

            let res = p
                .parse_module()
                .map_err(|e| e.into_diagnostic(handler).emit());

            for err in p.take_errors() {
                err.into_diagnostic(handler).emit();
            }

            if handler.has_errors() {
                return Err(());
            }

            Ok((res.unwrap(), comments))
        })
    }

    #[test]
    fn test_parsed_module_get_dependencies() {
        let source = r#"import * as bar from "./test.ts";
/** JSDoc */
import type { Foo } from "./foo.d.ts";
/// <reference foo="bar" />
export * as Buzz from "./buzz.ts";
// @some-pragma
/**
 * Foo
 */
export type { Fizz } from "./fizz.d.ts";
const { join } = require("path");

// dynamic
await import("./foo1.ts");

try {
    const foo = await import("./foo.ts");
} catch (e) {
    // pass
}

try {
    const foo = require("some_package");
} catch (e) {
    // pass
}

import foo2 = require("some_package_foo");
import type FooType = require('some_package_foo_type');
export import bar2 = require("some_package_bar");

const foo3 = require.resolve("some_package_resolve");
try {
    const foo4 = require.resolve("some_package_resolve_foo");
} catch (e) {
    // pass
}
      "#;
        let (module, comments) = helper("test.ts", source).unwrap();
        let dependencies = analyze_dependencies(&module, &comments);
        assert_eq!(dependencies.len(), 13);
        assert_eq!(
            dependencies,
            vec![
                DependencyDescriptor {
                    kind: DependencyKind::Import,
                    is_dynamic: false,
                    leading_comments: Vec::new(),
                    span: Span::new(BytePos(1), BytePos(34), Default::default()),
                    specifier: JsWord::from("./test.ts"),
                    specifier_span: Span::new(BytePos(22), BytePos(33), Default::default()),
                    import_attributes: Default::default(),
                },
                DependencyDescriptor {
                    kind: DependencyKind::ImportType,
                    is_dynamic: false,
                    leading_comments: vec![Comment {
                        kind: CommentKind::Block,
                        text: r#"* JSDoc "#.into(),
                        span: Span::new(BytePos(35), BytePos(47), SyntaxContext::empty()),
                    }],
                    span: Span::new(BytePos(48), BytePos(86), Default::default()),
                    specifier: JsWord::from("./foo.d.ts"),
                    specifier_span: Span::new(BytePos(73), BytePos(85), Default::default()),
                    import_attributes: Default::default(),
                },
                DependencyDescriptor {
                    kind: DependencyKind::Export,
                    is_dynamic: false,
                    leading_comments: vec![Comment {
                        kind: CommentKind::Line,
                        text: r#"/ <reference foo="bar" />"#.into(),
                        span: Span::new(BytePos(87), BytePos(114), SyntaxContext::empty()),
                    }],
                    span: Span::new(BytePos(115), BytePos(149), Default::default()),
                    specifier: JsWord::from("./buzz.ts"),
                    specifier_span: Span::new(BytePos(137), BytePos(148), Default::default()),
                    import_attributes: Default::default(),
                },
                DependencyDescriptor {
                    kind: DependencyKind::ExportType,
                    is_dynamic: false,
                    leading_comments: vec![
                        Comment {
                            kind: CommentKind::Line,
                            text: r#" @some-pragma"#.into(),
                            span: Span::new(BytePos(150), BytePos(165), SyntaxContext::empty()),
                        },
                        Comment {
                            kind: CommentKind::Block,
                            text: "*\n * Foo\n ".into(),
                            span: Span::new(BytePos(166), BytePos(180), SyntaxContext::empty()),
                        }
                    ],
                    span: Span::new(BytePos(181), BytePos(221), Default::default()),
                    specifier: JsWord::from("./fizz.d.ts"),
                    specifier_span: Span::new(BytePos(207), BytePos(220), Default::default()),
                    import_attributes: Default::default(),
                },
                DependencyDescriptor {
                    kind: DependencyKind::Require,
                    is_dynamic: false,
                    leading_comments: Vec::new(),
                    span: Span::new(BytePos(239), BytePos(254), Default::default()),
                    specifier: JsWord::from("path"),
                    specifier_span: Span::new(BytePos(247), BytePos(253), Default::default()),
                    import_attributes: Default::default(),
                },
                DependencyDescriptor {
                    kind: DependencyKind::Import,
                    is_dynamic: true,
                    leading_comments: Vec::new(),
                    span: Span::new(BytePos(274), BytePos(293), Default::default()),
                    specifier: JsWord::from("./foo1.ts"),
                    specifier_span: Span::new(BytePos(281), BytePos(292), Default::default()),
                    import_attributes: Default::default(),
                },
                DependencyDescriptor {
                    kind: DependencyKind::Import,
                    is_dynamic: true,
                    leading_comments: Vec::new(),
                    span: Span::new(BytePos(324), BytePos(342), Default::default()),
                    specifier: JsWord::from("./foo.ts"),
                    specifier_span: Span::new(BytePos(331), BytePos(341), Default::default()),
                    import_attributes: Default::default(),
                },
                DependencyDescriptor {
                    kind: DependencyKind::Require,
                    is_dynamic: true,
                    leading_comments: Vec::new(),
                    span: Span::new(BytePos(395), BytePos(418), Default::default()),
                    specifier: JsWord::from("some_package"),
                    specifier_span: Span::new(BytePos(403), BytePos(417), Default::default()),
                    import_attributes: Default::default(),
                },
                DependencyDescriptor {
                    kind: DependencyKind::ImportEquals,
                    is_dynamic: false,
                    leading_comments: Vec::new(),
                    span: Span::new(BytePos(449), BytePos(491), Default::default()),
                    specifier: JsWord::from("some_package_foo"),
                    specifier_span: Span::new(BytePos(471), BytePos(489), Default::default()),
                    import_attributes: Default::default(),
                },
                DependencyDescriptor {
                    kind: DependencyKind::ImportType,
                    is_dynamic: false,
                    leading_comments: Vec::new(),
                    span: Span::new(BytePos(492), BytePos(547), Default::default()),
                    specifier: JsWord::from("some_package_foo_type"),
                    specifier_span: Span::new(BytePos(522), BytePos(545), Default::default()),
                    import_attributes: Default::default(),
                },
                DependencyDescriptor {
                    kind: DependencyKind::ExportEquals,
                    is_dynamic: false,
                    leading_comments: Vec::new(),
                    span: Span::new(BytePos(548), BytePos(597), Default::default()),
                    specifier: JsWord::from("some_package_bar"),
                    specifier_span: Span::new(BytePos(577), BytePos(595), Default::default()),
                    import_attributes: Default::default(),
                },
                DependencyDescriptor {
                    kind: DependencyKind::Require,
                    is_dynamic: false,
                    leading_comments: Vec::new(),
                    span: Span::new(BytePos(612), BytePos(651), Default::default()),
                    specifier: JsWord::from("some_package_resolve"),
                    specifier_span: Span::new(BytePos(628), BytePos(650), Default::default()),
                    import_attributes: Default::default(),
                },
                DependencyDescriptor {
                    kind: DependencyKind::Require,
                    is_dynamic: true,
                    leading_comments: Vec::new(),
                    span: Span::new(BytePos(676), BytePos(719), Default::default()),
                    specifier: JsWord::from("some_package_resolve_foo"),
                    specifier_span: Span::new(BytePos(692), BytePos(718), Default::default()),
                    import_attributes: Default::default(),
                },
            ]
        );
    }

    #[test]
    fn test_import_assertions() {
        let source = r#"import * as bar from "./test.ts" assert { "type": "typescript" };
export * from "./test.ts" assert { "type": "typescript" };
export { bar } from "./test.json" assert { "type": "json" };
import foo from "./foo.json" assert { type: "json" };
const fizz = await import("./fizz.json", { "assert": { type: "json" } });
const buzz = await import("./buzz.json", { assert: { "type": "json" } });
const d1 = await import("./d1.json");
const d2 = await import("./d2.json", {});
const d3 = await import("./d3.json", bar);
const d4 = await import("./d4.json", { assert: {} });
const d5 = await import("./d5.json", { assert: bar });
const d6 = await import("./d6.json", { assert: {}, ...bar });
const d7 = await import("./d7.json", { assert: {}, ["assert"]: "bad" });
const d8 = await import("./d8.json", { assert: { type: bar } });
const d9 = await import("./d9.json", { assert: { type: "json", ...bar } });
const d10 = await import("./d10.json", { assert: { type: "json", ["type"]: "bad" } });
      "#;
        let (module, comments) = helper("test.ts", source).unwrap();
        let expected_assertions1 = ImportAttributes::Known({
            let mut map = HashMap::new();
            map.insert(
                "type".to_string(),
                ImportAssertion::Known("typescript".to_string()),
            );
            map
        });
        let expected_assertions2 = ImportAttributes::Known({
            let mut map = HashMap::new();
            map.insert(
                "type".to_string(),
                ImportAssertion::Known("json".to_string()),
            );
            map
        });
        let dynamic_expected_assertions2 = ImportAttributes::Known({
            let mut map = HashMap::new();
            map.insert(
                "type".to_string(),
                ImportAssertion::Known("json".to_string()),
            );
            map
        });
        let dependencies = analyze_dependencies(&module, &comments);
        assert_eq!(dependencies.len(), 16);
        assert_eq!(
            dependencies,
            vec![
                DependencyDescriptor {
                    kind: DependencyKind::Import,
                    is_dynamic: false,
                    leading_comments: Vec::new(),
                    span: Span::new(BytePos(1), BytePos(66), Default::default()),
                    specifier: JsWord::from("./test.ts"),
                    specifier_span: Span::new(BytePos(22), BytePos(33), Default::default()),
                    import_attributes: expected_assertions1.clone(),
                },
                DependencyDescriptor {
                    kind: DependencyKind::Export,
                    is_dynamic: false,
                    leading_comments: Vec::new(),
                    span: Span::new(BytePos(67), BytePos(125), Default::default()),
                    specifier: JsWord::from("./test.ts"),
                    specifier_span: Span::new(BytePos(81), BytePos(92), Default::default()),
                    import_attributes: expected_assertions1,
                },
                DependencyDescriptor {
                    kind: DependencyKind::Export,
                    is_dynamic: false,
                    leading_comments: Vec::new(),
                    span: Span::new(BytePos(126), BytePos(186), Default::default()),
                    specifier: JsWord::from("./test.json"),
                    specifier_span: Span::new(BytePos(146), BytePos(159), Default::default()),
                    import_attributes: expected_assertions2.clone(),
                },
                DependencyDescriptor {
                    kind: DependencyKind::Import,
                    is_dynamic: false,
                    leading_comments: Vec::new(),
                    span: Span::new(BytePos(187), BytePos(240), Default::default()),
                    specifier: JsWord::from("./foo.json"),
                    specifier_span: Span::new(BytePos(203), BytePos(215), Default::default()),
                    import_attributes: expected_assertions2,
                },
                DependencyDescriptor {
                    kind: DependencyKind::Import,
                    is_dynamic: true,
                    leading_comments: Vec::new(),
                    span: Span::new(BytePos(260), BytePos(313), Default::default()),
                    specifier: JsWord::from("./fizz.json"),
                    specifier_span: Span::new(BytePos(267), BytePos(280), Default::default()),
                    import_attributes: dynamic_expected_assertions2.clone(),
                },
                DependencyDescriptor {
                    kind: DependencyKind::Import,
                    is_dynamic: true,
                    leading_comments: Vec::new(),
                    span: Span::new(BytePos(334), BytePos(387), Default::default()),
                    specifier: JsWord::from("./buzz.json"),
                    specifier_span: Span::new(BytePos(341), BytePos(354), Default::default()),
                    import_attributes: dynamic_expected_assertions2,
                },
                DependencyDescriptor {
                    kind: DependencyKind::Import,
                    is_dynamic: true,
                    leading_comments: Vec::new(),
                    span: Span::new(BytePos(406), BytePos(425), Default::default()),
                    specifier: JsWord::from("./d1.json"),
                    specifier_span: Span::new(BytePos(413), BytePos(424), Default::default()),
                    import_attributes: Default::default(),
                },
                DependencyDescriptor {
                    kind: DependencyKind::Import,
                    is_dynamic: true,
                    leading_comments: Vec::new(),
                    span: Span::new(BytePos(444), BytePos(467), Default::default()),
                    specifier: JsWord::from("./d2.json"),
                    specifier_span: Span::new(BytePos(451), BytePos(462), Default::default()),
                    import_attributes: Default::default(),
                },
                DependencyDescriptor {
                    kind: DependencyKind::Import,
                    is_dynamic: true,
                    leading_comments: Vec::new(),
                    span: Span::new(BytePos(486), BytePos(510), Default::default()),
                    specifier: JsWord::from("./d3.json"),
                    specifier_span: Span::new(BytePos(493), BytePos(504), Default::default()),
                    import_attributes: ImportAttributes::Unknown,
                },
                DependencyDescriptor {
                    kind: DependencyKind::Import,
                    is_dynamic: true,
                    leading_comments: Vec::new(),
                    span: Span::new(BytePos(529), BytePos(564), Default::default()),
                    specifier: JsWord::from("./d4.json"),
                    specifier_span: Span::new(BytePos(536), BytePos(547), Default::default()),
                    import_attributes: ImportAttributes::Known(HashMap::new()),
                },
                DependencyDescriptor {
                    kind: DependencyKind::Import,
                    is_dynamic: true,
                    leading_comments: Vec::new(),
                    span: Span::new(BytePos(583), BytePos(619), Default::default()),
                    specifier: JsWord::from("./d5.json"),
                    specifier_span: Span::new(BytePos(590), BytePos(601), Default::default()),
                    import_attributes: ImportAttributes::Unknown,
                },
                DependencyDescriptor {
                    kind: DependencyKind::Import,
                    is_dynamic: true,
                    leading_comments: Vec::new(),
                    span: Span::new(BytePos(638), BytePos(681), Default::default()),
                    specifier: JsWord::from("./d6.json"),
                    specifier_span: Span::new(BytePos(645), BytePos(656), Default::default()),
                    import_attributes: ImportAttributes::Unknown,
                },
                DependencyDescriptor {
                    kind: DependencyKind::Import,
                    is_dynamic: true,
                    leading_comments: Vec::new(),
                    span: Span::new(BytePos(700), BytePos(754), Default::default()),
                    specifier: JsWord::from("./d7.json"),
                    specifier_span: Span::new(BytePos(707), BytePos(718), Default::default()),
                    import_attributes: ImportAttributes::Unknown,
                },
                DependencyDescriptor {
                    kind: DependencyKind::Import,
                    is_dynamic: true,
                    leading_comments: Vec::new(),
                    span: Span::new(BytePos(773), BytePos(819), Default::default()),
                    specifier: JsWord::from("./d8.json"),
                    specifier_span: Span::new(BytePos(780), BytePos(791), Default::default()),
                    import_attributes: ImportAttributes::Known({
                        let mut map = HashMap::new();
                        map.insert("type".to_string(), ImportAssertion::Unknown);
                        map
                    }),
                },
                DependencyDescriptor {
                    kind: DependencyKind::Import,
                    is_dynamic: true,
                    leading_comments: Vec::new(),
                    span: Span::new(BytePos(838), BytePos(895), Default::default()),
                    specifier: JsWord::from("./d9.json"),
                    specifier_span: Span::new(BytePos(845), BytePos(856), Default::default()),
                    import_attributes: ImportAttributes::Unknown,
                },
                DependencyDescriptor {
                    kind: DependencyKind::Import,
                    is_dynamic: true,
                    leading_comments: Vec::new(),
                    span: Span::new(BytePos(915), BytePos(982), Default::default()),
                    specifier: JsWord::from("./d10.json"),
                    specifier_span: Span::new(BytePos(922), BytePos(934), Default::default()),
                    import_attributes: ImportAttributes::Unknown,
                },
            ]
        );
    }
}