Skip to content

Latest commit

 

History

History
153 lines (99 loc) · 3.88 KB

ARCHITECTURE.md

File metadata and controls

153 lines (99 loc) · 3.88 KB

SWC architecture

This document gives a high level overview of SWC internals. You may find it useful if you want to contribute to SWC or if you are interested in the inner workings of SWC.

Macros

SWC uses proc macro extensively to reduce work. Please see links below to know what each macro do.

And some adhoc-macros are used.

These macro breaks macro hygiene.

Structure

Handle string interning for the SWC project. The crate depends on string_cache from servo.

Contains code related to span, hygiene and error reporting.

Also, it contains / re-exports codes for visitor pattern. Visit<T> is non-mutating visitor, while Fold<T> is a mutating visitor.

Contains AST nodes for javascript and typescript.

Converts javascript AST into javascript code.

Parses javascript and typescript

There are three core transforms named resolver, hygiene, fixer. Other transforms depend on them.

This pass resolves and marks all identifiers in the file.

e.g.

let a = 1;
{
    let a = 1;
}

becomes

let a#0 = 1;
{
  let a#1 = 1;
}

where the number after the hash (#) denotes the hygiene id. If two identifiers have the same symbol but different hygiene ids, they are considered different.

The hygiene pass actually changes symbols of identifiers with the same symbol but different hygiene ids to different symbols.

let a#0 = 1;
{
  let a#1 = 2;
}

becomes

let a = 1;
{
    let a1 = 2;
}

Fixes broken AST. This allow us to simply fold types like BinExpr without caring about operator precedence.

It means,

let v = BinExpr {
    left: "1 + 2",
    op: "*",
    right: "3",
};

(other passes generate AST like this)

is converted into

let v = BinExpr {
    left: "(1 + 2)",
    op: "*",
    right: "3",
};

and printed as

(1 + 2) * 3;

Tests

SWC uses the official ecmascript conformance test suite called test262 for testing.

Parser tests ensure that the parsed results of test262/pass are identical with test262/pass-explicit.

Codegen tests ensure that the generated code is equivalent to the golden fixture files located at tests/references.