/
symbols.rs
105 lines (92 loc) · 2.39 KB
/
symbols.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
mod util;
use oxc_semantic::SymbolFlags;
pub use util::SemanticTester;
#[test]
fn test_class_simple() {
SemanticTester::js("export class Foo {};")
.has_root_symbol("Foo")
.contains_flags(SymbolFlags::Class | SymbolFlags::Export)
.has_number_of_references(0)
.is_exported()
.test();
SemanticTester::js("class Foo {}; let f = new Foo()")
.has_root_symbol("Foo")
.has_number_of_reads(1)
.test();
}
#[test]
fn test_function_simple() {
SemanticTester::js("function foo() { return }")
.has_root_symbol("foo")
.contains_flags(SymbolFlags::Function)
.test();
}
#[test]
fn test_var_simple() {
SemanticTester::js("let x; { let y; }")
.has_some_symbol("x")
.intersects_flags(SymbolFlags::Variable)
.contains_flags(SymbolFlags::BlockScopedVariable)
.test();
}
#[test]
fn test_var_read_write() {
SemanticTester::js("let x; x += 1")
.has_root_symbol("x")
.has_number_of_references(1)
.has_number_of_reads(1)
.has_number_of_writes(1)
.test();
SemanticTester::js("let a; let b = 1 + (0, ((a)));")
.has_some_symbol("a")
.has_number_of_reads(1)
.has_number_of_writes(0)
.test();
SemanticTester::js(
"
let x;
function foo(a) {
console.log(x(a))
}",
)
.has_some_symbol("x")
.has_number_of_reads(1)
.has_number_of_writes(0)
.test();
}
#[test]
fn test_types_simple() {
let test = SemanticTester::ts(
"
interface A {
x: number;
y: string;
}
type T = { x: number; y: string; }
const t: T = { x: 1, y: 'foo' };
",
);
test.has_root_symbol("A")
.contains_flags(SymbolFlags::Interface)
.has_number_of_references(0)
.test();
test.has_root_symbol("T")
.contains_flags(SymbolFlags::TypeAlias)
.has_number_of_references(1)
.test();
}
#[test]
fn test_export_flag() {
let tester = SemanticTester::js(
"
const a = 1;
export { a, b as d };
class b {}
export default c;
function c() {}
",
);
tester.has_root_symbol("a").contains_flags(SymbolFlags::Export).test();
tester.has_root_symbol("b").contains_flags(SymbolFlags::Export).test();
tester.has_root_symbol("c").contains_flags(SymbolFlags::Export).test();
}