Skip to content

Commit 3bd4333

Browse files
authored
Transpile console.error/info/warn to regular print like console.log (#766)
* Transpile console.error to regular print like console.log * Added console.info and console.warn too
1 parent 105ff7b commit 3bd4333

File tree

3 files changed

+171
-0
lines changed

3 files changed

+171
-0
lines changed

src/transformation/builtins/console.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ export function transformConsoleCall(
1616
const parameters = transformArguments(context, expression.arguments, signature);
1717

1818
switch (methodName) {
19+
case "error":
20+
case "info":
1921
case "log":
22+
case "warn":
2023
if (expression.arguments.length > 0 && isStringFormatTemplate(expression.arguments[0])) {
2124
// print(string.format([arguments]))
2225
const stringFormatCall = lua.createCallExpression(

test/unit/builtins/__snapshots__/console.spec.ts.snap

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,94 @@ end
4646
return ____exports"
4747
`;
4848

49+
exports[`console.error ("console.error()") 1`] = `
50+
"local ____exports = {}
51+
function ____exports.__main(self)
52+
print()
53+
end
54+
return ____exports"
55+
`;
56+
57+
exports[`console.error ("console.error(\\"Hello %%s\\", \\"there\\")") 1`] = `
58+
"local ____exports = {}
59+
function ____exports.__main(self)
60+
print(
61+
string.format(\\"Hello %%s\\", \\"there\\")
62+
)
63+
end
64+
return ____exports"
65+
`;
66+
67+
exports[`console.error ("console.error(\\"Hello %s\\", \\"there\\")") 1`] = `
68+
"local ____exports = {}
69+
function ____exports.__main(self)
70+
print(
71+
string.format(\\"Hello %s\\", \\"there\\")
72+
)
73+
end
74+
return ____exports"
75+
`;
76+
77+
exports[`console.error ("console.error(\\"Hello\\")") 1`] = `
78+
"local ____exports = {}
79+
function ____exports.__main(self)
80+
print(\\"Hello\\")
81+
end
82+
return ____exports"
83+
`;
84+
85+
exports[`console.error ("console.error(\\"Hello\\", \\"There\\")") 1`] = `
86+
"local ____exports = {}
87+
function ____exports.__main(self)
88+
print(\\"Hello\\", \\"There\\")
89+
end
90+
return ____exports"
91+
`;
92+
93+
exports[`console.info ("console.info()") 1`] = `
94+
"local ____exports = {}
95+
function ____exports.__main(self)
96+
print()
97+
end
98+
return ____exports"
99+
`;
100+
101+
exports[`console.info ("console.info(\\"Hello %%s\\", \\"there\\")") 1`] = `
102+
"local ____exports = {}
103+
function ____exports.__main(self)
104+
print(
105+
string.format(\\"Hello %%s\\", \\"there\\")
106+
)
107+
end
108+
return ____exports"
109+
`;
110+
111+
exports[`console.info ("console.info(\\"Hello %s\\", \\"there\\")") 1`] = `
112+
"local ____exports = {}
113+
function ____exports.__main(self)
114+
print(
115+
string.format(\\"Hello %s\\", \\"there\\")
116+
)
117+
end
118+
return ____exports"
119+
`;
120+
121+
exports[`console.info ("console.info(\\"Hello\\")") 1`] = `
122+
"local ____exports = {}
123+
function ____exports.__main(self)
124+
print(\\"Hello\\")
125+
end
126+
return ____exports"
127+
`;
128+
129+
exports[`console.info ("console.info(\\"Hello\\", \\"There\\")") 1`] = `
130+
"local ____exports = {}
131+
function ____exports.__main(self)
132+
print(\\"Hello\\", \\"There\\")
133+
end
134+
return ____exports"
135+
`;
136+
49137
exports[`console.log ("console.log()") 1`] = `
50138
"local ____exports = {}
51139
function ____exports.__main(self)
@@ -143,3 +231,47 @@ function ____exports.__main(self)
143231
end
144232
return ____exports"
145233
`;
234+
235+
exports[`console.warn ("console.warn()") 1`] = `
236+
"local ____exports = {}
237+
function ____exports.__main(self)
238+
print()
239+
end
240+
return ____exports"
241+
`;
242+
243+
exports[`console.warn ("console.warn(\\"Hello %%s\\", \\"there\\")") 1`] = `
244+
"local ____exports = {}
245+
function ____exports.__main(self)
246+
print(
247+
string.format(\\"Hello %%s\\", \\"there\\")
248+
)
249+
end
250+
return ____exports"
251+
`;
252+
253+
exports[`console.warn ("console.warn(\\"Hello %s\\", \\"there\\")") 1`] = `
254+
"local ____exports = {}
255+
function ____exports.__main(self)
256+
print(
257+
string.format(\\"Hello %s\\", \\"there\\")
258+
)
259+
end
260+
return ____exports"
261+
`;
262+
263+
exports[`console.warn ("console.warn(\\"Hello\\")") 1`] = `
264+
"local ____exports = {}
265+
function ____exports.__main(self)
266+
print(\\"Hello\\")
267+
end
268+
return ____exports"
269+
`;
270+
271+
exports[`console.warn ("console.warn(\\"Hello\\", \\"There\\")") 1`] = `
272+
"local ____exports = {}
273+
function ____exports.__main(self)
274+
print(\\"Hello\\", \\"There\\")
275+
end
276+
return ____exports"
277+
`;

test/unit/builtins/console.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,42 @@ test.each([
1414
.expectLuaToMatchSnapshot();
1515
});
1616

17+
test.each([
18+
"console.info()",
19+
'console.info("Hello")',
20+
'console.info("Hello %s", "there")',
21+
'console.info("Hello %%s", "there")',
22+
'console.info("Hello", "There")',
23+
])("console.info (%p)", code => {
24+
util.testFunction(code)
25+
.setOptions(compilerOptions)
26+
.expectLuaToMatchSnapshot();
27+
});
28+
29+
test.each([
30+
"console.error()",
31+
'console.error("Hello")',
32+
'console.error("Hello %s", "there")',
33+
'console.error("Hello %%s", "there")',
34+
'console.error("Hello", "There")',
35+
])("console.error (%p)", code => {
36+
util.testFunction(code)
37+
.setOptions(compilerOptions)
38+
.expectLuaToMatchSnapshot();
39+
});
40+
41+
test.each([
42+
"console.warn()",
43+
'console.warn("Hello")',
44+
'console.warn("Hello %s", "there")',
45+
'console.warn("Hello %%s", "there")',
46+
'console.warn("Hello", "There")',
47+
])("console.warn (%p)", code => {
48+
util.testFunction(code)
49+
.setOptions(compilerOptions)
50+
.expectLuaToMatchSnapshot();
51+
});
52+
1753
test.each([
1854
"console.trace()",
1955
'console.trace("message")',

0 commit comments

Comments
 (0)