Skip to content

Commit f26315a

Browse files
author
Alan Wang
authored
fix: don't remove script setup block (#112)
1 parent fb0fe43 commit f26315a

File tree

3 files changed

+100
-17
lines changed

3 files changed

+100
-17
lines changed

src/__tests__/runTransformation.spec.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,74 @@ export default {
112112
`)
113113
})
114114

115+
it('don\'t transform scriptSetup blocks in .vue files', () => {
116+
const source = `<template>
117+
<div id="app">
118+
<img alt="Vue logo" src="./assets/logo.png">
119+
<HelloWorld msg="Welcome to Your Vue.js App"/>
120+
</div>
121+
</template>
122+
<script setup lang="ts">
123+
import { getFormattedTitle } from '../composable/useDocument'
124+
import { IDocmentHistory } from '@/api/types'
125+
126+
interface IProps {
127+
list: IDocmentHistory[]
128+
select?: (id: number) => void
129+
}
130+
131+
const props = withDefaults(defineProps<IProps>(), {
132+
list: () => [],
133+
select: (id: number) => {},
134+
})
135+
136+
const selectHistory = (id: number) => {
137+
props?.select(id)
138+
}
139+
</script>
140+
141+
<script lang="ts">
142+
export default {
143+
name: 'document-history',
144+
}
145+
</script>
146+
`
147+
const file = { path: '/tmp/a.vue', source }
148+
const result = runTransformation(file, addUseStrict)
149+
expect(result).toBe(`<template>
150+
<div id="app">
151+
<img alt="Vue logo" src="./assets/logo.png">
152+
<HelloWorld msg="Welcome to Your Vue.js App"/>
153+
</div>
154+
</template>
155+
<script lang="ts" setup>
156+
import { getFormattedTitle } from '../composable/useDocument'
157+
import { IDocmentHistory } from '@/api/types'
158+
159+
interface IProps {
160+
list: IDocmentHistory[]
161+
select?: (id: number) => void
162+
}
163+
164+
const props = withDefaults(defineProps<IProps>(), {
165+
list: () => [],
166+
select: (id: number) => {},
167+
})
168+
169+
const selectHistory = (id: number) => {
170+
props?.select(id)
171+
}
172+
</script>
173+
174+
<script lang="ts">
175+
'use strict';
176+
export default {
177+
name: 'document-history',
178+
}
179+
</script>
180+
`)
181+
})
182+
115183
it.todo('transforms script blocks with custom lang attributes in .vue files')
116184

117185
it('(jscodeshift transforms) skips .vue files without script blocks', () => {

src/sfcUtils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ import { Statement } from '@babel/types'
4141
*/
4242

4343
export function stringify(sfcDescriptor: SFCDescriptor) {
44-
const { template, script, styles, customBlocks } = sfcDescriptor
44+
const { template, script, scriptSetup, styles, customBlocks } = sfcDescriptor
4545

4646
return (
4747
(
48-
[template, script, ...styles, ...customBlocks]
48+
[template, script, scriptSetup, ...styles, ...customBlocks]
4949
// discard blocks that don't exist
5050
.filter(block => block != null) as Array<NonNullable<SFCBlock>>
5151
)

yarn.lock

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,6 +1170,13 @@
11701170
dependencies:
11711171
"@babel/types" "^7.3.0"
11721172

1173+
"@types/cli-progress@^3.9.2":
1174+
version "3.9.2"
1175+
resolved "https://registry.nlark.com/@types/cli-progress/download/@types/cli-progress-3.9.2.tgz?cache=0&sync_timestamp=1629711886759&other_urls=https%3A%2F%2Fregistry.nlark.com%2F%40types%2Fcli-progress%2Fdownload%2F%40types%2Fcli-progress-3.9.2.tgz#6ca355f96268af39bee9f9307f0ac96145639c26"
1176+
integrity sha1-bKNV+WJorzm+6fkwfwrJYUVjnCY=
1177+
dependencies:
1178+
"@types/node" "*"
1179+
11731180
"@types/debug@^4.1.5":
11741181
version "4.1.5"
11751182
resolved "https://registry.npmjs.org/@types/debug/-/debug-4.1.5.tgz"
@@ -1341,29 +1348,29 @@
13411348
"@typescript-eslint/types" "4.27.0"
13421349
eslint-visitor-keys "^2.0.0"
13431350

1344-
"@vue/[email protected].5", "@vue/compiler-core@^3.0.5":
1345-
version "3.0.5"
1346-
resolved "https://registry.npmjs.org/@vue/compiler-core/-/compiler-core-3.0.5.tgz"
1347-
integrity sha512-iFXwk2gmU/GGwN4hpBwDWWMLvpkIejf/AybcFtlQ5V1ur+5jwfBaV0Y1RXoR6ePfBPJixtKZ3PmN+M+HgMAtfQ==
1351+
"@vue/[email protected].11", "@vue/compiler-core@~3.0.5":
1352+
version "3.0.11"
1353+
resolved "https://registry.nlark.com/@vue/compiler-core/download/@vue/compiler-core-3.0.11.tgz#5ef579e46d7b336b8735228758d1c2c505aae69a"
1354+
integrity sha1-XvV55G17M2uHNSKHWNHCxQWq5po=
13481355
dependencies:
13491356
"@babel/parser" "^7.12.0"
13501357
"@babel/types" "^7.12.0"
1351-
"@vue/shared" "3.0.5"
1358+
"@vue/shared" "3.0.11"
13521359
estree-walker "^2.0.1"
13531360
source-map "^0.6.1"
13541361

1355-
"@vue/compiler-dom@^3.0.5":
1356-
version "3.0.5"
1357-
resolved "https://registry.npmjs.org/@vue/compiler-dom/-/compiler-dom-3.0.5.tgz"
1358-
integrity sha512-HSOSe2XSPuCkp20h4+HXSiPH9qkhz6YbW9z9ZtL5vef2T2PMugH7/osIFVSrRZP/Ul5twFZ7MIRlp8tPX6e4/g==
1362+
"@vue/compiler-dom@~3.0.5":
1363+
version "3.0.11"
1364+
resolved "https://registry.nlark.com/@vue/compiler-dom/download/@vue/compiler-dom-3.0.11.tgz#b15fc1c909371fd671746020ba55b5dab4a730ee"
1365+
integrity sha1-sV/ByQk3H9ZxdGAgulW12rSnMO4=
13591366
dependencies:
1360-
"@vue/compiler-core" "3.0.5"
1361-
"@vue/shared" "3.0.5"
1367+
"@vue/compiler-core" "3.0.11"
1368+
"@vue/shared" "3.0.11"
13621369

1363-
1364-
version "3.0.5"
1365-
resolved "https://registry.npmjs.org/@vue/shared/-/shared-3.0.5.tgz"
1366-
integrity sha512-gYsNoGkWejBxNO6SNRjOh/xKeZ0H0V+TFzaPzODfBjkAIb0aQgBuixC1brandC/CDJy1wYPwSoYrXpvul7m6yw==
1370+
1371+
version "3.0.11"
1372+
resolved "https://registry.nlark.com/@vue/shared/download/@vue/shared-3.0.11.tgz#20d22dd0da7d358bb21c17f9bde8628152642c77"
1373+
integrity sha1-INIt0Np9NYuyHBf5vehigVJkLHc=
13671374

13681375
abab@^2.0.3:
13691376
version "2.0.5"
@@ -1807,6 +1814,14 @@ class-utils@^0.3.5:
18071814
isobject "^3.0.0"
18081815
static-extend "^0.1.1"
18091816

1817+
cli-progress@^3.9.0:
1818+
version "3.9.0"
1819+
resolved "https://registry.npm.taobao.org/cli-progress/download/cli-progress-3.9.0.tgz#25db83447deb812e62d05bac1af9aec5387ef3d4"
1820+
integrity sha1-JduDRH3rgS5i0FusGvmuxTh+89Q=
1821+
dependencies:
1822+
colors "^1.1.2"
1823+
string-width "^4.2.0"
1824+
18101825
cliui@^6.0.0:
18111826
version "6.0.0"
18121827
resolved "https://registry.npmjs.org/cliui/-/cliui-6.0.0.tgz"

0 commit comments

Comments
 (0)