Skip to content

Commit

Permalink
Fix single quotes being converted to double quotes (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradlc authored Feb 9, 2022
1 parent 6666aea commit 0b2024e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"import-fresh": "^3.3.0",
"import-from": "^4.0.0",
"jest": "^27.4.7",
"jsesc": "^2.5.2",
"license-checker": "^25.0.1",
"line-column": "^1.0.2",
"object-hash": "^2.2.0",
Expand Down
8 changes: 8 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import requireFresh from 'import-fresh'
import objectHash from 'object-hash'
import * as svelte from 'prettier-plugin-svelte'
import lineColumn from 'line-column'
import jsesc from 'jsesc'

let contextMap = new Map()

Expand Down Expand Up @@ -168,6 +169,13 @@ function transformHtml(attributes, computedAttributes = []) {
if (isStringLiteral(path.node)) {
if (sortStringLiteral(path.node, { env })) {
didChange = true

// https://github.com/benjamn/recast/issues/171#issuecomment-224996336
let quote = path.node.extra.raw[0]
let value = jsesc(path.node.value, {
quotes: quote === "'" ? 'single' : 'double',
})
path.node.value = new String(quote + value + quote)
}
}
this.traverse(path)
Expand Down
1 change: 1 addition & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ let vue = [
t`<div :class="\`${yes}\`"></div>`,
t`<div :class="\`${yes} \${someVar}\`"></div>`,
t`<div :class="someVar! ? \`${yes}\` : \`${yes}\`"></div>`, // ts
t`<div :class="someVar ? someFunc(someVar as string) + '${yes}' : ''"></div>`, // ts
[
`<div :class="\`sm:block inline flex\${someVar}\`"></div>`,
`<div :class="\`inline sm:block flex\${someVar}\`"></div>`,
Expand Down

0 comments on commit 0b2024e

Please sign in to comment.