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.
feat(compiler-sfc): support defineAttrs macro
- Loading branch information
1 parent
2ffe3d5
commit 94a60f3
Showing
9 changed files
with
150 additions
and
3 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
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,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
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,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 | ||
} |
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
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