Skip to content

Commit

Permalink
fix(compiler-sfc): support TS runtime enum in <script setup>
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jun 30, 2021
1 parent f8a6b57 commit 1ffd48a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
Expand Up @@ -884,6 +884,21 @@ return { }
})"
`;
exports[`SFC compile <script setup> with TypeScript runtime Enum 1`] = `
"import { defineComponent as _defineComponent } from 'vue'
enum Foo { A = 123 }
export default _defineComponent({
setup(__props, { expose }) {
expose()
return { Foo }
}
})"
`;
exports[`SFC compile <script setup> with TypeScript withDefaults (dynamic) 1`] = `
"import { mergeDefaults as _mergeDefaults, defineComponent as _defineComponent } from 'vue'
import { defaults } from './foo'
Expand Down
12 changes: 12 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Expand Up @@ -824,6 +824,18 @@ const emit = defineEmits(['a', 'b'])
expect(content).toMatch(`emit: ((e: 'foo' | 'bar') => void),`)
expect(content).toMatch(`emits: ["foo", "bar"] as unknown as undefined`)
})

test('runtime Enum', () => {
const { content, bindings } = compile(
`<script setup lang="ts">
enum Foo { A = 123 }
</script>`
)
assertCode(content)
expect(bindings).toStrictEqual({
Foo: BindingTypes.SETUP_CONST
})
})
})

describe('async/await detection', () => {
Expand Down
32 changes: 18 additions & 14 deletions packages/compiler-sfc/src/compileScript.ts
Expand Up @@ -937,20 +937,6 @@ export function compileScript(
walkDeclaration(node, setupBindings, userImportAlias)
}

// Type declarations
if (node.type === 'VariableDeclaration' && node.declare) {
s.remove(start, end)
}

// move all type declarations to outer scope
if (
node.type.startsWith('TS') ||
(node.type === 'ExportNamedDeclaration' && node.exportKind === 'type')
) {
recordType(node, declaredTypes)
s.move(start, end, 0)
}

// walk statements & named exports / variable declarations for top level
// await
if (
Expand Down Expand Up @@ -986,6 +972,24 @@ export function compileScript(
node
)
}

if (isTS) {
// runtime enum
if (node.type === 'TSEnumDeclaration' && !node.const) {
registerBinding(setupBindings, node.id, BindingTypes.SETUP_CONST)
}

// move all Type declarations to outer scope
if (
node.type.startsWith('TS') ||
(node.type === 'ExportNamedDeclaration' &&
node.exportKind === 'type') ||
(node.type === 'VariableDeclaration' && node.declare)
) {
recordType(node, declaredTypes)
s.move(start, end, 0)
}
}
}

// in parse only mode, we should have collected all the information we need,
Expand Down

0 comments on commit 1ffd48a

Please sign in to comment.