-
-
Notifications
You must be signed in to change notification settings - Fork 555
/
Copy pathmod.rs
171 lines (149 loc) · 5.41 KB
/
mod.rs
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
mod class_tester;
mod expect;
mod symbol_tester;
use std::{path::PathBuf, sync::Arc};
use itertools::Itertools;
use oxc_allocator::Allocator;
use oxc_diagnostics::{Error, NamedSource, OxcDiagnostic};
use oxc_semantic::{DebugDot, DisplayDot, Semantic, SemanticBuilder};
use oxc_span::SourceType;
pub use class_tester::ClassTester;
pub use expect::Expect;
pub use symbol_tester::SymbolTester;
pub struct SemanticTester<'a> {
allocator: Allocator,
source_type: SourceType,
source_text: &'a str,
}
impl<'a> SemanticTester<'a> {
/// Create a new tester for a TypeScript test case.
///
/// Use [`SemanticTester::js`] for JavaScript test cases.
pub fn ts(source_text: &'static str) -> Self {
Self::new(source_text, SourceType::default().with_module(true).with_typescript(true))
}
/// Create a new tester for a JavaScript test case.
///
/// Use [`SemanticTester::ts`] for TypeScript test cases.
pub fn js(source_text: &'static str) -> Self {
Self::new(source_text, SourceType::default().with_module(true))
}
pub fn new(source_text: &'a str, source_type: SourceType) -> Self {
Self { allocator: Allocator::default(), source_type, source_text }
}
/// Set the [`SourceType`] to TypeScript (or JavaScript, using `false`)
#[must_use]
pub fn with_typescript(mut self, yes: bool) -> Self {
self.source_type = SourceType::default().with_typescript(yes);
self
}
/// Mark the [`SourceType`] as JSX
#[must_use]
pub fn with_jsx(mut self, yes: bool) -> Self {
self.source_type = self.source_type.with_jsx(yes);
self
}
#[must_use]
pub fn with_module(mut self, yes: bool) -> Self {
self.source_type = self.source_type.with_module(yes);
self
}
/// Parse the source text and produce a new [`Semantic`]
/// # Panics
#[allow(unstable_name_collisions)]
pub fn build(&self) -> Semantic<'_> {
let parse =
oxc_parser::Parser::new(&self.allocator, self.source_text, self.source_type).parse();
assert!(
parse.errors.is_empty(),
"\n Failed to parse source:\n{}\n\n{}",
self.source_text,
parse
.errors
.iter()
.map(|e| format!("{e}"))
.intersperse("\n\n".to_owned())
.collect::<String>()
);
let program = self.allocator.alloc(parse.program);
let semantic_ret = SemanticBuilder::new(self.source_text, self.source_type)
.with_check_syntax_error(true)
.with_trivias(parse.trivias)
.build_module_record(PathBuf::new(), program)
.build(program);
if !semantic_ret.errors.is_empty() {
let report = self.wrap_diagnostics(semantic_ret.errors);
panic!(
"Semantic analysis failed:\n\n{}",
report
.iter()
.map(ToString::to_string)
.intersperse("\n\n".to_owned())
.collect::<String>()
);
};
semantic_ret.semantic
}
pub fn basic_blocks_count(&self) -> usize {
let built = self.build();
built.cfg().basic_blocks.len()
}
pub fn basic_blocks_printed(&self) -> String {
let built = self.build();
built
.cfg()
.basic_blocks
.iter()
.map(DisplayDot::display_dot)
.enumerate()
.map(|(i, it)| {
format!(
"bb{i}: {{\n{}\n}}",
it.lines().map(|x| format!("\t{}", x.trim())).join("\n")
)
})
.join("\n\n")
}
pub fn cfg_dot_diagram(&self) -> String {
let semantic = self.build();
semantic.cfg().debug_dot(semantic.nodes().into())
}
/// Tests that a symbol with the given name exists at the top-level scope and provides a
/// wrapper for writing assertions about the found symbol.
///
/// ## Fails
/// If no symbol with the given name exists at the top-level scope.
pub fn has_root_symbol(&self, name: &str) -> SymbolTester {
SymbolTester::new_at_root(self, self.build(), name)
}
/// Tests that a class with the given name exists
///
/// ## Fails
/// If no class with the given name exists.
pub fn has_class(&self, name: &str) -> ClassTester {
ClassTester::has_class(self.build(), name)
}
/// Finds some symbol by name in the source code.
///
/// ## Fails
/// 1. No symbol with the given name exists,
/// 2. More than one symbol with the given name exists, so a symbol cannot
/// be uniquely obtained.
pub fn has_some_symbol(&self, name: &str) -> SymbolTester {
SymbolTester::new_unique(self, self.build(), name)
}
fn wrap_diagnostics(&self, diagnostics: Vec<OxcDiagnostic>) -> Vec<Error> {
let name = "test".to_owned()
+ match (self.source_type.is_javascript(), self.source_type.is_jsx()) {
(true, true) => ".jsx",
(true, false) => ".js",
(false, true) => ".tsx",
(false, false) => ".ts",
};
let source = Arc::new(NamedSource::new(name, self.source_text.to_owned()));
diagnostics
.into_iter()
.map(|diagnostic| diagnostic.with_source_code(Arc::clone(&source)))
.collect()
}
}