browserify / detective

Find all calls to require() no matter how deeply nested using a proper walk of the AST
Other
414 stars 60 forks source link

Skip unreachable requires #40

Closed zertosh closed 9 years ago

zertosh commented 9 years ago

This diff adds the ability to skip unreachable require() calls - it's turned on by default, but may be turned off by passing the {unreachables: true} option. This is originally from my unreachable-branch-transform. However, for performance considerations and from a "this-is-how-it-should-work" perspective, it makes sense to have this functionality baked into node-detective.

"Unreachability" is determined by very basic static analysis of if statements, ternaries (?) and logical short-circuit evaluations (||/&&). switchs and whiles are not analyzed. Only the operations that can be statically determined are considered - there is no guessing or heuristics - it's 100% safe. Once a code branch is determined to be unreachable, traversal of its children is aborted, but the rest continues.

Integrating with browserify: browserify users should ideally have control over this option. browserify already passes options to module-deps, but module-deps does not pass options to detective. If this diff is merged. I'll submit the appropriate downstream PRs to address this.

The additional analysis logic has a negligible performance impact: bench

cc: @substack