diff --git a/src/utils/template.ts b/src/utils/template.ts index 52bf568..f3beff0 100644 --- a/src/utils/template.ts +++ b/src/utils/template.ts @@ -164,6 +164,10 @@ export async function transpileVueTemplate( for (const item of expressions) { item.replacement = transformMap.get(item) ?? item.src + if (!shouldReplaceQuote(item)) { + continue + } + // the source should only have one of the quotes const sourceQuote = getSourceQuote( content, @@ -212,6 +216,20 @@ export function replaceQuote(code: string, target: string, replace: string): str return res } +function shouldReplaceQuote(expression: Expression): boolean { + if (!expression.src.includes(`'`) && !expression.src.includes(`"`)) { + return false + } + + // skip the expression in the interpolation, it doesn't care about the quote + const secondLastTrack = expression.track.at(-2) + if (secondLastTrack?.type === NodeTypes.INTERPOLATION) { + return false + } + + return true +} + function getSourceQuote(code: string, start: number, end: number): string | null { const source = code.slice(start, end) const quotes = ['"', '\''] diff --git a/test/template.test.ts b/test/template.test.ts index ce42397..815dbbf 100644 --- a/test/template.test.ts +++ b/test/template.test.ts @@ -184,6 +184,32 @@ describe('transform typescript template', () => { expect(output).toBe(`'test'`) }) + it('handles quotes in interpolations', async () => { + expect( + await fixture(` + + `), + ).toMatchInlineSnapshot(` + " + + " + `) + }) + async function fixture(src: string) { const requireFromVue = createRequire(resolveModulePath('vue')) const { parse } = requireFromVue('@vue/compiler-dom') as typeof import('@vue/compiler-dom')