Skip to content

feat: add getAssignVarNames API to complement getOutVarNames (#464) - #466

Open
chenjunwenhao wants to merge 1 commit into
alibaba:mainfrom
chenjunwenhao:feat/issue-464-get-assign-var-names
Open

feat: add getAssignVarNames API to complement getOutVarNames (#464)#466
chenjunwenhao wants to merge 1 commit into
alibaba:mainfrom
chenjunwenhao:feat/issue-464-get-assign-var-names

Conversation

@chenjunwenhao

Copy link
Copy Markdown
Contributor

Summary

Add Express4Runner.getAssignVarNames(String script) — a new introspection API that returns the set of context variables assigned (created or updated) by a script. This is the natural counterpart to the existing getOutVarNames(): while getOutVarNames identifies external inputs, getAssignVarNames identifies outputs/side-effects.

Behavior

Script getAssignVarNames getOutVarNames
a = b {a} {b}
object a = b {} (typed decl = local) {b}
a = 1; b += 1 {a, b} {}
a = 1; b = a + 1 {a, b} {}

Key design decisions:

  • Typed declarations (int a = 10, String name = "hello") are excluded — they create locally-scoped variables, not assignments to the execution context.
  • For-each loop variables (for(i : list)) are excluded — also locally-scoped.
  • Increment/decrement operators (a++, --b) are included — they modify the variable.
  • Field assignments (obj.field = value) do not include the base variable — only the field is modified.

Implementation

Introduces AssignVarNamesVisitor extending ScopeStackVisitor, following the same AST-walking pattern as the existing OutVarNamesVisitor and OutVarAttrsVisitor. The visitor collects LHS variable names from assignment expressions and handles suffix/prefix ++/-- operators.

Test plan

  • Simple assignment (a = b{a})
  • Typed declaration excluded (object a = b{})
  • Compound assignment (b += 1{b})
  • Multiple assignments
  • Self-assignment (a = 1; a = a + 1{a})
  • Complementarity with getOutVarNames on same script
  • For-each loop variable excluded
  • If-else / while scoping
  • Field assignment (obj.field = 10{})
  • Function parameters excluded
  • Switch with typed declarations
  • Selector variable assignment (${0} = ${1})
  • Increment/decrement in sub-expressions (b = a++ + 1{a, b})
  • No assignments (a + b, hello(){})

Closes #464

…#464)

Add Express4Runner.getAssignVarNames() which returns the set of context
variables that are assigned (created or updated) by a script, excluding
locally-scoped variables such as typed declarations and for-each loop
variables. This complements the existing getOutVarNames() method which
returns external input variables.

The implementation introduces AssignVarNamesVisitor, following the same
AST-walking pattern as OutVarNamesVisitor and OutVarAttrsVisitor.

Co-Authored-By: Qoder <noreply@qoder.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

希望加一个能获取被赋值(新增/更新)的上下文变量名的方法

1 participant