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

feat(types): support inferring attrs #7444

Open
wants to merge 20 commits into
base: minor
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`defineAttrs() > basic usage 1`] = `
"import { useAttrs as _useAttrs, defineComponent as _defineComponent } from 'vue'

export default /*#__PURE__*/_defineComponent({
setup(__props, { expose: __expose }) {
__expose();

const attrs = _useAttrs()

return { attrs }
}

})"
`;

exports[`defineAttrs() > w/o generic params 1`] = `
"import { useAttrs as _useAttrs } from 'vue'

export default {
setup(__props, { expose: __expose }) {
__expose();

const attrs = _useAttrs()

return { attrs }
}

}"
`;

exports[`defineAttrs() > w/o return value 1`] = `
"import { defineComponent as _defineComponent } from 'vue'

export default /*#__PURE__*/_defineComponent({
setup(__props, { expose: __expose }) {
__expose();



return { }
}

})"
`;
40 changes: 40 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript/defineAttrs.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { compileSFCScript as compile, assertCode } from '../utils'

describe('defineAttrs()', () => {
test('basic usage', () => {
const { content } = compile(`
<script setup lang="ts">
const attrs = defineAttrs<{
bar?: number
}>()
</script>
`)
assertCode(content)
expect(content).toMatch(`const attrs = _useAttrs()`)
expect(content).not.toMatch('defineAttrs')
})

test('w/o return value', () => {
const { content } = compile(`
<script setup lang="ts">
defineAttrs<{
bar?: number
}>()
</script>
`)
assertCode(content)
expect(content).not.toMatch('defineAttrs')
expect(content).not.toMatch(`_useAttrs`)
})

test('w/o generic params', () => {
const { content } = compile(`
<script setup>
const attrs = defineAttrs()
</script>
`)
assertCode(content)
expect(content).toMatch(`const attrs = _useAttrs()`)
expect(content).not.toMatch('defineAttrs')
})
})
7 changes: 5 additions & 2 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import {
import { analyzeScriptBindings } from './script/analyzeScriptBindings'
import { isImportUsed } from './script/importUsageCheck'
import { processAwait } from './script/topLevelAwait'
import { processDefineAttrs } from './script/defineAttrs'

export interface SFCScriptCompileOptions {
/**
Expand Down Expand Up @@ -512,7 +513,8 @@ export function compileScript(
processDefineProps(ctx, expr) ||
processDefineEmits(ctx, expr) ||
processDefineOptions(ctx, expr) ||
processDefineSlots(ctx, expr)
processDefineSlots(ctx, expr) ||
processDefineAttrs(ctx, expr)
) {
ctx.s.remove(node.start! + startOffset, node.end! + startOffset)
} else if (processDefineExpose(ctx, expr)) {
Expand Down Expand Up @@ -550,7 +552,8 @@ export function compileScript(
!isDefineProps && processDefineEmits(ctx, init, decl.id)
!isDefineEmits &&
(processDefineSlots(ctx, init, decl.id) ||
processDefineModel(ctx, init, decl.id))
processDefineModel(ctx, init, decl.id) ||
processDefineAttrs(ctx, init, decl.id))

if (
isDefineProps &&
Expand Down
1 change: 1 addition & 0 deletions packages/compiler-sfc/src/script/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class ScriptCompileContext {
hasDefineOptionsCall = false
hasDefineSlotsCall = false
hasDefineModelCall = false
hasDefineAttrsCall = false

// defineProps
propsCall: CallExpression | undefined
Expand Down
33 changes: 33 additions & 0 deletions packages/compiler-sfc/src/script/defineAttrs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { LVal, Node } from '@babel/types'
import { isCallOf } from './utils'
import { ScriptCompileContext } from './context'

export const DEFINE_ATTRS = 'defineAttrs'

export function processDefineAttrs(
ctx: ScriptCompileContext,
node: Node,
declId?: LVal
): boolean {
if (!isCallOf(node, DEFINE_ATTRS)) {
return false
}
if (ctx.hasDefineAttrsCall) {
ctx.error(`duplicate ${DEFINE_ATTRS}() call`, node)
}
ctx.hasDefineAttrsCall = true

if (node.arguments.length > 0) {
ctx.error(`${DEFINE_ATTRS}() cannot accept arguments`, node)
}

if (declId) {
ctx.s.overwrite(
ctx.startOffset! + node.start!,
ctx.startOffset! + node.end!,
`${ctx.helper('useAttrs')}()`
)
}

return true
}
Loading