Module Refactor

Refactoring module

Tables

StmtAST Stmt
ExprAST Expr

Fields

refactorGlobal refactoring state

Class RefactorState

RefactorState:run_command (name, args)Run a builtin refactoring command
RefactorState:transform (callback)Run a custom refactoring transformation

Class MatchCtxt

MatchCtxt:parse_stmts (pat)Parse statements and add them to this MatchCtxt
MatchCtxt:parse_expr (pat)Parse an expressiong and add it to this MatchCtxt
MatchCtxt:fold_with (needle, crate, callback)Find matches of pattern within crate and rewrite using callback
MatchCtxt:get_expr (Expression)Get matched binding for an expression variable
MatchCtxt:get_stmt (Statement)Get matched binding for a statement variable
MatchCtxt:try_match (pat, target)Attempt to match target against pat, updating bindings if matched.
MatchCtxt:subst (replacement)Substitute the currently matched AST node with a new AST node

Class TransformCtxt

TransformCtxt:replace_stmts_with (needle, callback)Replace matching statements using given callback
TransformCtxt:replace_expr_with (needle, callback)Replace matching expressions using given callback
TransformCtxt:match ()Create a new, empty MatchCtxt
TransformCtxt:get_ast (node)Retrieve a Lua version of an AST node

Tables

  • Stmt
  • AST Stmt

Fields:

  • type "Stmt"
  • kindstring StmtKind of this statement

StmtKind::Local only:

StmtKind::Item only:

StmtKind::Semi and StmtKind::Expr only:

  • exprAstNode Expression in this statement
    • Expr
    • AST Expr

Fields:

  • type "Expr"
  • kindstring ExprKind of this expression

ExprKind::Lit only:

  • value Literal value of this expression

Fields

  • refactor
  • Global refactoring state
    • refactor RefactorState object

Class RefactorState

Refactoring context

  • RefactorState:run_command (name, args)
  • Run a builtin refactoring command

Parameters:

  • namestring Command to run
  • args{string,…} List of arguments for the command
    • RefactorState:transform (callback)
    • Run a custom refactoring transformation

Parameters:

  • callbackfunction(TransformCtxt,AstNode) Transformation function called with a fresh TransformCtxt and the crate to be transformed.

Class MatchCtxt

A match context

  • MatchCtxt:parse_stmts (pat)
  • Parse statements and add them to this MatchCtxt

Parameters:

Returns:

AstNode The parsed statements

  • MatchCtxt:parse_expr (pat)
  • Parse an expressiong and add it to this MatchCtxt

Parameters:

Returns:

AstNode The parsed expression

  • MatchCtxt:fold_with (needle, crate, callback)
  • Find matches of pattern within crate and rewrite using callback

Parameters:

  • needleAstNode Pattern to search for
  • crateAstNode Crate to fold over
  • callbackfunction(AstNode,MatchCtxt) Function called for each match. Takes the matching node and a new MatchCtxt for that match.
    • MatchCtxt:get_expr (Expression)
    • Get matched binding for an expression variable

Parameters:

  • Expressionstring variable pattern

Returns:

AstNode Expression matched by this binding

  • MatchCtxt:get_stmt (Statement)
  • Get matched binding for a statement variable

Parameters:

  • Statementstring variable pattern

Returns:

AstNode Statement matched by this binding

  • MatchCtxt:try_match (pat, target)
  • Attempt to match target against pat, updating bindings if matched.

Parameters:

  • patAstNode AST (potentially with variable bindings) to match with
  • targetAstNode AST to match against

Returns:

bool true if match was successful

  • MatchCtxt:subst (replacement)
  • Substitute the currently matched AST node with a new AST node

Parameters:

  • replacementAstNode New AST node to replace the currently matched AST. May include variable bindings if these bindings were matched by the search pattern.

Returns:

AstNode New AST node with variable bindings replaced by their matched values

Class TransformCtxt

Transformation context

  • TransformCtxt:replace_stmts_with (needle, callback)
  • Replace matching statements using given callback

Parameters:

  • needlestring Statements pattern to search for, may include variable bindings
  • callbackfunction(AstNode,MatchCtxt) Function called for each match. Takes the matching node and a new MatchCtxt for that match. See MatchCtxt:fold_with
    • TransformCtxt:replace_expr_with (needle, callback)
    • Replace matching expressions using given callback

Parameters:

  • needlestring Expression pattern to search for, may include variable bindings
  • callbackfunction(AstNode,MatchCtxt) Function called for each match. Takes the matching node and a new MatchCtxt for that match. See MatchCtxt:fold_with
    • TransformCtxt:match ()
    • Create a new, empty MatchCtxt

Returns:

MatchCtxt New match context

  • TransformCtxt:get_ast (node)
  • Retrieve a Lua version of an AST node

Parameters:

Returns:

Struct representation of this AST node. Valid return types are Stmt, and Expr.