Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(compiler-sfc): should also expose regular script block bindings w…
…hen `<script setup>` is used

close #4369
  • Loading branch information
yyx990803 committed Aug 17, 2021
1 parent e22d7cd commit 872b3f7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
Expand Up @@ -610,10 +610,15 @@ export default {
function c() {}
class d {}
return { a, b, c, d, x }
return { aa, bb, cc, dd, a, b, c, d, xx, x }
}
}"
}
import { xx } from './x'
let aa = 1
const bb = 2
function cc() {}
class dd {}"
`;
exports[`SFC compile <script setup> with TypeScript const Enum 1`] = `
Expand Down
24 changes: 22 additions & 2 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Expand Up @@ -3,17 +3,37 @@ import { compileSFCScript as compile, assertCode } from './utils'

describe('SFC compile <script setup>', () => {
test('should expose top level declarations', () => {
const { content } = compile(`
const { content, bindings } = compile(`
<script setup>
import { x } from './x'
let a = 1
const b = 2
function c() {}
class d {}
</script>
<script>
import { xx } from './x'
let aa = 1
const bb = 2
function cc() {}
class dd {}
</script>
`)
expect(content).toMatch('return { aa, bb, cc, dd, a, b, c, d, xx, x }')
expect(bindings).toStrictEqual({
x: BindingTypes.SETUP_MAYBE_REF,
a: BindingTypes.SETUP_LET,
b: BindingTypes.SETUP_CONST,
c: BindingTypes.SETUP_CONST,
d: BindingTypes.SETUP_CONST,
xx: BindingTypes.SETUP_MAYBE_REF,
aa: BindingTypes.SETUP_LET,
bb: BindingTypes.SETUP_CONST,
cc: BindingTypes.SETUP_CONST,
dd: BindingTypes.SETUP_CONST
})
assertCode(content)
expect(content).toMatch('return { a, b, c, d, x }')
})

test('defineProps()', () => {
Expand Down
7 changes: 7 additions & 0 deletions packages/compiler-sfc/src/compileScript.ts
Expand Up @@ -827,6 +827,13 @@ export function compileScript(
)
}
}
} else if (
(node.type === 'VariableDeclaration' ||
node.type === 'FunctionDeclaration' ||
node.type === 'ClassDeclaration') &&
!node.declare
) {
walkDeclaration(node, setupBindings, userImportAlias)
}
}
}
Expand Down

0 comments on commit 872b3f7

Please sign in to comment.