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
4 changes: 3 additions & 1 deletion javascript/packages/formatter/src/format-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,9 @@ export function isERBTag(text: string): boolean {
* Check if a string ends with an ERB tag
*/
export function endsWithERBTag(text: string): boolean {
return /%>$/.test(text.trim())
const trimmed = text.trim()

return /%>$/.test(trimmed) || /%>\S+$/.test(trimmed)
}

/**
Expand Down
11 changes: 7 additions & 4 deletions javascript/packages/formatter/src/format-printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1862,7 +1862,7 @@ export class FormatPrinter extends Printer {
const firstWord = words[0]
const firstChar = firstWord[0]

if (!/[a-zA-Z0-9.!?:;]/.test(firstChar)) {
if (/\s/.test(firstChar)) {
return false
}

Expand All @@ -1879,6 +1879,11 @@ export class FormatPrinter extends Printer {
unit: { content: remainingText, type: 'text', isAtomic: false, breaksFlow: false },
node: textNode
})
} else if (endsWithWhitespace(textNode.content)) {
result.push({
unit: { content: ' ', type: 'text', isAtomic: false, breaksFlow: false },
node: textNode
})
}

return true
Expand Down Expand Up @@ -1955,10 +1960,8 @@ export class FormatPrinter extends Printer {
const hasWhitespace = this.hasWhitespaceBeforeNode(children, lastProcessedIndex, index, child)
const lastUnit = result[result.length - 1]
const lastIsAtomic = lastUnit.unit.isAtomic && (lastUnit.unit.type === 'erb' || lastUnit.unit.type === 'inline')
const trimmed = child.content.trim()
const startsWithClosingPunct = trimmed.length > 0 && /^[.!?:;]/.test(trimmed)

if (lastIsAtomic && (!hasWhitespace || startsWithClosingPunct) && this.tryMergeTextAfterAtomic(result, child)) {
if (lastIsAtomic && !hasWhitespace && this.tryMergeTextAfterAtomic(result, child)) {
return
}
}
Expand Down
2 changes: 1 addition & 1 deletion javascript/packages/formatter/test/erb/erb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ describe("@herb-tools/formatter", () => {

expect(result).toBe(dedent`
<h3>
<%= link_to "Start", start_path %> &rsquo;s overview of
<%= link_to "Start", start_path %>&rsquo;s overview of
<%= link_to "Section", section_path %>, <%= link_to "End", end_path %>.
</h3>
`)
Expand Down
Loading
Loading