Skip to content

Commit 0b2024e

Browse files
authored
Fix single quotes being converted to double quotes (#51)
1 parent 6666aea commit 0b2024e

File tree

4 files changed

+11
-0
lines changed

4 files changed

+11
-0
lines changed

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"import-fresh": "^3.3.0",
3131
"import-from": "^4.0.0",
3232
"jest": "^27.4.7",
33+
"jsesc": "^2.5.2",
3334
"license-checker": "^25.0.1",
3435
"line-column": "^1.0.2",
3536
"object-hash": "^2.2.0",

src/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import requireFresh from 'import-fresh'
1818
import objectHash from 'object-hash'
1919
import * as svelte from 'prettier-plugin-svelte'
2020
import lineColumn from 'line-column'
21+
import jsesc from 'jsesc'
2122

2223
let contextMap = new Map()
2324

@@ -168,6 +169,13 @@ function transformHtml(attributes, computedAttributes = []) {
168169
if (isStringLiteral(path.node)) {
169170
if (sortStringLiteral(path.node, { env })) {
170171
didChange = true
172+
173+
// https://github.com/benjamn/recast/issues/171#issuecomment-224996336
174+
let quote = path.node.extra.raw[0]
175+
let value = jsesc(path.node.value, {
176+
quotes: quote === "'" ? 'single' : 'double',
177+
})
178+
path.node.value = new String(quote + value + quote)
171179
}
172180
}
173181
this.traverse(path)

tests/test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ let vue = [
108108
t`<div :class="\`${yes}\`"></div>`,
109109
t`<div :class="\`${yes} \${someVar}\`"></div>`,
110110
t`<div :class="someVar! ? \`${yes}\` : \`${yes}\`"></div>`, // ts
111+
t`<div :class="someVar ? someFunc(someVar as string) + '${yes}' : ''"></div>`, // ts
111112
[
112113
`<div :class="\`sm:block inline flex\${someVar}\`"></div>`,
113114
`<div :class="\`inline sm:block flex\${someVar}\`"></div>`,

0 commit comments

Comments
 (0)