Skip to content

Commit 872b3f7

Browse files
committedAug 17, 2021
fix(compiler-sfc): should also expose regular script block bindings when <script setup> is used
close #4369
1 parent e22d7cd commit 872b3f7

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed
 

‎packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap

+7-2
Original file line numberDiff line numberDiff line change
@@ -610,10 +610,15 @@ export default {
610610
function c() {}
611611
class d {}
612612
613-
return { a, b, c, d, x }
613+
return { aa, bb, cc, dd, a, b, c, d, xx, x }
614614
}
615615
616-
}"
616+
}
617+
import { xx } from './x'
618+
let aa = 1
619+
const bb = 2
620+
function cc() {}
621+
class dd {}"
617622
`;
618623
619624
exports[`SFC compile <script setup> with TypeScript const Enum 1`] = `

‎packages/compiler-sfc/__tests__/compileScript.spec.ts

+22-2
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,37 @@ import { compileSFCScript as compile, assertCode } from './utils'
33

44
describe('SFC compile <script setup>', () => {
55
test('should expose top level declarations', () => {
6-
const { content } = compile(`
6+
const { content, bindings } = compile(`
77
<script setup>
88
import { x } from './x'
99
let a = 1
1010
const b = 2
1111
function c() {}
1212
class d {}
1313
</script>
14+
15+
<script>
16+
import { xx } from './x'
17+
let aa = 1
18+
const bb = 2
19+
function cc() {}
20+
class dd {}
21+
</script>
1422
`)
23+
expect(content).toMatch('return { aa, bb, cc, dd, a, b, c, d, xx, x }')
24+
expect(bindings).toStrictEqual({
25+
x: BindingTypes.SETUP_MAYBE_REF,
26+
a: BindingTypes.SETUP_LET,
27+
b: BindingTypes.SETUP_CONST,
28+
c: BindingTypes.SETUP_CONST,
29+
d: BindingTypes.SETUP_CONST,
30+
xx: BindingTypes.SETUP_MAYBE_REF,
31+
aa: BindingTypes.SETUP_LET,
32+
bb: BindingTypes.SETUP_CONST,
33+
cc: BindingTypes.SETUP_CONST,
34+
dd: BindingTypes.SETUP_CONST
35+
})
1536
assertCode(content)
16-
expect(content).toMatch('return { a, b, c, d, x }')
1737
})
1838

1939
test('defineProps()', () => {

‎packages/compiler-sfc/src/compileScript.ts

+7
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,13 @@ export function compileScript(
827827
)
828828
}
829829
}
830+
} else if (
831+
(node.type === 'VariableDeclaration' ||
832+
node.type === 'FunctionDeclaration' ||
833+
node.type === 'ClassDeclaration') &&
834+
!node.declare
835+
) {
836+
walkDeclaration(node, setupBindings, userImportAlias)
830837
}
831838
}
832839
}

0 commit comments

Comments
 (0)