/
scopes.rs
139 lines (129 loc) · 3.04 KB
/
scopes.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
mod util;
use oxc_semantic::ScopeFlags;
pub use util::{Expect, SemanticTester};
#[test]
fn test_top_level_strict() {
// Module with top-level "use strict"
SemanticTester::js(
r#"
"use strict";
function foo() {
return 1
}
"#,
)
.has_root_symbol("foo")
.is_in_scope(ScopeFlags::Top | ScopeFlags::StrictMode)
// .expect(expect_strict)
.test();
// Module without top-level "use strict"
SemanticTester::js(
r"
function foo() {
return 1
}
",
)
.has_root_symbol("foo")
.is_in_scope(ScopeFlags::Top | ScopeFlags::StrictMode)
.test();
// Script with top-level "use strict"
SemanticTester::js(
r#"
"use strict";
function foo() {
return 1
}
"#,
)
.with_module(false)
.has_root_symbol("foo")
.is_in_scope(ScopeFlags::Top | ScopeFlags::StrictMode)
.test();
// Script without top-level "use strict"
SemanticTester::js(
r"
function foo() {
return 1
}
",
)
.with_module(false)
.has_root_symbol("foo")
.is_in_scope(ScopeFlags::Top)
.is_not_in_scope(ScopeFlags::StrictMode)
.test();
}
#[test]
fn test_function_level_strict() {
let tester = SemanticTester::js(
r#"
function foo() {
"use strict";
let x = 1;
return x
}
"#,
)
.with_module(false);
tester.has_some_symbol("x")
.is_in_scope(ScopeFlags::StrictMode | ScopeFlags::Function)
.expect(|(semantic, symbol_id)| -> Result<(), &'static str> {
let scope_id = semantic.symbol_scope(symbol_id);
let Some(parent_scope_id) = semantic.scopes().get_parent_id(scope_id) else {
return Err("Expected x's scope to have a parent")
};
let parent_flags = semantic.scopes().get_flags(parent_scope_id);
if parent_flags.contains(ScopeFlags::Top) {
Ok(())
} else {
Err("Expected x to be in a top-level function declaration, but its parent scope has flags {parent_flags:?}")
}
})
.test();
tester.has_some_symbol("foo").is_not_in_scope(ScopeFlags::StrictMode).test();
}
#[test]
fn test_switch_case() {
SemanticTester::js(
"
const foo = 1;
switch (foo) {
case 1:
const foo = 2;
}
",
)
.has_root_symbol("foo")
.has_number_of_references(1)
.test();
}
#[test]
fn test_function_parameters() {
SemanticTester::js(
"
const foo = 2;
function func(a = foo, b = a) {
const foo = 0;
}
",
)
.has_root_symbol("foo")
.has_number_of_references(1)
.test();
}
#[test]
fn test_catch_clause_parameters() {
SemanticTester::js(
"
const a = 0;
try {
} catch ({ [a]: b }) {
const a = 1;
}
",
)
.has_root_symbol("a")
.has_number_of_references(1)
.test();
}