forked from vuejs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ffe3d5
commit 50bdf3c
Showing
8 changed files
with
165 additions
and
6 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
packages/compiler-sfc/__tests__/compileScript/__snapshots__/defineAttrs.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`defineAttrs() > basic usage 1`] = ` | ||
"import { useAttrs as _useAttrs, defineComponent as _defineComponent } from 'vue' | ||
const __sfc__ = /*#__PURE__*/_defineComponent({ | ||
setup(__props, { expose: __expose }) { | ||
__expose(); | ||
const attrs = _useAttrs() | ||
return { attrs } | ||
} | ||
}) | ||
export default __sfc__" | ||
`; | ||
|
||
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' | ||
const __sfc__ = /*#__PURE__*/_defineComponent({ | ||
setup(__props, { expose: __expose }) { | ||
__expose(); | ||
return { } | ||
} | ||
}) | ||
export default __sfc__" | ||
`; |
40 changes: 40 additions & 0 deletions
40
packages/compiler-sfc/__tests__/compileScript/defineAttrs.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
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) | ||
} | ||
const type = | ||
(node.typeParameters && node.typeParameters.params[0]) || undefined | ||
ctx.attrsTypeDecl = type | ||
|
||
if (declId) { | ||
ctx.s.overwrite( | ||
ctx.startOffset! + node.start!, | ||
ctx.startOffset! + node.end!, | ||
`${ctx.helper('useAttrs')}()` | ||
) | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters