Skip to main content

StatementSyntax Base Class

All statement nodes inherit from StatementSyntax, which provides:
  • SemicolonToken - Optional semicolon at the end of the statement
  • WithSemicolonToken(token) - Creates a new statement with a different semicolon

Variable Declarations

LocalVariableDeclarationStatementSyntax

Declares local variables with optional initialization. Syntax: local name1, name2 = value1, value2 Key properties:
  • LocalKeyword - The local keyword token
  • Names - List of variable names being declared (SeparatedSyntaxList<LocalDeclarationNameSyntax>)
  • EqualsValues - Optional equals clause with initial values
var localDecl = SyntaxFactory.LocalVariableDeclarationStatement(
    SyntaxFactory.SeparatedList(new[] {
        SyntaxFactory.LocalDeclarationName(
            SyntaxFactory.IdentifierName("x")
        ),
        SyntaxFactory.LocalDeclarationName(
            SyntaxFactory.IdentifierName("y")
        )
    }),
    SyntaxFactory.EqualsValuesClause(
        SyntaxFactory.SeparatedList<ExpressionSyntax>(new[] {
            SyntaxFactory.LiteralExpression(SyntaxKind.NumericalLiteralExpression,
                SyntaxFactory.Literal(1)),
            SyntaxFactory.LiteralExpression(SyntaxKind.NumericalLiteralExpression,
                SyntaxFactory.Literal(2))
        })
    )
);
// Produces: local x, y = 1, 2

LocalDeclarationNameSyntax

Represents a single variable name in a local declaration, with optional type annotation and attribute. Key properties:
  • IdentifierName - The variable name
  • Attribute - Optional attribute like <const> or <close>
  • TypeBinding - Optional type annotation (for Luau/typed Lua)
  • Name - String property for the variable name
  • AttributeName - String property for the attribute name
// local x: number
var typedName = SyntaxFactory.LocalDeclarationName(
    SyntaxFactory.IdentifierName("x"),
    null,  // no attribute
    SyntaxFactory.TypeBinding(
        SyntaxFactory.SimpleTypeName(SyntaxFactory.Identifier("number"))
    )
);

// local x <const> = 10
var constName = SyntaxFactory.LocalDeclarationName(
    SyntaxFactory.IdentifierName("x"),
    SyntaxFactory.VariableAttribute(SyntaxFactory.Identifier("const"))
);

Assignment Statements

AssignmentStatementSyntax

Assigns values to existing variables. Syntax: a, b = 1, 2 Key properties:
  • Variables - List of variables being assigned to (SeparatedSyntaxList<PrefixExpressionSyntax>)
  • EqualsValues - The equals clause with values
var assignment = SyntaxFactory.AssignmentStatement(
    SyntaxFactory.SeparatedList<PrefixExpressionSyntax>(new[] {
        SyntaxFactory.IdentifierName("x"),
        SyntaxFactory.IdentifierName("y")
    }),
    SyntaxFactory.EqualsValuesClause(
        SyntaxFactory.SeparatedList<ExpressionSyntax>(new[] {
            SyntaxFactory.LiteralExpression(SyntaxKind.NumericalLiteralExpression,
                SyntaxFactory.Literal(10)),
            SyntaxFactory.LiteralExpression(SyntaxKind.NumericalLiteralExpression,
                SyntaxFactory.Literal(20))
        })
    )
);
// Produces: x, y = 10, 20

CompoundAssignmentStatementSyntax

Compound assignment operators (requires syntax option enabled). Syntax: x += 1, y *= 2, etc. Key properties:
  • Variable - The variable being modified
  • AssignmentOperatorToken - The operator (+=, -=, *=, /=, %=, ^=, ..=)
  • Expression - The value expression
var compoundAssign = SyntaxFactory.CompoundAssignmentStatement(
    SyntaxKind.AddAssignmentStatement,
    SyntaxFactory.IdentifierName("x"),
    SyntaxFactory.Token(SyntaxKind.PlusEqualsToken),
    SyntaxFactory.LiteralExpression(SyntaxKind.NumericalLiteralExpression,
        SyntaxFactory.Literal(1))
);
// Produces: x += 1

Function Declarations

FunctionDeclarationStatementSyntax

Declares a named function. Syntax: function name(param1, param2) body end Key properties:
  • FunctionKeyword - The function keyword
  • Name - Function name (can be SimpleFunctionNameSyntax, MemberFunctionNameSyntax, or MethodFunctionNameSyntax)
  • TypeParameterList - Optional generic type parameters (Luau)
  • Parameters - Parameter list
  • TypeBinding - Optional return type annotation
  • Body - Function body statements
  • EndKeyword - The end keyword
var funcDecl = SyntaxFactory.FunctionDeclarationStatement(
    SyntaxFactory.SimpleFunctionName(SyntaxFactory.Identifier("add")),
    SyntaxFactory.ParameterList(
        SyntaxFactory.SeparatedList<ParameterSyntax>(new[] {
            SyntaxFactory.NamedParameter(SyntaxFactory.Identifier("a")),
            SyntaxFactory.NamedParameter(SyntaxFactory.Identifier("b"))
        })
    ),
    SyntaxFactory.StatementList(
        SyntaxFactory.ReturnStatement(
            SyntaxFactory.SeparatedList<ExpressionSyntax>(new[] {
                SyntaxFactory.BinaryExpression(
                    SyntaxKind.AddExpression,
                    SyntaxFactory.IdentifierName("a"),
                    SyntaxFactory.IdentifierName("b")
                )
            })
        )
    )
);
// Produces: function add(a, b) return a + b end

LocalFunctionDeclarationStatementSyntax

Declares a local function. Syntax: local function name(params) body end Key properties:
  • LocalKeyword - The local keyword
  • FunctionKeyword - The function keyword
  • Name - Function name (IdentifierNameSyntax)
  • TypeParameterList - Optional generic type parameters
  • Parameters - Parameter list
  • TypeBinding - Optional return type annotation
  • Body - Function body
  • EndKeyword - The end keyword

Control Flow Statements

IfStatementSyntax

Conditional execution. Syntax: if condition then body elseif condition2 then body2 else body3 end Key properties:
  • IfKeyword - The if keyword
  • Condition - The condition expression
  • ThenKeyword - The then keyword
  • Body - The if-body statements
  • ElseIfClauses - List of elseif clauses
  • ElseClause - Optional else clause
  • EndKeyword - The end keyword
var ifStmt = SyntaxFactory.IfStatement(
    SyntaxFactory.BinaryExpression(
        SyntaxKind.GreaterThanExpression,
        SyntaxFactory.IdentifierName("x"),
        SyntaxFactory.LiteralExpression(SyntaxKind.NumericalLiteralExpression,
            SyntaxFactory.Literal(0))
    ),
    SyntaxFactory.StatementList(
        SyntaxFactory.ExpressionStatement(
            SyntaxFactory.FunctionCallExpression(
                SyntaxFactory.IdentifierName("print"),
                SyntaxFactory.ExpressionListFunctionArgument(
                    SyntaxFactory.SeparatedList<ExpressionSyntax>(new[] {
                        SyntaxFactory.LiteralExpression(
                            SyntaxKind.StringLiteralExpression,
                            SyntaxFactory.Literal("positive")
                        )
                    })
                )
            )
        )
    )
);
// Produces: if x > 0 then print("positive") end

WhileStatementSyntax

Loop with pre-condition. Syntax: while condition do body end Key properties:
  • WhileKeyword - The while keyword
  • Condition - Loop condition
  • DoKeyword - The do keyword
  • Body - Loop body
  • EndKeyword - The end keyword

RepeatUntilStatementSyntax

Loop with post-condition. Syntax: repeat body until condition Key properties:
  • RepeatKeyword - The repeat keyword
  • Body - Loop body
  • UntilKeyword - The until keyword
  • Condition - Exit condition

NumericForStatementSyntax

Numeric for loop. Syntax: for i = start, finish, step do body end Key properties:
  • ForKeyword - The for keyword
  • Identifier - Loop variable
  • EqualsToken - The = token
  • InitialValue - Start value
  • FinalValue - End value
  • StepValue - Optional step value (default 1)
  • DoKeyword - The do keyword
  • Body - Loop body
  • EndKeyword - The end keyword
var numericFor = SyntaxFactory.NumericForStatement(
    SyntaxFactory.TypedIdentifierName(
        SyntaxFactory.IdentifierName("i")
    ),
    SyntaxFactory.LiteralExpression(SyntaxKind.NumericalLiteralExpression,
        SyntaxFactory.Literal(1)),
    SyntaxFactory.LiteralExpression(SyntaxKind.NumericalLiteralExpression,
        SyntaxFactory.Literal(10)),
    null,  // no step, defaults to 1
    SyntaxFactory.StatementList(
        SyntaxFactory.ExpressionStatement(
            SyntaxFactory.FunctionCallExpression(
                SyntaxFactory.IdentifierName("print"),
                SyntaxFactory.ExpressionListFunctionArgument(
                    SyntaxFactory.SeparatedList<ExpressionSyntax>(new[] {
                        SyntaxFactory.IdentifierName("i")
                    })
                )
            )
        )
    )
);
// Produces: for i = 1, 10 do print(i) end

GenericForStatementSyntax

Generic for loop (iterator-based). Syntax: for var1, var2 in expr1, expr2 do body end Key properties:
  • ForKeyword - The for keyword
  • Identifiers - Loop variables
  • InKeyword - The in keyword
  • Expressions - Iterator expressions
  • DoKeyword - The do keyword
  • Body - Loop body
  • EndKeyword - The end keyword
var genericFor = SyntaxFactory.GenericForStatement(
    SyntaxFactory.SeparatedList(new[] {
        SyntaxFactory.TypedIdentifierName(
            SyntaxFactory.IdentifierName("k")
        ),
        SyntaxFactory.TypedIdentifierName(
            SyntaxFactory.IdentifierName("v")
        )
    }),
    SyntaxFactory.SeparatedList<ExpressionSyntax>(new[] {
        SyntaxFactory.FunctionCallExpression(
            SyntaxFactory.IdentifierName("pairs"),
            SyntaxFactory.ExpressionListFunctionArgument(
                SyntaxFactory.SeparatedList<ExpressionSyntax>(new[] {
                    SyntaxFactory.IdentifierName("tbl")
                })
            )
        )
    }),
    SyntaxFactory.StatementList(
        SyntaxFactory.ExpressionStatement(
            SyntaxFactory.FunctionCallExpression(
                SyntaxFactory.IdentifierName("print"),
                SyntaxFactory.ExpressionListFunctionArgument(
                    SyntaxFactory.SeparatedList<ExpressionSyntax>(new[] {
                        SyntaxFactory.IdentifierName("k"),
                        SyntaxFactory.IdentifierName("v")
                    })
                )
            )
        )
    )
);
// Produces: for k, v in pairs(tbl) do print(k, v) end

DoStatementSyntax

Do-end block (creates a new scope). Syntax: do body end Key properties:
  • DoKeyword - The do keyword
  • Body - Block body
  • EndKeyword - The end keyword

Jump Statements

ReturnStatementSyntax

Returns from a function. Syntax: return expr1, expr2 Key properties:
  • ReturnKeyword - The return keyword
  • Expressions - List of return values

BreakStatementSyntax

Breaks out of a loop. Syntax: break Key properties:
  • BreakKeyword - The break keyword

ContinueStatementSyntax

Continues to next iteration (if enabled). Syntax: continue Key properties:
  • ContinueKeyword - The continue keyword

GotoStatementSyntax

Jumps to a label. Syntax: goto labelname Key properties:
  • GotoKeyword - The goto keyword
  • LabelName - The label identifier

GotoLabelStatementSyntax

Defines a goto label. Syntax: ::labelname:: Key properties:
  • LeftDelimiterToken - The opening ::
  • Identifier - The label name
  • RightDelimiterToken - The closing ::

Other Statements

ExpressionStatementSyntax

A standalone expression (typically a function call). Key properties:
  • Expression - The expression being evaluated

EmptyStatementSyntax

A standalone semicolon. Key properties:
  • Only the SemicolonToken

TypeDeclarationStatementSyntax

Type alias declaration (Luau). Syntax: type Name<T> = Type or export type Name = Type Key properties:
  • ExportKeyword - Optional export keyword
  • TypeKeyword - The type keyword
  • Name - The type alias name
  • TypeParameterList - Optional generic parameters
  • EqualsToken - The = token
  • Type - The type being aliased
var typeDecl = SyntaxFactory.TypeDeclarationStatement(
    SyntaxFactory.Token(SyntaxKind.None),  // no export
    SyntaxFactory.Identifier("Point"),
    null,  // no type parameters
    SyntaxFactory.TableType(
        SyntaxFactory.SeparatedList<TableTypeElementSyntax>(new[] {
            SyntaxFactory.TableTypeProperty(
                SyntaxFactory.Identifier("x"),
                SyntaxFactory.SimpleTypeName(SyntaxFactory.Identifier("number"))
            ),
            SyntaxFactory.TableTypeProperty(
                SyntaxFactory.Identifier("y"),
                SyntaxFactory.SimpleTypeName(SyntaxFactory.Identifier("number"))
            )
        })
    )
);
// Produces: type Point = { x: number, y: number }

See Also

Build docs developers (and LLMs) love