-
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathtemplateLiterals.spec.ts
More file actions
108 lines (97 loc) · 3.5 KB
/
templateLiterals.spec.ts
File metadata and controls
108 lines (97 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import * as util from "../util";
test.each([
{ a: 12, b: 23, c: 43 },
{ a: "test", b: "hello", c: "bye" },
{ a: "test", b: 42, c: "bye" },
{ a: "test", b: 42, c: 12 },
{ a: "test", b: 42, c: true },
{ a: false, b: 42, c: 12 },
])("template literal (%p)", ({ a, b, c }) => {
util.testExpressionTemplate`\`\${${a}} \${${b}} test \${${c}}\``.expectToMatchJsResult();
});
test.each(["a++", "a--", "--a", "++a"])("template literal with expression (%p)", expression => {
util.testFunction`
let a = 3;
return \`value\${${expression}}\`;
`.expectToMatchJsResult();
});
test.each(["`foo${'bar'}`.length", "`foo${'bar'}`.repeat(2)"])("template literal property access (%p)", expression => {
util.testExpression(expression).expectToMatchJsResult();
});
test.each([
"func``",
"func`hello`",
"func`hello ${1} ${2} ${3}`",
"func`hello ${(() => 'iife')()}`",
"func`hello ${1 + 2 + 3} arithmetic`",
"func`begin ${'middle'} end`",
"func`hello ${func`hello`}`",
"func`hello \\u00A9`",
"func`hello $ { }`",
"func`hello { ${'brackets'} }`",
"func`hello \\``",
"obj.func`hello ${'propertyAccessExpression'}`",
"obj['func']`hello ${'elementAccessExpression'}`",
])("tagged template literal (%p)", expression => {
util.testFunction`
function func(strings: TemplateStringsArray, ...expressions: any[]) {
return { strings: [...strings], raw: strings.raw, expressions };
}
const obj = { func };
return ${expression};
`.expectToMatchJsResult();
});
test.each(["func`noSelfParameter`", "obj.func`noSelfParameter`", "obj[`func`]`noSelfParameter`"])(
"tagged template literal function context (%p)",
expression => {
util.testFunction`
function func(this: void, strings: TemplateStringsArray) {
return [...strings];
}
const obj = { func };
return ${expression};
`.expectToMatchJsResult();
}
);
test.each(["number", "string | any"])("template span expect tostring for non string type (%p)", type => {
util.testFunction`
// @ts-ignore
const msg = "" as ${type};
return \`\${msg}\`;
`
.tap(builder => expect(builder.getMainLuaCodeChunk()).toContain("tostring"))
.expectToMatchJsResult();
});
test.each(["string", "'string literal type'", "string & unknown"])(
"template span expect no tostring for string-like type (%p)",
type => {
util.testFunction`
// @ts-ignore
const msg = "" as ${type};
return \`\${msg}\`;
`
.tap(builder => expect(builder.getMainLuaCodeChunk()).not.toContain("tostring"))
.expectToMatchJsResult();
}
);
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1637
test("tagged template literal returned from function call (#1637)", () => {
util.testModule`
function templateFactory() {
return (template: TemplateStringsArray) => {
return "bar";
}
}
export let result = templateFactory()\`foo\`;
`.expectToEqual({ result: "bar" });
});
test("tagged template literal returned from function call, explicit no context (#1637)", () => {
util.testModule`
function templateFactory() {
return function(this: void, template: TemplateStringsArray) {
return "bar";
}
}
export let result = templateFactory()\`foo\`;
`.expectToEqual({ result: "bar" });
});