Codegen’s enables manipulation of variable assignments via the following classes:

Simple Value Changes

Consider the following source code:

const userId = 123;
const [userName, userAge] = ["Eve", 25];

In Codegen, you can access assignments with the get_local_var_assignment method.

You can then manipulate the assignment with the set_value method.

id_assignment = file.code_block.get_local_var_assignment("userId")
id_assignment.set_value("456")

name_assignment = file.code_block.get_local_var_assignment("name")
name_assignment.rename("userName")

Assignments inherit both HasName and HasValue behaviors. See Inheritable Behaviors for more details.

Type Annotations

Similarly, you can set type annotations with the set_type_annotation method.

For example, consider the following source code:

let status;
const data = fetchData();

You can manipulate the assignments as follows:

status_assignment = file.code_block.get_local_var_assignment("status")
status_assignment.set_type_annotation("Status")
status_assignment.set_value("Status.ACTIVE")

data_assignment = file.code_block.get_local_var_assignment("data")
data_assignment.set_type_annotation("ResponseData<T>")

# Result:
let status: Status = Status.ACTIVE;
const data: ResponseData<T> = fetchData();

Tracking Usages and Dependencies

Like other symbols, Assignments support usages and dependencies.

assignment = file.code_block.get_local_var_assignment("userId")

# Get all usages of the assignment
usages = assignment.usages

# Get all dependencies of the assignment
dependencies = assignment.dependencies

See Dependencies and Usages for more details.

Was this page helpful?