oxc/crates/oxc_semantic/tests/symbols.rs
2023-12-18 11:26:33 +08:00

89 lines
2 KiB
Rust

mod util;
use oxc_semantic::SymbolFlags;
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();
}
#[ignore = "type aliases currently aren't in the symbol table"]
#[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();
}