Repository navigation
Conversation
Conditional export detection keeps the last matching assignment, even when its branch cannot run. This PoC makes final export outcomes conditional on control flow through an opt-in detection baseline. The third parse argument selects a versioned baseline that can combine detection rules. The default call retains its current behavior and result. This is a bounded syntax experiment. Unsupported syntax, opaque effects, and analysis limits produce unknown outcomes instead of speculative export lists. Synchronous ESM parsing includes the analyzer in the bundle, which adds about 18 KB. Refs: #99
Flow reports distinguish sequential replacements from conditional alternatives. Default calls defer the report until its first read so legacy consumers avoid control-flow parsing. Unsupported effects remain unknown. Reusing scalar lexer stacks reduces empty JavaScript parses from 4.05 us to 177 ns and from 4.32 us to 187 ns. Two fresh Node 22.23.3 processes use V8 12.4.254.21-node.57, 50k warmup iterations, and seven alternating 30k-iteration trials. Each result excludes the fastest and slowest trials. The Wasm counter adds a small cost without an additional host/guest call. Conditional reports can be slower because the lexer must establish the trigger first. Refs: #99
Conditional replacements lose live dependencies when only the last syntactic assignment is reported. Reexport groups separate alternative final values and combine dependencies spread into one value. The third argument retains an explicit legacy baseline. Flow analysis starts after two detected replacements and has one C owner for both parser entries. Unsupported effects produce incomplete results rather than guessed exports. Zero or one replacement retains the existing fast path. Against f580443, conditional analysis falls from 4.57 to 2.86 us in CommonJS and 4.23 to 1.90 us in Wasm. Straight replacements add about 0.90 us in CommonJS and 0.44 us in Wasm. The shared C owner keeps this bounded cost on the active path. Measurements use Node 22.23.3, V8 12.4.254.21-node.57, 50,000 warmups, 20,000 iterations per trial, seven alternating trials, and two fresh processes. Each run removes the best and worst trials. Refs: #99
Wrapping a single outcome changes the dependency shape for deterministic modules. Preserve their flat lists and nest only distinct conditional lists. The overloads reject undefined options although the runtime accepts them, which prevents callers from forwarding an optional third argument. Refs: #99
Extended mode must retain its flat dependency union unless flow analysis is selected explicitly. Snapshot detection options before the baseline getter and preserve their values through CommonJS delegation so both entry points make the same selection.
af3f5ff to
51110a0
Compare
guybedford
left a comment
There was a problem hiding this comment.
Thanks for exploring this. I'd prefer not to take on control-flow evaluation in the lexer, though. A lexer can't know reachability, so over-reporting is its natural answer, and the evaluator has to bail out on most real-world code (var, function, exports.x =, Object.defineProperty, IIFEs). With the current fallback, those cases lose their legacy exports entirely.
Multiple assignments are already supported directly. mode: 'extended' (#128) skips the reexport reset and returns the union:
if (c) module.exports = { a: 1, ...require('./x') };
else module.exports = { b: 1, ...require('./y') };
// extended: { exports: ['a', 'b'], reexports: ['./x', './y'] }If the grouping from #99 is needed, a much simpler version is to emit a group boundary at each module.exports = assignment syntactically, with no flow analysis. Each group is then one possible final value, and a sequential overwrite just shows up as an extra candidate. The null-start sentinel in res() already gives a mechanism for this, so it should be a few lines in the lexer rather than a separate analyzer.
That would need to stay opt-in under extended mode, since default detection has to remain frozen.
guybedford
left a comment
There was a problem hiding this comment.
Thanks for exploring this, but control-flow analysis is out of scope for this project. The lexer only detects syntax and doesn't evaluate reachability, and default detection stays frozen. For multiple module.exports assignments, mode: 'extended' (#128) already reports the union of reexports. Tools that need exact final dependencies are better served by a full parser.
Conditional export detection keeps the last matching assignment, even when its branch cannot run. This PoC makes final export outcomes conditional on control flow through an opt-in detection baseline.
The third
parseargument selects a versioned baseline that can combine detection rules. The default call retains its current behavior and result. This is a bounded syntax experiment. Unsupported syntax, opaque effects, and analysis limits produce unknown outcomes instead of speculative export lists.Synchronous ESM parsing includes the analyzer in the bundle, which adds about 18 KB.
Refs: #99