From af91396f80b79082117228e32f34f3e91aab9fd0 Mon Sep 17 00:00:00 2001 From: Jordan Kiesel Date: Mon, 13 Jul 2026 22:54:51 -0600 Subject: [PATCH] fix: omit indentation from text block assignments --- src/printers/helpers.ts | 18 +--- src/printers/lexical-structure.ts | 1 - test/unit-test/text-blocks/_output.java | 116 ++++++++++++------------ 3 files changed, 61 insertions(+), 74 deletions(-) diff --git a/src/printers/helpers.ts b/src/printers/helpers.ts index 87cfe419..990ce67d 100644 --- a/src/printers/helpers.ts +++ b/src/printers/helpers.ts @@ -403,20 +403,8 @@ export function printAssignment( ]); } -export function printTextBlock( - path: NamedNodePath, - contents: Doc -) { - const parts = ['"""', hardline, contents, '"""']; - const parentType = (path.parent as NamedNode | null)?.type; - const grandparentType = (path.grandparent as NamedNode | null)?.type; - return parentType === SyntaxType.AssignmentExpression || - parentType === SyntaxType.VariableDeclarator || - (path.node.fieldName === "object" && - (grandparentType === SyntaxType.AssignmentExpression || - grandparentType === SyntaxType.VariableDeclarator)) - ? indent(parts) - : parts; +export function printTextBlock(contents: Doc) { + return ['"""', hardline, contents, '"""']; } export function embedTextBlock(path: NamedNodePath) { @@ -438,7 +426,7 @@ export function embedTextBlock(path: NamedNodePath) { textToDoc: (text: string, options: Options) => Promise ) => { const doc = await textToDoc(text, { parser: language }); - return printTextBlock(path, [escapeDocForTextBlock(doc), hardline]); + return printTextBlock([escapeDocForTextBlock(doc), hardline]); }; } diff --git a/src/printers/lexical-structure.ts b/src/printers/lexical-structure.ts index 13fd32f2..62852316 100644 --- a/src/printers/lexical-structure.ts +++ b/src/printers/lexical-structure.ts @@ -19,7 +19,6 @@ export default { } return printTextBlock( - path, join(hardline, textBlockContents(path.node).split("\n")) ); }, diff --git a/test/unit-test/text-blocks/_output.java b/test/unit-test/text-blocks/_output.java index de2abca8..72ec88c6 100644 --- a/test/unit-test/text-blocks/_output.java +++ b/test/unit-test/text-blocks/_output.java @@ -2,34 +2,34 @@ public class TextBlock { void method() { String myTextBlock = """ - my text + my text - sentence\""" + sentence\""" - """; + """; String source = """ - public void print(%s object) { - System.out.println(Objects.toString(object)); - } - """.formatted(type); + public void print(%s object) { + System.out.println(Objects.toString(object)); + } + """.formatted(type); String html = """ - \r - \r -

Hello, world

\r - \r - \r - """; + \r + \r +

Hello, world

\r + \r + \r + """; html = """ - \r - \r -

Hello, world

\r - \r - \r - """; + \r + \r +

Hello, world

\r + \r + \r + """; System.out.println( // leading comment @@ -54,83 +54,83 @@ public void print(%s object) { } String escapes = """ - 1+1 equals \ - 2 maybe - """; + 1+1 equals \ + 2 maybe + """; String escapes = """ - \"""var msg = hello world!\"""; - """; + \"""var msg = hello world!\"""; + """; String escapes = """ - \n\t\r\f\b\s\\ - \077 - \u0041"""; + \n\t\r\f\b\s\\ + \077 + \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 - } - """; + { + "sql": "SELECT * FROM users WHERE active=1 AND deleted=0", + "limit": 10 + } + """; } void java() { // language=Java String java = """ - class Class { + class Class { - void method() { - // comment - } + void method() { + // comment } - """; + } + """; } void html() { // language=html String html = """ - - - - Page Title - - -

My First Heading

-

My first paragraph.

- - - """; + + + + Page Title + + +

My First Heading

+

My first paragraph.

+ + + """; } void typescript() { // language=typescript String typescript = """ - const s = `""\"`; - """; + const s = `""\"`; + """; // language=typescript String typescript = """ - const s = ""; // " - """; + const s = ""; // " + """; } void unsupported() { // language=unsupported String unsupported = """ - function f(){let i=0;} - """; + function f(){let i=0;} + """; } }