Skip to content

utils

luau
local utils = require("@std/syntax/utils")

WARNING

These APIs are still open to future evolution. In new major versions, they may change in backwards incompatible ways.

Summary

EntryDescription
isAttributeReturns n narrowed to CstAttribute if it is an attribute node, or nil otherwise.
isExprReturns n narrowed to CstExpr if it is any expression node, or nil otherwise.
isExprBinaryReturns n narrowed to CstExprBinary if it is a binary expression, or nil otherwise.
isExprCallReturns n narrowed to CstExprCall if it is a call expression, or nil otherwise.
isExprConstantBoolReturns n narrowed to CstExprConstantBool if it is a boolean literal expression, or nil otherwise.
isExprConstantNilReturns n narrowed to CstExprConstantNil if it is a nil literal expression, or nil otherwise.
isExprConstantNumberReturns n narrowed to CstExprConstantNumber if it is a number literal expression, or nil otherwise.
isExprConstantStringReturns n narrowed to CstExprConstantString if it is a string literal expression, or nil otherwise.
isExprFunctionReturns n narrowed to CstExprFunction if it is a function expression, or nil otherwise.
isExprGlobalReturns n narrowed to CstExprGlobal if it is a global variable reference expression, or nil otherwise.
isExprGroupReturns n narrowed to CstExprGroup if it is a parenthesized expression, or nil otherwise.
isExprIfElseReturns n narrowed to CstExprIfElse if it is an if-then-else expression, or nil otherwise.
isExprIndexExprReturns n narrowed to CstExprIndexExpr if it is an index expression (e.g. a[b]), or nil otherwise.
isExprIndexNameReturns n narrowed to CstExprIndexName if it is a field access expression (e.g. a.b), or nil otherwise.
isExprInterpStringReturns n narrowed to CstExprInterpString if it is an interpolated string expression, or nil otherwise.
isExprLocalReturns n narrowed to CstExprLocal if it is a local variable reference expression, or nil otherwise.
isExprTableReturns n narrowed to CstExprTable if it is a table constructor expression, or nil otherwise.
isExprTypeAssertionReturns n narrowed to CstExprTypeAssertion if it is a type assertion expression (expr :: Type), or nil otherwise.
isExprUnaryReturns n narrowed to CstExprUnary if it is a unary expression, or nil otherwise.
isExprVarargsReturns n narrowed to CstExprVarargs if it is a varargs expression (...), or nil otherwise.
isGenericTypeReturns n narrowed to CstGenericType if it is a generic type parameter, or nil otherwise.
isGenericTypePackReturns n narrowed to CstGenericTypePack if it is a generic type pack parameter, or nil otherwise.
isLocalReturns n narrowed to CstLocal if it is a local variable node, or nil otherwise.
isRequireCallReturns n as an CstExprCall if it is a require(...) call expression, or nil otherwise.
isStatReturns n narrowed to CstStat if it is any statement node, or nil otherwise.
isStatAssignReturns n narrowed to CstStatAssign if it is an assignment statement, or nil otherwise.
isStatBlockReturns n narrowed to CstStatBlock if it is a block statement (do...end), or nil otherwise.
isStatBreakReturns n narrowed to CstStatBreak if it is a break statement, or nil otherwise.
isStatCompoundAssignReturns n narrowed to CstStatCompoundAssign if it is a compound assignment statement (e.g. +=), or nil otherwise.
isStatConstReturns n narrowed to CstStatConst if it is a const variable declaration, or nil otherwise.
isStatContinueReturns n narrowed to CstStatContinue if it is a continue statement, or nil otherwise.
isStatExprReturns n narrowed to CstStatExpr if it is an expression statement, or nil otherwise.
isStatForReturns n narrowed to CstStatFor if it is a numeric for statement, or nil otherwise.
isStatForInReturns n narrowed to CstStatForIn if it is a generic for-in statement, or nil otherwise.
isStatFunctionReturns n narrowed to CstStatFunction if it is a function declaration statement, or nil otherwise.
isStatIfReturns n narrowed to CstStatIf if it is an if statement, or nil otherwise.
isStatLocalReturns n narrowed to CstStatLocal if it is a local variable declaration, or nil otherwise.
isStatLocalFunctionReturns n narrowed to CstStatLocalFunction if it is a local function declaration, or nil otherwise.
isStatRepeatReturns n narrowed to CstStatRepeat if it is a repeat-until statement, or nil otherwise.
isStatReturnReturns n narrowed to CstStatReturn if it is a return statement, or nil otherwise.
isStatTypeAliasReturns n narrowed to CstStatTypeAlias if it is a type alias declaration, or nil otherwise.
isStatTypeFunctionReturns n narrowed to CstStatTypeFunction if it is a type function declaration, or nil otherwise.
isStatWhileReturns n narrowed to CstStatWhile if it is a while statement, or nil otherwise.
isTableExprItemReturns n narrowed to CstTableExprItem if it is a table constructor item, or nil otherwise.
isTokenReturns n narrowed to CstToken if it is a token, or nil otherwise.
isTypeReturns n narrowed to CstType if it is any type annotation node, or nil otherwise.
isTypeArrayReturns n narrowed to CstTypeArray if it is an array type ({T}), or nil otherwise.
isTypeFunctionReturns n narrowed to CstTypeFunction if it is a function type, or nil otherwise.
isTypeGroupReturns n narrowed to CstTypeGroup if it is a parenthesized type, or nil otherwise.
isTypeIntersectionReturns n narrowed to CstTypeIntersection if it is a type intersection, or nil otherwise.
isTypeOptionalReturns n narrowed to CstTypeOptional if it is an optional type (T?), or nil otherwise.
isTypePackReturns n narrowed to CstTypePack if it is any type pack node, or nil otherwise.
isTypePackExplicitReturns n narrowed to CstTypePackExplicit if it is an explicit type pack, or nil otherwise.
isTypePackGenericReturns n narrowed to CstTypePackGeneric if it is a generic type pack, or nil otherwise.
isTypePackVariadicReturns n narrowed to CstTypePackVariadic if it is a variadic type pack, or nil otherwise.
isTypeReferenceReturns n narrowed to CstTypeReference if it is a type reference, or nil otherwise.
isTypeSingletonBoolReturns n narrowed to CstTypeSingletonBool if it is a boolean singleton type, or nil otherwise.
isTypeSingletonStringReturns n narrowed to CstTypeSingletonString if it is a string singleton type, or nil otherwise.
isTypeTableReturns n narrowed to CstTypeTable if it is a table type, or nil otherwise.
isTypeTypeofReturns n narrowed to CstTypeTypeof if it is a typeof(...) type, or nil otherwise.
isTypeUnionReturns n narrowed to CstTypeUnion if it is a type union, or nil otherwise.
replacementWithTriviaReturns a Replacement that replaces the original node with the given content, preserving trivia.
replacementWithoutTriviaReturns a Replacement that replaces the original node with the given content, discarding trivia.

Functions and Properties

utils.isAttribute

Returns n narrowed to CstAttribute if it is an attribute node, or nil otherwise.

luau
(n: types.CstNode) -> types.CstAttribute?

utils.isExpr

Returns n narrowed to CstExpr if it is any expression node, or nil otherwise.

luau
(n: types.CstNode) -> types.CstExpr?

utils.isExprBinary

Returns n narrowed to CstExprBinary if it is a binary expression, or nil otherwise.

luau
(n: types.CstNode) -> types.CstExprBinary?

utils.isExprCall

Returns n narrowed to CstExprCall if it is a call expression, or nil otherwise.

luau
(n: types.CstNode) -> types.CstExprCall?

utils.isExprConstantBool

Returns n narrowed to CstExprConstantBool if it is a boolean literal expression, or nil otherwise.

luau
(n: types.CstNode) -> types.CstExprConstantBool?

utils.isExprConstantNil

Returns n narrowed to CstExprConstantNil if it is a nil literal expression, or nil otherwise.

luau
(n: types.CstNode) -> types.CstExprConstantNil?

utils.isExprConstantNumber

Returns n narrowed to CstExprConstantNumber if it is a number literal expression, or nil otherwise.

luau
(n: types.CstNode) -> types.CstExprConstantNumber?

utils.isExprConstantString

Returns n narrowed to CstExprConstantString if it is a string literal expression, or nil otherwise.

luau
(n: types.CstNode) -> types.CstExprConstantString?

utils.isExprFunction

Returns n narrowed to CstExprFunction if it is a function expression, or nil otherwise.

luau
(n: types.CstNode) -> types.CstExprFunction?

utils.isExprGlobal

Returns n narrowed to CstExprGlobal if it is a global variable reference expression, or nil otherwise.

luau
(n: types.CstNode) -> types.CstExprGlobal?

utils.isExprGroup

Returns n narrowed to CstExprGroup if it is a parenthesized expression, or nil otherwise.

luau
(n: types.CstNode) -> types.CstExprGroup?

utils.isExprIfElse

Returns n narrowed to CstExprIfElse if it is an if-then-else expression, or nil otherwise.

luau
(n: types.CstNode) -> types.CstExprIfElse?

utils.isExprIndexExpr

Returns n narrowed to CstExprIndexExpr if it is an index expression (e.g. a[b]), or nil otherwise.

luau
(n: types.CstNode) -> types.CstExprIndexExpr?

utils.isExprIndexName

Returns n narrowed to CstExprIndexName if it is a field access expression (e.g. a.b), or nil otherwise.

luau
(n: types.CstNode) -> types.CstExprIndexName?

utils.isExprInterpString

Returns n narrowed to CstExprInterpString if it is an interpolated string expression, or nil otherwise.

luau
(n: types.CstNode) -> types.CstExprInterpString?

utils.isExprLocal

Returns n narrowed to CstExprLocal if it is a local variable reference expression, or nil otherwise.

luau
(n: types.CstNode) -> types.CstExprLocal?

utils.isExprTable

Returns n narrowed to CstExprTable if it is a table constructor expression, or nil otherwise.

luau
(n: types.CstNode) -> types.CstExprTable?

utils.isExprTypeAssertion

Returns n narrowed to CstExprTypeAssertion if it is a type assertion expression (expr :: Type), or nil otherwise.

luau
(n: types.CstNode) -> types.CstExprTypeAssertion?

utils.isExprUnary

Returns n narrowed to CstExprUnary if it is a unary expression, or nil otherwise.

luau
(n: types.CstNode) -> types.CstExprUnary?

utils.isExprVarargs

Returns n narrowed to CstExprVarargs if it is a varargs expression (...), or nil otherwise.

luau
(n: types.CstNode) -> types.CstExprVarargs?

utils.isGenericType

Returns n narrowed to CstGenericType if it is a generic type parameter, or nil otherwise.

luau
(n: types.CstNode) -> types.CstGenericType?

utils.isGenericTypePack

Returns n narrowed to CstGenericTypePack if it is a generic type pack parameter, or nil otherwise.

luau
(n: types.CstNode) -> types.CstGenericTypePack?

utils.isLocal

Returns n narrowed to CstLocal if it is a local variable node, or nil otherwise.

luau
(n: types.CstNode) -> types.CstLocal?

utils.isRequireCall

Returns n as an CstExprCall if it is a require(...) call expression, or nil otherwise.

luau
(n: types.CstNode) -> types.CstExprCall?

utils.isStat

Returns n narrowed to CstStat if it is any statement node, or nil otherwise.

luau
(n: types.CstNode) -> types.CstStat?

utils.isStatAssign

Returns n narrowed to CstStatAssign if it is an assignment statement, or nil otherwise.

luau
(n: types.CstNode) -> types.CstStatAssign?

utils.isStatBlock

Returns n narrowed to CstStatBlock if it is a block statement (do...end), or nil otherwise.

luau
(n: types.CstNode) -> types.CstStatBlock?

utils.isStatBreak

Returns n narrowed to CstStatBreak if it is a break statement, or nil otherwise.

luau
(n: types.CstNode) -> types.CstStatBreak?

utils.isStatCompoundAssign

Returns n narrowed to CstStatCompoundAssign if it is a compound assignment statement (e.g. +=), or nil otherwise.

luau
(n: types.CstNode) -> types.CstStatCompoundAssign?

utils.isStatConst

Returns n narrowed to CstStatConst if it is a const variable declaration, or nil otherwise.

luau
(n: types.CstNode) -> types.CstStatConst?

utils.isStatContinue

Returns n narrowed to CstStatContinue if it is a continue statement, or nil otherwise.

luau
(n: types.CstNode) -> types.CstStatContinue?

utils.isStatExpr

Returns n narrowed to CstStatExpr if it is an expression statement, or nil otherwise.

luau
(n: types.CstNode) -> types.CstStatExpr?

utils.isStatFor

Returns n narrowed to CstStatFor if it is a numeric for statement, or nil otherwise.

luau
(n: types.CstNode) -> types.CstStatFor?

utils.isStatForIn

Returns n narrowed to CstStatForIn if it is a generic for-in statement, or nil otherwise.

luau
(n: types.CstNode) -> types.CstStatForIn?

utils.isStatFunction

Returns n narrowed to CstStatFunction if it is a function declaration statement, or nil otherwise.

luau
(n: types.CstNode) -> types.CstStatFunction?

utils.isStatIf

Returns n narrowed to CstStatIf if it is an if statement, or nil otherwise.

luau
(n: types.CstNode) -> types.CstStatIf?

utils.isStatLocal

Returns n narrowed to CstStatLocal if it is a local variable declaration, or nil otherwise.

luau
(n: types.CstNode) -> types.CstStatLocal?

utils.isStatLocalFunction

Returns n narrowed to CstStatLocalFunction if it is a local function declaration, or nil otherwise.

luau
(n: types.CstNode) -> types.CstStatLocalFunction?

utils.isStatRepeat

Returns n narrowed to CstStatRepeat if it is a repeat-until statement, or nil otherwise.

luau
(n: types.CstNode) -> types.CstStatRepeat?

utils.isStatReturn

Returns n narrowed to CstStatReturn if it is a return statement, or nil otherwise.

luau
(n: types.CstNode) -> types.CstStatReturn?

utils.isStatTypeAlias

Returns n narrowed to CstStatTypeAlias if it is a type alias declaration, or nil otherwise.

luau
(n: types.CstNode) -> types.CstStatTypeAlias?

utils.isStatTypeFunction

Returns n narrowed to CstStatTypeFunction if it is a type function declaration, or nil otherwise.

luau
(n: types.CstNode) -> types.CstStatTypeFunction?

utils.isStatWhile

Returns n narrowed to CstStatWhile if it is a while statement, or nil otherwise.

luau
(n: types.CstNode) -> types.CstStatWhile?

utils.isTableExprItem

Returns n narrowed to CstTableExprItem if it is a table constructor item, or nil otherwise.

luau
(n: types.CstNode | types.CstTableExprItem) -> types.CstTableExprItem?

utils.isToken

Returns n narrowed to CstToken if it is a token, or nil otherwise.

luau
(n: types.CstNode) -> types.CstToken?

utils.isType

Returns n narrowed to CstType if it is any type annotation node, or nil otherwise.

luau
(n: types.CstNode) -> types.CstType?

utils.isTypeArray

Returns n narrowed to CstTypeArray if it is an array type ({T}), or nil otherwise.

luau
(n: types.CstNode) -> types.CstTypeArray?

utils.isTypeFunction

Returns n narrowed to CstTypeFunction if it is a function type, or nil otherwise.

luau
(n: types.CstNode) -> types.CstTypeFunction?

utils.isTypeGroup

Returns n narrowed to CstTypeGroup if it is a parenthesized type, or nil otherwise.

luau
(n: types.CstNode) -> types.CstTypeGroup?

utils.isTypeIntersection

Returns n narrowed to CstTypeIntersection if it is a type intersection, or nil otherwise.

luau
(n: types.CstNode) -> types.CstTypeIntersection?

utils.isTypeOptional

Returns n narrowed to CstTypeOptional if it is an optional type (T?), or nil otherwise.

luau
(n: types.CstNode) -> types.CstTypeOptional?

utils.isTypePack

Returns n narrowed to CstTypePack if it is any type pack node, or nil otherwise.

luau
(n: types.CstNode) -> types.CstTypePack?

utils.isTypePackExplicit

Returns n narrowed to CstTypePackExplicit if it is an explicit type pack, or nil otherwise.

luau
(n: types.CstNode) -> types.CstTypePackExplicit?

utils.isTypePackGeneric

Returns n narrowed to CstTypePackGeneric if it is a generic type pack, or nil otherwise.

luau
(n: types.CstNode) -> types.CstTypePackGeneric?

utils.isTypePackVariadic

Returns n narrowed to CstTypePackVariadic if it is a variadic type pack, or nil otherwise.

luau
(n: types.CstNode) -> types.CstTypePackVariadic?

utils.isTypeReference

Returns n narrowed to CstTypeReference if it is a type reference, or nil otherwise.

luau
(n: types.CstNode) -> types.CstTypeReference?

utils.isTypeSingletonBool

Returns n narrowed to CstTypeSingletonBool if it is a boolean singleton type, or nil otherwise.

luau
(n: types.CstNode) -> types.CstTypeSingletonBool?

utils.isTypeSingletonString

Returns n narrowed to CstTypeSingletonString if it is a string singleton type, or nil otherwise.

luau
(n: types.CstNode) -> types.CstTypeSingletonString?

utils.isTypeTable

Returns n narrowed to CstTypeTable if it is a table type, or nil otherwise.

luau
(n: types.CstNode) -> types.CstTypeTable?

utils.isTypeTypeof

Returns n narrowed to CstTypeTypeof if it is a typeof(...) type, or nil otherwise.

luau
(n: types.CstNode) -> types.CstTypeTypeof?

utils.isTypeUnion

Returns n narrowed to CstTypeUnion if it is a type union, or nil otherwise.

luau
(n: types.CstNode) -> types.CstTypeUnion?

utils.replacementWithTrivia

Returns a Replacement that replaces the original node with the given content, preserving trivia.

luau
(content: string) -> types.Replacement

utils.replacementWithoutTrivia

Returns a Replacement that replaces the original node with the given content, discarding trivia.

luau
(content: string) -> types.Replacement