Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/transformation/builtins/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ export function transformConsoleCall(
const parameters = transformArguments(context, expression.arguments, signature);

switch (methodName) {
case "error":
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also add the remaining console API functions like log, warn ...

https://console.spec.whatwg.org/#console-namespace

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, added .info and .warn too

case "info":
case "log":
case "warn":
if (expression.arguments.length > 0 && isStringFormatTemplate(expression.arguments[0])) {
// print(string.format([arguments]))
const stringFormatCall = lua.createCallExpression(
Expand Down
132 changes: 132 additions & 0 deletions test/unit/builtins/__snapshots__/console.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,94 @@ end
return ____exports"
`;

exports[`console.error ("console.error()") 1`] = `
"local ____exports = {}
function ____exports.__main(self)
print()
end
return ____exports"
`;

exports[`console.error ("console.error(\\"Hello %%s\\", \\"there\\")") 1`] = `
"local ____exports = {}
function ____exports.__main(self)
print(
string.format(\\"Hello %%s\\", \\"there\\")
)
end
return ____exports"
`;

exports[`console.error ("console.error(\\"Hello %s\\", \\"there\\")") 1`] = `
"local ____exports = {}
function ____exports.__main(self)
print(
string.format(\\"Hello %s\\", \\"there\\")
)
end
return ____exports"
`;

exports[`console.error ("console.error(\\"Hello\\")") 1`] = `
"local ____exports = {}
function ____exports.__main(self)
print(\\"Hello\\")
end
return ____exports"
`;

exports[`console.error ("console.error(\\"Hello\\", \\"There\\")") 1`] = `
"local ____exports = {}
function ____exports.__main(self)
print(\\"Hello\\", \\"There\\")
end
return ____exports"
`;

exports[`console.info ("console.info()") 1`] = `
"local ____exports = {}
function ____exports.__main(self)
print()
end
return ____exports"
`;

exports[`console.info ("console.info(\\"Hello %%s\\", \\"there\\")") 1`] = `
"local ____exports = {}
function ____exports.__main(self)
print(
string.format(\\"Hello %%s\\", \\"there\\")
)
end
return ____exports"
`;

exports[`console.info ("console.info(\\"Hello %s\\", \\"there\\")") 1`] = `
"local ____exports = {}
function ____exports.__main(self)
print(
string.format(\\"Hello %s\\", \\"there\\")
)
end
return ____exports"
`;

exports[`console.info ("console.info(\\"Hello\\")") 1`] = `
"local ____exports = {}
function ____exports.__main(self)
print(\\"Hello\\")
end
return ____exports"
`;

exports[`console.info ("console.info(\\"Hello\\", \\"There\\")") 1`] = `
"local ____exports = {}
function ____exports.__main(self)
print(\\"Hello\\", \\"There\\")
end
return ____exports"
`;

exports[`console.log ("console.log()") 1`] = `
"local ____exports = {}
function ____exports.__main(self)
Expand Down Expand Up @@ -143,3 +231,47 @@ function ____exports.__main(self)
end
return ____exports"
`;

exports[`console.warn ("console.warn()") 1`] = `
"local ____exports = {}
function ____exports.__main(self)
print()
end
return ____exports"
`;

exports[`console.warn ("console.warn(\\"Hello %%s\\", \\"there\\")") 1`] = `
"local ____exports = {}
function ____exports.__main(self)
print(
string.format(\\"Hello %%s\\", \\"there\\")
)
end
return ____exports"
`;

exports[`console.warn ("console.warn(\\"Hello %s\\", \\"there\\")") 1`] = `
"local ____exports = {}
function ____exports.__main(self)
print(
string.format(\\"Hello %s\\", \\"there\\")
)
end
return ____exports"
`;

exports[`console.warn ("console.warn(\\"Hello\\")") 1`] = `
"local ____exports = {}
function ____exports.__main(self)
print(\\"Hello\\")
end
return ____exports"
`;

exports[`console.warn ("console.warn(\\"Hello\\", \\"There\\")") 1`] = `
"local ____exports = {}
function ____exports.__main(self)
print(\\"Hello\\", \\"There\\")
end
return ____exports"
`;
36 changes: 36 additions & 0 deletions test/unit/builtins/console.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,42 @@ test.each([
.expectLuaToMatchSnapshot();
});

test.each([
"console.info()",
'console.info("Hello")',
'console.info("Hello %s", "there")',
'console.info("Hello %%s", "there")',
'console.info("Hello", "There")',
])("console.info (%p)", code => {
util.testFunction(code)
.setOptions(compilerOptions)
.expectLuaToMatchSnapshot();
});

test.each([
"console.error()",
'console.error("Hello")',
'console.error("Hello %s", "there")',
'console.error("Hello %%s", "there")',
'console.error("Hello", "There")',
])("console.error (%p)", code => {
util.testFunction(code)
.setOptions(compilerOptions)
.expectLuaToMatchSnapshot();
});

test.each([
"console.warn()",
'console.warn("Hello")',
'console.warn("Hello %s", "there")',
'console.warn("Hello %%s", "there")',
'console.warn("Hello", "There")',
])("console.warn (%p)", code => {
util.testFunction(code)
.setOptions(compilerOptions)
.expectLuaToMatchSnapshot();
});

test.each([
"console.trace()",
'console.trace("message")',
Expand Down