Skip to content

Commit 5afaa2d

Browse files
Janne252Perryvw
authored andcommitted
Support for single-character escape sequences (#160)
* Support for string literal character escape sequences * Add test files for character escape sequences * Escape template string escape sequences Rename to escapeString * Complex template string escape test
1 parent 4c10c28 commit 5afaa2d

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

src/Transpiler.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,30 @@ export abstract class LuaTranspiler {
691691
}
692692
}
693693

694+
public escapeString(text: string): string {
695+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
696+
const escapeSequences: Array<[RegExp, string]> = [
697+
[/[\\]/g, "\\\\"],
698+
[/[\']/g, "\\\'"],
699+
[/[\`]/g, "\\\`"],
700+
[/[\"]/g, "\\\""],
701+
[/[\n]/g, "\\n"],
702+
[/[\r]/g, "\\r"],
703+
[/[\v]/g, "\\v"],
704+
[/[\t]/g, "\\t"],
705+
[/[\b]/g, "\\b"],
706+
[/[\f]/g, "\\f"],
707+
[/[\0]/g, "\\0"],
708+
];
709+
710+
if (text.length > 0) {
711+
for (const [regex, replacement] of escapeSequences) {
712+
text = text.replace(regex, replacement);
713+
}
714+
}
715+
return text;
716+
}
717+
694718
public transpileExpression(node: ts.Node, brackets?: boolean): string {
695719
switch (node.kind) {
696720
case ts.SyntaxKind.BinaryExpression:
@@ -714,7 +738,7 @@ export abstract class LuaTranspiler {
714738
return this.transpileIdentifier(node as ts.Identifier);
715739
case ts.SyntaxKind.StringLiteral:
716740
case ts.SyntaxKind.NoSubstitutionTemplateLiteral:
717-
const text = (node as ts.StringLiteral).text;
741+
const text = this.escapeString((node as ts.StringLiteral).text);
718742
return `"${text}"`;
719743
case ts.SyntaxKind.TemplateExpression:
720744
return this.transpileTemplateExpression(node as ts.TemplateExpression);
@@ -889,13 +913,15 @@ export abstract class LuaTranspiler {
889913
}
890914

891915
public transpileTemplateExpression(node: ts.TemplateExpression): string {
892-
const parts = [`"${node.head.text}"`];
916+
const parts = [`"${this.escapeString(node.head.text)}"`];
893917
node.templateSpans.forEach(span => {
894918
const expr = this.transpileExpression(span.expression, true);
919+
const text = this.escapeString(span.literal.text);
920+
895921
if (ts.isTemplateTail(span.literal)) {
896-
parts.push(`tostring(${expr}).."${span.literal.text}"`);
922+
parts.push(`tostring(${expr}).."${text}"`);
897923
} else {
898-
parts.push(`tostring(${expr}).."${span.literal.text}"`);
924+
parts.push(`tostring(${expr}).."${text}"`);
899925
}
900926
});
901927
return parts.join("..");
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
local quoteInDoubleQuotes = "\' \' \'"
2+
3+
local quoteInTemplateString = "\' \' \'"
4+
5+
local doubleQuoteInQuotes = "\" \" \""
6+
7+
local doubleQuoteInDoubleQuotes = "\" \" \""
8+
9+
local doubleQuoteInTemplateString = "\" \" \""
10+
11+
local escapedCharsInQuotes = "\\ \0 \b \t \n \v \f \" \' \`"
12+
13+
local escapedCharsInDoubleQUotes = "\\ \0 \b \t \n \v \f \" \' \`"
14+
15+
local escapedCharsInTemplateString = "\\ \0 \b \t \n \v \f \" \' \`"
16+
17+
local nonEmptyTemplateString = "Level 0: \n\t "..tostring("Level 1: \n\t\t "..tostring("Level 3: \n\t\t\t "..tostring("Last level \n --").." \n --").." \n --").." \n --"
18+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
let quoteInDoubleQuotes = "' ' '";
2+
let quoteInTemplateString = `' ' '`;
3+
4+
let doubleQuoteInQuotes = '" " "';
5+
let doubleQuoteInDoubleQuotes = "\" \" \"";
6+
let doubleQuoteInTemplateString = `" " "`;
7+
8+
let escapedCharsInQuotes = '\\ \0 \b \t \n \v \f \" \' \`';
9+
let escapedCharsInDoubleQUotes = "\\ \0 \b \t \n \v \f \" \' \`";
10+
let escapedCharsInTemplateString = `\\ \0 \b \t \n \v \f \" \' \``;
11+
12+
let nonEmptyTemplateString = `Level 0: \n\t ${`Level 1: \n\t\t ${`Level 3: \n\t\t\t ${'Last level \n --'} \n --`} \n --`} \n --`;

0 commit comments

Comments
 (0)