Skip to content

A general way to test scope resolving logic #1227

@hyf0

Description

@hyf0

pub struct SemanticTester {
allocator: Allocator,
source_type: SourceType,
source_text: &'static str,
}

The current SemanticTester is great to do many kinds of assertions. Such as

SemanticTester::js("export class Foo {}")
.has_root_symbol("Foo")
.contains_flags(SymbolFlags::Class | SymbolFlags::Export)
.has_number_of_references(0)
.is_exported()
.test();
.

But I kind of think the SemanticTester might not be that intuitive for testing general scope resolving logic, which means weather the symbol has correct scope and don't care about the flags or others.

Let's see we have test case

log()

let a = 1
log(a)
let b = 1
log(b)
{
  let a = 2
  log(a)
  log(b)
}

we could transform it to

log#0()

let a#1 = 1
log(a#1)
let b#1 = 1;
log(b#1);
{
  let a#2 = 2;
  log(a#2)
  log(b#1)
}

the x in #x is the ScopeId we get from the Sematics.

In this way, we could easily visualize the whether the scope is correct. And with snapshot testing, this could be a general way for testing without any other kinds of assertions.


I get this idea from contributing to swc. https://github.com/swc-project/swc/blob/main/crates/swc_ecma_transforms_base/tests/resolver/hoisting/output.js

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions