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
54 changes: 18 additions & 36 deletions src/printers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,19 +351,12 @@ export function embedTextBlock(path: NamedNodePath<SyntaxType.StringLiteral>) {
textToDoc: (text: string, options: Options) => Promise<Doc>
) => {
const doc = await textToDoc(text, { parser: language });
return printTextBlock(path, escapeDocForTextBlock(doc));
return printTextBlock(path, [escapeDocForTextBlock(doc), hardline]);
};
}

export function textBlockContents(node: NamedNode<SyntaxType.StringLiteral>) {
const lines = node.value
.replace(
/(?<=^|[^\\])((?:\\\\)*)\\u+([0-9a-fA-F]{4})/g,
(_, backslashPairs: string, hex: string) =>
backslashPairs + String.fromCharCode(parseInt(hex, 16))
)
.split("\n")
.slice(1);
const lines = node.value.split("\n").slice(1);
const baseIndent = findBaseIndent(lines);
return lines
.map(line => line.slice(baseIndent))
Expand Down Expand Up @@ -394,39 +387,28 @@ function findEmbeddedLanguage(path: NamedNodePath) {
function escapeDocForTextBlock(doc: Doc) {
return mapDoc(doc, currentDoc =>
typeof currentDoc === "string"
? currentDoc.replace(/\\|"""/g, match => `\\${match}`)
? currentDoc.replace(/\\|"""/g, match =>
match === "\\" ? "\\\\" : '""\\"'
)
: currentDoc
);
}

function unescapeTextBlockContents(text: string) {
return text.replace(
/\\(?:([bstnfr"'\\])|\n|\r\n?|([0-3][0-7]{0,2}|[0-7]{1,2}))/g,
(_, single, octal) => {
if (single) {
switch (single) {
case "b":
return "\b";
case "s":
return " ";
case "t":
return "\t";
case "n":
return "\n";
case "f":
return "\f";
case "r":
return "\r";
default:
return single;
}
} else if (octal) {
return String.fromCharCode(parseInt(octal, 8));
} else {
return "";
}
return text.replace(/\\(?:([stnr"'\\])|\n|\r\n?)/g, (_, escaped) => {
switch (escaped) {
case "s":
return " ";
case "t":
return "\t";
case "n":
return "\n";
case "r":
return "\r";
default:
return escaped ?? "";
}
);
});
}

export type NamedNodePrinters = {
Expand Down
15 changes: 13 additions & 2 deletions test/unit-test/text-blocks/_input.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ public void print(%s object) {
void json() {
// language = json
String someJson = """
{"glossary":{"title": "example glossary"}}
{"glossary":{"title": "example \'glossary\'"}}
""";

// language=json
String config = """
{ "name":"example",
{ \t "name":"example",
"enabled" :true,
"timeout":30}
""";
Expand Down Expand Up @@ -108,6 +108,17 @@ void html() {
""";
}

void typescript() {
// language=typescript
String typescript = """
const s = `\"""`;
""";

// language=typescript
String typescript = """
const s = ""; // \"""";
}

void unsupported() {
// language=unsupported
String unsupported = """
Expand Down
29 changes: 23 additions & 6 deletions test/unit-test/text-blocks/_output.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,26 @@ public void print(%s object) {
String escapes = """
\n\t\r\f\b\s\\
\077
A""";
\u0041""";

void json() {
// language = json
String someJson = """
{ "glossary": { "title": "example glossary" } }""";
{ "glossary": { "title": "example 'glossary'" } }
""";

// language=json
String config = """
{ "name": "example", "enabled": true, "timeout": 30 }""";
{ "name": "example", "enabled": true, "timeout": 30 }
""";

/* language = JSON */
String query = """
{
"sql": "SELECT * FROM users WHERE active=1 AND deleted=0",
"limit": 10
}""";
}
""";
}

void java() {
Expand All @@ -92,7 +95,8 @@ class Class {
void method() {
// comment
}
}""";
}
""";
}

void html() {
Expand All @@ -107,7 +111,20 @@ void html() {
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>""";
</html>
""";
}

void typescript() {
// language=typescript
String typescript = """
const s = `""\"`;
""";

// language=typescript
String typescript = """
const s = ""; // "
""";
}

void unsupported() {
Expand Down