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
// use swc_common::{input::SourceFileInput, FileName, Mark, Span, DUMMY_SP};
// use swc_ecma_ast::*;
// use swc_ecma_parser::{lexer::Lexer, Parser};
// use swc_ecma_transforms::resolver_with_mark;
// use swc_ecma_visit::{Node, Visit, VisitMutWith, VisitWith};
// use crate::marks::Marks;
// use super::info_marker;
// fn assert_standalone(src: &str, expected: usize) {
// testing::run_test(false, |cm, _handler| {
// let marks = Marks::new();
// let top_level_mark = Mark::fresh(Mark::root());
// let fm = cm.new_source_file(FileName::Anon, src.to_string());
// let lexer = Lexer::new(
// Default::default(),
// EsVersion::latest(),
// SourceFileInput::from(&*fm),
// None,
// );
// let mut parser = Parser::new_from(lexer);
// let mut m = parser.parse_module().expect("failed to parse");
// m.visit_mut_with(&mut resolver_with_mark(top_level_mark));
// m.visit_mut_with(&mut info_marker(None, marks, top_level_mark));
// eprintln!("Expected: {} modules in bundle", expected);
// let actual = {
// let mut counter = MarkCounter {
// mark: marks.standalone,
// count: 0,
// };
// m.visit_with( &mut counter);
// counter.count
// };
// eprintln!("Actual: {} modules in bundle", actual);
// assert_eq!(expected, actual);
// if expected != 0 {
// assert!(
// m.span.has_mark(marks.bundle_of_standalones),
// "Expected module to be marked as a bundle"
// );
// } else {
// assert!(
// !m.span.has_mark(marks.bundle_of_standalones),
// "Expected module to be not marked as a bundle"
// );
// }
// Ok(())
// })
// .unwrap();
// }
// struct MarkCounter {
// mark: Mark,
// count: usize,
// }
// impl Visit for MarkCounter {
// fn visit_span(&mut self, span: &Span) {
// if span.has_mark(self.mark) {
// self.count += 1;
// }
// }
// }
// #[test]
// fn standalone_base() {
// assert_standalone("function foo() {}", 0);
// }
// #[test]
// fn standalone_no_usage() {
// assert_standalone(
// "function foo() {
// declare(function (module, exports) {
// }, function (module, exports) {
// });
// }",
// 2,
// );
// }
// #[test]
// fn usage_of_var_1() {
// assert_standalone(
// "function foo() {
// var bar = 2;
// declare(function (module, exports) {
// bar = 1;
// }, function (module, exports) {
// });
// }",
// 1,
// );
// }
// #[test]
// fn usage_of_class_1() {
// assert_standalone(
// "function foo() {
// class Foo {
// }
// declare(function (module, exports) {
// const bar = new Foo();
// }, function (module, exports) {
// });
// }",
// 1,
// );
// }
// #[test]
// fn usage_of_fn_1() {
// assert_standalone(
// "function foo() {
// function bar() {
// }
// declare(function (module, exports) {
// const baz = new bar();
// }, function (module, exports) {
// });
// }",
// 1,
// );
// }
// #[test]
// fn usage_of_var_2() {
// assert_standalone(
// "var C = 1;
// var obj = {
// bar: function (module, exports) {
// return C + C;
// },
// };
// console.log(obj.bar());
// ",
// 0,
// );
// }
// #[test]
// fn export_default_fn_1() {
// assert_standalone("export default function f(module, exports) {}", 0);
// }