Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler-sfc): unwrap TS node #7340

Merged
merged 3 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions packages/compiler-core/src/babelUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ export function walkIdentifiers(
if (
parent &&
parent.type.startsWith('TS') &&
parent.type !== 'TSAsExpression' &&
parent.type !== 'TSNonNullExpression' &&
parent.type !== 'TSTypeAssertion'
!TS_NODE_TYPES.includes(parent.type)
) {
return this.skip()
}
Expand Down Expand Up @@ -422,3 +420,18 @@ function isReferenced(node: Node, parent: Node, grandparent?: Node): boolean {

return true
}

export const TS_NODE_TYPES = [
'TSAsExpression', // foo as number
'TSTypeAssertion', // (<number>foo)
'TSNonNullExpression', // foo!
'TSInstantiationExpression', // foo<string>
'TSSatisfiesExpression' // foo satisfies T
]
export function unwrapTSNode(node: Node): Node {
if (TS_NODE_TYPES.includes(node.type)) {
return unwrapTSNode((node as any).expression)
} else {
return node
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,22 @@ return { emit }
})"
`;

exports[`SFC compile <script setup> > with TypeScript > defineProps w/ TS assertion 1`] = `
"import { defineComponent as _defineComponent } from 'vue'

export default /*#__PURE__*/_defineComponent({
props: ['foo'],
setup(__props, { expose }) {
expose();



return { }
}

})"
`;

exports[`SFC compile <script setup> > with TypeScript > defineProps w/ exported interface 1`] = `
"import { defineComponent as _defineComponent } from 'vue'
export interface Props { x?: number }
Expand Down
13 changes: 13 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,19 @@ const emit = defineEmits(['a', 'b'])
})
})

test('defineProps w/ TS assertion', () => {
const { content, bindings } = compile(`
<script setup lang="ts">
defineProps(['foo'])! as any
</script>
`)
expect(content).toMatch(`props: ['foo']`)
assertCode(content)
expect(bindings).toStrictEqual({
foo: BindingTypes.PROPS
})
})

test('withDefaults (static)', () => {
const { content, bindings } = compile(`
<script setup lang="ts">
Expand Down
30 changes: 17 additions & 13 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
UNREF,
SimpleExpressionNode,
isFunctionType,
walkIdentifiers
walkIdentifiers,
unwrapTSNode
} from '@vue/compiler-dom'
import { DEFAULT_FILENAME, SFCDescriptor, SFCScriptBlock } from './parse'
import {
Expand Down Expand Up @@ -1229,17 +1230,18 @@ export function compileScript(
}

if (node.type === 'ExpressionStatement') {
const expr = unwrapTSNode(node.expression)
// process `defineProps` and `defineEmit(s)` calls
if (
processDefineProps(node.expression) ||
processDefineEmits(node.expression) ||
processDefineOptions(node.expression) ||
processWithDefaults(node.expression)
processDefineProps(expr) ||
processDefineEmits(expr) ||
processDefineOptions(expr) ||
processWithDefaults(expr)
) {
s.remove(node.start! + startOffset, node.end! + startOffset)
} else if (processDefineExpose(node.expression)) {
} else if (processDefineExpose(expr)) {
// defineExpose({}) -> expose({})
const callee = (node.expression as CallExpression).callee
const callee = (expr as CallExpression).callee
s.overwrite(
callee.start! + startOffset,
callee.end! + startOffset,
Expand All @@ -1253,8 +1255,9 @@ export function compileScript(
let left = total
for (let i = 0; i < total; i++) {
const decl = node.declarations[i]
if (decl.init) {
if (processDefineOptions(decl.init)) {
const init = decl.init && unwrapTSNode(decl.init)
if (init) {
if (processDefineOptions(init)) {
error(
`${DEFINE_OPTIONS}() has no returning value, it cannot be assigned.`,
node
Expand All @@ -1263,9 +1266,9 @@ export function compileScript(

// defineProps / defineEmits
const isDefineProps =
processDefineProps(decl.init, decl.id, node.kind) ||
processWithDefaults(decl.init, decl.id, node.kind)
const isDefineEmits = processDefineEmits(decl.init, decl.id)
processDefineProps(init, decl.id, node.kind) ||
processWithDefaults(init, decl.id, node.kind)
const isDefineEmits = processDefineEmits(init, decl.id)
if (isDefineProps || isDefineEmits) {
if (left === 1) {
s.remove(node.start! + startOffset, node.end! + startOffset)
Expand Down Expand Up @@ -1801,7 +1804,8 @@ function walkDeclaration(
)

// export const foo = ...
for (const { id, init } of node.declarations) {
for (const { id, init: _init } of node.declarations) {
const init = _init && unwrapTSNode(_init)
const isDefineCall = !!(
isConst &&
isCallOf(
Expand Down