Transform ID:Documentation Index
Fetch the complete documentation index at: https://docs.refactron.dev/llms.txt
Use this file to discover all available pages before exploring further.
var_to_const_let
Language: TypeScript
What it does
For everyvar declaration in the file, decide per binding:
constif the binding is never reassigned after initialization.letif the binding is reassigned at least once.
var statements in one file may map to a mix of const and let.
Detector pattern
The detector atsrc/analyze/detectors/typescript/var-declarations.ts collects all VariableStatement nodes with the var keyword via ts-morph and then walks the surrounding scope to count reassignments per binding name.
Preconditions
- No binding in the file is referenced before its declaration (relying on
varhoisting). If hoisting is used anywhere, the entire file is skipped — the transform refuses to break working hoisted code. - No
varis declared inside awithstatement (legacy scope rules differ fromlet/const). - The file contains at least one
varstatement.
Before / after
Edge cases handled
- Mixed const/let outcomes inside a single file based on each binding’s mutability.
- Multiple independent
vars in one file (var a = 1; var b = 2; b = b + 1;→const a = 1; let b = 2; b = b + 1;). for (var i = 0; ...)becomesfor (let i = 0; ...)wheniis mutated by the loop step.
Edge cases NOT handled (skip via precondition)
- File relies on hoisting (e.g.
console.log(x); var x = 1;). vardeclared inside awithblock..cjsfiles (handled bycommonjs_to_esmwhen relevant;varinside CommonJS modules has different historical patterns).