Fix uninterpolated-text newline-insertion case (#9513)

Fixes #9494.
This commit is contained in:
Kaz Wesley 2024-03-21 10:20:53 -04:00 committed by GitHub
parent c22d7422e6
commit ef9f3cc11e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 4 deletions

View File

@ -1428,7 +1428,7 @@ export class MutableTextLiteral extends TextLiteral implements MutableAst {
setRawTextContent(rawText: string) {
let boundary = this.boundaryTokenCode()
const isInterpolated = this.isInterpolated()
const mustBecomeInterpolated = !isInterpolated && (!boundary || rawText.includes(boundary))
const mustBecomeInterpolated = !isInterpolated && (!boundary || rawText.match(/["\n\r]/))
if (mustBecomeInterpolated) {
boundary = "'"
this.setBoundaries(boundary)

View File

@ -900,9 +900,23 @@ test.prop({
literal.setBoundaries(boundary)
literal.setRawTextContent(rawText)
expect(literal.rawTextContent).toBe(rawText)
const expectInterpolated = rawText.includes('"') || boundary === "'"
const expectedCode = expectInterpolated ? `'${escapeTextLiteral(rawText)}'` : `"${rawText}"`
expect(literal.code()).toBe(expectedCode)
const codeAsInterpolated = `'${escapeTextLiteral(rawText)}'`
if (boundary === "'") {
expect(literal.code()).toBe(codeAsInterpolated)
} else {
const codeAsRaw = `"${rawText}"`
// Uninterpolated text will be promoted to interpolated if necessary to escape a special character.
expect([codeAsInterpolated, codeAsRaw]).toContainEqual(literal.code())
}
})
test('setRawTextContent promotes single-line uninterpolated text to interpolated if a newline is added', () => {
const literal = TextLiteral.new('')
literal.setBoundaries('"')
const rawText = '\n'
literal.setRawTextContent(rawText)
expect(literal.rawTextContent).toBe(rawText)
expect(literal.code()).toBe(`'${escapeTextLiteral(rawText)}'`)
})
const docEditCases = [