Skip to content

Commit 9c91f0e

Browse files
committed
Handle numbers
1 parent f4fc8da commit 9c91f0e

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

examples/codemods/identity.luau

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ end
1111
codemod.apply("examples/a.luau", identityTransform)
1212
codemod.apply("examples/b.luau", identityTransform)
1313
codemod.apply("examples/json.luau", identityTransform)
14+
codemod.apply("examples/main.luau", identityTransform)
15+
codemod.apply("examples/parsing.luau", identityTransform)
16+
codemod.apply("examples/time_example.luau", identityTransform)

luau/src/luau.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,9 +489,9 @@ struct AstSerialize : public Luau::AstVisitor
489489

490490
void serialize(Luau::AstExprConstantNumber* node)
491491
{
492-
lua_rawcheckstack(L, 2);
493-
lua_createtable(L, 0, preambleSize + 1);
492+
const auto cstNode = lookupCstNode<Luau::CstExprConstantNumber>(node);
494493

494+
serializeToken(node->location.begin, cstNode ? cstNode->value.data : std::to_string(node->value).data(), preambleSize + 1);
495495
serializeNodePreamble(node, "number");
496496

497497
lua_pushnumber(L, node->value);

std/codemod/ast_types.luau

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export type AstLocal = {
4545

4646
export type AstExprConstantBool = Token<"true" | "false"> & { tag: "boolean", value: boolean }
4747

48+
export type AstExprConstantNumber = Token<string> & { tag: "number", value: number }
4849

4950
export type AstExprConstantString = Token<string> & {
5051
tag: "string",
@@ -118,6 +119,7 @@ export type AstExprBinary = {
118119

119120
export type AstExpr =
120121
| AstExprConstantBool
122+
| AstExprConstantNumber
121123
| AstExprConstantString
122124
| AstExprLocal
123125
| AstExprGlobal

std/codemod/visitor.luau

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type Visitor = {
1818
visitToken: (T.Token) -> boolean,
1919
visitString: (T.AstExprConstantString) -> boolean,
2020
visitBoolean: (T.AstExprConstantBool) -> boolean,
21+
visitNumber: (T.AstExprConstantNumber) -> boolean,
2122
visitLocal: (T.AstLocal) -> boolean,
2223
}
2324

@@ -41,6 +42,7 @@ local defaultVisitor: Visitor = {
4142
visitToken = alwaysVisit :: any,
4243
visitString = alwaysVisit :: any,
4344
visitBoolean = alwaysVisit :: any,
45+
visitNumber = alwaysVisit :: any,
4446
visitLocal = alwaysVisit :: any,
4547
}
4648

@@ -105,6 +107,12 @@ local function visitBoolean(node: T.AstExprConstantBool, visitor: Visitor)
105107
end
106108
end
107109

110+
local function visitNumber(node: T.AstExprConstantNumber, visitor: Visitor)
111+
if visitor.visitNumber(node) then
112+
visitToken(node, visitor)
113+
end
114+
end
115+
108116
local function visitLocalReference(node: T.AstExprLocal, visitor: Visitor)
109117
if visitor.visitLocalReference(node) then
110118
visitor.visitToken(node.token)
@@ -183,6 +191,8 @@ end
183191
function visitExpression(expression: T.AstExpr, visitor: Visitor)
184192
if expression.tag == "boolean" then
185193
visitBoolean(expression, visitor)
194+
elseif expression.tag == "number" then
195+
visitNumber(expression, visitor)
186196
elseif expression.tag == "string" then
187197
visitString(expression, visitor)
188198
elseif expression.tag == "local" then

0 commit comments

Comments
 (0)