Skip to content

The flow graph ​

tsox_checker::flow (the code is bridge/flow.rs, and tsox_checker::reachability is bridge/reachability.rs: both read the Oxc AST, so they sit in bridge/, and cargo xtask layering fails if any other file of the crate names an Oxc crate) builds, for every function body and the top level of a file, a table of flow nodes in the shape tsc's binder produces. It is arena-free data and a pure function of the source, so it fits the query layer like the other unit outputs. Nothing reads it for narrowing yet; see "Phases" below.

Hybrid: what comes from where ​

From oxc_cfg (the skeleton)From one AST pass (what the skeleton lacks)
Basic blocks and edgesWhich successor is the true one and which the false one: the edge into an if, ternary or loop body, or into the right operand of &&, ||, ??
Finalize and explicit Error edges (try, catch, finally)Assignments: declarators, x = v, x++, for (x of ...), destructuring, a.b.c = v
The node-to-block map (nodes().cfg_id), used for every AST nodeSwitch dispatch: the block each case is entered by, and what falling through means
Block order inside a bodyWhere an expression ends when it branches (a ?? b ends in the block where its paths meet)

Back edges are found by a depth-first walk, not read from oxc: oxc marks the closing edge of a while or for as Backedge but a continue as an ordinary jump.

Reachability: the one place the skeleton is not used ​

"Is this statement dead" is answered by crate::reachability, not by oxc's is_unreachable. oxc builds a correct graph of JavaScript, and tsc's binder builds a different one, and every difference changes which statements tsc calls dead:

  • a condition is constant only when it is the bare true or false keyword; (true), !true, 1 and null are not, while && and || carry constants through their operands and ?? does not;
  • break and continue jump straight to their target, past an enclosing finally;
  • a catch and a finally are entered from the start of their try, so they are live whenever the try is;
  • a switch with no default always falls out of the bottom;
  • a diagnostic covers a run of adjacent dead statements, and a function declaration, an empty statement, a var without initializer or a block ends the run.

reachability::analyze replays those rules over the AST and returns the dead statements and the TS7027 / TS7029 spans. It is a pure function of the program, so definite assignment and the narrowing resolver can ask the same question and get the same answer. The graph stays what it is good for, blocks and the node-to-block map, for the dataflow phases below.

Still open here because they need types: a call to a never-returning or asserts false function, and a switch that exhausts its discriminant. tsc folds both into the same question when it checks.

Definite assignment (TS2454) ​

The same structural walk carries a second thing besides "is this point reachable": which variables are definitely assigned. Two paths meet by keeping the variables both assigned; a dead path gives way to the other. Assignments only add variables, so a loop needs no fixpoint: what is assigned at the top of a loop is what was assigned on the way in.

A tracked variable is a let or var with a type annotation and no initializer. The walk runs twice, the first time only to learn which variables are tracked and where they are assigned, so a read that comes before its declaration (a hoisted var) is known to be of a tracked variable.

The rules that are not obvious, all taken from tsc:

  • x = v, a destructuring, a for (x of ...) head and a logical assignment assign; x += 1 and x++ do not, so the variable still counts as possibly undefined afterwards.
  • ||= and ??= read their target as a condition: where they short-circuit the variable is already known not to be undefined; &&= short-circuits on a value that can be.
  • A catch and a finally start from what was assigned before the try, not from what the try assigned. break skips a finally.
  • A test that names the variable (if (x), x !== undefined, typeof x === "number", x == null) removes undefined on the edge where it rules it out. Anything fancier waits for the narrowing resolver.
  • Inside a function that is not called where it is written, tsc assumes the variable is assigned, unless it is a plain let that nothing assigns with = anywhere. Then an arrow or function expression whose read is past the last assignment continues from the flow where it was created, and anything else starts a flow in which nothing is assigned. An async or generator function that is called where it is written counts as part of the function around it for this.
  • A read where the declared type is any, unknown, void, or holds undefined, or is under x!, is never reported.

The flow pass is arena-free, so it only records the reads. The checker applies the declared-type rule afterwards, in unreachable_code, and not where an identifier's type is inferred: the expression checker does not cover every expression yet, and a read inside one it skips would be lost. Where the checker has no type for the variable (it never walked the body the variable is declared in), the annotation's own syntax decides when it can, and a name that would need resolving is left alone.

Known to differ from tsc: a while (v === undefined) loop nested inside another loop, where tsc does not report the read in the condition. See scripts/flow-stress.

Return paths (TS2355, TS2366, TS2534, TS7030) ​

The flow pass records, for every function with a block body, whether the end of the body can be reached and whether a return can be. The checker records each function's declared or inferred return type where it checks the body. The report pairs the two in tsc's order, for a function whose end can be reached:

  1. a declared type of void, any or undefined, or a union with void, is never an error;
  2. never cannot have a reachable end (TS2534);
  3. a declared type and no reachable return at all: the function must return a value (TS2355);
  4. under strict null checks, a declared type that undefined is not assignable to: the function lacks an ending return (TS2366);
  5. otherwise, under noImplicitReturns, not all code paths return a value (TS7030).

Details that are easy to get wrong, all from tsc:

  • A return counts as explicit only if the flow can reach it, and a bare return; counts. One in dead code does not, so if (false) return 1; leaves a : number function with no return at all.
  • An unannotated function is checked by its inferred type, which is void, undefined or any unless some return gives another value. The checker's collected types are used when it has them; otherwise the return statements are read: none with a value, or only undefined.
  • A bare return; is itself reported (TS7030) under noImplicitReturns only without strict null checks. With them tsc checks it as returning undefined and reports that instead.
  • An immediately invoked function's end is reachable only if the code around it is, and a function in dead code that is not invoked there still starts reachable.

Not covered: an annotated generator, and an annotated async function whose Promise<T> the checker cannot read (it resolves to an empty object while the library's Promise is not modelled) unless the syntax of T settles it: a keyword, a literal type, a union of those. A name that would need resolving is left alone.

The backward resolver in shadow mode (F2) ​

The forward overlay in bridge/narrow/ threads "what is known about each variable" through the statements. The flow table answers the same question the other way: the type of a read is found by walking antecedents back from it. An assignment gives the assigned type, a condition narrows the type before it, a join unions its paths, a loop head iterates until its type stops changing, a switch clause narrows by its tests. bridge/flow_shadow.rs does that walk for every read of a variable and compares the answer with the overlay's, and changes nothing else.

It is off unless asked for: TSOX_FLOW_SHADOW=1 reports each difference as a warning (TSR9201), TSOX_FLOW_SHADOW=all also reports every read the walk answered (TSR9202) and prints a summary line. In a test, flow_shadow::enable_for_current_thread.

What it reuses rather than rewrites, so a difference is about the graph walk and not about how one test narrows one type: a condition narrows by the overlay's own narrow_condition (a scratch overlay holding just that variable at the type being narrowed), a switch clause by narrow_switch_case / narrow_switch_default, and an assignment by the rule in assigned_narrowed_type, fed the assigned type the forward pass recorded at the assignment. The walks run after the whole file is checked, because a read at the top of a loop is met before the assignments in the loop's body.

What it does not answer it says so and skips the read: an a ?? b edge, an assignment it was not told the type of (x += 1, a destructuring), a variable the checker has no type for.

What it has found ​

scripts/flow-stress/shadow.sh generates narrowing-heavy code and joins three answers for each statement-level read: tsc's, the overlay's, the walk's. Over seeds 1 to 3 (100 files each, no try), about 19,000 statement-level reads:

OutcomeShare
both match tsc79%
the walk matches tsc, the overlay does not8 to 9%
both agree with each other, tsc differs7 to 11%
neither matches tscabout 2%
the overlay matches tsc, the walk does not0.6 to 0.9%

So where the two differ, the walk is right about ten times as often. Known causes:

  • The overlay does not narrow a clause entered by falling through from the one above it (case "string": ... case "number": leaves string out), nor does it reason through a loop head. The walk gets both from the graph. These are the bulk of the second row.
  • The graph enters a catch from the end of its try only, where tsc enters it from the start and from after every assignment. With try in the mix the walk-wrong share rises to about 1.5%. The fix is in F1's exception edges, not in the walk.
  • Narrowing rules both sides share and tsc does not (the third row) are in narrow_condition, and moving to the walk does not change them.

The cutover per feature (the next step) goes in the order of how many reads each moves to tsc's answer, and the overlay is removed only when the second and fifth rows are empty.

Flow nodes ​

Start, Join, LoopHead, Assign { target, at }, Condition { test, assume } and Clause { discriminant, matched, includes_default, all_tests }. A node lists its antecedents, each with an edge kind (Normal, Back, Finalize, Exception). Every Condition and Clause sits on an edge, so a path that skips an operand does not carry that operand's condition. An if test is carried whole on both of its edges (narrow_condition already knows how to take a && b apart), and the operands of &&, ||, ?? carry their own on the edge into the right operand.

BodyFlow::references lists every read of a name with the flow node it is read at, so the question "what is the type of this reference" is answered by walking antecedents backwards from one node, and only for the references that are asked about.

Identity ​

An expression is an ExprRef: the AST-order index of its node minus that of the body's root. A comment or blank line above a function does not change its graph (tested). A symbol is its index in the file's symbol table plus its name; adding a declaration earlier in the file renumbers symbols, so the query layer should key on the name when it needs stability.

Not covered yet ​

Optional chains do not split the graph. Class property initializers and static blocks have no body of their own. A call is not an event, so assertion functions and never-returning calls are missing. A declaration without an initializer is not an event, so definite assignment is missing. A catch block is entered from the end of the try block only, not from every point in it. These are the F3 items of the plan.

Phases ​

  1. F1 (this change): the table and its tests. No behaviour change.
  2. F2: a backward, lazy resolver over the table runs beside the forward overlay in bridge::narrow; any disagreement is reported, and nothing is cut over until the difference is empty on the fixtures and the corpus.
  3. F3: definite assignment, implicit return, exhaustiveness, assertion functions, catch entry from every point of the try.

Checking it ​

cargo test -p tsox-checker --lib flow:: runs the hand-written cases against crates/tsox-checker/tests/flow_fixtures/, and the invariants (valid ids, a node without an antecedent is a Start, no cycle without a back edge) over every file in tests/fixtures/. The same invariants over a larger corpus:

text
TSOX_FLOW_CORPUS=.corpus cargo test -p tsox-checker --lib flow::tests::corpus -- --ignored --nocapture

TSOX_FLOW_FILE=x.ts cargo test -p tsox-checker --lib flow::tests::show -- --ignored --nocapture prints one file's graph.

Checking reachability against tsc ​

tests/reachability_rules.rs, tests/definite_assignment.rs and tests/return_paths.rs pin the exact lines tsc reports for one case per rule above. scripts/flow-stress/run.sh generates random control flow and compares tsox with tsc line by line:

text
scripts/flow-stress/run.sh --seed 1 --files 400 --codes 7027,7029
scripts/flow-stress/run.sh --mode assign --seed 1 --files 300 --codes 2454
scripts/flow-stress/run.sh --mode return --seed 1 --files 300 --codes 2355,2366,2534,7030
scripts/flow-stress/shadow.sh --seed 1 --files 100

It prints, per code, how many lines agree and differ, then the statement around the first differences. Any seed should report 0 differ.

MIT OR Apache-2.0