Skip to content

Commit f8b5900

Browse files
committed
fix: identify the replace quote by source code
1 parent e349607 commit f8b5900

File tree

1 file changed

+14
-23
lines changed

1 file changed

+14
-23
lines changed

src/utils/template.ts

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,18 @@ export async function transpileVueTemplate(
164164
for (const item of expressions) {
165165
item.replacement = transformMap.get(item) ?? item.src
166166

167-
const surrounding = getSurrounding(
167+
// the source should only have one of the quotes
168+
const sourceQuote = getSourceQuote(
168169
content,
169170
item.loc.start.offset - offset,
170171
item.loc.end.offset - offset,
171172
)
172-
if (surrounding) {
173-
const replace = surrounding.code === `"` ? `'` : `"`
173+
if (sourceQuote !== null) {
174+
const search = sourceQuote === `"` ? `'` : `"`
174175
item.replacement = replaceQuote(
175176
item.replacement,
176-
surrounding.code,
177-
replace,
177+
search,
178+
sourceQuote,
178179
)
179180
}
180181
}
@@ -211,25 +212,15 @@ function replaceQuote(code: string, target: string, replace: string): string {
211212
return res
212213
}
213214

214-
function getSurrounding(code: string, start: number, end: number) {
215-
const empty = new Set<string | undefined>([' ', '\n', '\r', '\t'])
216-
let startIndex = start - 1
217-
let endIndex = end
218-
219-
while (startIndex > 0 && empty.has(code.at(startIndex))) {
220-
startIndex--
221-
}
222-
223-
while (endIndex < code.length && empty.has(code.at(endIndex))) {
224-
endIndex++
215+
function getSourceQuote(code: string, start: number, end: number): string | null {
216+
const source = code.slice(start, end)
217+
const quotes = ['"', '\'']
218+
for (const quote of quotes) {
219+
if (source.includes(quote)) {
220+
return quote
221+
}
225222
}
226-
227-
const prev = startIndex >= 0 ? code.at(startIndex) : ''
228-
const next = endIndex < code.length ? code.at(endIndex) : ''
229-
230-
return prev && next && prev === next
231-
? { code: prev, prevAt: startIndex, nextAt: endIndex }
232-
: undefined
223+
return null
233224
}
234225

235226
interface SnippetHandler {

0 commit comments

Comments
 (0)