mirror of https://github.com/vuejs/core.git
fix(compiler-sfc): unwrap TS node for defineProps (#7340)
This commit is contained in:
parent
6391daf658
commit
1b69d5f2f4
|
@ -38,9 +38,7 @@ export function walkIdentifiers(
|
||||||
if (
|
if (
|
||||||
parent &&
|
parent &&
|
||||||
parent.type.startsWith('TS') &&
|
parent.type.startsWith('TS') &&
|
||||||
parent.type !== 'TSAsExpression' &&
|
!TS_NODE_TYPES.includes(parent.type)
|
||||||
parent.type !== 'TSNonNullExpression' &&
|
|
||||||
parent.type !== 'TSTypeAssertion'
|
|
||||||
) {
|
) {
|
||||||
return this.skip()
|
return this.skip()
|
||||||
}
|
}
|
||||||
|
@ -422,3 +420,18 @@ function isReferenced(node: Node, parent: Node, grandparent?: Node): boolean {
|
||||||
|
|
||||||
return true
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -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`] = `
|
exports[`SFC compile <script setup> > with TypeScript > defineProps w/ exported interface 1`] = `
|
||||||
"import { defineComponent as _defineComponent } from 'vue'
|
"import { defineComponent as _defineComponent } from 'vue'
|
||||||
export interface Props { x?: number }
|
export interface Props { x?: number }
|
||||||
|
|
|
@ -1145,6 +1145,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)', () => {
|
test('withDefaults (static)', () => {
|
||||||
const { content, bindings } = compile(`
|
const { content, bindings } = compile(`
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
|
|
|
@ -9,7 +9,8 @@ import {
|
||||||
UNREF,
|
UNREF,
|
||||||
SimpleExpressionNode,
|
SimpleExpressionNode,
|
||||||
isFunctionType,
|
isFunctionType,
|
||||||
walkIdentifiers
|
walkIdentifiers,
|
||||||
|
unwrapTSNode
|
||||||
} from '@vue/compiler-dom'
|
} from '@vue/compiler-dom'
|
||||||
import { DEFAULT_FILENAME, SFCDescriptor, SFCScriptBlock } from './parse'
|
import { DEFAULT_FILENAME, SFCDescriptor, SFCScriptBlock } from './parse'
|
||||||
import {
|
import {
|
||||||
|
@ -1229,17 +1230,18 @@ export function compileScript(
|
||||||
}
|
}
|
||||||
|
|
||||||
if (node.type === 'ExpressionStatement') {
|
if (node.type === 'ExpressionStatement') {
|
||||||
|
const expr = unwrapTSNode(node.expression)
|
||||||
// process `defineProps` and `defineEmit(s)` calls
|
// process `defineProps` and `defineEmit(s)` calls
|
||||||
if (
|
if (
|
||||||
processDefineProps(node.expression) ||
|
processDefineProps(expr) ||
|
||||||
processDefineEmits(node.expression) ||
|
processDefineEmits(expr) ||
|
||||||
processDefineOptions(node.expression) ||
|
processDefineOptions(expr) ||
|
||||||
processWithDefaults(node.expression)
|
processWithDefaults(expr)
|
||||||
) {
|
) {
|
||||||
s.remove(node.start! + startOffset, node.end! + startOffset)
|
s.remove(node.start! + startOffset, node.end! + startOffset)
|
||||||
} else if (processDefineExpose(node.expression)) {
|
} else if (processDefineExpose(expr)) {
|
||||||
// defineExpose({}) -> expose({})
|
// defineExpose({}) -> expose({})
|
||||||
const callee = (node.expression as CallExpression).callee
|
const callee = (expr as CallExpression).callee
|
||||||
s.overwrite(
|
s.overwrite(
|
||||||
callee.start! + startOffset,
|
callee.start! + startOffset,
|
||||||
callee.end! + startOffset,
|
callee.end! + startOffset,
|
||||||
|
@ -1253,8 +1255,9 @@ export function compileScript(
|
||||||
let left = total
|
let left = total
|
||||||
for (let i = 0; i < total; i++) {
|
for (let i = 0; i < total; i++) {
|
||||||
const decl = node.declarations[i]
|
const decl = node.declarations[i]
|
||||||
if (decl.init) {
|
const init = decl.init && unwrapTSNode(decl.init)
|
||||||
if (processDefineOptions(decl.init)) {
|
if (init) {
|
||||||
|
if (processDefineOptions(init)) {
|
||||||
error(
|
error(
|
||||||
`${DEFINE_OPTIONS}() has no returning value, it cannot be assigned.`,
|
`${DEFINE_OPTIONS}() has no returning value, it cannot be assigned.`,
|
||||||
node
|
node
|
||||||
|
@ -1263,9 +1266,9 @@ export function compileScript(
|
||||||
|
|
||||||
// defineProps / defineEmits
|
// defineProps / defineEmits
|
||||||
const isDefineProps =
|
const isDefineProps =
|
||||||
processDefineProps(decl.init, decl.id, node.kind) ||
|
processDefineProps(init, decl.id, node.kind) ||
|
||||||
processWithDefaults(decl.init, decl.id, node.kind)
|
processWithDefaults(init, decl.id, node.kind)
|
||||||
const isDefineEmits = processDefineEmits(decl.init, decl.id)
|
const isDefineEmits = processDefineEmits(init, decl.id)
|
||||||
if (isDefineProps || isDefineEmits) {
|
if (isDefineProps || isDefineEmits) {
|
||||||
if (left === 1) {
|
if (left === 1) {
|
||||||
s.remove(node.start! + startOffset, node.end! + startOffset)
|
s.remove(node.start! + startOffset, node.end! + startOffset)
|
||||||
|
@ -1801,7 +1804,8 @@ function walkDeclaration(
|
||||||
)
|
)
|
||||||
|
|
||||||
// export const foo = ...
|
// 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 = !!(
|
const isDefineCall = !!(
|
||||||
isConst &&
|
isConst &&
|
||||||
isCallOf(
|
isCallOf(
|
||||||
|
|
Loading…
Reference in New Issue