Skip to main content

Module inline_variable

Module inline_variable 

Source
Expand description

Inline-variable recipe: replace all uses of a variable with its initializer and remove the binding.

Algorithm:

  1. Parse the file with tree-sitter.
  2. Locate the variable declaration node at the given position (line:col of the variable name).
  3. Extract: variable name, initializer expression text, declaration node byte range, and the scope node (the function/block that contains the declaration).
  4. Within that scope: walk all identifier nodes, collect those whose text matches the variable name (conservative: skip if ambiguous).
  5. Check for reassignments — error out if any exist.
  6. Replace each reference with the initializer expression text (wrapping in parens if needed).
  7. Remove the declaration statement line.

Language-specific declaration node kinds:

  • Rust: let_declaration — pattern child is identifier or _pattern, value child via =
  • JS/TS: lexical_declaration (const/let) / variable_declaration (var) — contains variable_declarator which has name: identifier and value: <expr>
  • Python: assignment — left child is identifier, right is the value

Structs§

InlineVariableOutcome
Outcome of a successful inline-variable plan.

Functions§

line_col_to_byte
Convert a 1-based line:col pair to a byte offset in content.
plan_inline_variable
Build an inline-variable plan without touching the filesystem.