Skip to content

Commit

Permalink
fix: some string parse, resolve #3
Browse files Browse the repository at this point in the history
  • Loading branch information
zguolee committed Aug 17, 2022
1 parent 9e4a136 commit 89788ba
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -9,3 +9,4 @@ lib-cov
logs
node_modules
temp
.hbuilderx
28 changes: 20 additions & 8 deletions packages/transformer-rename-class/src/index.ts
Expand Up @@ -55,7 +55,8 @@ export default function transformerRenameClass(options: RenameClassOptions = {})
const charReg = /[.:%!#()[\/\],]/

const classRE = /:?(hover-)?class=\".*?\"/g
const stringRE = /(['\`]).*?(['\`])/g
const string1RE = /([']).*?(['])/g
const string2RE = /([\`]).*?([\`])/g

return {
name: 'rename-class',
Expand Down Expand Up @@ -88,13 +89,9 @@ export default function transformerRenameClass(options: RenameClassOptions = {})
}
}

// process string
const stringMatches = [...s.original.matchAll(stringRE)]
for (const match of stringMatches) {
// skip `${...}`
if (/\$\{.*\}/g.test(match[0]))
continue

// process string1
const string1Matches = [...s.original.matchAll(string1RE)]
for (const match of string1Matches) {
// There may be no need
// https://tailwindcss.com/docs/background-image#arbitrary-values
// skip all the image formats in HTML for bg-[url('...')]
Expand All @@ -111,6 +108,21 @@ export default function transformerRenameClass(options: RenameClassOptions = {})
s.overwrite(start, start + match[0].length, `'${replacements.join(' ')}'`)
}
}

// process string2
const string2Matches = [...s.original.matchAll(string2RE)]
for (const match of string2Matches) {
// skip `${...}`
if (/\$\{.*\}/g.test(match[0]))
continue

const start = match.index!
const body = match[0].slice(1, -1)
if (charReg.test(body)) {
const replacements = await compileApplet(body, ctx)
s.overwrite(start, start + match[0].length, `'${replacements.join(' ')}'`)
}
}
}
},
}
Expand Down
4 changes: 3 additions & 1 deletion playground/package.json
Expand Up @@ -44,6 +44,7 @@
"@dcloudio/uni-mp-toutiao": "3.0.0-alpha-3040820220424001",
"@dcloudio/uni-mp-weixin": "3.0.0-alpha-3040820220424001",
"@dcloudio/uni-quickapp-webview": "3.0.0-alpha-3040820220424001",
"@dcloudio/uni-ui": "^1.4.20",
"vue": "^3.2.33",
"vue-i18n": "^9.1.9",
"vuex": "^4.0.2"
Expand All @@ -52,6 +53,7 @@
"@dcloudio/types": "^3.0.7",
"@dcloudio/uni-automator": "3.0.0-alpha-3040820220424001",
"@dcloudio/uni-cli-shared": "3.0.0-alpha-3040820220424001",
"@dcloudio/vite-plugin-uni": "3.0.0-alpha-3040820220424001"
"@dcloudio/vite-plugin-uni": "3.0.0-alpha-3040820220424001",
"sass": "^1.54.4"
}
}
6 changes: 6 additions & 0 deletions playground/src/pages.json
Expand Up @@ -12,5 +12,11 @@
"navigationBarTitleText": "uni-app",
"navigationBarBackgroundColor": "#F8F8F8",
"backgroundColor": "#F8F8F8"
},
"easycom": {
"autoscan": true,
"custom": {
"^uni-(.*)": "@dcloudio/uni-ui/lib/uni-$1/uni-$1.vue"
}
}
}
6 changes: 4 additions & 2 deletions playground/src/pages/index/index.vue
Expand Up @@ -2,6 +2,7 @@
const bg = 'bg-[hsl(2.7,81.9%,69.6%)]'
const index = 1
const type = 'text'
const bool = true
</script>

<template>
Expand All @@ -12,12 +13,13 @@ const type = 'text'
0123456789
</div>
</div>
<div class="p-1" :class="true ? 'text-yellow-500' : ''">
<div class="p-1" :class="bool ? 'text-yellow-500' : ''">
{{ `index${index + 1}` }}
</div>
<div class="py-3.5 grid-cols-[0.7fr_repeat(7,1fr)]">
py-4.5
</div>
<uni-easyinput />
<div :class="{ 'is-textarea-icon': type === 'textarea' }">
uniapp
</div>
Expand All @@ -27,7 +29,7 @@ const type = 'text'
>
uniapp
</div>
<div class="m-0.5 p-1 text-2xl" :class="true ? '' : 'text-yellow-500 p-2.5'">
<div class="m-0.5 p-1 text-2xl" :class="bool ? '' : 'text-yellow-500 p-2.5'">
abckefghijklmnopqrstuvwxyz
</div>
</div>
Expand Down
51 changes: 48 additions & 3 deletions pnpm-lock.yaml

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

6 changes: 4 additions & 2 deletions test/fixtures/rules.vue
Expand Up @@ -2,6 +2,7 @@
const bg = 'bg-[hsl(2.7,81.9%,69.6%)]'
const index = 1
const type = 'text'
const bool = true
</script>

<template>
Expand All @@ -12,12 +13,13 @@ const type = 'text'
0123456789
</div>
</div>
<div class="p-1" :class="true ? 'text-yellow-500' : ''">
<div class="p-1" :class="bool ? 'text-yellow-500' : ''">
{{ `index${index + 1}` }}
</div>
<div class="py-3.5 grid-cols-[0.7fr_repeat(7,1fr)]">
py-4.5
</div>
<uni-easyinput />
<div :class="{ 'is-textarea-icon': type === 'textarea' }">
uniapp
</div>
Expand All @@ -27,7 +29,7 @@ const type = 'text'
>
uniapp
</div>
<div class="m-0.5 p-1 text-2xl" :class="true ? '' : 'text-yellow-500 p-2.5'">
<div class="m-0.5 p-1 text-2xl" :class="bool ? '' : 'text-yellow-500 p-2.5'">
abckefghijklmnopqrstuvwxyz
</div>
</div>
Expand Down
8 changes: 5 additions & 3 deletions test/transformer-rename-class.test.ts
Expand Up @@ -37,6 +37,7 @@ describe('transformer-rename-class', () => {
const bg = 'uno-98db2v'
const index = 1
const type = 'text'
const bool = true
</script>
<template>
Expand All @@ -47,22 +48,23 @@ describe('transformer-rename-class', () => {
0123456789
</div>
</div>
<div class=\\"p-1\\" :class=\\"true ? 'text-yellow-500' : ''\\">
<div class=\\"p-1\\" :class=\\"bool ? 'text-yellow-500' : ''\\">
{{ \`index\${index + 1}\` }}
</div>
<div class=\\"uno-0tr0xg\\">
py-4.5
</div>
<uni-easyinput />
<div :class=\\"{ 'is-textarea-icon': type === 'textarea' }\\">
uniapp
</div>
<div
class=\\"uno-g7ig4t\\"
:class=\\"{ 'uno-g7ig4t': type === 'textarea' }\\"
:class=\\"{ 'bg-[url(https://avatars.githubusercontent.com/u/40738594?s=40&v=4)]': type === 'textarea' }\\"
>
uniapp
</div>
<div class=\\"uno-tw4biu\\" :class=\\"true ? '' : 'uno-qju0i9'\\">
<div class=\\"uno-tw4biu\\" :class=\\"bool ? '' : 'uno-qju0i9'\\">
abckefghijklmnopqrstuvwxyz
</div>
</div>
Expand Down

0 comments on commit 89788ba

Please sign in to comment.